summaryrefslogtreecommitdiff
path: root/src/com/android/dialer/contactinfo/ContactPhotoLoader.java
blob: f36c438f601faa194de2d0271de091ea323d983c (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
/*
 * Copyright (C) 2016 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.dialer.contactinfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;

import com.android.contacts.common.GeoUtil;
import com.android.contacts.common.lettertiles.LetterTileDrawable;
import com.android.dialer.R;
import com.android.dialer.calllog.ContactInfo;
import com.android.dialer.calllog.ContactInfoHelper;
import com.android.dialer.util.Assert;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;

import java.io.IOException;
/**
 * Class to create the appropriate contact icon from a ContactInfo.
 * This class is for synchronous, blocking calls to generate bitmaps, while
 * ContactCommons.ContactPhotoManager is to cache, manage and update a ImageView asynchronously.
 */
public class ContactPhotoLoader {

    private static final String TAG = "ContactPhotoLoader";

    private final Context mContext;
    private final ContactInfo mContactInfo;

    public ContactPhotoLoader(Context context, ContactInfo contactInfo) {
        mContext = Preconditions.checkNotNull(context);
        mContactInfo = Preconditions.checkNotNull(contactInfo);
    }

    /**
     * Create a contact photo icon bitmap appropriate for the ContactInfo.
     */
    public Bitmap loadPhotoIcon() {
        Assert.assertNotUiThread("ContactPhotoLoader#loadPhotoIcon called on UI thread");
        int photoSize = mContext.getResources().getDimensionPixelSize(R.dimen.contact_photo_size);
        return drawableToBitmap(getIcon(), photoSize, photoSize);
    }

    @VisibleForTesting
    Drawable getIcon() {
        Drawable drawable = createPhotoIconDrawable();
        if (drawable == null) {
            drawable = createLetterTileDrawable();
        }
        return drawable;
    }

    /**
     * @return a {@link Drawable} of  circular photo icon if the photo can be loaded, {@code null}
     * otherwise.
     */
    @Nullable
    private Drawable createPhotoIconDrawable() {
        if (mContactInfo.photoUri == null) {
            return null;
        }
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(),
                    mContactInfo.photoUri);
            final RoundedBitmapDrawable drawable =
                    RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
            drawable.setAntiAlias(true);
            drawable.setCornerRadius(bitmap.getHeight() / 2);
            return drawable;
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            return null;
        }
    }

    /**
     * @return a {@link LetterTileDrawable} based on the ContactInfo.
     */
    private Drawable createLetterTileDrawable() {
        LetterTileDrawable drawable = new LetterTileDrawable(mContext.getResources());
        drawable.setIsCircular(true);
        ContactInfoHelper helper =
                new ContactInfoHelper(mContext, GeoUtil.getCurrentCountryIso(mContext));
        if (helper.isBusiness(mContactInfo.sourceType)) {
            drawable.setContactType(LetterTileDrawable.TYPE_BUSINESS);
        }
        drawable.setLetterAndColorFromContactDetails(mContactInfo.name, mContactInfo.lookupKey);
        return drawable;
    }

    private static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

}