aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/avatar/AvatarCache.java
blob: 5e10f27cdc3373c2070640a6af8f2a055a0ec0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package de.thedevstack.conversationsplus.services.avatar;

import android.graphics.Bitmap;
import android.net.Uri;

import java.util.ArrayList;

import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Bookmark;
import de.thedevstack.conversationsplus.entities.Contact;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.ListItem;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.entities.MucOptions;
import de.thedevstack.conversationsplus.enums.MessageDirection;
import de.thedevstack.conversationsplus.utils.AvatarUtil;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.thedevstack.conversationsplus.utils.UIHelper;

/**
 */
public final class AvatarCache {

    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 final ArrayList<Integer> sizes = new ArrayList<>();
    private final static AvatarCache INSTANCE = new AvatarCache();

    private AvatarCache() {
    }

    private static Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
        final String KEY = key(contact, size);
        Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
        if (avatar != null || cachedOnly) {
            return avatar;
        }
        if (contact.getProfilePhoto() != null) {
            avatar = ImageUtil.cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
        }
        if (avatar == null && contact.getAvatar() != null) {
            avatar = AvatarUtil.getAvatar(contact.getAvatar(), size);
        }
        if (avatar == null) {
            avatar = get(contact.getDisplayName(), size, cachedOnly);
        }
        ImageUtil.addBitmapToCache(KEY, avatar);
        return avatar;
    }

    public static Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
        Contact c = user.getContact();
        if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
            return get(c, size, cachedOnly);
        } else {
            return getImpl(user, size, cachedOnly);
        }
    }

    private static Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
        final String KEY = key(user, size);
        Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
        if (avatar != null || cachedOnly) {
            return avatar;
        }
        if (user.getAvatar() != null) {
            avatar = AvatarUtil.getAvatar(user.getAvatar(), size);
        }
        if (avatar == null) {
            Contact contact = user.getContact();
            if (contact != null) {
                avatar = get(contact, size, cachedOnly);
            } else {
                avatar = get(user.getName(), size, cachedOnly);
            }
        }
        ImageUtil.addBitmapToCache(KEY, avatar);
        return avatar;
    }

    public static void clear(Contact contact) {
        synchronized (INSTANCE.sizes) {
            for (Integer size : INSTANCE.sizes) {
                ImageUtil.removeBitmapFromCache(key(contact, size));
            }
        }
    }

    private static String key(Contact contact, int size) {
        synchronized (INSTANCE.sizes) {
            if (!INSTANCE.sizes.contains(size)) {
                INSTANCE.sizes.add(size);
            }
        }
        return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
                + contact.getJid() + "_" + String.valueOf(size);
    }

    private static String key(MucOptions.User user, int size) {
        synchronized (INSTANCE.sizes) {
            if (!INSTANCE.sizes.contains(size)) {
                INSTANCE.sizes.add(size);
            }
        }
        return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
                + user.getFullJid() + "_" + String.valueOf(size);
    }

    public static Bitmap get(ListItem item, int size) {
        return get(item,size,false);
    }

    public static Bitmap get(ListItem item, int size, boolean cachedOnly) {
        if (item instanceof Contact) {
            return get((Contact) item, size,cachedOnly);
        } else if (item instanceof Bookmark) {
            Bookmark bookmark = (Bookmark) item;
            if (bookmark.getConversation() != null) {
                return get(bookmark.getConversation(), size, cachedOnly);
            } else {
                return get(bookmark.getDisplayName(), size, cachedOnly);
            }
        } else {
            return get(item.getDisplayName(), size, cachedOnly);
        }
    }

    public static Bitmap get(Conversation conversation, int size) {
        return get(conversation,size,false);
    }

    public static Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
        if (conversation.getMode() == Conversation.MODE_SINGLE) {
            return get(conversation.getContact(), size, cachedOnly);
        } else {
            return get(conversation.getMucOptions(), size, cachedOnly);
        }
    }

    public static void clear(MucOptions options) {
        synchronized (INSTANCE.sizes) {
            for (Integer size : INSTANCE.sizes) {
                ImageUtil.removeBitmapFromCache(key(options, size));
            }
        }
    }

    private static String key(MucOptions options, int size) {
        synchronized (INSTANCE.sizes) {
            if (!INSTANCE.sizes.contains(size)) {
                INSTANCE.sizes.add(size);
            }
        }
        return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
                + "_" + String.valueOf(size);
    }

    public static Bitmap get(Account account, int size) {
        return get(account, size, false);
    }

    public static Bitmap get(Account account, int size, boolean cachedOnly) {
        final String KEY = key(account, size);
        Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
        if (avatar != null || cachedOnly) {
            return avatar;
        }
        avatar = AvatarUtil.getAvatar(account.getAvatar(), size);
        if (avatar == null) {
            avatar = get(account.getJid().toBareJid().toString(), size,false);
        }
        ImageUtil.addBitmapToCache(KEY, avatar);
        return avatar;
    }

    public static Bitmap get(Message message, int size, boolean cachedOnly) {
        final Conversation conversation = message.getConversation();
        if (MessageUtil.isIncomingMessage(message)) {
            Contact c = message.getContact();
            if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
                return get(c, size, cachedOnly);
            } else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
                MucOptions.User user = conversation.getMucOptions().findUser(message.getCounterpart().getResourcepart());
                if (user != null) {
                    return getImpl(user,size,cachedOnly);
                }
            }
            return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
        } else  {
            return get(conversation.getAccount(), size, cachedOnly);
        }
    }

    public static void clear(Account account) {
        synchronized (INSTANCE.sizes) {
            for (Integer size : INSTANCE.sizes) {
                ImageUtil.removeBitmapFromCache(key(account, size));
            }
        }
    }

    public static void clear(MucOptions.User user) {
        synchronized (INSTANCE.sizes) {
            for (Integer size : INSTANCE.sizes) {
                ImageUtil.removeBitmapFromCache(key(user, size));
            }
        }
    }

    private static String key(Account account, int size) {
        synchronized (INSTANCE.sizes) {
            if (!INSTANCE.sizes.contains(size)) {
                INSTANCE.sizes.add(size);
            }
        }
        return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
                + String.valueOf(size);
    }

    public static Bitmap get(String name, int size) {
        return get(name,size,false);
    }

    public static Bitmap get(final String name, final int size, boolean cachedOnly) {
        final String KEY = key(name, size);
        Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY);
        if (bitmap != null || cachedOnly) {
            return bitmap;
        }

        bitmap = AvatarBitmapUtil.createAvatarBitmap(name, size);
        ImageUtil.addBitmapToCache(KEY, bitmap);
        return bitmap;
    }

    private static String key(String name, int size) {
        synchronized (INSTANCE.sizes) {
            if (!INSTANCE.sizes.contains(size)) {
                INSTANCE.sizes.add(size);
            }
        }
        return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
    }

    public static void clear(Conversation conversation) {
        if (conversation.getMode() == Conversation.MODE_SINGLE) {
            clear(conversation.getContact());
        } else {
            clear(conversation.getMucOptions());
        }
    }

    private static Bitmap get(MucOptions mucOptions, int size,  boolean cachedOnly) {
        final String KEY = key(mucOptions, size);
        Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY);
        if (bitmap != null || cachedOnly) {
            return bitmap;
        }

        bitmap = AvatarBitmapUtil.createAvatarBitmap(mucOptions, size);
        ImageUtil.addBitmapToCache(KEY, bitmap);
        return bitmap;
    }
}