summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/AvatarUriUtil.java
blob: df7d08527df503be8b0b181a132a17b2f5f1c363 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.messaging.util;

import android.graphics.Color;
import android.net.Uri;
import android.net.Uri.Builder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.android.messaging.datamodel.data.ParticipantData;

import java.util.ArrayList;
import java.util.List;

/**
 * A helper utility for creating {@link android.net.Uri}s to describe what avatar to fetch or
 * generate and will help verify and extract information from avatar {@link android.net.Uri}s.
 *
 * There are three types of avatar {@link android.net.Uri}.
 *
 * 1) Group Avatars - These are avatars which are used to represent a group conversation. Group
 * avatars uris are basically multiple avatar uri which can be any of the below types but not
 * another group avatar. The group avatars can hold anywhere from two to four avatars uri and can
 * be in any of the following format
 * messaging://avatar/g?p=<avatarUri>&p=<avatarUri2>
 * messaging://avatar/g?p=<avatarUri>&p=<avatarUri2>&p=<avatarUri3>
 * messaging://avatar/g?p=<avatarUri>&p=<avatarUri2>&p=<avatarUri3>&p=<avatarUri4>
 *
 * 2) Local Resource - A local resource avatar is use when there is a profile photo for the
 * participant. This can be any local resource.
 *
 * 3) Letter Tile - A letter tile is used when a participant has a name but no profile photo. A
 * letter tile will contain the first code point of the participant's name and a background color
 * based on the hash of the participant's full name. Letter tiles will be in the following format.
 * messaging://avatar/l?n=<fullName>
 *
 * 4) Default Avatars - These are avatars are used when the participant has no profile photo or
 * name. In these cases we use the default person icon with a color background. The color
 * background is based on a hash of the normalized phone number.
 *
 * 5) Default Background Avatars - This is a special case for Default Avatars where we use the
 * default background color for the default avatar.
 *
 * 6) SIM Selector Avatars - These are avatars used in the SIM selector. This may either be a
 * regular local resource avatar (2) or an avatar with a SIM identifier (i.e. SIM background with
 * a letter or a slot number).
 */
public class AvatarUriUtil {
    private static final int MAX_GROUP_PARTICIPANTS = 4;

    public static final String TYPE_GROUP_URI = "g";
    public static final String TYPE_LOCAL_RESOURCE_URI = "r";
    public static final String TYPE_LETTER_TILE_URI = "l";
    public static final String TYPE_DEFAULT_URI = "d";
    public static final String TYPE_DEFAULT_BACKGROUND_URI = "b";
    public static final String TYPE_SIM_SELECTOR_URI = "s";

    private static final String SCHEME = "messaging";
    private static final String AUTHORITY = "avatar";
    private static final String PARAM_NAME = "n";
    private static final String PARAM_PRIMARY_URI = "m";
    private static final String PARAM_FALLBACK_URI = "f";
    private static final String PARAM_PARTICIPANT = "p";
    private static final String PARAM_IDENTIFIER = "i";
    private static final String PARAM_SIM_COLOR = "c";
    private static final String PARAM_SIM_SELECTED = "s";
    private static final String PARAM_SIM_INCOMING = "g";

    public static final Uri DEFAULT_BACKGROUND_AVATAR = new Uri.Builder().scheme(SCHEME)
            .authority(AUTHORITY).appendPath(TYPE_DEFAULT_BACKGROUND_URI).build();

    private static final Uri BLANK_SIM_INDICATOR_INCOMING_URI = createSimIconUri("",
            false /* selected */, Color.TRANSPARENT, true /* incoming */);
    private static final Uri BLANK_SIM_INDICATOR_OUTGOING_URI = createSimIconUri("",
            false /* selected */, Color.TRANSPARENT, false /* incoming */);

    /**
     * Creates an avatar uri based on a list of ParticipantData. The list of participants may not
     * be null or empty. Depending on the size of the list either a group avatar uri will be create
     * or an individual's avatar will be created. This will never return a null uri.
     */
    public static Uri createAvatarUri(@NonNull final List<ParticipantData> participants) {
        Assert.notNull(participants);
        Assert.isTrue(!participants.isEmpty());

        if (participants.size() == 1) {
            return createAvatarUri(participants.get(0));
        }

        final int numParticipants = Math.min(participants.size(), MAX_GROUP_PARTICIPANTS);
        final ArrayList<Uri> avatarUris = new ArrayList<Uri>(numParticipants);
        for (int i = 0; i < numParticipants; i++) {
            avatarUris.add(createAvatarUri(participants.get(i)));
        }
        return AvatarUriUtil.joinAvatarUriToGroup(avatarUris);
    }

