Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 17 | |
| 18 | import android.annotation.TargetApi; |
| 19 | import android.app.job.JobInfo; |
| 20 | import android.app.job.JobParameters; |
| 21 | import android.app.job.JobScheduler; |
| 22 | import android.app.job.JobService; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.ContentUris; |
| 27 | import android.content.Context; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 30 | import android.content.pm.ShortcutInfo; |
| 31 | import android.content.pm.ShortcutManager; |
| 32 | import android.database.Cursor; |
| 33 | import android.graphics.Bitmap; |
| 34 | import android.graphics.BitmapFactory; |
| 35 | import android.graphics.BitmapRegionDecoder; |
| 36 | import android.graphics.Canvas; |
| 37 | import android.graphics.Rect; |
| 38 | import android.graphics.drawable.Drawable; |
| 39 | import android.graphics.drawable.Icon; |
| 40 | import android.net.Uri; |
| 41 | import android.os.AsyncTask; |
| 42 | import android.os.Build; |
| 43 | import android.os.PersistableBundle; |
| 44 | import android.provider.ContactsContract; |
| 45 | import android.provider.ContactsContract.Contacts; |
| 46 | import android.support.annotation.VisibleForTesting; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 47 | import android.support.v4.content.LocalBroadcastManager; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 48 | import android.util.Log; |
| 49 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 50 | import com.android.contacts.activities.RequestPermissionsActivity; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 51 | import com.android.contacts.compat.CompatUtils; |
| 52 | import com.android.contacts.util.BitmapUtil; |
| 53 | import com.android.contacts.util.ImplicitIntentsUtil; |
| 54 | import com.android.contacts.util.PermissionsUtil; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 55 | import com.android.contactsbind.experiments.Flags; |
| 56 | |
| 57 | import java.io.IOException; |
| 58 | import java.io.InputStream; |
| 59 | import java.util.ArrayList; |
| 60 | import java.util.Collections; |
| 61 | import java.util.List; |
| 62 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 63 | /** |
| 64 | * This class creates and updates the dynamic shortcuts displayed on the Nexus launcher for the |
| 65 | * Contacts app. |
| 66 | * |
| 67 | * Currently it adds shortcuts for the top 3 contacts in the {@link Contacts#CONTENT_STREQUENT_URI} |
| 68 | * |
| 69 | * Usage: DynamicShortcuts.initialize should be called during Application creation. This will |
Gary Mai | 91520d7 | 2017-02-28 10:56:44 -0800 | [diff] [blame^] | 70 | * schedule a Job to keep the shortcuts up-to-date so no further interactions should be necessary. |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 71 | */ |
| 72 | @TargetApi(Build.VERSION_CODES.N_MR1) |
| 73 | public class DynamicShortcuts { |
| 74 | private static final String TAG = "DynamicShortcuts"; |
| 75 | |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 76 | // Must be the same as shortcutId in res/xml/shortcuts.xml |
| 77 | // Note: This doesn't fit very well because this is a "static" shortcut but it's still the most |
| 78 | // sensible place to put it right now. |
| 79 | public static final String SHORTCUT_ADD_CONTACT = "shortcut-add-contact"; |
| 80 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 81 | // Note the Nexus launcher automatically truncates shortcut labels if they exceed these limits |
| 82 | // however, we implement our own truncation in case the shortcut is shown on a launcher that |
| 83 | // has different behavior |
| 84 | private static final int SHORT_LABEL_MAX_LENGTH = 12; |
| 85 | private static final int LONG_LABEL_MAX_LENGTH = 30; |
| 86 | private static final int MAX_SHORTCUTS = 3; |
| 87 | |
Marcus Hagerott | 5d1ec1d | 2016-12-13 08:49:55 -0800 | [diff] [blame] | 88 | private static final String EXTRA_SHORTCUT_TYPE = "extraShortcutType"; |
| 89 | |
| 90 | // Because pinned shortcuts persist across app upgrades these values should not be changed |
| 91 | // though new ones may be added |
| 92 | private static final int SHORTCUT_TYPE_UNKNOWN = 0; |
| 93 | private static final int SHORTCUT_TYPE_CONTACT_URI = 1; |
| 94 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 95 | // The spec specifies that it should be 44dp @ xxxhdpi |
| 96 | // Note that ShortcutManager.getIconMaxWidth and ShortcutManager.getMaxHeight return different |
| 97 | // (larger) values. |
| 98 | private static final int RECOMMENDED_ICON_PIXEL_LENGTH = 176; |
| 99 | |
| 100 | @VisibleForTesting |
| 101 | static final String[] PROJECTION = new String[] { |
| 102 | Contacts._ID, Contacts.LOOKUP_KEY, Contacts.DISPLAY_NAME_PRIMARY |
| 103 | }; |
| 104 | |
| 105 | private final Context mContext; |
| 106 | private final ContentResolver mContentResolver; |
| 107 | private final ShortcutManager mShortcutManager; |
| 108 | private int mShortLabelMaxLength = SHORT_LABEL_MAX_LENGTH; |
| 109 | private int mLongLabelMaxLength = LONG_LABEL_MAX_LENGTH; |
Arthur Wang | 8debbac | 2016-09-21 16:39:04 -0700 | [diff] [blame] | 110 | private final int mContentChangeMinUpdateDelay; |
| 111 | private final int mContentChangeMaxUpdateDelay; |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 112 | private final JobScheduler mJobScheduler; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 113 | |
| 114 | public DynamicShortcuts(Context context) { |
| 115 | this(context, context.getContentResolver(), (ShortcutManager) |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 116 | context.getSystemService(Context.SHORTCUT_SERVICE), |
| 117 | (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 120 | @VisibleForTesting |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 121 | public DynamicShortcuts(Context context, ContentResolver contentResolver, |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 122 | ShortcutManager shortcutManager, JobScheduler jobScheduler) { |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 123 | mContext = context; |
| 124 | mContentResolver = contentResolver; |
| 125 | mShortcutManager = shortcutManager; |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 126 | mJobScheduler = jobScheduler; |
Walter Jang | df86ede | 2016-10-19 09:48:29 -0700 | [diff] [blame] | 127 | mContentChangeMinUpdateDelay = Flags.getInstance() |
Arthur Wang | 8debbac | 2016-09-21 16:39:04 -0700 | [diff] [blame] | 128 | .getInteger(Experiments.DYNAMIC_MIN_CONTENT_CHANGE_UPDATE_DELAY_MILLIS); |
Walter Jang | df86ede | 2016-10-19 09:48:29 -0700 | [diff] [blame] | 129 | mContentChangeMaxUpdateDelay = Flags.getInstance() |
Arthur Wang | 8debbac | 2016-09-21 16:39:04 -0700 | [diff] [blame] | 130 | .getInteger(Experiments.DYNAMIC_MAX_CONTENT_CHANGE_UPDATE_DELAY_MILLIS); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @VisibleForTesting |
| 134 | void setShortLabelMaxLength(int length) { |
| 135 | this.mShortLabelMaxLength = length; |
| 136 | } |
| 137 | |
| 138 | @VisibleForTesting |
| 139 | void setLongLabelMaxLength(int length) { |
| 140 | this.mLongLabelMaxLength = length; |
| 141 | } |
| 142 | |
| 143 | @VisibleForTesting |
| 144 | void refresh() { |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 145 | // Guard here in addition to initialize because this could be run by the JobScheduler |
| 146 | // after permissions are revoked (maybe) |
| 147 | if (!hasRequiredPermissions()) return; |
| 148 | |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 149 | final List<ShortcutInfo> shortcuts = getStrequentShortcuts(); |
| 150 | mShortcutManager.setDynamicShortcuts(shortcuts); |
| 151 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 152 | Log.d(TAG, "set dynamic shortcuts " + shortcuts); |
| 153 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 154 | updatePinned(); |
| 155 | } |
| 156 | |
| 157 | @VisibleForTesting |
| 158 | void updatePinned() { |
| 159 | final List<ShortcutInfo> updates = new ArrayList<>(); |
| 160 | final List<String> removedIds = new ArrayList<>(); |
| 161 | final List<String> enable = new ArrayList<>(); |
| 162 | |
| 163 | for (ShortcutInfo shortcut : mShortcutManager.getPinnedShortcuts()) { |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 164 | final PersistableBundle extras = shortcut.getExtras(); |
Marcus Hagerott | 5d1ec1d | 2016-12-13 08:49:55 -0800 | [diff] [blame] | 165 | |
| 166 | if (!shortcut.isDynamic() || extras == null || |
| 167 | extras.getInt(EXTRA_SHORTCUT_TYPE, SHORTCUT_TYPE_UNKNOWN) != |
| 168 | SHORTCUT_TYPE_CONTACT_URI) { |
| 169 | continue; |
| 170 | } |
| 171 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 172 | // The contact ID may have changed but that's OK because it is just an optimization |
Marcus Hagerott | 5d1ec1d | 2016-12-13 08:49:55 -0800 | [diff] [blame] | 173 | final long contactId = extras.getLong(Contacts._ID); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 174 | |
| 175 | final ShortcutInfo update = createShortcutForUri( |
| 176 | Contacts.getLookupUri(contactId, shortcut.getId())); |
| 177 | if (update != null) { |
| 178 | updates.add(update); |
| 179 | if (!shortcut.isEnabled()) { |
| 180 | // Handle the case that a contact is disabled because it doesn't exist but |
| 181 | // later is created (for instance by a sync) |
| 182 | enable.add(update.getId()); |
| 183 | } |
| 184 | } else if (shortcut.isEnabled()) { |
| 185 | removedIds.add(shortcut.getId()); |
| 186 | } |
| 187 | } |
| 188 | |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 189 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 190 | Log.d(TAG, "updating " + updates); |
| 191 | Log.d(TAG, "enabling " + enable); |
| 192 | Log.d(TAG, "disabling " + removedIds); |
| 193 | } |
| 194 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 195 | mShortcutManager.updateShortcuts(updates); |
| 196 | mShortcutManager.enableShortcuts(enable); |
| 197 | mShortcutManager.disableShortcuts(removedIds, |
| 198 | mContext.getString(R.string.dynamic_shortcut_contact_removed_message)); |
| 199 | } |
| 200 | |
| 201 | private ShortcutInfo createShortcutForUri(Uri contactUri) { |
| 202 | final Cursor cursor = mContentResolver.query(contactUri, PROJECTION, null, null, null); |
| 203 | if (cursor == null) return null; |
| 204 | |
| 205 | try { |
| 206 | if (cursor.moveToFirst()) { |
| 207 | return createShortcutFromRow(cursor); |
| 208 | } |
| 209 | } finally { |
| 210 | cursor.close(); |
| 211 | } |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | public List<ShortcutInfo> getStrequentShortcuts() { |
| 216 | // The limit query parameter doesn't seem to work for this uri but we'll leave it because in |
| 217 | // case it does work on some phones or platform versions. |
| 218 | final Uri uri = Contacts.CONTENT_STREQUENT_URI.buildUpon() |
| 219 | .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, |
| 220 | String.valueOf(MAX_SHORTCUTS)) |
| 221 | .build(); |
| 222 | final Cursor cursor = mContentResolver.query(uri, PROJECTION, null, null, null); |
| 223 | |
| 224 | if (cursor == null) return Collections.emptyList(); |
| 225 | |
| 226 | final List<ShortcutInfo> result = new ArrayList<>(); |
| 227 | |
| 228 | try { |
Marcus Hagerott | f2e3808 | 2016-11-16 15:47:20 -0800 | [diff] [blame] | 229 | int i = 0; |
| 230 | while (i < MAX_SHORTCUTS && cursor.moveToNext()) { |
| 231 | final ShortcutInfo shortcut = createShortcutFromRow(cursor); |
| 232 | if (shortcut == null) { |
| 233 | continue; |
| 234 | } |
| 235 | result.add(shortcut); |
| 236 | i++; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 237 | } |
| 238 | } finally { |
| 239 | cursor.close(); |
| 240 | } |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | @VisibleForTesting |
| 246 | ShortcutInfo createShortcutFromRow(Cursor cursor) { |
| 247 | final ShortcutInfo.Builder builder = builderForContactShortcut(cursor); |
Marcus Hagerott | f2e3808 | 2016-11-16 15:47:20 -0800 | [diff] [blame] | 248 | if (builder == null) { |
| 249 | return null; |
| 250 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 251 | addIconForContact(cursor, builder); |
| 252 | return builder.build(); |
| 253 | } |
| 254 | |
| 255 | @VisibleForTesting |
| 256 | ShortcutInfo.Builder builderForContactShortcut(Cursor cursor) { |
| 257 | final long id = cursor.getLong(0); |
| 258 | final String lookupKey = cursor.getString(1); |
| 259 | final String displayName = cursor.getString(2); |
| 260 | return builderForContactShortcut(id, lookupKey, displayName); |
| 261 | } |
| 262 | |
| 263 | @VisibleForTesting |
| 264 | ShortcutInfo.Builder builderForContactShortcut(long id, String lookupKey, String displayName) { |
Marcus Hagerott | f2e3808 | 2016-11-16 15:47:20 -0800 | [diff] [blame] | 265 | if (lookupKey == null || displayName == null) { |
| 266 | return null; |
| 267 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 268 | final PersistableBundle extras = new PersistableBundle(); |
| 269 | extras.putLong(Contacts._ID, id); |
Marcus Hagerott | 5d1ec1d | 2016-12-13 08:49:55 -0800 | [diff] [blame] | 270 | extras.putInt(EXTRA_SHORTCUT_TYPE, SHORTCUT_TYPE_CONTACT_URI); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 271 | |
| 272 | final ShortcutInfo.Builder builder = new ShortcutInfo.Builder(mContext, lookupKey) |
| 273 | .setIntent(ImplicitIntentsUtil.getIntentForQuickContactLauncherShortcut(mContext, |
| 274 | Contacts.getLookupUri(id, lookupKey))) |
| 275 | .setDisabledMessage(mContext.getString(R.string.dynamic_shortcut_disabled_message)) |
| 276 | .setExtras(extras); |
| 277 | |
| 278 | if (displayName.length() < mLongLabelMaxLength) { |
| 279 | builder.setLongLabel(displayName); |
| 280 | } else { |
| 281 | builder.setLongLabel(displayName.substring(0, mLongLabelMaxLength - 1).trim() + "…"); |
| 282 | } |
| 283 | |
| 284 | if (displayName.length() < mShortLabelMaxLength) { |
| 285 | builder.setShortLabel(displayName); |
| 286 | } else { |
| 287 | builder.setShortLabel(displayName.substring(0, mShortLabelMaxLength - 1).trim() + "…"); |
| 288 | } |
| 289 | return builder; |
| 290 | } |
| 291 | |
Gary Mai | 91520d7 | 2017-02-28 10:56:44 -0800 | [diff] [blame^] | 292 | public ShortcutInfo getQuickContactShortcutInfo(long id, String lookupKey, String displayName) { |
| 293 | final ShortcutInfo.Builder builder = builderForContactShortcut(id, lookupKey, displayName); |
| 294 | addIconForContact(id, lookupKey, displayName, builder); |
| 295 | return builder.build(); |
| 296 | } |
| 297 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 298 | private void addIconForContact(Cursor cursor, ShortcutInfo.Builder builder) { |
| 299 | final long id = cursor.getLong(0); |
| 300 | final String lookupKey = cursor.getString(1); |
| 301 | final String displayName = cursor.getString(2); |
Gary Mai | 91520d7 | 2017-02-28 10:56:44 -0800 | [diff] [blame^] | 302 | addIconForContact(id, lookupKey, displayName, builder); |
| 303 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 304 | |
Gary Mai | 91520d7 | 2017-02-28 10:56:44 -0800 | [diff] [blame^] | 305 | private void addIconForContact(long id, String lookupKey, String displayName, |
| 306 | ShortcutInfo.Builder builder) { |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 307 | final Bitmap bitmap = getContactPhoto(id); |
| 308 | if (bitmap != null) { |
| 309 | builder.setIcon(Icon.createWithBitmap(bitmap)); |
| 310 | } else { |
| 311 | builder.setIcon(Icon.createWithBitmap(getFallbackAvatar(displayName, lookupKey))); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private Bitmap getContactPhoto(long id) { |
| 316 | final InputStream photoStream = Contacts.openContactPhotoInputStream( |
| 317 | mContext.getContentResolver(), |
| 318 | ContentUris.withAppendedId(Contacts.CONTENT_URI, id), true); |
| 319 | |
| 320 | if (photoStream == null) return null; |
| 321 | try { |
| 322 | final Bitmap bitmap = decodeStreamForShortcut(photoStream); |
| 323 | photoStream.close(); |
| 324 | return bitmap; |
| 325 | } catch (IOException e) { |
| 326 | Log.e(TAG, "Failed to decode contact photo for shortcut. ID=" + id, e); |
| 327 | return null; |
| 328 | } finally { |
| 329 | try { |
| 330 | photoStream.close(); |
| 331 | } catch (IOException e) { |
| 332 | // swallow |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private Bitmap decodeStreamForShortcut(InputStream stream) throws IOException { |
| 338 | final BitmapRegionDecoder bitmapDecoder = BitmapRegionDecoder.newInstance(stream, false); |
| 339 | |
| 340 | final int sourceWidth = bitmapDecoder.getWidth(); |
| 341 | final int sourceHeight = bitmapDecoder.getHeight(); |
| 342 | |
| 343 | final int iconMaxWidth = mShortcutManager.getIconMaxWidth();; |
| 344 | final int iconMaxHeight = mShortcutManager.getIconMaxHeight(); |
| 345 | |
| 346 | final int sampleSize = Math.min( |
| 347 | BitmapUtil.findOptimalSampleSize(sourceWidth, |
| 348 | RECOMMENDED_ICON_PIXEL_LENGTH), |
| 349 | BitmapUtil.findOptimalSampleSize(sourceHeight, |
| 350 | RECOMMENDED_ICON_PIXEL_LENGTH)); |
| 351 | final BitmapFactory.Options opts = new BitmapFactory.Options(); |
| 352 | opts.inSampleSize = sampleSize; |
| 353 | |
| 354 | final int scaledWidth = sourceWidth / opts.inSampleSize; |
| 355 | final int scaledHeight = sourceHeight / opts.inSampleSize; |
| 356 | |
| 357 | final int targetWidth = Math.min(scaledWidth, iconMaxWidth); |
| 358 | final int targetHeight = Math.min(scaledHeight, iconMaxHeight); |
| 359 | |
| 360 | // Make it square. |
| 361 | final int targetSize = Math.min(targetWidth, targetHeight); |
| 362 | |
| 363 | // The region is defined in the coordinates of the source image then the sampling is |
| 364 | // done on the extracted region. |
| 365 | final int prescaledXOffset = ((scaledWidth - targetSize) * opts.inSampleSize) / 2; |
| 366 | final int prescaledYOffset = ((scaledHeight - targetSize) * opts.inSampleSize) / 2; |
| 367 | |
| 368 | final Bitmap bitmap = bitmapDecoder.decodeRegion(new Rect( |
| 369 | prescaledXOffset, prescaledYOffset, |
| 370 | sourceWidth - prescaledXOffset, sourceHeight - prescaledYOffset |
| 371 | ), opts); |
| 372 | |
| 373 | bitmapDecoder.recycle(); |
| 374 | |
| 375 | return BitmapUtil.getRoundedBitmap(bitmap, targetSize, targetSize); |
| 376 | } |
| 377 | |
| 378 | private Bitmap getFallbackAvatar(String displayName, String lookupKey) { |
| 379 | final int w = RECOMMENDED_ICON_PIXEL_LENGTH; |
| 380 | final int h = RECOMMENDED_ICON_PIXEL_LENGTH; |
| 381 | |
| 382 | final ContactPhotoManager.DefaultImageRequest request = |
| 383 | new ContactPhotoManager.DefaultImageRequest(displayName, lookupKey, true); |
| 384 | final Drawable avatar = ContactPhotoManager.getDefaultAvatarDrawableForContact( |
| 385 | mContext.getResources(), true, request); |
| 386 | final Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 387 | // The avatar won't draw unless it thinks it is visible |
| 388 | avatar.setVisible(true, true); |
| 389 | final Canvas canvas = new Canvas(result); |
| 390 | avatar.setBounds(0, 0, w, h); |
| 391 | avatar.draw(canvas); |
| 392 | return result; |
| 393 | } |
| 394 | |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 395 | @VisibleForTesting |
| 396 | void handleFlagDisabled() { |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 397 | removeAllShortcuts(); |
| 398 | mJobScheduler.cancel(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID); |
| 399 | } |
| 400 | |
| 401 | private void removeAllShortcuts() { |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 402 | mShortcutManager.removeAllDynamicShortcuts(); |
| 403 | |
| 404 | final List<ShortcutInfo> pinned = mShortcutManager.getPinnedShortcuts(); |
| 405 | final List<String> ids = new ArrayList<>(pinned.size()); |
| 406 | for (ShortcutInfo shortcut : pinned) { |
| 407 | ids.add(shortcut.getId()); |
| 408 | } |
| 409 | mShortcutManager.disableShortcuts(ids, mContext |
| 410 | .getString(R.string.dynamic_shortcut_disabled_message)); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 411 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 412 | Log.d(TAG, "DynamicShortcuts have been removed."); |
| 413 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @VisibleForTesting |
| 417 | void scheduleUpdateJob() { |
| 418 | final JobInfo job = new JobInfo.Builder( |
| 419 | ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID, |
| 420 | new ComponentName(mContext, ContactsJobService.class)) |
| 421 | // We just observe all changes to contacts. It would be better to be more granular |
| 422 | // but CP2 only notifies using this URI anyway so there isn't any point in adding |
| 423 | // that complexity. |
| 424 | .addTriggerContentUri(new JobInfo.TriggerContentUri(ContactsContract.AUTHORITY_URI, |
| 425 | JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)) |
Arthur Wang | 8debbac | 2016-09-21 16:39:04 -0700 | [diff] [blame] | 426 | .setTriggerContentUpdateDelay(mContentChangeMinUpdateDelay) |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 427 | .setTriggerContentMaxDelay(mContentChangeMaxUpdateDelay) |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 428 | .build(); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 429 | mJobScheduler.schedule(job); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 432 | void updateInBackground() { |
| 433 | new ShortcutUpdateTask(this).execute(); |
| 434 | } |
| 435 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 436 | public synchronized static void initialize(Context context) { |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 437 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
Walter Jang | df86ede | 2016-10-19 09:48:29 -0700 | [diff] [blame] | 438 | final Flags flags = Flags.getInstance(); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 439 | Log.d(TAG, "DyanmicShortcuts.initialize\nVERSION >= N_MR1? " + |
| 440 | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) + |
Marcus Hagerott | b2504c7 | 2016-12-13 09:29:18 -0800 | [diff] [blame] | 441 | "\nisJobScheduled? " + |
| 442 | (CompatUtils.isLauncherShortcutCompatible() && isJobScheduled(context)) + |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 443 | "\nminDelay=" + |
| 444 | flags.getInteger(Experiments.DYNAMIC_MIN_CONTENT_CHANGE_UPDATE_DELAY_MILLIS) + |
| 445 | "\nmaxDelay=" + |
| 446 | flags.getInteger(Experiments.DYNAMIC_MAX_CONTENT_CHANGE_UPDATE_DELAY_MILLIS)); |
| 447 | } |
| 448 | |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 449 | if (!CompatUtils.isLauncherShortcutCompatible()) return; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 450 | |
| 451 | final DynamicShortcuts shortcuts = new DynamicShortcuts(context); |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 452 | |
Walter Jang | 9adc9ef | 2016-11-02 18:50:38 -0700 | [diff] [blame] | 453 | if (!shortcuts.hasRequiredPermissions()) { |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 454 | final IntentFilter filter = new IntentFilter(); |
| 455 | filter.addAction(RequestPermissionsActivity.BROADCAST_PERMISSIONS_GRANTED); |
| 456 | LocalBroadcastManager.getInstance(shortcuts.mContext).registerReceiver( |
| 457 | new PermissionsGrantedReceiver(), filter); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 458 | } else if (!isJobScheduled(context)) { |
| 459 | // Update the shortcuts. If the job is already scheduled then either the app is being |
| 460 | // launched to run the job in which case the shortcuts will get updated when it runs or |
| 461 | // it has been launched for some other reason and the data we care about for shortcuts |
| 462 | // hasn't changed. Because the job reschedules itself after completion this check |
| 463 | // essentially means that this will run on each app launch that happens after a reboot. |
| 464 | // Note: the task schedules the job after completing. |
| 465 | new ShortcutUpdateTask(shortcuts).execute(); |
| 466 | } |
| 467 | } |
| 468 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 469 | @VisibleForTesting |
| 470 | public static void reset(Context context) { |
| 471 | final JobScheduler jobScheduler = |
| 472 | (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 473 | jobScheduler.cancel(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID); |
| 474 | |
| 475 | if (!CompatUtils.isLauncherShortcutCompatible()) { |
| 476 | return; |
| 477 | } |
| 478 | new DynamicShortcuts(context).removeAllShortcuts(); |
| 479 | } |
| 480 | |
| 481 | @VisibleForTesting |
| 482 | boolean hasRequiredPermissions() { |
| 483 | return PermissionsUtil.hasContactsPermissions(mContext); |
| 484 | } |
| 485 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 486 | public static void updateFromJob(final JobService service, final JobParameters jobParams) { |
| 487 | new ShortcutUpdateTask(new DynamicShortcuts(service)) { |
| 488 | @Override |
| 489 | protected void onPostExecute(Void aVoid) { |
| 490 | // Must call super first which will reschedule the job before we call jobFinished |
| 491 | super.onPostExecute(aVoid); |
| 492 | service.jobFinished(jobParams, false); |
| 493 | } |
| 494 | }.execute(); |
| 495 | } |
| 496 | |
| 497 | @VisibleForTesting |
| 498 | public static boolean isJobScheduled(Context context) { |
| 499 | final JobScheduler scheduler = (JobScheduler) context |
| 500 | .getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 501 | return scheduler.getPendingJob(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID) != null; |
| 502 | } |
| 503 | |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 504 | public static void reportShortcutUsed(Context context, String lookupKey) { |
Marcus Hagerott | 677ee2b | 2016-10-28 15:31:55 -0700 | [diff] [blame] | 505 | if (!CompatUtils.isLauncherShortcutCompatible() || lookupKey == null) return; |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 506 | final ShortcutManager shortcutManager = (ShortcutManager) context |
| 507 | .getSystemService(Context.SHORTCUT_SERVICE); |
| 508 | shortcutManager.reportShortcutUsed(lookupKey); |
| 509 | } |
| 510 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 511 | private static class ShortcutUpdateTask extends AsyncTask<Void, Void, Void> { |
| 512 | private DynamicShortcuts mDynamicShortcuts; |
| 513 | |
| 514 | public ShortcutUpdateTask(DynamicShortcuts shortcuts) { |
| 515 | mDynamicShortcuts = shortcuts; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | protected Void doInBackground(Void... voids) { |
| 520 | mDynamicShortcuts.refresh(); |
| 521 | return null; |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | protected void onPostExecute(Void aVoid) { |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 526 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 527 | Log.d(TAG, "ShorcutUpdateTask.onPostExecute"); |
| 528 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 529 | // The shortcuts may have changed so update the job so that we are observing the |
| 530 | // correct Uris |
| 531 | mDynamicShortcuts.scheduleUpdateJob(); |
| 532 | } |
| 533 | } |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 534 | |
| 535 | private static class PermissionsGrantedReceiver extends BroadcastReceiver { |
| 536 | @Override |
| 537 | public void onReceive(Context context, Intent intent) { |
| 538 | // Clear the receiver. |
| 539 | LocalBroadcastManager.getInstance(context).unregisterReceiver(this); |
| 540 | DynamicShortcuts.initialize(context); |
| 541 | } |
| 542 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 543 | } |