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 com.android.dialer.speeddial.loader.SpeedDialUiItem; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** Refreshes launcher shortcuts from UI components using provided list of contacts. */ 33 public final class ShortcutRefresher { 34 35 /** Asynchronously updates launcher shortcuts using the provided list of contacts. */ 36 @MainThread refresh(@onNull Context context, List<ContactEntry> contacts)37 public static void refresh(@NonNull Context context, List<ContactEntry> contacts) { 38 Assert.isMainThread(); 39 Assert.isNotNull(context); 40 41 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { 42 return; 43 } 44 45 if (!Shortcuts.areDynamicShortcutsEnabled(context)) { 46 return; 47 } 48 49 DialerExecutorComponent.get(context) 50 .dialerExecutorFactory() 51 .createNonUiTaskBuilder(new RefreshWorker(context)) 52 .build() 53 .executeSerial(new ArrayList<>(contacts)); 54 } 55 speedDialUiItemsToContactEntries(List<SpeedDialUiItem> items)56 public static List<ContactEntry> speedDialUiItemsToContactEntries(List<SpeedDialUiItem> items) { 57 List<ContactEntry> contactEntries = new ArrayList<>(); 58 for (SpeedDialUiItem item : items) { 59 ContactEntry entry = new ContactEntry(); 60 entry.id = item.contactId(); 61 entry.lookupKey = item.lookupKey(); 62 // SpeedDialUiItem name's are already configured for alternative display orders, so we don't 63 // need to account for them in these entries. 64 entry.namePrimary = item.name(); 65 contactEntries.add(entry); 66 } 67 return contactEntries; 68 } 69 70 private static final class RefreshWorker implements Worker<List<ContactEntry>, Void> { 71 private final Context context; 72 RefreshWorker(Context context)73 RefreshWorker(Context context) { 74 this.context = context; 75 } 76 77 @Override doInBackground(List<ContactEntry> contacts)78 public Void doInBackground(List<ContactEntry> contacts) { 79 LogUtil.enterBlock("ShortcutRefresher.Task.doInBackground"); 80 81 // Only dynamic shortcuts are maintained from UI components. Pinned shortcuts are maintained 82 // by the job scheduler. This is because a pinned contact may not necessarily still be in the 83 // favorites tiles, so refreshing it would require an additional database query. We don't want 84 // to incur the cost of that extra database query every time the favorites tiles change. 85 new DynamicShortcuts(context, new IconFactory(context)).refresh(contacts); // Blocking 86 87 return null; 88 } 89 } 90 } 91