The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.BroadcastReceiver; |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.database.Cursor; |
| 25 | import android.net.Uri; |
Romain Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 26 | import android.widget.Toast; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 30 | import java.net.URISyntaxException; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Iterator; |
| 34 | import java.util.Set; |
| 35 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | public class UninstallShortcutReceiver extends BroadcastReceiver { |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 37 | private static final String ACTION_UNINSTALL_SHORTCUT = |
| 38 | "com.android.launcher.action.UNINSTALL_SHORTCUT"; |
| 39 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 40 | // 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | public void onReceive(Context context, Intent data) { |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 57 | if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) { |
| 58 | return; |
| 59 | } |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 60 | |
| 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 Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 84 | String spKey = LauncherApplication.getSharedPreferencesKey(); |
| 85 | SharedPreferences sharedPrefs = context.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 86 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 87 | final Intent data = pendingInfo.data; |
| 88 | |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 89 | LauncherApplication app = (LauncherApplication) context.getApplicationContext(); |
| 90 | synchronized (app) { |
| 91 | removeShortcut(context, data, sharedPrefs); |
| 92 | } |
| 93 | } |
| 94 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 95 | private static void removeShortcut(Context context, Intent data, |
| 96 | final SharedPreferences sharedPrefs) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | 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 Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 104 | new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT }, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | 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 Guy | 1ce1a24 | 2009-06-23 17:34:54 -0700 | [diff] [blame] | 115 | if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | 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 Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 132 | 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 Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 137 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |