Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.contacts.common.list; |
| 17 | |
| 18 | import android.app.ActivityManager; |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.res.Resources; |
| 23 | import android.database.Cursor; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.Paint; |
| 28 | import android.graphics.Paint.FontMetricsInt; |
| 29 | import android.graphics.Rect; |
| 30 | import android.graphics.drawable.BitmapDrawable; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.net.Uri; |
| 33 | import android.os.AsyncTask; |
| 34 | import android.provider.ContactsContract; |
| 35 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
| 37 | import android.provider.ContactsContract.Contacts; |
| 38 | import android.provider.ContactsContract.Data; |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 39 | import android.support.v4.graphics.drawable.RoundedBitmapDrawable; |
Chris Craik | 93fb435 | 2014-08-05 18:51:49 -0700 | [diff] [blame] | 40 | import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; |
Tyler Gunn | 5cf7f01 | 2014-09-10 15:20:21 -0700 | [diff] [blame] | 41 | import android.telecom.PhoneAccount; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 42 | import android.text.TextPaint; |
| 43 | import android.text.TextUtils; |
| 44 | import android.text.TextUtils.TruncateAt; |
| 45 | |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 46 | import com.android.contacts.common.ContactsUtils; |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 47 | import com.android.contacts.common.ContactPhotoManager; |
| 48 | import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 49 | import com.android.contacts.common.R; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 50 | import com.android.contacts.common.util.ImplicitIntentsUtil; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Constructs shortcut intents. |
| 54 | */ |
| 55 | public class ShortcutIntentBuilder { |
| 56 | |
| 57 | private static final String[] CONTACT_COLUMNS = { |
| 58 | Contacts.DISPLAY_NAME, |
| 59 | Contacts.PHOTO_ID, |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 60 | Contacts.LOOKUP_KEY |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | private static final int CONTACT_DISPLAY_NAME_COLUMN_INDEX = 0; |
| 64 | private static final int CONTACT_PHOTO_ID_COLUMN_INDEX = 1; |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 65 | private static final int CONTACT_LOOKUP_KEY_COLUMN_INDEX = 2; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 66 | |
| 67 | private static final String[] PHONE_COLUMNS = { |
| 68 | Phone.DISPLAY_NAME, |
| 69 | Phone.PHOTO_ID, |
| 70 | Phone.NUMBER, |
| 71 | Phone.TYPE, |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 72 | Phone.LABEL, |
| 73 | Phone.LOOKUP_KEY |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | private static final int PHONE_DISPLAY_NAME_COLUMN_INDEX = 0; |
| 77 | private static final int PHONE_PHOTO_ID_COLUMN_INDEX = 1; |
| 78 | private static final int PHONE_NUMBER_COLUMN_INDEX = 2; |
| 79 | private static final int PHONE_TYPE_COLUMN_INDEX = 3; |
| 80 | private static final int PHONE_LABEL_COLUMN_INDEX = 4; |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 81 | private static final int PHONE_LOOKUP_KEY_COLUMN_INDEX = 5; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 82 | |
| 83 | private static final String[] PHOTO_COLUMNS = { |
| 84 | Photo.PHOTO, |
| 85 | }; |
| 86 | |
| 87 | private static final int PHOTO_PHOTO_COLUMN_INDEX = 0; |
| 88 | |
| 89 | private static final String PHOTO_SELECTION = Photo._ID + "=?"; |
| 90 | |
| 91 | private final OnShortcutIntentCreatedListener mListener; |
| 92 | private final Context mContext; |
| 93 | private int mIconSize; |
| 94 | private final int mIconDensity; |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 95 | private final int mOverlayTextBackgroundColor; |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 96 | private final Resources mResources; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * This is a hidden API of the launcher in JellyBean that allows us to disable the animation |
Brian Attwell | 8a0cbec | 2014-07-07 16:39:01 -0700 | [diff] [blame] | 100 | * that it would usually do, because it interferes with our own animation for QuickContact. |
| 101 | * This is needed since some versions of the launcher override the intent flags and therefore |
| 102 | * ignore Intent.FLAG_ACTIVITY_NO_ANIMATION. |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 103 | */ |
| 104 | public static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION = |
| 105 | "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION"; |
| 106 | |
| 107 | /** |
| 108 | * Listener interface. |
| 109 | */ |
| 110 | public interface OnShortcutIntentCreatedListener { |
| 111 | |
| 112 | /** |
| 113 | * Callback for shortcut intent creation. |
| 114 | * |
| 115 | * @param uri the original URI for which the shortcut intent has been |
| 116 | * created. |
| 117 | * @param shortcutIntent resulting shortcut intent. |
| 118 | */ |
| 119 | void onShortcutIntentCreated(Uri uri, Intent shortcutIntent); |
| 120 | } |
| 121 | |
| 122 | public ShortcutIntentBuilder(Context context, OnShortcutIntentCreatedListener listener) { |
| 123 | mContext = context; |
| 124 | mListener = listener; |
| 125 | |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 126 | mResources = context.getResources(); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 127 | final ActivityManager am = (ActivityManager) context |
| 128 | .getSystemService(Context.ACTIVITY_SERVICE); |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 129 | mIconSize = mResources.getDimensionPixelSize(R.dimen.shortcut_icon_size); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 130 | if (mIconSize == 0) { |
| 131 | mIconSize = am.getLauncherLargeIconSize(); |
| 132 | } |
| 133 | mIconDensity = am.getLauncherLargeIconDensity(); |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 134 | mOverlayTextBackgroundColor = mResources.getColor(R.color.shortcut_overlay_text_background); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void createContactShortcutIntent(Uri contactUri) { |
| 138 | new ContactLoadingAsyncTask(contactUri).execute(); |
| 139 | } |
| 140 | |
| 141 | public void createPhoneNumberShortcutIntent(Uri dataUri, String shortcutAction) { |
| 142 | new PhoneNumberLoadingAsyncTask(dataUri, shortcutAction).execute(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * An asynchronous task that loads name, photo and other data from the database. |
| 147 | */ |
| 148 | private abstract class LoadingAsyncTask extends AsyncTask<Void, Void, Void> { |
| 149 | protected Uri mUri; |
| 150 | protected String mContentType; |
| 151 | protected String mDisplayName; |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 152 | protected String mLookupKey; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 153 | protected byte[] mBitmapData; |
| 154 | protected long mPhotoId; |
| 155 | |
| 156 | public LoadingAsyncTask(Uri uri) { |
| 157 | mUri = uri; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | protected Void doInBackground(Void... params) { |
| 162 | mContentType = mContext.getContentResolver().getType(mUri); |
| 163 | loadData(); |
| 164 | loadPhoto(); |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | protected abstract void loadData(); |
| 169 | |
| 170 | private void loadPhoto() { |
| 171 | if (mPhotoId == 0) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | ContentResolver resolver = mContext.getContentResolver(); |
| 176 | Cursor cursor = resolver.query(Data.CONTENT_URI, PHOTO_COLUMNS, PHOTO_SELECTION, |
| 177 | new String[] { String.valueOf(mPhotoId) }, null); |
| 178 | if (cursor != null) { |
| 179 | try { |
| 180 | if (cursor.moveToFirst()) { |
| 181 | mBitmapData = cursor.getBlob(PHOTO_PHOTO_COLUMN_INDEX); |
| 182 | } |
| 183 | } finally { |
| 184 | cursor.close(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private final class ContactLoadingAsyncTask extends LoadingAsyncTask { |
| 191 | public ContactLoadingAsyncTask(Uri uri) { |
| 192 | super(uri); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected void loadData() { |
| 197 | ContentResolver resolver = mContext.getContentResolver(); |
| 198 | Cursor cursor = resolver.query(mUri, CONTACT_COLUMNS, null, null, null); |
| 199 | if (cursor != null) { |
| 200 | try { |
| 201 | if (cursor.moveToFirst()) { |
| 202 | mDisplayName = cursor.getString(CONTACT_DISPLAY_NAME_COLUMN_INDEX); |
| 203 | mPhotoId = cursor.getLong(CONTACT_PHOTO_ID_COLUMN_INDEX); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 204 | mLookupKey = cursor.getString(CONTACT_LOOKUP_KEY_COLUMN_INDEX); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 205 | } |
| 206 | } finally { |
| 207 | cursor.close(); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | @Override |
| 212 | protected void onPostExecute(Void result) { |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 213 | createContactShortcutIntent(mUri, mContentType, mDisplayName, mLookupKey, mBitmapData); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | private final class PhoneNumberLoadingAsyncTask extends LoadingAsyncTask { |
| 218 | private final String mShortcutAction; |
| 219 | private String mPhoneNumber; |
| 220 | private int mPhoneType; |
| 221 | private String mPhoneLabel; |
| 222 | |
| 223 | public PhoneNumberLoadingAsyncTask(Uri uri, String shortcutAction) { |
| 224 | super(uri); |
| 225 | mShortcutAction = shortcutAction; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | protected void loadData() { |
| 230 | ContentResolver resolver = mContext.getContentResolver(); |
| 231 | Cursor cursor = resolver.query(mUri, PHONE_COLUMNS, null, null, null); |
| 232 | if (cursor != null) { |
| 233 | try { |
| 234 | if (cursor.moveToFirst()) { |
| 235 | mDisplayName = cursor.getString(PHONE_DISPLAY_NAME_COLUMN_INDEX); |
| 236 | mPhotoId = cursor.getLong(PHONE_PHOTO_ID_COLUMN_INDEX); |
| 237 | mPhoneNumber = cursor.getString(PHONE_NUMBER_COLUMN_INDEX); |
| 238 | mPhoneType = cursor.getInt(PHONE_TYPE_COLUMN_INDEX); |
| 239 | mPhoneLabel = cursor.getString(PHONE_LABEL_COLUMN_INDEX); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 240 | mLookupKey = cursor.getString(PHONE_LOOKUP_KEY_COLUMN_INDEX); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 241 | } |
| 242 | } finally { |
| 243 | cursor.close(); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | protected void onPostExecute(Void result) { |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 250 | createPhoneNumberShortcutIntent(mUri, mDisplayName, mLookupKey, mBitmapData, |
| 251 | mPhoneNumber, mPhoneType, mPhoneLabel, mShortcutAction); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 255 | private Drawable getPhotoDrawable(byte[] bitmapData, String displayName, String lookupKey) { |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 256 | if (bitmapData != null) { |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 257 | Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, null); |
| 258 | return new BitmapDrawable(mContext.getResources(), bitmap); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 259 | } else { |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 260 | return ContactPhotoManager.getDefaultAvatarDrawableForContact(mContext.getResources(), |
Yorke Lee | c4a2a23 | 2014-04-28 17:53:42 -0700 | [diff] [blame] | 261 | false, new DefaultImageRequest(displayName, lookupKey, false)); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 262 | } |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private void createContactShortcutIntent(Uri contactUri, String contentType, String displayName, |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 266 | String lookupKey, byte[] bitmapData) { |
| 267 | Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 268 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 269 | final Intent shortcutIntent = ImplicitIntentsUtil.getIntentForQuickContactLauncherShortcut( |
| 270 | mContext, contactUri); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 271 | |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 272 | final Bitmap icon = generateQuickContactIcon(drawable); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 273 | |
| 274 | Intent intent = new Intent(); |
| 275 | intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); |
| 276 | intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
| 277 | if (TextUtils.isEmpty(displayName)) { |
| 278 | intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getResources().getString( |
| 279 | R.string.missing_name)); |
| 280 | } else { |
| 281 | intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, displayName); |
| 282 | } |
| 283 | |
| 284 | mListener.onShortcutIntentCreated(contactUri, intent); |
| 285 | } |
| 286 | |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 287 | private void createPhoneNumberShortcutIntent(Uri uri, String displayName, String lookupKey, |
| 288 | byte[] bitmapData, String phoneNumber, int phoneType, String phoneLabel, |
| 289 | String shortcutAction) { |
| 290 | Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 291 | |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 292 | Bitmap bitmap; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 293 | Uri phoneUri; |
| 294 | if (Intent.ACTION_CALL.equals(shortcutAction)) { |
| 295 | // Make the URI a direct tel: URI so that it will always continue to work |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 296 | phoneUri = Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 297 | bitmap = generatePhoneNumberIcon(drawable, phoneType, phoneLabel, |
Andrew Lee | 2b23191 | 2015-03-27 15:38:39 -0700 | [diff] [blame] | 298 | R.drawable.ic_call); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 299 | } else { |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 300 | phoneUri = Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phoneNumber, null); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 301 | bitmap = generatePhoneNumberIcon(drawable, phoneType, phoneLabel, |
Walter Jang | 353570e | 2016-05-04 17:43:56 -0700 | [diff] [blame] | 302 | R.drawable.ic_message_24dp_mirrored); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | Intent shortcutIntent = new Intent(shortcutAction, phoneUri); |
| 306 | shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 307 | |
| 308 | Intent intent = new Intent(); |
| 309 | intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap); |
| 310 | intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
guanxiongliu | 4f715ff | 2016-03-10 14:14:54 -0800 | [diff] [blame] | 311 | |
| 312 | if (TextUtils.isEmpty(displayName)) { |
| 313 | displayName = mContext.getResources().getString(R.string.missing_name); |
| 314 | } |
| 315 | if (TextUtils.equals(shortcutAction, Intent.ACTION_CALL)) { |
| 316 | intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, |
| 317 | mContext.getResources().getString(R.string.call_by_shortcut, displayName)); |
| 318 | } else if (TextUtils.equals(shortcutAction, Intent.ACTION_SENDTO)) { |
| 319 | intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, |
| 320 | mContext.getResources().getString(R.string.sms_by_shortcut, displayName)); |
| 321 | } |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 322 | |
| 323 | mListener.onShortcutIntentCreated(uri, intent); |
| 324 | } |
| 325 | |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 326 | private Bitmap generateQuickContactIcon(Drawable photo) { |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 327 | |
| 328 | // Setup the drawing classes |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 329 | Bitmap bitmap = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888); |
| 330 | Canvas canvas = new Canvas(bitmap); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 331 | |
| 332 | // Copy in the photo |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 333 | Rect dst = new Rect(0,0, mIconSize, mIconSize); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 334 | photo.setBounds(dst); |
| 335 | photo.draw(canvas); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 336 | |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 337 | // Draw the icon with a rounded border |
Chris Craik | 93fb435 | 2014-08-05 18:51:49 -0700 | [diff] [blame] | 338 | RoundedBitmapDrawable roundedDrawable = |
| 339 | RoundedBitmapDrawableFactory.create(mResources, bitmap); |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 340 | roundedDrawable.setAntiAlias(true); |
| 341 | roundedDrawable.setCornerRadius(mIconSize / 2); |
| 342 | Bitmap roundedBitmap = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888); |
| 343 | canvas.setBitmap(roundedBitmap); |
| 344 | roundedDrawable.setBounds(dst); |
| 345 | roundedDrawable.draw(canvas); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 346 | canvas.setBitmap(null); |
| 347 | |
Brian Attwell | 5b53b8d | 2014-07-07 22:22:51 -0700 | [diff] [blame] | 348 | return roundedBitmap; |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone |
| 353 | * number, and if there is a photo also adds the call action icon. |
| 354 | */ |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 355 | private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 356 | int actionResId) { |
| 357 | final Resources r = mContext.getResources(); |
| 358 | final float density = r.getDisplayMetrics().density; |
| 359 | |
| 360 | Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)) |
| 361 | .getBitmap(); |
| 362 | |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 363 | Bitmap icon = generateQuickContactIcon(photo); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 364 | Canvas canvas = new Canvas(icon); |
| 365 | |
| 366 | // Copy in the photo |
| 367 | Paint photoPaint = new Paint(); |
| 368 | photoPaint.setDither(true); |
| 369 | photoPaint.setFilterBitmap(true); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 370 | Rect dst = new Rect(0, 0, mIconSize, mIconSize); |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 371 | |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 372 | // Create an overlay for the phone number type |
| 373 | CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel); |
| 374 | |
| 375 | if (overlay != null) { |
| 376 | TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); |
| 377 | textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size)); |
| 378 | textPaint.setColor(r.getColor(R.color.textColorIconOverlay)); |
| 379 | textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow)); |
| 380 | |
| 381 | final FontMetricsInt fmi = textPaint.getFontMetricsInt(); |
| 382 | |
| 383 | // First fill in a darker background around the text to be drawn |
| 384 | final Paint workPaint = new Paint(); |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 385 | workPaint.setColor(mOverlayTextBackgroundColor); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 386 | workPaint.setStyle(Paint.Style.FILL); |
| 387 | final int textPadding = r |
| 388 | .getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding); |
| 389 | final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2; |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 390 | dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 391 | canvas.drawRect(dst, workPaint); |
| 392 | |
Brian Attwell | 98771ce | 2014-09-02 12:54:53 -0700 | [diff] [blame] | 393 | overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 394 | final float textWidth = textPaint.measureText(overlay, 0, overlay.length()); |
| 395 | canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2, mIconSize |
| 396 | - fmi.descent - textPadding, textPaint); |
| 397 | } |
| 398 | |
| 399 | // Draw the phone action icon as an overlay |
Yorke Lee | 8baea88 | 2014-03-03 11:08:17 -0800 | [diff] [blame] | 400 | Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight()); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 401 | int iconWidth = icon.getWidth(); |
| 402 | dst.set(iconWidth - ((int) (20 * density)), -1, |
| 403 | iconWidth, ((int) (19 * density))); |
Chiao Cheng | beca856 | 2012-11-28 18:06:44 -0800 | [diff] [blame] | 404 | canvas.drawBitmap(phoneIcon, src, dst, photoPaint); |
| 405 | |
| 406 | canvas.setBitmap(null); |
| 407 | |
| 408 | return icon; |
| 409 | } |
| 410 | } |