1 /* 2 * Copyright (C) 2018 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.settings.shortcut; 18 19 import static com.android.settings.shortcut.CreateShortcutPreferenceController.SHORTCUT_ID_PREFIX; 20 import static com.android.settings.shortcut.CreateShortcutPreferenceController.SHORTCUT_PROBE; 21 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.content.pm.ShortcutInfo; 28 import android.content.pm.ShortcutManager; 29 import android.os.AsyncTask; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 34 public class ShortcutsUpdateTask extends AsyncTask<Void, Void, Void> { 35 36 private final Context mContext; 37 ShortcutsUpdateTask(Context context)38 public ShortcutsUpdateTask(Context context) { 39 mContext = context; 40 } 41 42 @Override doInBackground(Void... params)43 public Void doInBackground(Void... params) { 44 ShortcutManager sm = mContext.getSystemService(ShortcutManager.class); 45 PackageManager pm = mContext.getPackageManager(); 46 47 List<ShortcutInfo> updates = new ArrayList<>(); 48 for (ShortcutInfo info : sm.getPinnedShortcuts()) { 49 if (!info.getId().startsWith(SHORTCUT_ID_PREFIX)) { 50 continue; 51 } 52 ComponentName cn = ComponentName.unflattenFromString( 53 info.getId().substring(SHORTCUT_ID_PREFIX.length())); 54 ResolveInfo ri = pm.resolveActivity(new Intent(SHORTCUT_PROBE).setComponent(cn), 0); 55 if (ri == null) { 56 continue; 57 } 58 updates.add(new ShortcutInfo.Builder(mContext, info.getId()) 59 .setShortLabel(ri.loadLabel(pm)).build()); 60 } 61 if (!updates.isEmpty()) { 62 sm.updateShortcuts(updates); 63 } 64 return null; 65 } 66 } 67