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 com.android.contacts.common.list.ContactEntry; 24 import com.android.dialer.common.Assert; 25 import com.android.dialer.common.LogUtil; 26 import com.android.dialer.common.concurrent.DialerExecutor.Worker; 27 import com.android.dialer.common.concurrent.DialerExecutorComponent; 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** Refreshes launcher shortcuts from UI components using provided list of contacts. */ 32 public final class ShortcutRefresher { 33 34 /** Asynchronously updates launcher shortcuts using the provided list of contacts. */ 35 @MainThread refresh(@onNull Context context, List<ContactEntry> contacts)36 public static void refresh(@NonNull Context context, List<ContactEntry> contacts) { 37 Assert.isMainThread(); 38 Assert.isNotNull(context); 39 40 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { 41 return; 42 } 43 44 if (!Shortcuts.areDynamicShortcutsEnabled(context)) { 45 return; 46 } 47 48 DialerExecutorComponent.get(context) 49 .dialerExecutorFactory() 50 .createNonUiTaskBuilder(new RefreshWorker(context)) 51 .build() 52 .executeSerial(new ArrayList<>(contacts)); 53 } 54 55 private static final class RefreshWorker implements Worker<List<ContactEntry>, Void> { 56 private final Context context; 57 RefreshWorker(Context context)58 RefreshWorker(Context context) { 59 this.context = context; 60 } 61 62 @Override doInBackground(List<ContactEntry> contacts)63 public Void doInBackground(List<ContactEntry> contacts) { 64 LogUtil.enterBlock("ShortcutRefresher.Task.doInBackground"); 65 66 // Only dynamic shortcuts are maintained from UI components. Pinned shortcuts are maintained 67 // by the job scheduler. This is because a pinned contact may not necessarily still be in the 68 // favorites tiles, so refreshing it would require an additional database query. We don't want 69 // to incur the cost of that extra database query every time the favorites tiles change. 70 new DynamicShortcuts(context, new IconFactory(context)).refresh(contacts); // Blocking 71 72 return null; 73 } 74 } 75 } 76