    /**
     * Joins together a list of valid avatar uri into a group uri.The list of participants may not
     * be null or empty. If a lit of one is given then the first element will be return back
     * instead of a group avatar uri. All uris in the list must be a valid avatar uri. This will
     * never return a null uri.
     */
    public static Uri joinAvatarUriToGroup(@NonNull final List<Uri> avatarUris) {
        Assert.notNull(avatarUris);
        Assert.isTrue(!avatarUris.isEmpty());

        if (avatarUris.size() == 1) {
            final Uri firstAvatar = avatarUris.get(0);
            Assert.isTrue(AvatarUriUtil.isAvatarUri(firstAvatar));
            return firstAvatar;
        }

        final Builder builder = new Builder();
        builder.scheme(SCHEME);
        builder.authority(AUTHORITY);
        builder.appendPath(TYPE_GROUP_URI);
        final int numParticipants = Math.min(avatarUris.size(), MAX_GROUP_PARTICIPANTS);
        for (int i = 0; i < numParticipants; i++) {
            final Uri uri = avatarUris.get(i);
            Assert.notNull(uri);
            Assert.isTrue(UriUtil.isLocalResourceUri(uri) || AvatarUriUtil.isAvatarUri(uri));
            builder.appendQueryParameter(PARAM_PARTICIPANT, uri.toString());
        }
        return builder.build();
    }

    /**
     * Creates an avatar uri based on ParticipantData which may not be null and expected to have
     * profilePhotoUri, fullName and normalizedDestination populated. This will never return a null
     * uri.
     */
    public static Uri createAvatarUri(@NonNull final ParticipantData participant) {
        Assert.notNull(participant);
        final String photoUriString = participant.getProfilePhotoUri();
        final Uri profilePhotoUri = (photoUriString == null) ? null : Uri.parse(photoUriString);
        final String name = participant.getFullName();
        final String destination = participant.getNormalizedDestination();
        final String contactLookupKey = participant.getLookupKey();
        return createAvatarUri(profilePhotoUri, name, destination, contactLookupKey);
    }

    /**
     * Creates an avatar uri based on a the input data.
     */
    public static Uri createAvatarUri(final Uri profilePhotoUri, final CharSequence name,
            final String defaultIdentifier, final String contactLookupKey) {
        Uri generatedUri;
        if (!TextUtils.isEmpty(name) && isValidFirstCharacter(name)) {
            generatedUri = AvatarUriUtil.fromName(name, contactLookupKey);
        } else {
            final String identifier = TextUtils.isEmpty(contactLookupKey)
                    ? defaultIdentifier : contactLookupKey;
            generatedUri = AvatarUriUtil.fromIdentifier(identifier);
        }

        if (profilePhotoUri != null) {
            if (UriUtil.isLocalResourceUri(profilePhotoUri)) {
                return fromLocalResourceWithFallback(profilePhotoUri, generatedUri);
            } else {
                return profilePhotoUri;
            }
        } else {
            return generatedUri;
        }
    }

    public static boolean isValidFirstCharacter(final CharSequence name) {
        final char c = name.charAt(0);
        return c != '+';
    }

    /**
     * Creates an avatar URI used for the SIM selector.
     * @param participantData the self participant data for an <i>active</i> SIM
     * @param slotIdentifier when null, this will simply use a regular avatar; otherwise, the
     *        first letter of slotIdentifier will be used for the icon.
     * @param selected is this the currently selected SIM?
     * @param incoming is this for an incoming message or outgoing message?
     */
    public static Uri createAvatarUri(@NonNull final ParticipantData participantData,
            @Nullable final String slotIdentifier, final boolean selected, final boolean incoming) {
        Assert.notNull(participantData);
        Assert.isTrue(participantData.isActiveSubscription());
        Assert.isTrue(!TextUtils.isEmpty(slotIdentifier) ||
                !TextUtils.isEmpty(participantData.getProfilePhotoUri()));
        if (TextUtils.isEmpty(slotIdentifier)) {
            return createAvatarUri(participantData);
        }

        return createSimIconUri(slotIdentifier, selected, participantData.getSubscriptionColor(),
                incoming);
    }

