blob: 84b1ad50e1dd6af135e74638f2d9721a9ee5bdb9 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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
Romain Guyedcce092010-03-04 13:03:17 -080028import com.android.launcher.R;
29
Winson Chungf561bdf2012-05-03 11:20:19 -070030import java.net.URISyntaxException;
31import java.util.ArrayList;
32import java.util.HashSet;
33import java.util.Iterator;
34import java.util.Set;
35
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036public class UninstallShortcutReceiver extends BroadcastReceiver {
Romain Guy51ed5b92009-06-17 10:20:34 -070037 private static final String ACTION_UNINSTALL_SHORTCUT =
38 "com.android.launcher.action.UNINSTALL_SHORTCUT";
39
Winson Chungf561bdf2012-05-03 11:20:19 -070040 // The set of shortcuts that are pending uninstall
41 private static ArrayList<PendingUninstallShortcutInfo> mUninstallQueue =
42 new ArrayList<PendingUninstallShortcutInfo>();
43
44 // Determines whether to defer uninstalling shortcuts immediately until
45 // disableAndFlushUninstallQueue() is called.
46 private static boolean mUseUninstallQueue = false;
47
48 private static class PendingUninstallShortcutInfo {
49 Intent data;
50
51 public PendingUninstallShortcutInfo(Intent rawData) {
52 data = rawData;
53 }
54 }
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 public void onReceive(Context context, Intent data) {
Romain Guy51ed5b92009-06-17 10:20:34 -070057 if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) {
58 return;
59 }
Winson Chungf561bdf2012-05-03 11:20:19 -070060
61 PendingUninstallShortcutInfo info = new PendingUninstallShortcutInfo(data);
62 if (mUseUninstallQueue) {
63 mUninstallQueue.add(info);
64 } else {
65 processUninstallShortcut(context, info);
66 }
67 }
68
69 static void enableUninstallQueue() {
70 mUseUninstallQueue = true;
71 }
72
73 static void disableAndFlushUninstallQueue(Context context) {
74 mUseUninstallQueue = false;
75 Iterator<PendingUninstallShortcutInfo> iter = mUninstallQueue.iterator();
76 while (iter.hasNext()) {
77 processUninstallShortcut(context, iter.next());
78 iter.remove();
79 }
80 }
81
82 private static void processUninstallShortcut(Context context,
83 PendingUninstallShortcutInfo pendingInfo) {
Winson Chunga2413752012-04-03 14:22:34 -070084 String spKey = LauncherApplication.getSharedPreferencesKey();
85 SharedPreferences sharedPrefs = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
Romain Guy51ed5b92009-06-17 10:20:34 -070086
Winson Chungf561bdf2012-05-03 11:20:19 -070087 final Intent data = pendingInfo.data;
88
Winson Chunga2413752012-04-03 14:22:34 -070089 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
90 synchronized (app) {
91 removeShortcut(context, data, sharedPrefs);
92 }
93 }
94
Winson Chungf561bdf2012-05-03 11:20:19 -070095 private static void removeShortcut(Context context, Intent data,
96 final SharedPreferences sharedPrefs) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
98 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
99 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
100
101 if (intent != null && name != null) {
102 final ContentResolver cr = context.getContentResolver();
103 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -0700104 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);
106
107 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
108 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
109
110 boolean changed = false;
111
112 try {
113 while (c.moveToNext()) {
114 try {
Romain Guy1ce1a242009-06-23 17:34:54 -0700115 if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 final long id = c.getLong(idIndex);
117 final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
118 cr.delete(uri, null, null);
119 changed = true;
120 if (!duplicate) {
121 break;
122 }
123 }
124 } catch (URISyntaxException e) {
125 // Ignore
126 }
127 }
128 } finally {
129 c.close();
130 }
131
Romain Guyd93a7d12009-03-24 21:17:50 -0700132 if (changed) {
133 cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null);
134 Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name),
135 Toast.LENGTH_SHORT).show();
136 }
Winson Chunga2413752012-04-03 14:22:34 -0700137
138 // Remove any items due to be animated
139 boolean appRemoved;
140 Set<String> newApps = new HashSet<String>();
141 newApps = sharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
142 do {
143 appRemoved = newApps.remove(intent.toUri(0).toString());
144 } while (appRemoved);
145 if (appRemoved) {
146 final Set<String> savedNewApps = newApps;
147 new Thread("setNewAppsThread-remove") {
148 public void run() {
149 SharedPreferences.Editor editor = sharedPrefs.edit();
150 editor.putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY,
151 savedNewApps);
152 if (savedNewApps.isEmpty()) {
153 // Reset the page index if there are no more items
154 editor.putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1);
155 }
156 editor.commit();
157 }
158 }.start();
159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161 }
162}