summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/app/contactinfo/ContactPhotoLoader.java
blob: 71e4a16adf2c4f00b50282a725a7fe52e5ede86e (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
/*
 * 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.app.contactinfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import com.android.contacts.common.GeoUtil;
import com.android.contacts.common.lettertiles.LetterTileDrawable;
import com.android.dialer.app.R;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.phonenumbercache.ContactInfo;
import com.android.dialer.phonenumbercache.ContactInfoHelper;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * 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 final Context mContext;
  private final ContactInfo mContactInfo;

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

  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;
  }

  /** Create a contact photo icon bitmap appropriate for the ContactInfo. */
  public Bitmap loadPhotoIcon() {
    Assert.isWorkerThread();
    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 {
      InputStream input = mContext.getContentResolver().openInputStream(mContactInfo.photoUri);
      if (input == null) {
        LogUtil.w(
            "ContactPhotoLoader.createPhotoIconDrawable",
            "createPhotoIconDrawable: InputStream is null");
        return null;
      }
      Bitmap bitmap = BitmapFactory.decodeStream(input);
      input.close();

      if (bitmap == null) {
        LogUtil.w(
            "ContactPhotoLoader.createPhotoIconDrawable",
            "createPhotoIconDrawable: Bitmap is null");
        return null;
      }
      final RoundedBitmapDrawable drawable =
          RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
      drawable.setAntiAlias(true);
      drawable.setCircular(true);
      return drawable;
    } catch (IOException e) {
      LogUtil.e("ContactPhotoLoader.createPhotoIconDrawable", e.toString());
      return null;
    }
  }

  /** @return a {@link LetterTileDrawable} based on the ContactInfo. */
  private Drawable createLetterTileDrawable() {
    ContactInfoHelper helper =
        new ContactInfoHelper(mContext, GeoUtil.getCurrentCountryIso(mContext));
    LetterTileDrawable drawable = new LetterTileDrawable(mContext.getResources());
    drawable.setCanonicalDialerLetterTileDetails(
        mContactInfo.name,
        mContactInfo.lookupKey,
        LetterTileDrawable.SHAPE_CIRCLE,
        helper.isBusiness(mContactInfo.sourceType)
            ? LetterTileDrawable.TYPE_BUSINESS
            : LetterTileDrawable.TYPE_DEFAULT);
    return drawable;
  }
}