blob: fb7bf30e0860c82621e94ee6109b5cb66af83ff1 [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;
20import android.content.Context;
21import android.content.Intent;
Winson Chungf0c6ae02012-03-21 16:10:31 -070022import android.content.SharedPreferences;
Winson Chungc3a747a2012-02-29 10:56:19 -080023import android.content.pm.ActivityInfo;
24import android.content.pm.PackageManager;
Romain Guyd93a7d12009-03-24 21:17:50 -070025import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
Romain Guyedcce092010-03-04 13:03:17 -080027import com.android.launcher.R;
28
Winson Chunge428e292012-01-10 16:20:33 -080029import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070030import java.util.HashSet;
Winson Chungf561bdf2012-05-03 11:20:19 -070031import java.util.Iterator;
Winson Chungf0c6ae02012-03-21 16:10:31 -070032import java.util.Set;
Winson Chunge428e292012-01-10 16:20:33 -080033
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034public class InstallShortcutReceiver extends BroadcastReceiver {
Winson Chunga9abd0e2010-10-27 17:18:37 -070035 public static final String ACTION_INSTALL_SHORTCUT =
Romain Guy51ed5b92009-06-17 10:20:34 -070036 "com.android.launcher.action.INSTALL_SHORTCUT";
Winson Chungf0c6ae02012-03-21 16:10:31 -070037 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 Guy51ed5b92009-06-17 10:20:34 -070046
Winson Chung68846fd2010-10-29 11:00:27 -070047 // A mime-type representing shortcut data
48 public static final String SHORTCUT_MIMETYPE =
49 "com.android.launcher/shortcut";
50
Winson Chungf561bdf2012-05-03 11:20:19 -070051 // 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 Project31dd5032009-03-03 19:32:27 -080071
72 public void onReceive(Context context, Intent data) {
Romain Guy51ed5b92009-06-17 10:20:34 -070073 if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
74 return;
75 }
76
Winson Chungf561bdf2012-05-03 11:20:19 -070077 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
Winson Chungc3a747a2012-02-29 10:56:19 -080078 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 Chungde0fb8f2012-05-08 14:37:08 -070093 // Queue the item up for adding if launcher has not loaded properly yet
94 boolean launcherNotLoaded = LauncherModel.getCellCountX() <= 0 ||
95 LauncherModel.getCellCountY() <= 0;
Winson Chungb7bea812012-02-13 12:53:54 -080096
Winson Chungf561bdf2012-05-03 11:20:19 -070097 PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent);
Winson Chungde0fb8f2012-05-08 14:37:08 -070098 if (mUseInstallQueue || launcherNotLoaded) {
Winson Chungf561bdf2012-05-03 11:20:19 -070099 mInstallQueue.add(info);
100 } else {
101 processInstallShortcut(context, info);
102 }
103 }
104
105 static void enableInstallQueue() {
106 mUseInstallQueue = true;
107 }
Winson Chungf561bdf2012-05-03 11:20:19 -0700108 static void disableAndFlushInstallQueue(Context context) {
109 mUseInstallQueue = false;
Winson Chungde0fb8f2012-05-08 14:37:08 -0700110 flushInstallQueue(context);
111 }
112 static void flushInstallQueue(Context context) {
Winson Chungf561bdf2012-05-03 11:20:19 -0700113 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 Chunga2413752012-04-03 14:22:34 -0700129 // 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 Chungf0c6ae02012-03-21 16:10:31 -0700131 final int[] result = {INSTALL_SHORTCUT_SUCCESSFUL};
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132 boolean found = false;
Winson Chunga2413752012-04-03 14:22:34 -0700133 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 Project31dd5032009-03-03 19:32:27 -0800146 }
147 }
Winson Chunge428e292012-01-10 16:20:33 -0800148
Winson Chungf0c6ae02012-03-21 16:10:31 -0700149 // 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 Chung169c3d72012-04-24 14:43:54 -0700153 Toast.makeText(context, context.getString(R.string.completely_out_of_space),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700154 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 Chunge428e292012-01-10 16:20:33 -0800159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
Winson Chungf561bdf2012-05-03 11:20:19 -0700162 private static boolean installShortcut(Context context, Intent data, ArrayList<ItemInfo> items,
Winson Chunga2413752012-04-03 14:22:34 -0700163 String name, Intent intent, final int screen, boolean shortcutExists,
164 final SharedPreferences sharedPrefs, int[] result) {
Winson Chungf561bdf2012-05-03 11:20:19 -0700165 int[] tmpCoordinates = new int[2];
166 if (findEmptyCell(context, items, tmpCoordinates, screen)) {
Winson Chungb3781d92011-01-27 15:21:41 -0800167 if (intent != null) {
168 if (intent.getAction() == null) {
169 intent.setAction(Intent.ACTION_VIEW);
Michael Jurka96879562012-03-22 05:54:33 -0700170 } 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 Chungb3781d92011-01-27 15:21:41 -0800174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Winson Chungb3781d92011-01-27 15:21:41 -0800176 // By default, we allow for duplicate entries (located in
177 // different places)
178 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
Winson Chungb7bea812012-02-13 12:53:54 -0800179 if (duplicate || !shortcutExists) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700180 // 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 Chungbfeac062012-06-06 15:56:08 -0700187 synchronized (newApps) {
188 newApps.add(intent.toUri(0).toString());
189 }
Winson Chunga2413752012-04-03 14:22:34 -0700190 final Set<String> savedNewApps = newApps;
191 new Thread("setNewAppsThread") {
192 public void run() {
Winson Chungd9e20bf2012-06-11 13:57:49 -0700193 synchronized (savedNewApps) {
194 sharedPrefs.edit()
195 .putInt(NEW_APPS_PAGE_KEY, screen)
196 .putStringSet(NEW_APPS_LIST_KEY, savedNewApps)
197 .commit();
198 }
Winson Chunga2413752012-04-03 14:22:34 -0700199 }
200 }.start();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700201
202 // Update the Launcher db
Winson Chungb3781d92011-01-27 15:21:41 -0800203 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Adam Cohend9198822011-11-22 16:42:47 -0800204 ShortcutInfo info = app.getModel().addShortcut(context, data,
Winson Chungf0c6ae02012-03-21 16:10:31 -0700205 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
Winson Chungf561bdf2012-05-03 11:20:19 -0700206 tmpCoordinates[0], tmpCoordinates[1], true);
Winson Chungf0c6ae02012-03-21 16:10:31 -0700207 if (info == null) {
Adam Cohend9198822011-11-22 16:42:47 -0800208 return false;
209 }
Winson Chungb3781d92011-01-27 15:21:41 -0800210 } else {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700211 result[0] = INSTALL_SHORTCUT_IS_DUPLICATE;
Winson Chungb3781d92011-01-27 15:21:41 -0800212 }
213
214 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Romain Guyd93a7d12009-03-24 21:17:50 -0700216 } else {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700217 result[0] = INSTALL_SHORTCUT_NO_SPACE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 }
219
220 return false;
221 }
222
Winson Chungb7bea812012-02-13 12:53:54 -0800223 private static boolean findEmptyCell(Context context, ArrayList<ItemInfo> items, int[] xy,
224 int screen) {
Adam Cohend22015c2010-07-26 22:02:18 -0700225 final int xCount = LauncherModel.getCellCountX();
226 final int yCount = LauncherModel.getCellCountY();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 boolean[][] occupied = new boolean[xCount][yCount];
228
Winson Chungaafa03c2010-06-11 17:34:16 -0700229 ItemInfo item = null;
230 int cellX, cellY, spanX, spanY;
231 for (int i = 0; i < items.size(); ++i) {
232 item = items.get(i);
Winson Chung3d503fb2011-07-13 17:25:49 -0700233 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 Chungd7e9e372012-04-24 14:49:03 -0700239 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 Chung3d503fb2011-07-13 17:25:49 -0700241 occupied[x][y] = true;
242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244 }
245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
248 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
249 }
250}