    private static Uri createSimIconUri(final String slotIdentifier, final boolean selected,
            final int subColor, final boolean incoming) {
        final Builder builder = new Builder();
        builder.scheme(SCHEME);
        builder.authority(AUTHORITY);
        builder.appendPath(TYPE_SIM_SELECTOR_URI);
        builder.appendQueryParameter(PARAM_IDENTIFIER, slotIdentifier);
        builder.appendQueryParameter(PARAM_SIM_COLOR, String.valueOf(subColor));
        builder.appendQueryParameter(PARAM_SIM_SELECTED, String.valueOf(selected));
        builder.appendQueryParameter(PARAM_SIM_INCOMING, String.valueOf(incoming));
        return builder.build();
    }

    public static Uri getBlankSimIndicatorUri(final boolean incoming) {
        return incoming ? BLANK_SIM_INDICATOR_INCOMING_URI : BLANK_SIM_INDICATOR_OUTGOING_URI;
    }

    /**
     * Creates an avatar uri from the given local resource Uri, followed by a fallback Uri in case
     * the local resource one could not be loaded.
     */
    private static Uri fromLocalResourceWithFallback(@NonNull final Uri profilePhotoUri,
            @NonNull Uri fallbackUri) {
        Assert.notNull(profilePhotoUri);
        Assert.notNull(fallbackUri);
        final Builder builder = new Builder();
        builder.scheme(SCHEME);
        builder.authority(AUTHORITY);
        builder.appendPath(TYPE_LOCAL_RESOURCE_URI);
        builder.appendQueryParameter(PARAM_PRIMARY_URI, profilePhotoUri.toString());
        builder.appendQueryParameter(PARAM_FALLBACK_URI, fallbackUri.toString());
        return builder.build();
    }

    private static Uri fromName(@NonNull final CharSequence name, final String contactLookupKey) {
        Assert.notNull(name);
        final Builder builder = new Builder();
        builder.scheme(SCHEME);
        builder.authority(AUTHORITY);
        builder.appendPath(TYPE_LETTER_TILE_URI);
        final String nameString = String.valueOf(name);
        builder.appendQueryParameter(PARAM_NAME, nameString);
        final String identifier =
                TextUtils.isEmpty(contactLookupKey) ? nameString : contactLookupKey;
        builder.appendQueryParameter(PARAM_IDENTIFIER, identifier);
        return builder.build();
    }

    private static Uri fromIdentifier(@NonNull final String identifier) {
        final Builder builder = new Builder();
        builder.scheme(SCHEME);
        builder.authority(AUTHORITY);
        builder.appendPath(TYPE_DEFAULT_URI);
        builder.appendQueryParameter(PARAM_IDENTIFIER, identifier);
        return builder.build();
    }

    public static boolean isAvatarUri(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return uri != null && TextUtils.equals(SCHEME, uri.getScheme()) &&
                TextUtils.equals(AUTHORITY, uri.getAuthority());
    }

    public static String getAvatarType(@NonNull final Uri uri) {
        Assert.notNull(uri);
        final List<String> path = uri.getPathSegments();
        return path.isEmpty() ? null : path.get(0);
    }

    public static String getIdentifier(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return uri.getQueryParameter(PARAM_IDENTIFIER);
    }

    public static String getName(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return uri.getQueryParameter(PARAM_NAME);
    }

    public static List<String> getGroupParticipantUris(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return uri.getQueryParameters(PARAM_PARTICIPANT);
    }

    public static int getSimColor(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return Integer.valueOf(uri.getQueryParameter(PARAM_SIM_COLOR));
    }

    public static boolean getSimSelected(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return Boolean.valueOf(uri.getQueryParameter(PARAM_SIM_SELECTED));
    }

    public static boolean getSimIncoming(@NonNull final Uri uri) {
        Assert.notNull(uri);
        return Boolean.valueOf(uri.getQueryParameter(PARAM_SIM_INCOMING));
    }

    public static Uri getPrimaryUri(@NonNull final Uri uri) {
        Assert.notNull(uri);
        final String primaryUriString = uri.getQueryParameter(PARAM_PRIMARY_URI);
        return primaryUriString == null ? null : Uri.parse(primaryUriString);
    }

    public static Uri getFallbackUri(@NonNull final Uri uri) {
        Assert.notNull(uri);
        final String fallbackUriString = uri.getQueryParameter(PARAM_FALLBACK_URI);
        return fallbackUriString == null ? null : Uri.parse(fallbackUriString);
    }
}