aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/utils')
-rw-r--r--src/eu/siacs/conversations/utils/ExceptionHelper.java27
-rw-r--r--src/eu/siacs/conversations/utils/MessageParser.java16
-rw-r--r--src/eu/siacs/conversations/utils/PhoneHelper.java27
-rw-r--r--src/eu/siacs/conversations/utils/UIHelper.java272
4 files changed, 261 insertions, 81 deletions
diff --git a/src/eu/siacs/conversations/utils/ExceptionHelper.java b/src/eu/siacs/conversations/utils/ExceptionHelper.java
index c6e857c0..f9cd375e 100644
--- a/src/eu/siacs/conversations/utils/ExceptionHelper.java
+++ b/src/eu/siacs/conversations/utils/ExceptionHelper.java
@@ -34,6 +34,18 @@ public class ExceptionHelper {
if (neverSend) {
return;
}
+ List<Account> accounts = service.getAccounts();
+ Account account = null;
+ for(int i = 0; i < accounts.size(); ++i) {
+ if (!accounts.get(i).isOptionSet(Account.OPTION_DISABLED)) {
+ account = accounts.get(i);
+ break;
+ }
+ }
+ if (account==null) {
+ return;
+ }
+ final Account finalAccount = account;
FileInputStream file = context.openFileInput("stacktrace.txt");
InputStreamReader inputStreamReader = new InputStreamReader(
file);
@@ -54,20 +66,11 @@ public class ExceptionHelper {
@Override
public void onClick(DialogInterface dialog, int which) {
- List<Account> accounts = service.getAccounts();
- Account account = null;
- for(int i = 0; i < accounts.size(); ++i) {
- if (!accounts.get(i).isOptionSet(Account.OPTION_DISABLED)) {
- account = accounts.get(i);
- break;
- }
- }
- if (account!=null) {
- Log.d("xmppService","using account="+account.getJid()+" to send in stack trace");
- Conversation conversation = service.findOrCreateConversation(account, "bugs@siacs.eu", false);
+
+ Log.d("xmppService","using account="+finalAccount.getJid()+" to send in stack trace");
+ Conversation conversation = service.findOrCreateConversation(finalAccount, "bugs@siacs.eu", false);
Message message = new Message(conversation, stacktrace.toString(), Message.ENCRYPTION_NONE);
service.sendMessage(message, null);
- }
}
});
builder.setNegativeButton(context.getText(R.string.send_never),new OnClickListener() {
diff --git a/src/eu/siacs/conversations/utils/MessageParser.java b/src/eu/siacs/conversations/utils/MessageParser.java
index 936d0e9a..568386d5 100644
--- a/src/eu/siacs/conversations/utils/MessageParser.java
+++ b/src/eu/siacs/conversations/utils/MessageParser.java
@@ -149,19 +149,9 @@ public class MessageParser {
return new Message(conversation,fullJid, message.findChild("body").getContent(), Message.ENCRYPTION_NONE,status);
}
- public static Message parseError(MessagePacket packet, Account account, XmppConnectionService service) {
-
- String[] fromParts = packet.getFrom().split("/");
- Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
- Element error = packet.findChild("error");
- String errorName = error.getChildren().get(0).getName();
- String displayError;
- if (errorName.equals("service-unavailable")) {
- displayError = "Contact is offline and does not have offline storage";
- } else {
- displayError = errorName.replace("-", " ");
- }
- return new Message(conversation, packet.getFrom(), displayError, Message.ENCRYPTION_NONE, Message.STATUS_ERROR);
+ public static void parseError(MessagePacket packet, Account account, XmppConnectionService service) {
+ String[] fromParts = packet.getFrom().split("/");
+ service.markMessage(account, fromParts[0], packet.getId(), Message.STATUS_SEND_FAILED);
}
public static String getPgpBody(MessagePacket packet) {
diff --git a/src/eu/siacs/conversations/utils/PhoneHelper.java b/src/eu/siacs/conversations/utils/PhoneHelper.java
index 5c652afe..6355e378 100644
--- a/src/eu/siacs/conversations/utils/PhoneHelper.java
+++ b/src/eu/siacs/conversations/utils/PhoneHelper.java
@@ -13,18 +13,19 @@ import android.os.Bundle;
import android.os.Looper;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Profile;
+import android.provider.MediaStore;
public class PhoneHelper {
-
- public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
- if (Looper.myLooper()==null) {
+
+ public static void loadPhoneContacts(Context context,
+ final OnPhoneContactsLoadedListener listener) {
+ if (Looper.myLooper() == null) {
Looper.prepare();
}
final Looper mLooper = Looper.myLooper();
final Hashtable<String, Bundle> phoneContacts = new Hashtable<String, Bundle>();
-
- final String[] PROJECTION = new String[] {
- ContactsContract.Data._ID,
+
+ final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_THUMBNAIL_URI,
ContactsContract.Data.LOOKUP_KEY,
@@ -35,7 +36,7 @@ public class PhoneHelper {
+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
+ "\")";
-
+
CursorLoader mCursorLoader = new CursorLoader(context,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
@@ -55,14 +56,14 @@ public class PhoneHelper {
"photouri",
cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)));
- contact.putString("lookup",cursor.getString(cursor
+ contact.putString("lookup", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
phoneContacts.put(
cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)),
contact);
}
- if (listener!=null) {
+ if (listener != null) {
listener.onPhoneContactsLoaded(phoneContacts);
}
mLooper.quit();
@@ -71,18 +72,18 @@ public class PhoneHelper {
mCursorLoader.startLoading();
}
- public static Uri getSefliUri(Activity activity) {
+ public static Uri getSefliUri(Context context) {
String[] mProjection = new String[] { Profile._ID,
Profile.PHOTO_THUMBNAIL_URI };
- Cursor mProfileCursor = activity.getContentResolver().query(
+ Cursor mProfileCursor = context.getContentResolver().query(
Profile.CONTENT_URI, mProjection, null, null, null);
- if (mProfileCursor.getCount()==0) {
+ if (mProfileCursor.getCount() == 0) {
return null;
} else {
mProfileCursor.moveToFirst();
String uri = mProfileCursor.getString(1);
- if (uri==null) {
+ if (uri == null) {
return null;
} else {
return Uri.parse(uri);
diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java
index 73a0494b..f058b9ee 100644
--- a/src/eu/siacs/conversations/utils/UIHelper.java
+++ b/src/eu/siacs/conversations/utils/UIHelper.java
@@ -12,6 +12,8 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.entities.MucOptions;
+import eu.siacs.conversations.entities.MucOptions.User;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ManageAccountActivity;
@@ -31,6 +33,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
+import android.graphics.Typeface;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.Contacts;
@@ -38,6 +41,7 @@ import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.TaskStackBuilder;
import android.text.Html;
+import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -46,6 +50,10 @@ import android.widget.QuickContactBadge;
import android.widget.TextView;
public class UIHelper {
+ private static final int BG_COLOR = 0xFF181818;
+ private static final int FG_COLOR = 0xFFE5E5E5;
+ private static final int TRANSPARENT = 0x00000000;
+
public static String readableTimeDifference(long time) {
if (time == 0) {
return "just now";
@@ -65,49 +73,235 @@ public class UIHelper {
}
}
- private static Bitmap getUnknownContactPicture(String name, int size) {
- String firstLetter = (name.length() > 0) ? name.substring(0, 1).toUpperCase(Locale.US) : " ";
+ public static int getRealPx(int dp, Context context) {
+ final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
+ return ((int) (dp * metrics.density));
+ }
+ private static int getNameColor(String name) {
int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713,
0xFFe92727 };
-
int color = holoColors[Math.abs(name.toLowerCase(Locale.getDefault()).hashCode()) % holoColors.length];
+ return color;
+ }
+ private static Bitmap getUnknownContactPicture(String[] names, int size, int bgColor, int fgColor) {
+ int tiles = (names.length > 4)? 4 :
+ (names.length < 1)? 1 :
+ names.length;
Bitmap bitmap = Bitmap
.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
- bitmap.eraseColor(color);
+ String[] letters = new String[tiles];
+ int[] colors = new int[tiles];
+ if (names.length < 1) {
+ letters[0] = "?";
+ colors[0] = 0xFFe92727;
+ } else {
+ for(int i = 0; i < tiles; ++i) {
+ letters[i] = (names[i].length() > 0) ?
+ names[i].substring(0, 1).toUpperCase(Locale.US) : " ";
+ colors[i] = getNameColor(names[i]);
+ }
- Paint paint = new Paint();
- paint.setColor(0xffe5e5e5);
- paint.setTextSize((float) (size * 0.9));
- paint.setAntiAlias(true);
- Rect rect = new Rect();
- paint.getTextBounds(firstLetter, 0, 1, rect);
- float width = paint.measureText(firstLetter);
- canvas.drawText(firstLetter, (size / 2) - (width / 2), (size / 2)
- + (rect.height() / 2), paint);
+ if (names.length > 4) {
+ letters[3] = "\u2026"; // Unicode ellipsis
+ colors[3] = 0xFF444444;
+ }
+ }
+ Paint textPaint = new Paint(), tilePaint = new Paint();
+ textPaint.setColor(fgColor);
+ textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
+ Rect rect, left, right, topLeft, bottomLeft, topRight, bottomRight;
+ float width;
+
+ switch(tiles) {
+ case 1:
+ bitmap.eraseColor(colors[0]);
+
+ textPaint.setTextSize((float) (size * 0.8));
+ textPaint.setAntiAlias(true);
+ rect = new Rect();
+ textPaint.getTextBounds(letters[0], 0, 1, rect);
+ width = textPaint.measureText(letters[0]);
+ canvas.drawText(letters[0], (size / 2) - (width / 2), (size / 2)
+ + (rect.height() / 2), textPaint);
+ break;
+
+ case 2:
+ bitmap.eraseColor(bgColor);
+
+ tilePaint.setColor(colors[0]);
+ left = new Rect(0, 0, (size/2)-1, size);
+ canvas.drawRect(left, tilePaint);
+
+ tilePaint.setColor(colors[1]);
+ right = new Rect((size/2)+1, 0, size, size);
+ canvas.drawRect(right, tilePaint);
+
+ textPaint.setTextSize((float) (size * 0.8*0.5));
+ textPaint.setAntiAlias(true);
+ rect = new Rect();
+ textPaint.getTextBounds(letters[0], 0, 1, rect);
+ width = textPaint.measureText(letters[0]);
+ canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 2)
+ + (rect.height() / 2), textPaint);
+ textPaint.getTextBounds(letters[1], 0, 1, rect);
+ width = textPaint.measureText(letters[1]);
+ canvas.drawText(letters[1], (3 * size / 4) - (width / 2), (size / 2)
+ + (rect.height() / 2), textPaint);
+ break;
+
+ case 3:
+ bitmap.eraseColor(bgColor);
+
+ tilePaint.setColor(colors[0]);
+ left = new Rect(0, 0, (size/2)-1, size);
+ canvas.drawRect(left, tilePaint);
+
+ tilePaint.setColor(colors[1]);
+ topRight = new Rect((size/2)+1, 0, size, (size/2 - 1));
+ canvas.drawRect(topRight, tilePaint);
+
+ tilePaint.setColor(colors[2]);
+ bottomRight = new Rect((size/2)+1, (size/2 + 1), size, size);
+ canvas.drawRect(bottomRight, tilePaint);
+
+ textPaint.setTextSize((float) (size * 0.8*0.5));
+ textPaint.setAntiAlias(true);
+ rect = new Rect();
+
+ textPaint.getTextBounds(letters[0], 0, 1, rect);
+ width = textPaint.measureText(letters[0]);
+ canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 2)
+ + (rect.height() / 2), textPaint);
+
+ textPaint.getTextBounds(letters[1], 0, 1, rect);
+ width = textPaint.measureText(letters[1]);
+ canvas.drawText(letters[1], (3 * size / 4) - (width / 2), (size / 4)
+ + (rect.height() / 2), textPaint);
+
+ textPaint.getTextBounds(letters[2], 0, 1, rect);
+ width = textPaint.measureText(letters[2]);
+ canvas.drawText(letters[2], (3 * size / 4) - (width / 2), (3* size / 4)
+ + (rect.height() / 2), textPaint);
+ break;
+
+ case 4:
+ bitmap.eraseColor(bgColor);
+
+ tilePaint.setColor(colors[0]);
+ topLeft = new Rect(0, 0, (size/2)-1, (size/2)-1);
+ canvas.drawRect(topLeft, tilePaint);
+
+ tilePaint.setColor(colors[1]);
+ bottomLeft = new Rect(0, (size/2)+1, (size/2)-1, size);
+ canvas.drawRect(bottomLeft, tilePaint);
+ tilePaint.setColor(colors[2]);
+ topRight = new Rect((size/2)+1, 0, size, (size/2 - 1));
+ canvas.drawRect(topRight, tilePaint);
+
+ tilePaint.setColor(colors[3]);
+ bottomRight = new Rect((size/2)+1, (size/2 + 1), size, size);
+ canvas.drawRect(bottomRight, tilePaint);
+
+ textPaint.setTextSize((float) (size * 0.8*0.5));
+ textPaint.setAntiAlias(true);
+ rect = new Rect();
+
+ textPaint.getTextBounds(letters[0], 0, 1, rect);
+ width = textPaint.measureText(letters[0]);
+ canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 4)
+ + (rect.height() / 2), textPaint);
+
+ textPaint.getTextBounds(letters[1], 0, 1, rect);
+ width = textPaint.measureText(letters[1]);
+ canvas.drawText(letters[1], (size / 4) - (width / 2), (3* size / 4)
+ + (rect.height() / 2), textPaint);
+
+ textPaint.getTextBounds(letters[2], 0, 1, rect);
+ width = textPaint.measureText(letters[2]);
+ canvas.drawText(letters[2], (3 * size / 4) - (width / 2), (size / 4)
+ + (rect.height() / 2), textPaint);
+
+ textPaint.getTextBounds(letters[3], 0, 1, rect);
+ width = textPaint.measureText(letters[3]);
+ canvas.drawText(letters[3], (3 * size / 4) - (width / 2), (3* size / 4)
+ + (rect.height() / 2), textPaint);
+ break;
+ }
return bitmap;
}
-
- public static Bitmap getContactPicture(Contact contact, String fallback, int size, Context context) {
- if (contact==null) {
- return getUnknownContactPicture(fallback, size);
+
+ private static Bitmap getUnknownContactPicture(String[] names, int size) {
+ return getUnknownContactPicture(names, size, UIHelper.BG_COLOR, UIHelper.FG_COLOR);
+ }
+
+ private static Bitmap getMucContactPicture(Conversation conversation, int size, int bgColor, int fgColor) {
+ List<User> members = conversation.getMucOptions().getUsers();
+ if (members.size() == 0) {
+ return getUnknownContactPicture(new String[]{conversation.getName(false)}, size, bgColor, fgColor);
+ }
+ String[] names = new String[members.size()+1];
+ names[0] = conversation.getMucOptions().getNick();
+ for(int i = 0; i < members.size(); ++i) {
+ names[i+1] = members.get(i).getName();
+ }
+
+ return getUnknownContactPicture(names, size, bgColor, fgColor);
+ }
+
+ public static Bitmap getContactPicture(Conversation conversation, int dpSize, Context context, boolean notification) {
+ if(conversation.getMode() == Conversation.MODE_SINGLE) {
+ if (conversation.getContact() != null){
+ return getContactPicture(conversation.getContact(), dpSize,
+ context, notification);
+ } else {
+ return getContactPicture(conversation.getName(false), dpSize,
+ context, notification);
+ }
+ } else{
+ int fgColor = UIHelper.FG_COLOR,
+ bgColor = (notification) ?
+ UIHelper.BG_COLOR : UIHelper.TRANSPARENT;
+
+ return getMucContactPicture(conversation, getRealPx(dpSize, context),
+ bgColor, fgColor);
}
+ }
+
+ public static Bitmap getContactPicture(Contact contact, int dpSize, Context context, boolean notification) {
+ int fgColor = UIHelper.FG_COLOR,
+ bgColor = (notification) ?
+ UIHelper.BG_COLOR : UIHelper.TRANSPARENT;
+
String uri = contact.getProfilePhoto();
if (uri==null) {
- return getUnknownContactPicture(contact.getDisplayName(), size);
+ return getContactPicture(contact.getDisplayName(), dpSize,
+ context, notification);
}
try {
- Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri)));
- return Bitmap.createScaledBitmap(bm, size, size, false);
+ Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver()
+ .openInputStream(Uri.parse(uri)));
+ return Bitmap.createScaledBitmap(bm, getRealPx(dpSize, context),
+ getRealPx(dpSize, context), false);
} catch (FileNotFoundException e) {
- return getUnknownContactPicture(contact.getDisplayName(), size);
+ return getContactPicture(contact.getDisplayName(), dpSize,
+ context, notification);
}
}
+ public static Bitmap getContactPicture(String name, int dpSize, Context context, boolean notification) {
+ int fgColor = UIHelper.FG_COLOR,
+ bgColor = (notification) ?
+ UIHelper.BG_COLOR : UIHelper.TRANSPARENT;
+
+ return getUnknownContactPicture(new String[]{name}, getRealPx(dpSize, context),
+ bgColor, fgColor);
+ }
+
public static Bitmap getErrorPicture(int size) {
Bitmap bitmap = Bitmap
.createBitmap(size, size, Bitmap.Config.ARGB_8888);
@@ -203,7 +397,6 @@ public class UIHelper {
}
String ringtone = preferences.getString("notification_ringtone", null);
- Resources res = context.getResources();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
if (unread.size() == 0) {
@@ -212,8 +405,8 @@ public class UIHelper {
} else if (unread.size() == 1) {
Conversation conversation = unread.get(0);
targetUuid = conversation.getUuid();
- mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation.getContact(), conversation.getName(useSubject), (int) res
- .getDimension(android.R.dimen.notification_large_icon_width), context));
+ mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation, 64,
+ context, true));
mBuilder.setContentTitle(conversation.getName(useSubject));
if (notify) {
mBuilder.setTicker(conversation.getLatestMessage().getBody().trim());
@@ -255,12 +448,15 @@ public class UIHelper {
mBuilder.setContentText(names.toString());
mBuilder.setStyle(style);
}
+ if ((currentCon!=null)&&(notify)) {
+ targetUuid=currentCon.getUuid();
+ }
if (unread.size() != 0) {
mBuilder.setSmallIcon(R.drawable.notification);
if (notify) {
if (vibrate) {
int dat = 70;
- long[] pattern = {0,3*dat,dat,dat,dat,3*dat,dat,dat};
+ long[] pattern = {0,3*dat,dat,dat};
mBuilder.setVibrate(pattern);
}
mBuilder.setLights(0xffffffff, 2000, 4000);
@@ -307,23 +503,13 @@ public class UIHelper {
}
public static void prepareContactBadge(final Activity activity,
- QuickContactBadge badge, final Contact contact) {
+ QuickContactBadge badge, final Contact contact, Context context) {
if (contact.getSystemAccount() != null) {
String[] systemAccount = contact.getSystemAccount().split("#");
long id = Long.parseLong(systemAccount[0]);
badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
-
- if (contact.getProfilePhoto() != null) {
- badge.setImageURI(Uri.parse(contact.getProfilePhoto()));
- } else {
- badge.setImageBitmap(UIHelper.getUnknownContactPicture(
- contact.getDisplayName(), 400));
- }
- } else {
- badge.setImageBitmap(UIHelper.getUnknownContactPicture(
- contact.getDisplayName(), 400));
}
-
+ badge.setImageBitmap(UIHelper.getContactPicture(contact, 72, context, false));
}
public static AlertDialog getVerifyFingerprintDialog(
@@ -359,20 +545,20 @@ public class UIHelper {
return builder.create();
}
- public static Bitmap getSelfContactPicture(Account account, int size, boolean showPhoneSelfContactPicture, Activity activity) {
+ public static Bitmap getSelfContactPicture(Account account, int size, boolean showPhoneSelfContactPicture, Context context) {
if (showPhoneSelfContactPicture) {
- Uri selfiUri = PhoneHelper.getSefliUri(activity);
+ Uri selfiUri = PhoneHelper.getSefliUri(context);
if (selfiUri != null) {
try {
- return BitmapFactory.decodeStream(activity
+ return BitmapFactory.decodeStream(context
.getContentResolver().openInputStream(selfiUri));
} catch (FileNotFoundException e) {
- return getUnknownContactPicture(account.getJid(), size);
+ return getContactPicture(account.getJid(), size, context, false);
}
}
- return getUnknownContactPicture(account.getJid(), size);
+ return getContactPicture(account.getJid(), size, context, false);
} else {
- return getUnknownContactPicture(account.getJid(), size);
+ return getContactPicture(account.getJid(), size, context, false);
}
}
}