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 |
| 70 | * schedule a Job to keep the shortcuts up-to-date so no further interations should be necessary. |
| 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 | |
| 292 | private void addIconForContact(Cursor cursor, ShortcutInfo.Builder builder) { |
| 293 | final long id = cursor.getLong(0); |
| 294 | final String lookupKey = cursor.getString(1); |
| 295 | final String displayName = cursor.getString(2); |
| 296 | |
| 297 | final Bitmap bitmap = getContactPhoto(id); |
| 298 | if (bitmap != null) { |
| 299 | builder.setIcon(Icon.createWithBitmap(bitmap)); |
| 300 | } else { |
| 301 | builder.setIcon(Icon.createWithBitmap(getFallbackAvatar(displayName, lookupKey))); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private Bitmap getContactPhoto(long id) { |
| 306 | final InputStream photoStream = Contacts.openContactPhotoInputStream( |
| 307 | mContext.getContentResolver(), |
| 308 | ContentUris.withAppendedId(Contacts.CONTENT_URI, id), true); |
| 309 | |
| 310 | if (photoStream == null) return null; |
| 311 | try { |
| 312 | final Bitmap bitmap = decodeStreamForShortcut(photoStream); |
| 313 | photoStream.close(); |
| 314 | return bitmap; |
| 315 | } catch (IOException e) { |
| 316 | Log.e(TAG, "Failed to decode contact photo for shortcut. ID=" + id, e); |
| 317 | return null; |
| 318 | } finally { |
| 319 | try { |
| 320 | photoStream.close(); |
| 321 | } catch (IOException e) { |
| 322 | // swallow |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | private Bitmap decodeStreamForShortcut(InputStream stream) throws IOException { |
| 328 | final BitmapRegionDecoder bitmapDecoder = BitmapRegionDecoder.newInstance(stream, false); |
| 329 | |
| 330 | final int sourceWidth = bitmapDecoder.getWidth(); |
| 331 | final int sourceHeight = bitmapDecoder.getHeight(); |
| 332 | |
| 333 | final int iconMaxWidth = mShortcutManager.getIconMaxWidth();; |
| 334 | final int iconMaxHeight = mShortcutManager.getIconMaxHeight(); |
| 335 | |
| 336 | final int sampleSize = Math.min( |
| 337 | BitmapUtil.findOptimalSampleSize(sourceWidth, |
| 338 | RECOMMENDED_ICON_PIXEL_LENGTH), |
| 339 | BitmapUtil.findOptimalSampleSize(sourceHeight, |
| 340 | RECOMMENDED_ICON_PIXEL_LENGTH)); |
| 341 | final BitmapFactory.Options opts = new BitmapFactory.Options(); |
| 342 | opts.inSampleSize = sampleSize; |
| 343 | |
| 344 | final int scaledWidth = sourceWidth / opts.inSampleSize; |
| 345 | final int scaledHeight = sourceHeight / opts.inSampleSize; |
| 346 | |
| 347 | final int targetWidth = Math.min(scaledWidth, iconMaxWidth); |
| 348 | final int targetHeight = Math.min(scaledHeight, iconMaxHeight); |
| 349 | |
| 350 | // Make it square. |
| 351 | final int targetSize = Math.min(targetWidth, targetHeight); |
| 352 | |
| 353 | // The region is defined in the coordinates of the source image then the sampling is |
| 354 | // done on the extracted region. |
| 355 | final int prescaledXOffset = ((scaledWidth - targetSize) * opts.inSampleSize) / 2; |
| 356 | final int prescaledYOffset = ((scaledHeight - targetSize) * opts.inSampleSize) / 2; |
| 357 | |
| 358 | final Bitmap bitmap = bitmapDecoder.decodeRegion(new Rect( |
| 359 | prescaledXOffset, prescaledYOffset, |
| 360 | sourceWidth - prescaledXOffset, sourceHeight - prescaledYOffset |
| 361 | ), opts); |
| 362 | |
| 363 | bitmapDecoder.recycle(); |
| 364 | |
| 365 | return BitmapUtil.getRoundedBitmap(bitmap, targetSize, targetSize); |
| 366 | } |
| 367 | |
| 368 | private Bitmap getFallbackAvatar(String displayName, String lookupKey) { |
| 369 | final int w = RECOMMENDED_ICON_PIXEL_LENGTH; |
| 370 | final int h = RECOMMENDED_ICON_PIXEL_LENGTH; |
| 371 | |
| 372 | final ContactPhotoManager.DefaultImageRequest request = |
| 373 | new ContactPhotoManager.DefaultImageRequest(displayName, lookupKey, true); |
| 374 | final Drawable avatar = ContactPhotoManager.getDefaultAvatarDrawableForContact( |
| 375 | mContext.getResources(), true, request); |
| 376 | final Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 377 | // The avatar won't draw unless it thinks it is visible |
| 378 | avatar.setVisible(true, true); |
| 379 | final Canvas canvas = new Canvas(result); |
| 380 | avatar.setBounds(0, 0, w, h); |
| 381 | avatar.draw(canvas); |
| 382 | return result; |
| 383 | } |
| 384 | |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 385 | @VisibleForTesting |
| 386 | void handleFlagDisabled() { |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 387 | removeAllShortcuts(); |
| 388 | mJobScheduler.cancel(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID); |
| 389 | } |
| 390 | |
| 391 | private void removeAllShortcuts() { |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 392 | mShortcutManager.removeAllDynamicShortcuts(); |
| 393 | |
| 394 | final List<ShortcutInfo> pinned = mShortcutManager.getPinnedShortcuts(); |
| 395 | final List<String> ids = new ArrayList<>(pinned.size()); |
| 396 | for (ShortcutInfo shortcut : pinned) { |
| 397 | ids.add(shortcut.getId()); |
| 398 | } |
| 399 | mShortcutManager.disableShortcuts(ids, mContext |
| 400 | .getString(R.string.dynamic_shortcut_disabled_message)); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 401 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 402 | Log.d(TAG, "DynamicShortcuts have been removed."); |
| 403 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | @VisibleForTesting |
| 407 | void scheduleUpdateJob() { |
| 408 | final JobInfo job = new JobInfo.Builder( |
| 409 | ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID, |
| 410 | new ComponentName(mContext, ContactsJobService.class)) |
| 411 | // We just observe all changes to contacts. It would be better to be more granular |
| 412 | // but CP2 only notifies using this URI anyway so there isn't any point in adding |
| 413 | // that complexity. |
| 414 | .addTriggerContentUri(new JobInfo.TriggerContentUri(ContactsContract.AUTHORITY_URI, |
| 415 | JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)) |
Arthur Wang | 8debbac | 2016-09-21 16:39:04 -0700 | [diff] [blame] | 416 | .setTriggerContentUpdateDelay(mContentChangeMinUpdateDelay) |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 417 | .setTriggerContentMaxDelay(mContentChangeMaxUpdateDelay) |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 418 | .build(); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 419 | mJobScheduler.schedule(job); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 422 | void updateInBackground() { |
| 423 | new ShortcutUpdateTask(this).execute(); |
| 424 | } |
| 425 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 426 | public synchronized static void initialize(Context context) { |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 427 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
Walter Jang | df86ede | 2016-10-19 09:48:29 -0700 | [diff] [blame] | 428 | final Flags flags = Flags.getInstance(); |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 429 | Log.d(TAG, "DyanmicShortcuts.initialize\nVERSION >= N_MR1? " + |
| 430 | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) + |
Marcus Hagerott | b2504c7 | 2016-12-13 09:29:18 -0800 | [diff] [blame] | 431 | "\nisJobScheduled? " + |
| 432 | (CompatUtils.isLauncherShortcutCompatible() && isJobScheduled(context)) + |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 433 | "\nminDelay=" + |
| 434 | flags.getInteger(Experiments.DYNAMIC_MIN_CONTENT_CHANGE_UPDATE_DELAY_MILLIS) + |
| 435 | "\nmaxDelay=" + |
| 436 | flags.getInteger(Experiments.DYNAMIC_MAX_CONTENT_CHANGE_UPDATE_DELAY_MILLIS)); |
| 437 | } |
| 438 | |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 439 | if (!CompatUtils.isLauncherShortcutCompatible()) return; |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 440 | |
| 441 | final DynamicShortcuts shortcuts = new DynamicShortcuts(context); |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 442 | |
Walter Jang | 9adc9ef | 2016-11-02 18:50:38 -0700 | [diff] [blame] | 443 | if (!shortcuts.hasRequiredPermissions()) { |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 444 | final IntentFilter filter = new IntentFilter(); |
| 445 | filter.addAction(RequestPermissionsActivity.BROADCAST_PERMISSIONS_GRANTED); |
| 446 | LocalBroadcastManager.getInstance(shortcuts.mContext).registerReceiver( |
| 447 | new PermissionsGrantedReceiver(), filter); |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 448 | } else if (!isJobScheduled(context)) { |
| 449 | // Update the shortcuts. If the job is already scheduled then either the app is being |
| 450 | // launched to run the job in which case the shortcuts will get updated when it runs or |
| 451 | // it has been launched for some other reason and the data we care about for shortcuts |
| 452 | // hasn't changed. Because the job reschedules itself after completion this check |
| 453 | // essentially means that this will run on each app launch that happens after a reboot. |
| 454 | // Note: the task schedules the job after completing. |
| 455 | new ShortcutUpdateTask(shortcuts).execute(); |
| 456 | } |
| 457 | } |
| 458 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 459 | @VisibleForTesting |
| 460 | public static void reset(Context context) { |
| 461 | final JobScheduler jobScheduler = |
| 462 | (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 463 | jobScheduler.cancel(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID); |
| 464 | |
| 465 | if (!CompatUtils.isLauncherShortcutCompatible()) { |
| 466 | return; |
| 467 | } |
| 468 | new DynamicShortcuts(context).removeAllShortcuts(); |
| 469 | } |
| 470 | |
| 471 | @VisibleForTesting |
| 472 | boolean hasRequiredPermissions() { |
| 473 | return PermissionsUtil.hasContactsPermissions(mContext); |
| 474 | } |
| 475 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 476 | public static void updateFromJob(final JobService service, final JobParameters jobParams) { |
| 477 | new ShortcutUpdateTask(new DynamicShortcuts(service)) { |
| 478 | @Override |
| 479 | protected void onPostExecute(Void aVoid) { |
| 480 | // Must call super first which will reschedule the job before we call jobFinished |
| 481 | super.onPostExecute(aVoid); |
| 482 | service.jobFinished(jobParams, false); |
| 483 | } |
| 484 | }.execute(); |
| 485 | } |
| 486 | |
| 487 | @VisibleForTesting |
| 488 | public static boolean isJobScheduled(Context context) { |
| 489 | final JobScheduler scheduler = (JobScheduler) context |
| 490 | .getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 491 | return scheduler.getPendingJob(ContactsJobService.DYNAMIC_SHORTCUTS_JOB_ID) != null; |
| 492 | } |
| 493 | |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 494 | public static void reportShortcutUsed(Context context, String lookupKey) { |
Marcus Hagerott | 677ee2b | 2016-10-28 15:31:55 -0700 | [diff] [blame] | 495 | if (!CompatUtils.isLauncherShortcutCompatible() || lookupKey == null) return; |
Marcus Hagerott | d105c1e | 2016-09-30 14:28:00 -0700 | [diff] [blame] | 496 | final ShortcutManager shortcutManager = (ShortcutManager) context |
| 497 | .getSystemService(Context.SHORTCUT_SERVICE); |
| 498 | shortcutManager.reportShortcutUsed(lookupKey); |
| 499 | } |
| 500 | |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 501 | private static class ShortcutUpdateTask extends AsyncTask<Void, Void, Void> { |
| 502 | private DynamicShortcuts mDynamicShortcuts; |
| 503 | |
| 504 | public ShortcutUpdateTask(DynamicShortcuts shortcuts) { |
| 505 | mDynamicShortcuts = shortcuts; |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | protected Void doInBackground(Void... voids) { |
| 510 | mDynamicShortcuts.refresh(); |
| 511 | return null; |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | protected void onPostExecute(Void aVoid) { |
Marcus Hagerott | 020f041 | 2016-09-22 12:13:49 -0700 | [diff] [blame] | 516 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 517 | Log.d(TAG, "ShorcutUpdateTask.onPostExecute"); |
| 518 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 519 | // The shortcuts may have changed so update the job so that we are observing the |
| 520 | // correct Uris |
| 521 | mDynamicShortcuts.scheduleUpdateJob(); |
| 522 | } |
| 523 | } |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 524 | |
| 525 | private static class PermissionsGrantedReceiver extends BroadcastReceiver { |
| 526 | @Override |
| 527 | public void onReceive(Context context, Intent intent) { |
| 528 | // Clear the receiver. |
| 529 | LocalBroadcastManager.getInstance(context).unregisterReceiver(this); |
| 530 | DynamicShortcuts.initialize(context); |
| 531 | } |
| 532 | } |
Marcus Hagerott | c5083f9 | 2016-09-14 08:34:29 -0700 | [diff] [blame] | 533 | } |