aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/services
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/eu/siacs/conversations/services/AvatarService.java146
-rw-r--r--src/eu/siacs/conversations/services/NotificationService.java22
-rw-r--r--src/eu/siacs/conversations/services/XmppConnectionService.java27
3 files changed, 162 insertions, 33 deletions
diff --git a/src/eu/siacs/conversations/services/AvatarService.java b/src/eu/siacs/conversations/services/AvatarService.java
index def3bfd8a..c0668a193 100644
--- a/src/eu/siacs/conversations/services/AvatarService.java
+++ b/src/eu/siacs/conversations/services/AvatarService.java
@@ -1,8 +1,10 @@
package eu.siacs.conversations.services;
+import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact;
@@ -15,20 +17,34 @@ import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
+import android.util.Log;
public class AvatarService {
private static final int FG_COLOR = 0xFFFAFAFA;
private static final int TRANSPARENT = 0x00000000;
+ private static final String PREFIX_CONTACT = "contact";
+ private static final String PREFIX_CONVERSATION = "conversation";
+ private static final String PREFIX_ACCOUNT = "account";
+ private static final String PREFIX_GENERIC = "generic";
+
+ private ArrayList<Integer> sizes = new ArrayList<Integer>();
+
protected XmppConnectionService mXmppConnectionService = null;
public AvatarService(XmppConnectionService service) {
this.mXmppConnectionService = service;
}
- public Bitmap getAvatar(Contact contact, int size) {
- Bitmap avatar = mXmppConnectionService.getFileBackend().getAvatar(
+ public Bitmap get(Contact contact, int size) {
+ final String KEY = key(contact, size);
+ Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
+ if (avatar != null) {
+ return avatar;
+ }
+ Log.d(Config.LOGTAG, "no cache hit for " + KEY);
+ avatar = mXmppConnectionService.getFileBackend().getAvatar(
contact.getAvatar(), size);
if (avatar == null) {
if (contact.getProfilePhoto() != null) {
@@ -36,43 +52,74 @@ public class AvatarService {
.cropCenterSquare(Uri.parse(contact.getProfilePhoto()),
size);
if (avatar == null) {
- avatar = getAvatar(contact.getDisplayName(), size);
+ avatar = get(contact.getDisplayName(), size);
}
} else {
- avatar = getAvatar(contact.getDisplayName(), size);
+ avatar = get(contact.getDisplayName(), size);
}
}
+ this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
return avatar;
}
- public Bitmap getAvatar(ListItem item, int size) {
+ public void clear(Contact contact) {
+ for (Integer size : sizes) {
+ this.mXmppConnectionService.getBitmapCache().remove(
+ key(contact, size));
+ }
+ }
+
+ private String key(Contact contact, int size) {
+ synchronized (this.sizes) {
+ if (!this.sizes.contains(size)) {
+ this.sizes.add(size);
+ }
+ }
+ return PREFIX_CONTACT + "_" + contact.getAccount().getJid() + "_"
+ + contact.getJid() + "_" + String.valueOf(size);
+ }
+
+ public Bitmap get(ListItem item, int size) {
if (item instanceof Contact) {
- return getAvatar((Contact) item, size);
+ return get((Contact) item, size);
} else if (item instanceof Bookmark) {
Bookmark bookmark = (Bookmark) item;
if (bookmark.getConversation() != null) {
- return getAvatar(bookmark.getConversation(), size);
+ return get(bookmark.getConversation(), size);
} else {
- return getAvatar(bookmark.getDisplayName(), size);
+ return get(bookmark.getDisplayName(), size);
}
} else {
- return getAvatar(item.getDisplayName(), size);
+ return get(item.getDisplayName(), size);
}
}
- public Bitmap getAvatar(Conversation conversation, int size) {
+ public Bitmap get(Conversation conversation, int size) {
if (conversation.getMode() == Conversation.MODE_SINGLE) {
- return getAvatar(conversation.getContact(), size);
+ return get(conversation.getContact(), size);
} else {
- return getAvatar(conversation.getMucOptions(), size);
+ return get(conversation.getMucOptions(), size);
}
}
- public Bitmap getAvatar(MucOptions mucOptions, int size) {
+ public void clear(Conversation conversation) {
+ if (conversation.getMode() == Conversation.MODE_SINGLE) {
+ clear(conversation.getContact());
+ } else {
+ clear(conversation.getMucOptions());
+ }
+ }
+
+ public Bitmap get(MucOptions mucOptions, int size) {
+ final String KEY = key(mucOptions, size);
+ Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
+ if (bitmap != null) {
+ return bitmap;
+ }
+ Log.d(Config.LOGTAG, "no cache hit for " + KEY);
List<MucOptions.User> users = mucOptions.getUsers();
int count = users.size();
- Bitmap bitmap = Bitmap
- .createBitmap(size, size, Bitmap.Config.ARGB_8888);
+ bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(TRANSPARENT);
@@ -104,28 +151,85 @@ public class AvatarService {
drawTile(canvas, "\u2026", 0xFF202020, size / 2 + 1, size / 2 + 1,
size, size);
}
+ this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
return bitmap;
}
- public Bitmap getAvatar(Account account, int size) {
- Bitmap avatar = mXmppConnectionService.getFileBackend().getAvatar(
+ public void clear(MucOptions options) {
+ for (Integer size : sizes) {
+ this.mXmppConnectionService.getBitmapCache().remove(
+ key(options, size));
+ }
+ }
+
+ private String key(MucOptions options, int size) {
+ synchronized (this.sizes) {
+ if (!this.sizes.contains(size)) {
+ this.sizes.add(size);
+ }
+ }
+ return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
+ + "_" + String.valueOf(size);
+ }
+
+ public Bitmap get(Account account, int size) {
+ final String KEY = key(account, size);
+ Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
+ if (avatar != null) {
+ return avatar;
+ }
+ Log.d(Config.LOGTAG, "no cache hit for " + KEY);
+ avatar = mXmppConnectionService.getFileBackend().getAvatar(
account.getAvatar(), size);
if (avatar == null) {
- avatar = getAvatar(account.getJid(), size);
+ avatar = get(account.getJid(), size);
}
+ mXmppConnectionService.getBitmapCache().put(KEY, avatar);
return avatar;
}
- public Bitmap getAvatar(String name, int size) {
- Bitmap bitmap = Bitmap
- .createBitmap(size, size, Bitmap.Config.ARGB_8888);
+ public void clear(Account account) {
+ for (Integer size : sizes) {
+ this.mXmppConnectionService.getBitmapCache().remove(
+ key(account, size));
+ }
+ }
+
+ private String key(Account account, int size) {
+ synchronized (this.sizes) {
+ if (!this.sizes.contains(size)) {
+ this.sizes.add(size);
+ }
+ }
+ return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
+ + String.valueOf(size);
+ }
+
+ public Bitmap get(String name, int size) {
+ final String KEY = key(name, size);
+ Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
+ if (bitmap != null) {
+ return bitmap;
+ }
+ Log.d(Config.LOGTAG, "no cache hit for " + KEY);
+ bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
String letter = name.substring(0, 1);
int color = this.getColorForName(name);
drawTile(canvas, letter, color, 0, 0, size, size);
+ mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
return bitmap;
}
+ private String key(String name, int size) {
+ synchronized (this.sizes) {
+ if (!this.sizes.contains(size)) {
+ this.sizes.add(size);
+ }
+ }
+ return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
+ }
+
private void drawTile(Canvas canvas, String letter, int tileColor,
int left, int top, int right, int bottom) {
letter = letter.toUpperCase(Locale.getDefault());
diff --git a/src/eu/siacs/conversations/services/NotificationService.java b/src/eu/siacs/conversations/services/NotificationService.java
index a1336123a..8eba49a9c 100644
--- a/src/eu/siacs/conversations/services/NotificationService.java
+++ b/src/eu/siacs/conversations/services/NotificationService.java
@@ -16,6 +16,7 @@ import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.Html;
+import android.util.DisplayMetrics;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
@@ -26,7 +27,6 @@ import eu.siacs.conversations.ui.ConversationActivity;
public class NotificationService {
private XmppConnectionService mXmppConnectionService;
- private NotificationManager mNotificationManager;
private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
@@ -36,8 +36,6 @@ public class NotificationService {
public NotificationService(XmppConnectionService service) {
this.mXmppConnectionService = service;
- this.mNotificationManager = (NotificationManager) service
- .getSystemService(Context.NOTIFICATION_SERVICE);
}
public void push(Message message) {
@@ -79,6 +77,8 @@ public class NotificationService {
}
private void updateNotification(boolean notify) {
+ NotificationManager notificationManager = (NotificationManager) mXmppConnectionService
+ .getSystemService(Context.NOTIFICATION_SERVICE);
SharedPreferences preferences = mXmppConnectionService.getPreferences();
String ringtone = preferences.getString("notification_ringtone", null);
@@ -86,7 +86,7 @@ public class NotificationService {
true);
if (notifications.size() == 0) {
- mNotificationManager.cancel(NOTIFICATION_ID);
+ notificationManager.cancel(NOTIFICATION_ID);
} else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mXmppConnectionService);
@@ -97,8 +97,8 @@ public class NotificationService {
if (messages.size() >= 1) {
Conversation conversation = messages.get(0)
.getConversation();
- // mBuilder.setLargeIcon(conversation.getImage(mXmppConnectionService,
- // 64));
+ mBuilder.setLargeIcon(mXmppConnectionService
+ .getAvatarService().get(conversation, getPixel(64)));
mBuilder.setContentTitle(conversation.getName());
StringBuilder text = new StringBuilder();
for (int i = 0; i < messages.size(); ++i) {
@@ -119,7 +119,7 @@ public class NotificationService {
mBuilder.setContentIntent(createContentIntent(conversation
.getUuid()));
} else {
- mNotificationManager.cancel(NOTIFICATION_ID);
+ notificationManager.cancel(NOTIFICATION_ID);
return;
}
} else {
@@ -170,7 +170,7 @@ public class NotificationService {
mBuilder.setDeleteIntent(createDeleteIntent());
mBuilder.setLights(0xffffffff, 2000, 4000);
Notification notification = mBuilder.build();
- mNotificationManager.notify(NOTIFICATION_ID, notification);
+ notificationManager.notify(NOTIFICATION_ID, notification);
}
}
@@ -227,4 +227,10 @@ public class NotificationService {
public void setIsInForeground(boolean foreground) {
this.mIsInForeground = foreground;
}
+
+ private int getPixel(int dp) {
+ DisplayMetrics metrics = mXmppConnectionService.getResources()
+ .getDisplayMetrics();
+ return ((int) (dp * metrics.density));
+ }
}
diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java
index 6c8dc17a5..5da87b101 100644
--- a/src/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/eu/siacs/conversations/services/XmppConnectionService.java
@@ -84,11 +84,12 @@ import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
+import android.util.LruCache;
public class XmppConnectionService extends Service {
public DatabaseBackend databaseBackend;
- private FileBackend fileBackend;
+ private FileBackend fileBackend = new FileBackend(this);
public long startDate;
@@ -97,7 +98,8 @@ public class XmppConnectionService extends Service {
private MemorizingTrustManager mMemorizingTrustManager;
- private NotificationService mNotificationService;
+ private NotificationService mNotificationService = new NotificationService(
+ this);
private MessageParser mMessageParser = new MessageParser(this);
private PresenceParser mPresenceParser = new PresenceParser(this);
@@ -269,6 +271,7 @@ public class XmppConnectionService extends Service {
}
}
};
+ private LruCache<String, Bitmap> mBitmapCache;
public PgpEngine getPgpEngine() {
if (pgpServiceConnection.isBound()) {
@@ -429,10 +432,18 @@ public class XmppConnectionService extends Service {
this.mRandom = new SecureRandom();
this.mMemorizingTrustManager = new MemorizingTrustManager(
getApplicationContext());
- this.mNotificationService = new NotificationService(this);
+
+ int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
+ int cacheSize = maxMemory / 8;
+ this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
+ @Override
+ protected int sizeOf(String key, Bitmap bitmap) {
+ return bitmap.getByteCount() / 1024;
+ }
+ };
+
this.databaseBackend = DatabaseBackend
.getInstance(getApplicationContext());
- this.fileBackend = new FileBackend(getApplicationContext());
this.accounts = databaseBackend.getAccounts();
for (Account account : this.accounts) {
@@ -801,6 +812,7 @@ public class XmppConnectionService extends Service {
.getString("photouri"));
contact.setSystemName(phoneContact
.getString("displayname"));
+ getAvatarService().clear(contact);
}
}
}
@@ -1497,10 +1509,12 @@ public class XmppConnectionService extends Service {
if (account.setAvatar(avatar.getFilename())) {
databaseBackend.updateAccount(account);
}
+ getAvatarService().clear(account);
} else {
Contact contact = account.getRoster()
.getContact(avatar.owner);
contact.setAvatar(avatar.getFilename());
+ getAvatarService().clear(contact);
}
if (callback != null) {
callback.success(avatar);
@@ -1550,6 +1564,7 @@ public class XmppConnectionService extends Service {
if (account.setAvatar(avatar.getFilename())) {
databaseBackend.updateAccount(account);
}
+ getAvatarService().clear(account);
callback.success(avatar);
} else {
fetchAvatar(account, avatar, callback);
@@ -1762,6 +1777,10 @@ public class XmppConnectionService extends Service {
return this.pm;
}
+ public LruCache<String, Bitmap> getBitmapCache() {
+ return this.mBitmapCache;
+ }
+
public void replyWithNotAcceptable(Account account, MessagePacket packet) {
if (account.getStatus() == Account.STATUS_ONLINE) {
MessagePacket error = this.mMessageGenerator