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; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Winson Chung | c3a747a | 2012-02-29 10:56:19 -0800 | [diff] [blame] | 23 | import android.content.pm.ActivityInfo; |
| 24 | import android.content.pm.PackageManager; |
Romain Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 25 | import android.widget.Toast; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 27 | import com.android.launcher.R; |
| 28 | |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 29 | import java.util.ArrayList; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 30 | import java.util.HashSet; |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 31 | import java.util.Iterator; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 32 | import java.util.Set; |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | public class InstallShortcutReceiver extends BroadcastReceiver { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 35 | public static final String ACTION_INSTALL_SHORTCUT = |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 36 | "com.android.launcher.action.INSTALL_SHORTCUT"; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 37 | public static final String NEW_APPS_PAGE_KEY = "apps.new.page"; |
| 38 | public static final String NEW_APPS_LIST_KEY = "apps.new.list"; |
| 39 | |
| 40 | public static final int NEW_SHORTCUT_BOUNCE_DURATION = 450; |
| 41 | public static final int NEW_SHORTCUT_STAGGER_DELAY = 75; |
| 42 | |
| 43 | private static final int INSTALL_SHORTCUT_SUCCESSFUL = 0; |
| 44 | private static final int INSTALL_SHORTCUT_IS_DUPLICATE = -1; |
| 45 | private static final int INSTALL_SHORTCUT_NO_SPACE = -2; |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 46 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 47 | // A mime-type representing shortcut data |
| 48 | public static final String SHORTCUT_MIMETYPE = |
| 49 | "com.android.launcher/shortcut"; |
| 50 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 51 | // The set of shortcuts that are pending install |
| 52 | private static ArrayList<PendingInstallShortcutInfo> mInstallQueue = |
| 53 | new ArrayList<PendingInstallShortcutInfo>(); |
| 54 | |
| 55 | // Determines whether to defer installing shortcuts immediately until |
| 56 | // processAllPendingInstalls() is called. |
| 57 | private static boolean mUseInstallQueue = false; |
| 58 | |
| 59 | private static class PendingInstallShortcutInfo { |
| 60 | Intent data; |
| 61 | Intent launchIntent; |
| 62 | String name; |
| 63 | |
| 64 | public PendingInstallShortcutInfo(Intent rawData, String shortcutName, |
| 65 | Intent shortcutIntent) { |
| 66 | data = rawData; |
| 67 | name = shortcutName; |
| 68 | launchIntent = shortcutIntent; |
| 69 | } |
| 70 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
| 72 | public void onReceive(Context context, Intent data) { |
Romain Guy | 51ed5b9 | 2009-06-17 10:20:34 -0700 | [diff] [blame] | 73 | if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) { |
| 74 | return; |
| 75 | } |
| 76 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 77 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
Winson Chung | c3a747a | 2012-02-29 10:56:19 -0800 | [diff] [blame] | 78 | if (intent == null) { |
| 79 | return; |
| 80 | } |
| 81 | // This name is only used for comparisons and notifications, so fall back to activity name |
| 82 | // if not supplied |
| 83 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 84 | if (name == null) { |
| 85 | try { |
| 86 | PackageManager pm = context.getPackageManager(); |
| 87 | ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0); |
| 88 | name = info.loadLabel(pm).toString(); |
| 89 | } catch (PackageManager.NameNotFoundException nnfe) { |
| 90 | return; |
| 91 | } |
| 92 | } |
Winson Chung | de0fb8f | 2012-05-08 14:37:08 -0700 | [diff] [blame] | 93 | // Queue the item up for adding if launcher has not loaded properly yet |
| 94 | boolean launcherNotLoaded = LauncherModel.getCellCountX() <= 0 || |
| 95 | LauncherModel.getCellCountY() <= 0; |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame] | 96 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 97 | PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent); |
Winson Chung | de0fb8f | 2012-05-08 14:37:08 -0700 | [diff] [blame] | 98 | if (mUseInstallQueue || launcherNotLoaded) { |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 99 | mInstallQueue.add(info); |
| 100 | } else { |
| 101 | processInstallShortcut(context, info); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void enableInstallQueue() { |
| 106 | mUseInstallQueue = true; |
| 107 | } |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 108 | static void disableAndFlushInstallQueue(Context context) { |
| 109 | mUseInstallQueue = false; |
Winson Chung | de0fb8f | 2012-05-08 14:37:08 -0700 | [diff] [blame] | 110 | flushInstallQueue(context); |
| 111 | } |
| 112 | static void flushInstallQueue(Context context) { |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 113 | Iterator<PendingInstallShortcutInfo> iter = mInstallQueue.iterator(); |
| 114 | while (iter.hasNext()) { |
| 115 | processInstallShortcut(context, iter.next()); |
| 116 | iter.remove(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private static void processInstallShortcut(Context context, |
| 121 | PendingInstallShortcutInfo pendingInfo) { |
| 122 | String spKey = LauncherApplication.getSharedPreferencesKey(); |
| 123 | SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 124 | |
| 125 | final Intent data = pendingInfo.data; |
| 126 | final Intent intent = pendingInfo.launchIntent; |
| 127 | final String name = pendingInfo.name; |
| 128 | |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 129 | // Lock on the app so that we don't try and get the items while apps are being added |
| 130 | LauncherApplication app = (LauncherApplication) context.getApplicationContext(); |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 131 | final int[] result = {INSTALL_SHORTCUT_SUCCESSFUL}; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 132 | boolean found = false; |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 133 | synchronized (app) { |
| 134 | final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context); |
| 135 | final boolean exists = LauncherModel.shortcutExists(context, name, intent); |
| 136 | |
| 137 | // Try adding to the workspace screens incrementally, starting at the default or center |
| 138 | // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1)) |
| 139 | final int screen = Launcher.DEFAULT_SCREEN; |
| 140 | for (int i = 0; i < (2 * Launcher.SCREEN_COUNT) + 1 && !found; ++i) { |
| 141 | int si = screen + (int) ((i / 2f) + 0.5f) * ((i % 2 == 1) ? 1 : -1); |
| 142 | if (0 <= si && si < Launcher.SCREEN_COUNT) { |
| 143 | found = installShortcut(context, data, items, name, intent, si, exists, sp, |
| 144 | result); |
| 145 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 148 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 149 | // We only report error messages (duplicate shortcut or out of space) as the add-animation |
| 150 | // will provide feedback otherwise |
| 151 | if (!found) { |
| 152 | if (result[0] == INSTALL_SHORTCUT_NO_SPACE) { |
Winson Chung | 169c3d7 | 2012-04-24 14:43:54 -0700 | [diff] [blame] | 153 | Toast.makeText(context, context.getString(R.string.completely_out_of_space), |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 154 | Toast.LENGTH_SHORT).show(); |
| 155 | } else if (result[0] == INSTALL_SHORTCUT_IS_DUPLICATE) { |
| 156 | Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name), |
| 157 | Toast.LENGTH_SHORT).show(); |
| 158 | } |
Winson Chung | e428e29 | 2012-01-10 16:20:33 -0800 | [diff] [blame] | 159 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 162 | private static boolean installShortcut(Context context, Intent data, ArrayList<ItemInfo> items, |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 163 | String name, Intent intent, final int screen, boolean shortcutExists, |
| 164 | final SharedPreferences sharedPrefs, int[] result) { |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 165 | int[] tmpCoordinates = new int[2]; |
| 166 | if (findEmptyCell(context, items, tmpCoordinates, screen)) { |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 167 | if (intent != null) { |
| 168 | if (intent.getAction() == null) { |
| 169 | intent.setAction(Intent.ACTION_VIEW); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 170 | } else if (intent.getAction().equals(Intent.ACTION_MAIN) && |
| 171 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
| 172 | intent.addFlags( |
| 173 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 174 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 175 | |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 176 | // By default, we allow for duplicate entries (located in |
| 177 | // different places) |
| 178 | boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true); |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame] | 179 | if (duplicate || !shortcutExists) { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 180 | // If the new app is going to fall into the same page as before, then just |
| 181 | // continue adding to the current page |
| 182 | int newAppsScreen = sharedPrefs.getInt(NEW_APPS_PAGE_KEY, screen); |
| 183 | Set<String> newApps = new HashSet<String>(); |
| 184 | if (newAppsScreen == screen) { |
| 185 | newApps = sharedPrefs.getStringSet(NEW_APPS_LIST_KEY, newApps); |
| 186 | } |
Winson Chung | bfeac06 | 2012-06-06 15:56:08 -0700 | [diff] [blame] | 187 | synchronized (newApps) { |
| 188 | newApps.add(intent.toUri(0).toString()); |
| 189 | } |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 190 | final Set<String> savedNewApps = newApps; |
| 191 | new Thread("setNewAppsThread") { |
| 192 | public void run() { |
Winson Chung | d9e20bf | 2012-06-11 13:57:49 -0700 | [diff] [blame] | 193 | synchronized (savedNewApps) { |
| 194 | sharedPrefs.edit() |
| 195 | .putInt(NEW_APPS_PAGE_KEY, screen) |
| 196 | .putStringSet(NEW_APPS_LIST_KEY, savedNewApps) |
| 197 | .commit(); |
| 198 | } |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 199 | } |
| 200 | }.start(); |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 201 | |
| 202 | // Update the Launcher db |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 203 | LauncherApplication app = (LauncherApplication) context.getApplicationContext(); |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 204 | ShortcutInfo info = app.getModel().addShortcut(context, data, |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 205 | LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 206 | tmpCoordinates[0], tmpCoordinates[1], true); |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 207 | if (info == null) { |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 210 | } else { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 211 | result[0] = INSTALL_SHORTCUT_IS_DUPLICATE; |
Winson Chung | b3781d9 | 2011-01-27 15:21:41 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 215 | } |
Romain Guy | d93a7d1 | 2009-03-24 21:17:50 -0700 | [diff] [blame] | 216 | } else { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 217 | result[0] = INSTALL_SHORTCUT_NO_SPACE; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
Winson Chung | b7bea81 | 2012-02-13 12:53:54 -0800 | [diff] [blame] | 223 | private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy, |
| 224 | int screen) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 225 | final int xCount = LauncherModel.getCellCountX(); |
| 226 | final int yCount = LauncherModel.getCellCountY(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | boolean[][] occupied = new boolean[xCount][yCount]; |
| 228 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 229 | ItemInfo item = null; |
| 230 | int cellX, cellY, spanX, spanY; |
| 231 | for (int i = 0; i < items.size(); ++i) { |
| 232 | item = items.get(i); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 233 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 234 | if (item.screen == screen) { |
| 235 | cellX = item.cellX; |
| 236 | cellY = item.cellY; |
| 237 | spanX = item.spanX; |
| 238 | spanY = item.spanY; |
Winson Chung | d7e9e37 | 2012-04-24 14:49:03 -0700 | [diff] [blame] | 239 | for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) { |
| 240 | for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 241 | occupied[x][y] = true; |
| 242 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied); |
| 249 | } |
| 250 | } |