1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.media;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.res.Resources;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Bitmap;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Canvas;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Matrix;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Paint;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Rect;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.RectF;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Typeface;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.drawable.BitmapDrawable;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.media.ExifInterface;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.AvatarUriUtil;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.UriUtil;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.FileNotFoundException;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.IOException;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.InputStream;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.List;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class AvatarRequest extends UriImageRequest<AvatarRequestDescriptor> {
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static Bitmap sDefaultPersonBitmap;
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static Bitmap sDefaultPersonBitmapLarge;
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public AvatarRequest(final Context context,
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final AvatarRequestDescriptor descriptor) {
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super(context, descriptor);
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected InputStream getInputStreamForResource() throws FileNotFoundException {
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (UriUtil.isLocalResourceUri(mDescriptor.uri)) {
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return super.getInputStreamForResource();
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Uri primaryUri = AvatarUriUtil.getPrimaryUri(mDescriptor.uri);
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            Assert.isTrue(UriUtil.isLocalResourceUri(primaryUri));
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return mContext.getContentResolver().openInputStream(primaryUri);
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * We can load multiple types of images for avatars depending on the uri. The uri should be
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * built by {@link com.android.messaging.util.AvatarUriUtil} which will decide on
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * what uri to build based on the available profile photo and name. Here we will check if the
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * image is a local resource (ie profile photo uri), if the resource isn't a local one we will
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * generate a tile with the first letter of the name.
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected ImageResource loadMediaInternal(List<MediaRequest<ImageResource>> chainedTasks)
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            throws IOException {
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.isNotMainThread();
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        String avatarType = AvatarUriUtil.getAvatarType(mDescriptor.uri);
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Bitmap bitmap = null;
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int orientation = ExifInterface.ORIENTATION_NORMAL;
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final boolean isLocalResourceUri = UriUtil.isLocalResourceUri(mDescriptor.uri) ||
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                AvatarUriUtil.TYPE_LOCAL_RESOURCE_URI.equals(avatarType);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isLocalResourceUri) {
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            try {
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ImageResource imageResource = super.loadMediaInternal(chainedTasks);
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bitmap = imageResource.getBitmap();
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                orientation = imageResource.mOrientation;
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            } catch (Exception ex) {
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // If we encountered any exceptions trying to load the local avatar resource,
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // fall back to generated avatar.
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                LogUtil.w(LogUtil.BUGLE_IMAGE_TAG, "AvatarRequest: failed to load local avatar " +
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        "resource, switching to fallback rendering", ex);
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int width = mDescriptor.desiredWidth;
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int height = mDescriptor.desiredHeight;
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Check to see if we already got the bitmap. If not get a fallback avatar
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (bitmap == null) {
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            Uri generatedUri = mDescriptor.uri;
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (isLocalResourceUri) {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // If we are here, we just failed to load the local resource. Use the fallback Uri
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // if possible.
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                generatedUri = AvatarUriUtil.getFallbackUri(mDescriptor.uri);
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (generatedUri == null) {
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    // No fallback Uri was provided, use the default avatar.
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    generatedUri = AvatarUriUtil.DEFAULT_BACKGROUND_AVATAR;
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            avatarType = AvatarUriUtil.getAvatarType(generatedUri);
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (AvatarUriUtil.TYPE_LETTER_TILE_URI.equals(avatarType)) {
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final String name = AvatarUriUtil.getName(generatedUri);
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bitmap = renderLetterTile(name, width, height);
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            } else {
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bitmap = renderDefaultAvatar(width, height);
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return new DecodedImageResource(getKey(), bitmap, orientation);
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Bitmap renderDefaultAvatar(final int width, final int height) {
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Bitmap bitmap = getBitmapPool().createOrReuseBitmap(width, height,
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getBackgroundColor());
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Canvas canvas = new Canvas(bitmap);
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (sDefaultPersonBitmap == null) {
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final BitmapDrawable defaultPerson = (BitmapDrawable) mContext.getResources()
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    .getDrawable(R.drawable.ic_person_light);
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sDefaultPersonBitmap = defaultPerson.getBitmap();
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (sDefaultPersonBitmapLarge == null) {
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final BitmapDrawable largeDefaultPerson = (BitmapDrawable) mContext.getResources()
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    .getDrawable(R.drawable.ic_person_light_large);
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sDefaultPersonBitmapLarge = largeDefaultPerson.getBitmap();
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Bitmap defaultPerson = null;
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mDescriptor.isWearBackground) {
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final BitmapDrawable wearDefaultPerson = (BitmapDrawable) mContext.getResources()
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    .getDrawable(R.drawable.ic_person_wear);
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            defaultPerson = wearDefaultPerson.getBitmap();
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final boolean isLargeDefault = (width > sDefaultPersonBitmap.getWidth()) ||
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    (height > sDefaultPersonBitmap.getHeight());
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            defaultPerson =
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    isLargeDefault ? sDefaultPersonBitmapLarge : sDefaultPersonBitmap;
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Matrix matrix = new Matrix();
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final RectF source = new RectF(0, 0, defaultPerson.getWidth(), defaultPerson.getHeight());
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final RectF dest = new RectF(0, 0, width, height);
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        matrix.setRectToRect(source, dest, Matrix.ScaleToFit.FILL);
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        canvas.drawBitmap(defaultPerson, matrix, paint);
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return bitmap;
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Bitmap renderLetterTile(final String name, final int width, final int height) {
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final float halfWidth = width / 2;
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final float halfHeight = height / 2;
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int minOfWidthAndHeight = Math.min(width, height);
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Bitmap bitmap = getBitmapPool().createOrReuseBitmap(width, height,
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getBackgroundColor());
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Resources resources = mContext.getResources();
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        paint.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        paint.setColor(resources.getColor(R.color.letter_tile_font_color));
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final float letterToTileRatio = resources.getFraction(R.dimen.letter_to_tile_ratio, 1, 1);
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        paint.setTextSize(letterToTileRatio * minOfWidthAndHeight);
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String firstCharString = name.substring(0, 1).toUpperCase();
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Rect textBound = new Rect();
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        paint.getTextBounds(firstCharString, 0, 1, textBound);
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Canvas canvas = new Canvas(bitmap);
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final float xOffset = halfWidth - textBound.centerX();
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final float yOffset = halfHeight - textBound.centerY();
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        canvas.drawText(firstCharString, xOffset, yOffset, paint);
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return bitmap;
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int getBackgroundColor() {
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mContext.getResources().getColor(R.color.primary_color);
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
184d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
185d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
186d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public int getCacheId() {
187d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return BugleMediaCacheManager.AVATAR_IMAGE_CACHE;
188d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
189d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
190