blob: 2e1ed6967bfd43251f8b440277a4b2e368ffe844 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.content.BroadcastReceiver;
Winson Chungf561bdf2012-05-03 11:20:19 -070020import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
22import android.content.Intent;
Winson Chunga2413752012-04-03 14:22:34 -070023import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.database.Cursor;
25import android.net.Uri;
Romain Guyd93a7d12009-03-24 21:17:50 -070026import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027
Winson Chungf561bdf2012-05-03 11:20:19 -070028import java.net.URISyntaxException;
29import java.util.ArrayList;
30import java.util.HashSet;
31import java.util.Iterator;
32import java.util.Set;
33
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034public class UninstallShortcutReceiver extends BroadcastReceiver {
Romain Guy51ed5b92009-06-17 10:20:34 -070035 private static final String ACTION_UNINSTALL_SHORTCUT =
Daniel Sandler325dc232013-06-05 22:57:57 -040036 "com.android.launcher3.action.UNINSTALL_SHORTCUT";
Romain Guy51ed5b92009-06-17 10:20:34 -070037
Winson Chungf561bdf2012-05-03 11:20:19 -070038 // The set of shortcuts that are pending uninstall
39 private static ArrayList<PendingUninstallShortcutInfo> mUninstallQueue =
40 new ArrayList<PendingUninstallShortcutInfo>();
41
42 // Determines whether to defer uninstalling shortcuts immediately until
43 // disableAndFlushUninstallQueue() is called.
44 private static boolean mUseUninstallQueue = false;
45
46 private static class PendingUninstallShortcutInfo {
47 Intent data;
48
49 public PendingUninstallShortcutInfo(Intent rawData) {
50 data = rawData;
51 }
52 }
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 public void onReceive(Context context, Intent data) {
Romain Guy51ed5b92009-06-17 10:20:34 -070055 if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) {
56 return;
57 }
Winson Chungf561bdf2012-05-03 11:20:19 -070058
59 PendingUninstallShortcutInfo info = new PendingUninstallShortcutInfo(data);
60 if (mUseUninstallQueue) {
61 mUninstallQueue.add(info);
62 } else {
63 processUninstallShortcut(context, info);
64 }
65 }
66
67 static void enableUninstallQueue() {
68 mUseUninstallQueue = true;
69 }
70
71 static void disableAndFlushUninstallQueue(Context context) {
72 mUseUninstallQueue = false;
73 Iterator<PendingUninstallShortcutInfo> iter = mUninstallQueue.iterator();
74 while (iter.hasNext()) {
75 processUninstallShortcut(context, iter.next());
76 iter.remove();
77 }
78 }
79
80 private static void processUninstallShortcut(Context context,
81 PendingUninstallShortcutInfo pendingInfo) {
Winson Chungf561bdf2012-05-03 11:20:19 -070082 final Intent data = pendingInfo.data;
83
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084 LauncherAppState app = LauncherAppState.getInstance();
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 synchronized (app) { // TODO: make removeShortcut internally threadsafe
Winson Chung997a9232013-07-24 15:33:46 -070086 removeShortcut(context, data);
Winson Chunga2413752012-04-03 14:22:34 -070087 }
88 }
89
Winson Chung997a9232013-07-24 15:33:46 -070090 private static void removeShortcut(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
92 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
93 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
94
95 if (intent != null && name != null) {
96 final ContentResolver cr = context.getContentResolver();
97 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -070098 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);
100
101 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
102 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
103
104 boolean changed = false;
105
106 try {
107 while (c.moveToNext()) {
108 try {
Romain Guy1ce1a242009-06-23 17:34:54 -0700109 if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 final long id = c.getLong(idIndex);
111 final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
112 cr.delete(uri, null, null);
113 changed = true;
114 if (!duplicate) {
115 break;
116 }
117 }
118 } catch (URISyntaxException e) {
119 // Ignore
120 }
121 }
122 } finally {
123 c.close();
124 }
125
Romain Guyd93a7d12009-03-24 21:17:50 -0700126 if (changed) {
127 cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null);
128 Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name),
129 Toast.LENGTH_SHORT).show();
130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132 }
133}