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 17 package com.android.dialer.shortcuts; 18 19 import android.content.Context; 20 import android.os.Build; 21 import android.support.annotation.MainThread; 22 import android.support.annotation.NonNull; 23 import android.support.annotation.WorkerThread; 24 import com.android.contacts.common.list.ContactEntry; 25 import com.android.dialer.common.Assert; 26 import com.android.dialer.common.LogUtil; 27 import com.android.dialer.common.concurrent.AsyncTaskExecutor; 28 import com.android.dialer.common.concurrent.AsyncTaskExecutors; 29 import com.android.dialer.common.concurrent.FallibleAsyncTask; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** Refreshes launcher shortcuts from UI components using provided list of contacts. */ 34 public final class ShortcutRefresher { 35 36 private static final AsyncTaskExecutor EXECUTOR = AsyncTaskExecutors.createThreadPoolExecutor(); 37 38 /** Asynchronously updates launcher shortcuts using the provided list of contacts. */ 39 @MainThread refresh(@onNull Context context, List<ContactEntry> contacts)40 public static void refresh(@NonNull Context context, List<ContactEntry> contacts) { 41 Assert.isMainThread(); 42 Assert.isNotNull(context); 43 44 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { 45 return; 46 } 47 48 if (!Shortcuts.areDynamicShortcutsEnabled(context)) { 49 return; 50 } 51 52 //noinspection unchecked 53 EXECUTOR.submit(Task.ID, new Task(context), new ArrayList<>(contacts)); 54 } 55 56 private static final class Task extends FallibleAsyncTask<List<ContactEntry>, Void, Void> { 57 private static final String ID = "ShortcutRefresher.Task"; 58 59 private final Context context; 60 Task(Context context)61 Task(Context context) { 62 this.context = context; 63 } 64 65 /** 66 * @param params array containing exactly one element, the list of contacts from favorites 67 * tiles, ordered in tile order. 68 */ 69 @SafeVarargs 70 @Override 71 @NonNull 72 @WorkerThread doInBackgroundFallible(List<ContactEntry>.... params)73 protected final Void doInBackgroundFallible(List<ContactEntry>... params) { 74 Assert.isWorkerThread(); 75 LogUtil.enterBlock("ShortcutRefresher.Task.doInBackground"); 76 77 // Only dynamic shortcuts are maintained from UI components. Pinned shortcuts are maintained 78 // by the job scheduler. This is because a pinned contact may not necessarily still be in the 79 // favorites tiles, so refreshing it would require an additional database query. We don't want 80 // to incur the cost of that extra database query every time the favorites tiles change. 81 new DynamicShortcuts(context, new IconFactory(context)).refresh(params[0]); // Blocking 82 83 return null; 84 } 85 } 86 } 87