• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.launcher3;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.widget.Toast;
26 
27 import java.net.URISyntaxException;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 
31 public class UninstallShortcutReceiver extends BroadcastReceiver {
32     private static final String ACTION_UNINSTALL_SHORTCUT =
33             "com.android.launcher.action.UNINSTALL_SHORTCUT";
34 
35     // The set of shortcuts that are pending uninstall
36     private static ArrayList<PendingUninstallShortcutInfo> mUninstallQueue =
37             new ArrayList<PendingUninstallShortcutInfo>();
38 
39     // Determines whether to defer uninstalling shortcuts immediately until
40     // disableAndFlushUninstallQueue() is called.
41     private static boolean mUseUninstallQueue = false;
42 
43     private static class PendingUninstallShortcutInfo {
44         Intent data;
45 
PendingUninstallShortcutInfo(Intent rawData)46         public PendingUninstallShortcutInfo(Intent rawData) {
47             data = rawData;
48         }
49     }
50 
onReceive(Context context, Intent data)51     public void onReceive(Context context, Intent data) {
52         if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) {
53             return;
54         }
55 
56         PendingUninstallShortcutInfo info = new PendingUninstallShortcutInfo(data);
57         if (mUseUninstallQueue) {
58             mUninstallQueue.add(info);
59         } else {
60             processUninstallShortcut(context, info);
61         }
62     }
63 
enableUninstallQueue()64     static void enableUninstallQueue() {
65         mUseUninstallQueue = true;
66     }
67 
disableAndFlushUninstallQueue(Context context)68     static void disableAndFlushUninstallQueue(Context context) {
69         mUseUninstallQueue = false;
70         Iterator<PendingUninstallShortcutInfo> iter = mUninstallQueue.iterator();
71         while (iter.hasNext()) {
72             processUninstallShortcut(context, iter.next());
73             iter.remove();
74         }
75     }
76 
processUninstallShortcut(Context context, PendingUninstallShortcutInfo pendingInfo)77     private static void processUninstallShortcut(Context context,
78             PendingUninstallShortcutInfo pendingInfo) {
79         final Intent data = pendingInfo.data;
80 
81         LauncherAppState.setApplicationContext(context.getApplicationContext());
82         LauncherAppState app = LauncherAppState.getInstance();
83         synchronized (app) { // TODO: make removeShortcut internally threadsafe
84             removeShortcut(context, data);
85         }
86     }
87 
removeShortcut(Context context, Intent data)88     private static void removeShortcut(Context context, Intent data) {
89         Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
90         String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
91         boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
92 
93         if (intent != null && name != null) {
94             final ContentResolver cr = context.getContentResolver();
95             Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
96                 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
97                 LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);
98 
99             final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
100             final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
101 
102             boolean changed = false;
103 
104             try {
105                 while (c.moveToNext()) {
106                     try {
107                         if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
108                             final long id = c.getLong(idIndex);
109                             final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
110                             cr.delete(uri, null, null);
111                             changed = true;
112                             if (!duplicate) {
113                                 break;
114                             }
115                         }
116                     } catch (URISyntaxException e) {
117                         // Ignore
118                     }
119                 }
120             } finally {
121                 c.close();
122             }
123 
124             if (changed) {
125                 cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null);
126                 Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name),
127                         Toast.LENGTH_SHORT).show();
128             }
129         }
130     }
131 }
132