blob: 228b7a59ff387f97fdfb35a368fd5be92864fc72 [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
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040021import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080023import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
29import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
Romain Guy5c16f3e2010-01-12 17:24:58 -080039import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import java.lang.ref.WeakReference;
45import java.net.URISyntaxException;
46import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import java.util.Comparator;
49import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import java.util.HashMap;
51import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
Romain Guyedcce092010-03-04 13:03:17 -080053import com.android.launcher.R;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * Maintains in-memory state of the Launcher. It is expected that there should be only one
57 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070058 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040060public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080061 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040062 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070063
Joe Onoratof99f8c12009-10-31 17:27:36 -040064 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 private final Object mLock = new Object();
66 private DeferredHandler mHandler = new DeferredHandler();
67 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Joe Onoratof99f8c12009-10-31 17:27:36 -040069 private boolean mBeforeFirstLoad = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
Joe Onorato0589f0f2010-02-08 13:44:00 -080072 private AllAppsList mAllAppsList;
73 private IconCache mIconCache;
74
75 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Joe Onorato9c1289c2009-08-17 11:03:03 -040077 public interface Callbacks {
78 public int getCurrentWorkspaceScreen();
79 public void startBinding();
80 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -050081 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 public void finishBindingItems();
83 public void bindAppWidget(LauncherAppWidgetInfo info);
84 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -050085 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
86 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
87 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Joe Onorato0589f0f2010-02-08 13:44:00 -080090 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040091 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -080092 mAllAppsList = new AllAppsList(iconCache);
93 mIconCache = iconCache;
94
95 mDefaultIcon = Utilities.createIconBitmap(
96 app.getPackageManager().getDefaultActivityIcon(), app);
97 }
98
99 public Bitmap getDefaultIcon() {
100 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 /**
104 * Adds an item to the DB if it was not created previously, or move it to a new
105 * <container, screen, cellX, cellY>
106 */
107 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
108 int screen, int cellX, int cellY) {
109 if (item.container == ItemInfo.NO_ID) {
110 // From all apps
111 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
112 } else {
113 // From somewhere else
114 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
116 }
117
118 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700120 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
122 int cellX, int cellY) {
123 item.container = container;
124 item.screen = screen;
125 item.cellX = cellX;
126 item.cellY = cellY;
127
128 final ContentValues values = new ContentValues();
129 final ContentResolver cr = context.getContentResolver();
130
131 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
132 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
133 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
134 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
135
136 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700137 }
138
139 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400140 * Returns true if the shortcuts already exists in the database.
141 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 static boolean shortcutExists(Context context, String title, Intent intent) {
144 final ContentResolver cr = context.getContentResolver();
145 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
146 new String[] { "title", "intent" }, "title=? and intent=?",
147 new String[] { title, intent.toUri(0) }, null);
148 boolean result = false;
149 try {
150 result = c.moveToFirst();
151 } finally {
152 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700155 }
156
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 /**
158 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
159 */
160 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
161 final ContentResolver cr = context.getContentResolver();
162 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
163 "_id=? and (itemType=? or itemType=?)",
164 new String[] { String.valueOf(id),
165 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
166 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700167
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 try {
169 if (c.moveToFirst()) {
170 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
171 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
172 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
173 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
174 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
175 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 FolderInfo folderInfo = null;
178 switch (c.getInt(itemTypeIndex)) {
179 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
180 folderInfo = findOrMakeUserFolder(folderList, id);
181 break;
182 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
183 folderInfo = findOrMakeLiveFolder(folderList, id);
184 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700185 }
186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 folderInfo.title = c.getString(titleIndex);
188 folderInfo.id = id;
189 folderInfo.container = c.getInt(containerIndex);
190 folderInfo.screen = c.getInt(screenIndex);
191 folderInfo.cellX = c.getInt(cellXIndex);
192 folderInfo.cellY = c.getInt(cellYIndex);
193
194 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 } finally {
197 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700198 }
199
200 return null;
201 }
202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 /**
204 * Add an item to the database in a specified container. Sets the container, screen, cellX and
205 * cellY fields of the item. Also assigns an ID to the item.
206 */
207 static void addItemToDatabase(Context context, ItemInfo item, long container,
208 int screen, int cellX, int cellY, boolean notify) {
209 item.container = container;
210 item.screen = screen;
211 item.cellX = cellX;
212 item.cellY = cellY;
213
214 final ContentValues values = new ContentValues();
215 final ContentResolver cr = context.getContentResolver();
216
217 item.onAddToDatabase(values);
218
219 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
220 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
221
222 if (result != null) {
223 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700224 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700225 }
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 /**
228 * Update an item to the database in a specified container.
229 */
230 static void updateItemInDatabase(Context context, ItemInfo item) {
231 final ContentValues values = new ContentValues();
232 final ContentResolver cr = context.getContentResolver();
233
234 item.onAddToDatabase(values);
235
236 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
237 }
238
239 /**
240 * Removes the specified item from the database
241 * @param context
242 * @param item
243 */
244 static void deleteItemFromDatabase(Context context, ItemInfo item) {
245 final ContentResolver cr = context.getContentResolver();
246
247 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
248 }
249
250 /**
251 * Remove the contents of the specified folder from the database
252 */
253 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
254 final ContentResolver cr = context.getContentResolver();
255
256 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
257 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
258 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
259 }
260
261 /**
262 * Set this as the current Launcher activity object for the loader.
263 */
264 public void initialize(Callbacks callbacks) {
265 synchronized (mLock) {
266 mCallbacks = new WeakReference<Callbacks>(callbacks);
267 }
268 }
269
270 public void startLoader(Context context, boolean isLaunching) {
271 mLoader.startLoader(context, isLaunching);
272 }
273
274 public void stopLoader() {
275 mLoader.stopLoader();
276 }
277
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700278 /**
279 * We pick up most of the changes to all apps.
280 */
281 public void setAllAppsDirty() {
282 mLoader.setAllAppsDirty();
283 }
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 public void setWorkspaceDirty() {
286 mLoader.setWorkspaceDirty();
287 }
288
289 /**
290 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
291 * ACTION_PACKAGE_CHANGED.
292 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400293 public void onReceive(Context context, Intent intent) {
Joe Onorato64e6be72010-03-05 15:05:52 -0500294 Log.d(TAG, "onReceive intent=" + intent);
295
Joe Onoratof99f8c12009-10-31 17:27:36 -0400296 // Use the app as the context.
297 context = mApp;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 ArrayList<ApplicationInfo> added = null;
300 ArrayList<ApplicationInfo> removed = null;
301 ArrayList<ApplicationInfo> modified = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302
303 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400304 if (mBeforeFirstLoad) {
305 // If we haven't even loaded yet, don't bother, since we'll just pick
306 // up the changes.
307 return;
308 }
309
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310 final String action = intent.getAction();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
Joe Onorato64e6be72010-03-05 15:05:52 -0500312 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
313 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
314 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
315 final String packageName = intent.getData().getSchemeSpecificPart();
316 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400317
Joe Onorato64e6be72010-03-05 15:05:52 -0500318 if (packageName == null || packageName.length() == 0) {
319 // they sent us a bad intent
320 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 }
Joe Onorato64e6be72010-03-05 15:05:52 -0500322
323 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400324 mAllAppsList.updatePackage(context, packageName);
Joe Onorato64e6be72010-03-05 15:05:52 -0500325 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
326 if (!replacing) {
327 mAllAppsList.removePackage(packageName);
328 }
329 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
330 // later, we will update the package at this time
331 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
332 if (!replacing) {
333 mAllAppsList.addPackage(context, packageName);
334 } else {
335 mAllAppsList.updatePackage(context, packageName);
336 }
337 }
338 } else {
339 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
340 String packages[] = intent.getStringArrayExtra(
341 Intent.EXTRA_CHANGED_PACKAGE_LIST);
342 if (packages == null || packages.length == 0) {
343 return;
344 }
345 Log.d(TAG, "they're back! " + packages);
346 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
347 String packages[] = intent.getStringArrayExtra(
348 Intent.EXTRA_CHANGED_PACKAGE_LIST);
349 if (packages == null || packages.length == 0) {
350 return;
351 }
352 Log.d(TAG, "they're gone! " + packages);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353 }
354 }
355
356 if (mAllAppsList.added.size() > 0) {
357 added = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800358 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 }
360 if (mAllAppsList.removed.size() > 0) {
361 removed = mAllAppsList.removed;
Romain Guy84f296c2009-11-04 15:00:44 -0800362 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363 for (ApplicationInfo info: removed) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800364 mIconCache.remove(info.intent.getComponent());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 }
366 }
367 if (mAllAppsList.modified.size() > 0) {
368 modified = mAllAppsList.modified;
Romain Guy84f296c2009-11-04 15:00:44 -0800369 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 }
371
Marco Nelissen3c8b90d2009-09-11 14:49:50 -0700372 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 if (callbacks == null) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800374 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 return;
376 }
377
378 if (added != null) {
379 final ArrayList<ApplicationInfo> addedFinal = added;
380 mHandler.post(new Runnable() {
381 public void run() {
Joe Onorato64e6be72010-03-05 15:05:52 -0500382 callbacks.bindAppsAdded(addedFinal);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 }
384 });
385 }
Joe Onorato418928e2009-11-19 18:05:36 -0800386 if (modified != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 final ArrayList<ApplicationInfo> modifiedFinal = modified;
388 mHandler.post(new Runnable() {
389 public void run() {
Joe Onorato64e6be72010-03-05 15:05:52 -0500390 callbacks.bindAppsUpdated(modifiedFinal);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 }
392 });
393 }
Joe Onorato418928e2009-11-19 18:05:36 -0800394 if (removed != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 final ArrayList<ApplicationInfo> removedFinal = removed;
396 mHandler.post(new Runnable() {
397 public void run() {
Joe Onorato64e6be72010-03-05 15:05:52 -0500398 callbacks.bindAppsRemoved(removedFinal);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 }
400 });
401 }
402 }
403 }
404
405 public class Loader {
406 private static final int ITEMS_CHUNK = 6;
407
408 private LoaderThread mLoaderThread;
409
410 private int mLastWorkspaceSeq = 0;
411 private int mWorkspaceSeq = 1;
412
413 private int mLastAllAppsSeq = 0;
414 private int mAllAppsSeq = 1;
415
Romain Guy84f296c2009-11-04 15:00:44 -0800416 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
417 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
Joe Onoratoad72e172009-11-06 16:25:04 -0500418 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419
420 /**
421 * Call this from the ui thread so the handler is initialized on the correct thread.
422 */
423 public Loader() {
424 }
425
426 public void startLoader(Context context, boolean isLaunching) {
427 synchronized (mLock) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800428 if (DEBUG_LOADERS) {
429 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 // Don't bother to start the thread if we know it's not going to do anything
432 if (mCallbacks.get() != null) {
433 LoaderThread oldThread = mLoaderThread;
434 if (oldThread != null) {
435 if (oldThread.isLaunching()) {
436 // don't downgrade isLaunching if we're already running
437 isLaunching = true;
438 }
439 oldThread.stopLocked();
440 }
441 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
442 mLoaderThread.start();
443 }
444 }
445 }
446
447 public void stopLoader() {
448 synchronized (mLock) {
449 if (mLoaderThread != null) {
450 mLoaderThread.stopLocked();
451 }
452 }
453 }
454
455 public void setWorkspaceDirty() {
456 synchronized (mLock) {
457 mWorkspaceSeq++;
458 }
459 }
460
461 public void setAllAppsDirty() {
462 synchronized (mLock) {
463 mAllAppsSeq++;
464 }
465 }
466
467 /**
468 * Runnable for the thread that loads the contents of the launcher:
469 * - workspace icons
470 * - widgets
471 * - all apps icons
472 */
473 private class LoaderThread extends Thread {
474 private Context mContext;
475 private Thread mWaitThread;
476 private boolean mIsLaunching;
477 private boolean mStopped;
478 private boolean mWorkspaceDoneBinding;
479
480 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
481 mContext = context;
482 mWaitThread = waitThread;
483 mIsLaunching = isLaunching;
484 }
485
486 boolean isLaunching() {
487 return mIsLaunching;
488 }
489
490 /**
491 * If another LoaderThread was supplied, we need to wait for that to finish before
492 * we start our processing. This keeps the ordering of the setting and clearing
493 * of the dirty flags correct by making sure we don't start processing stuff until
494 * they've had a chance to re-set them. We do this waiting the worker thread, not
495 * the ui thread to avoid ANRs.
496 */
497 private void waitForOtherThread() {
498 if (mWaitThread != null) {
499 boolean done = false;
500 while (!done) {
501 try {
502 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700503 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400504 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800505 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400506 }
507 }
508 mWaitThread = null;
509 }
510 }
511
512 public void run() {
513 waitForOtherThread();
514
515 // Elevate priority when Home launches for the first time to avoid
516 // starving at boot time. Staring at a blank home is not cool.
517 synchronized (mLock) {
518 android.os.Process.setThreadPriority(mIsLaunching
519 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
520 }
521
522 // Load the workspace only if it's dirty.
523 int workspaceSeq;
524 boolean workspaceDirty;
525 synchronized (mLock) {
526 workspaceSeq = mWorkspaceSeq;
527 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
528 }
529 if (workspaceDirty) {
530 loadWorkspace();
531 }
532 synchronized (mLock) {
533 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
534 if (mStopped) {
535 return;
536 }
537 if (workspaceSeq == mWorkspaceSeq) {
538 mLastWorkspaceSeq = mWorkspaceSeq;
539 }
540 }
541
542 // Bind the workspace
543 bindWorkspace();
544
545 // Wait until the either we're stopped or the other threads are done.
546 // This way we don't start loading all apps until the workspace has settled
547 // down.
548 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500549 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 public void run() {
551 synchronized (LoaderThread.this) {
552 mWorkspaceDoneBinding = true;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800553 if (DEBUG_LOADERS) {
554 Log.d(TAG, "done with workspace");
555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 LoaderThread.this.notify();
557 }
558 }
559 });
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800560 if (DEBUG_LOADERS) {
561 Log.d(TAG, "waiting to be done with workspace");
562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 while (!mStopped && !mWorkspaceDoneBinding) {
564 try {
565 this.wait();
566 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800567 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 }
569 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800570 if (DEBUG_LOADERS) {
571 Log.d(TAG, "done waiting to be done with workspace");
572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 }
574
575 // Load all apps if they're dirty
576 int allAppsSeq;
577 boolean allAppsDirty;
578 synchronized (mLock) {
579 allAppsSeq = mAllAppsSeq;
580 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800581 if (DEBUG_LOADERS) {
582 Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
583 + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400585 }
586 if (allAppsDirty) {
587 loadAllApps();
588 }
589 synchronized (mLock) {
590 // If we're not stopped, and nobody has incremented mAllAppsSeq.
591 if (mStopped) {
592 return;
593 }
594 if (allAppsSeq == mAllAppsSeq) {
595 mLastAllAppsSeq = mAllAppsSeq;
596 }
597 }
598
599 // Bind all apps
Joe Onorato34b02492009-10-14 11:13:48 -0700600 if (allAppsDirty) {
601 bindAllApps();
602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603
604 // Clear out this reference, otherwise we end up holding it until all of the
605 // callback runnables are done.
606 mContext = null;
607
608 synchronized (mLock) {
609 // Setting the reference is atomic, but we can't do it inside the other critical
610 // sections.
611 mLoaderThread = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 }
613 }
614
615 public void stopLocked() {
616 synchronized (LoaderThread.this) {
617 mStopped = true;
618 this.notify();
619 }
620 }
621
622 /**
623 * Gets the callbacks object. If we've been stopped, or if the launcher object
624 * has somehow been garbage collected, return null instead.
625 */
626 Callbacks tryGetCallbacks() {
627 synchronized (mLock) {
628 if (mStopped) {
629 return null;
630 }
631
632 final Callbacks callbacks = mCallbacks.get();
633 if (callbacks == null) {
634 Log.w(TAG, "no mCallbacks");
635 return null;
636 }
637
638 return callbacks;
639 }
640 }
641
642 private void loadWorkspace() {
643 long t = SystemClock.uptimeMillis();
644
645 final Context context = mContext;
646 final ContentResolver contentResolver = context.getContentResolver();
647 final PackageManager manager = context.getPackageManager();
Romain Guy629de3e2010-01-13 12:20:59 -0800648 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800649 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
651 /* TODO
652 if (mLocaleChanged) {
653 updateShortcutLabels(contentResolver, manager);
654 }
655 */
656
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400657 mItems.clear();
Joe Onorato511ab642009-11-08 14:14:07 -0500658 mAppWidgets.clear();
Joe Onorato1db7a972009-11-16 18:32:22 -0800659 mFolders.clear();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400660
Romain Guy5c16f3e2010-01-12 17:24:58 -0800661 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
662
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 final Cursor c = contentResolver.query(
664 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
665
666 try {
667 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
668 final int intentIndex = c.getColumnIndexOrThrow
669 (LauncherSettings.Favorites.INTENT);
670 final int titleIndex = c.getColumnIndexOrThrow
671 (LauncherSettings.Favorites.TITLE);
672 final int iconTypeIndex = c.getColumnIndexOrThrow(
673 LauncherSettings.Favorites.ICON_TYPE);
674 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
675 final int iconPackageIndex = c.getColumnIndexOrThrow(
676 LauncherSettings.Favorites.ICON_PACKAGE);
677 final int iconResourceIndex = c.getColumnIndexOrThrow(
678 LauncherSettings.Favorites.ICON_RESOURCE);
679 final int containerIndex = c.getColumnIndexOrThrow(
680 LauncherSettings.Favorites.CONTAINER);
681 final int itemTypeIndex = c.getColumnIndexOrThrow(
682 LauncherSettings.Favorites.ITEM_TYPE);
683 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
684 LauncherSettings.Favorites.APPWIDGET_ID);
685 final int screenIndex = c.getColumnIndexOrThrow(
686 LauncherSettings.Favorites.SCREEN);
687 final int cellXIndex = c.getColumnIndexOrThrow
688 (LauncherSettings.Favorites.CELLX);
689 final int cellYIndex = c.getColumnIndexOrThrow
690 (LauncherSettings.Favorites.CELLY);
691 final int spanXIndex = c.getColumnIndexOrThrow
692 (LauncherSettings.Favorites.SPANX);
693 final int spanYIndex = c.getColumnIndexOrThrow(
694 LauncherSettings.Favorites.SPANY);
695 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
696 final int displayModeIndex = c.getColumnIndexOrThrow(
697 LauncherSettings.Favorites.DISPLAY_MODE);
698
Joe Onorato0589f0f2010-02-08 13:44:00 -0800699 ShortcutInfo info;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 String intentDescription;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 LauncherAppWidgetInfo appWidgetInfo;
702 int container;
703 long id;
704 Intent intent;
705
706 while (!mStopped && c.moveToNext()) {
707 try {
708 int itemType = c.getInt(itemTypeIndex);
709
710 switch (itemType) {
711 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
712 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
713 intentDescription = c.getString(intentIndex);
714 try {
715 intent = Intent.parseUri(intentDescription, 0);
716 } catch (URISyntaxException e) {
717 continue;
718 }
719
720 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800721 info = getShortcutInfo(manager, intent, context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 } else {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800723 info = getShortcutInfo(c, context, iconTypeIndex,
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 iconPackageIndex, iconResourceIndex, iconIndex);
725 }
726
727 if (info == null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800728 info = new ShortcutInfo();
729 info.setIcon(getDefaultIcon());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730 }
731
732 if (info != null) {
Joe Onorato028b6242009-11-10 18:26:13 -0800733 if (itemType
734 != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
735 info.title = c.getString(titleIndex);
736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 info.intent = intent;
738
739 info.id = c.getLong(idIndex);
740 container = c.getInt(containerIndex);
741 info.container = container;
742 info.screen = c.getInt(screenIndex);
743 info.cellX = c.getInt(cellXIndex);
744 info.cellY = c.getInt(cellYIndex);
745
746 switch (container) {
747 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
748 mItems.add(info);
749 break;
750 default:
751 // Item is in a user folder
752 UserFolderInfo folderInfo =
Joe Onoratoad72e172009-11-06 16:25:04 -0500753 findOrMakeUserFolder(mFolders, container);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 folderInfo.add(info);
755 break;
756 }
757 }
758 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759
Joe Onoratoad72e172009-11-06 16:25:04 -0500760 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 id = c.getLong(idIndex);
Joe Onoratoad72e172009-11-06 16:25:04 -0500762 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763
764 folderInfo.title = c.getString(titleIndex);
765
766 folderInfo.id = id;
767 container = c.getInt(containerIndex);
768 folderInfo.container = container;
769 folderInfo.screen = c.getInt(screenIndex);
770 folderInfo.cellX = c.getInt(cellXIndex);
771 folderInfo.cellY = c.getInt(cellYIndex);
772
773 switch (container) {
774 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
775 mItems.add(folderInfo);
776 break;
777 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500778
779 mFolders.put(folderInfo.id, folderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500781
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 id = c.getLong(idIndex);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800784 Uri uri = Uri.parse(c.getString(uriIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785
Romain Guy5c16f3e2010-01-12 17:24:58 -0800786 // Make sure the live folder exists
787 final ProviderInfo providerInfo =
788 context.getPackageManager().resolveContentProvider(
789 uri.getAuthority(), 0);
790
791 if (providerInfo == null && !isSafeMode) {
792 itemsToRemove.add(id);
793 } else {
794 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
795
796 intentDescription = c.getString(intentIndex);
797 intent = null;
798 if (intentDescription != null) {
799 try {
800 intent = Intent.parseUri(intentDescription, 0);
801 } catch (URISyntaxException e) {
802 // Ignore, a live folder might not have a base intent
803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800805
806 liveFolderInfo.title = c.getString(titleIndex);
807 liveFolderInfo.id = id;
808 liveFolderInfo.uri = uri;
809 container = c.getInt(containerIndex);
810 liveFolderInfo.container = container;
811 liveFolderInfo.screen = c.getInt(screenIndex);
812 liveFolderInfo.cellX = c.getInt(cellXIndex);
813 liveFolderInfo.cellY = c.getInt(cellYIndex);
814 liveFolderInfo.baseIntent = intent;
815 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
816
817 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
818 iconResourceIndex, liveFolderInfo);
819
820 switch (container) {
821 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
822 mItems.add(liveFolderInfo);
823 break;
824 }
825 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500828
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
830 // Read all Launcher-specific widget details
831 int appWidgetId = c.getInt(appWidgetIdIndex);
Romain Guy629de3e2010-01-13 12:20:59 -0800832 id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833
Romain Guy629de3e2010-01-13 12:20:59 -0800834 final AppWidgetProviderInfo provider =
835 widgets.getAppWidgetInfo(appWidgetId);
836
837 if (!isSafeMode && (provider == null || provider.provider == null ||
838 provider.provider.getPackageName() == null)) {
839 itemsToRemove.add(id);
840 } else {
841 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
842 appWidgetInfo.id = id;
843 appWidgetInfo.screen = c.getInt(screenIndex);
844 appWidgetInfo.cellX = c.getInt(cellXIndex);
845 appWidgetInfo.cellY = c.getInt(cellYIndex);
846 appWidgetInfo.spanX = c.getInt(spanXIndex);
847 appWidgetInfo.spanY = c.getInt(spanYIndex);
848
849 container = c.getInt(containerIndex);
850 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
851 Log.e(TAG, "Widget found where container "
852 + "!= CONTAINER_DESKTOP -- ignoring!");
853 continue;
854 }
855 appWidgetInfo.container = c.getInt(containerIndex);
856
857 mAppWidgets.add(appWidgetInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 break;
860 }
861 } catch (Exception e) {
862 Log.w(TAG, "Desktop items loading interrupted:", e);
863 }
864 }
865 } finally {
866 c.close();
867 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800868
869 if (itemsToRemove.size() > 0) {
870 ContentProviderClient client = contentResolver.acquireContentProviderClient(
871 LauncherSettings.Favorites.CONTENT_URI);
872 // Remove dead items
873 for (long id : itemsToRemove) {
874 if (DEBUG_LOADERS) {
875 Log.d(TAG, "Removed id = " + id);
876 }
877 // Don't notify content observers
878 try {
879 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
880 null, null);
881 } catch (RemoteException e) {
882 Log.w(TAG, "Could not remove id = " + id);
883 }
884 }
885 }
886
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800887 if (DEBUG_LOADERS) {
888 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890 }
891
892 /**
893 * Read everything out of our database.
894 */
895 private void bindWorkspace() {
896 final long t = SystemClock.uptimeMillis();
897
898 // Don't use these two variables in any of the callback runnables.
899 // Otherwise we hold a reference to them.
900 Callbacks callbacks = mCallbacks.get();
901 if (callbacks == null) {
902 // This launcher has exited and nobody bothered to tell us. Just bail.
903 Log.w(TAG, "LoaderThread running with no launcher");
904 return;
905 }
906
907 int N;
908 // Tell the workspace that we're about to start firing items at it
909 mHandler.post(new Runnable() {
910 public void run() {
911 Callbacks callbacks = tryGetCallbacks();
912 if (callbacks != null) {
913 callbacks.startBinding();
914 }
915 }
916 });
917 // Add the items to the workspace.
918 N = mItems.size();
919 for (int i=0; i<N; i+=ITEMS_CHUNK) {
920 final int start = i;
921 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
922 mHandler.post(new Runnable() {
923 public void run() {
924 Callbacks callbacks = tryGetCallbacks();
925 if (callbacks != null) {
926 callbacks.bindItems(mItems, start, start+chunkSize);
927 }
928 }
929 });
930 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500931 mHandler.post(new Runnable() {
932 public void run() {
933 Callbacks callbacks = tryGetCallbacks();
934 if (callbacks != null) {
935 callbacks.bindFolders(mFolders);
936 }
937 }
938 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 // Wait until the queue goes empty.
940 mHandler.postIdle(new Runnable() {
941 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800942 if (DEBUG_LOADERS) {
943 Log.d(TAG, "Going to start binding widgets soon.");
944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 }
946 });
947 // Bind the widgets, one at a time.
948 // WARNING: this is calling into the workspace from the background thread,
949 // but since getCurrentScreen() just returns the int, we should be okay. This
950 // is just a hint for the order, and if it's wrong, we'll be okay.
951 // TODO: instead, we should have that push the current screen into here.
952 final int currentScreen = callbacks.getCurrentWorkspaceScreen();
953 N = mAppWidgets.size();
954 // once for the current screen
955 for (int i=0; i<N; i++) {
956 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
957 if (widget.screen == currentScreen) {
958 mHandler.post(new Runnable() {
959 public void run() {
960 Callbacks callbacks = tryGetCallbacks();
961 if (callbacks != null) {
962 callbacks.bindAppWidget(widget);
963 }
964 }
965 });
966 }
967 }
968 // once for the other screens
969 for (int i=0; i<N; i++) {
970 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
971 if (widget.screen != currentScreen) {
972 mHandler.post(new Runnable() {
973 public void run() {
974 Callbacks callbacks = tryGetCallbacks();
975 if (callbacks != null) {
976 callbacks.bindAppWidget(widget);
977 }
978 }
979 });
980 }
981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 // Tell the workspace that we're done.
983 mHandler.post(new Runnable() {
984 public void run() {
985 Callbacks callbacks = tryGetCallbacks();
986 if (callbacks != null) {
987 callbacks.finishBindingItems();
988 }
989 }
990 });
991 // If we're profiling, this is the last thing in the queue.
992 mHandler.post(new Runnable() {
993 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800994 if (DEBUG_LOADERS) {
995 Log.d(TAG, "bound workspace in "
996 + (SystemClock.uptimeMillis()-t) + "ms");
997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 if (Launcher.PROFILE_ROTATE) {
999 android.os.Debug.stopMethodTracing();
1000 }
1001 }
1002 });
1003 }
1004
1005 private void loadAllApps() {
1006 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1007 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1008
1009 final Callbacks callbacks = tryGetCallbacks();
1010 if (callbacks == null) {
1011 return;
1012 }
1013
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
1016
1017 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -04001018 mBeforeFirstLoad = false;
1019
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mAllAppsList.clear();
1021 if (apps != null) {
1022 long t = SystemClock.uptimeMillis();
1023
1024 int N = apps.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 for (int i=0; i<N && !mStopped; i++) {
1026 // This builds the icon bitmaps.
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 }
Joe Onoratob0c27f22009-12-01 16:19:38 -08001029 Collections.sort(mAllAppsList.data, APP_NAME_COMPARATOR);
1030 Collections.sort(mAllAppsList.added, APP_NAME_COMPARATOR);
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001031 if (DEBUG_LOADERS) {
1032 Log.d(TAG, "cached app icons in "
1033 + (SystemClock.uptimeMillis()-t) + "ms");
1034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 }
1036 }
1037 }
1038
1039 private void bindAllApps() {
1040 synchronized (mLock) {
Joe Onorato0c4513e2009-11-19 12:24:48 -08001041 final ArrayList<ApplicationInfo> results
Romain Guy5c16f3e2010-01-12 17:24:58 -08001042 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato0c4513e2009-11-19 12:24:48 -08001043 // We're adding this now, so clear out this so we don't re-send them.
Romain Guy84f296c2009-11-04 15:00:44 -08001044 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 mHandler.post(new Runnable() {
1046 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -07001047 final long t = SystemClock.uptimeMillis();
1048 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049
1050 Callbacks callbacks = tryGetCallbacks();
1051 if (callbacks != null) {
1052 callbacks.bindAllApplications(results);
1053 }
1054
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001055 if (DEBUG_LOADERS) {
1056 Log.d(TAG, "bound app " + count + " icons in "
Romain Guy5c16f3e2010-01-12 17:24:58 -08001057 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 }
1060 });
1061 }
1062 }
Joe Onoratobe386092009-11-17 17:32:16 -08001063
1064 public void dumpState() {
1065 Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext);
1066 Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread);
1067 Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching);
1068 Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped);
1069 Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding);
1070 }
1071 }
1072
1073 public void dumpState() {
1074 Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq);
1075 Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq);
1076 Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq);
1077 Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq);
1078 Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size());
1079 if (mLoaderThread != null) {
1080 mLoaderThread.dumpState();
1081 } else {
1082 Log.d(TAG, "mLoader.mLoaderThread=null");
1083 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 }
1085 }
1086
1087 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088 * Make an ShortcutInfo object for a sortcut that is an application.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 Context context) {
1092 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1093
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001094 if (resolveInfo == null) {
1095 return null;
1096 }
1097
Joe Onorato0589f0f2010-02-08 13:44:00 -08001098 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 info.setIcon(mIconCache.getIcon(intent.getComponent(), resolveInfo));
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 if (info.title == null || info.title.length() == 0) {
1102 info.title = activityInfo.loadLabel(manager);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001103 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 if (info.title == null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001105 info.title = activityInfo.name;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1108 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001112 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
Joe Onorato0589f0f2010-02-08 13:44:00 -08001117 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 int iconType = c.getInt(iconTypeIndex);
1121 switch (iconType) {
1122 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1123 String packageName = c.getString(iconPackageIndex);
1124 String resourceName = c.getString(iconResourceIndex);
1125 PackageManager packageManager = context.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 Resources resources = packageManager.getResourcesForApplication(packageName);
1128 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001129 info.setIcon(Utilities.createIconBitmap(resources.getDrawable(id), context));
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131 info.setIcon(getDefaultIcon());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 info.iconResource = new Intent.ShortcutIconResource();
1134 info.iconResource.packageName = packageName;
1135 info.iconResource.resourceName = resourceName;
1136 info.customIcon = false;
1137 break;
1138 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
1139 byte[] data = c.getBlob(iconIndex);
1140 try {
1141 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001142 info.setIcon(bitmap);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001144 info.setIcon(getDefaultIcon());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 info.customIcon = true;
1147 break;
1148 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001149 info.setIcon(getDefaultIcon());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 info.customIcon = false;
1151 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 return info;
1154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155
Joe Onorato0589f0f2010-02-08 13:44:00 -08001156 ShortcutInfo addShortcut(Context context, Intent data,
1157 CellLayout.CellInfo cellInfo, boolean notify) {
1158
1159 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1160 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1161 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1162
1163 return info;
1164 }
1165
1166 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1167 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1168 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1169 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1170
1171 Bitmap icon = null;
1172 boolean filtered = false;
1173 boolean customIcon = false;
1174 ShortcutIconResource iconResource = null;
1175
1176 if (bitmap != null && bitmap instanceof Bitmap) {
1177 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1178 filtered = true;
1179 customIcon = true;
1180 } else {
1181 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1182 if (extra != null && extra instanceof ShortcutIconResource) {
1183 try {
1184 iconResource = (ShortcutIconResource) extra;
1185 final PackageManager packageManager = context.getPackageManager();
1186 Resources resources = packageManager.getResourcesForApplication(
1187 iconResource.packageName);
1188 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1189 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1190 } catch (Exception e) {
1191 Log.w(TAG, "Could not load shortcut icon: " + extra);
1192 }
1193 }
1194 }
1195
1196 if (icon == null) {
1197 icon = getDefaultIcon();
1198 }
1199
1200 final ShortcutInfo info = new ShortcutInfo();
1201 info.setIcon(icon);
1202 info.title = name;
1203 info.intent = intent;
1204 info.customIcon = customIcon;
1205 info.iconResource = iconResource;
1206
1207 return info;
1208 }
1209
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1211 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1212
1213 int iconType = c.getInt(iconTypeIndex);
1214 switch (iconType) {
1215 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1216 String packageName = c.getString(iconPackageIndex);
1217 String resourceName = c.getString(iconResourceIndex);
1218 PackageManager packageManager = context.getPackageManager();
1219 try {
1220 Resources resources = packageManager.getResourcesForApplication(packageName);
1221 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001222 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1223 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001225 liveFolderInfo.icon = Utilities.createIconBitmap(
1226 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1227 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
1229 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1230 liveFolderInfo.iconResource.packageName = packageName;
1231 liveFolderInfo.iconResource.resourceName = resourceName;
1232 break;
1233 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001234 liveFolderInfo.icon = Utilities.createIconBitmap(
1235 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1236 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 }
1238 }
1239
1240 /**
1241 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1242 * or make a new one.
1243 */
1244 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1245 // See if a placeholder was created for us already
1246 FolderInfo folderInfo = folders.get(id);
1247 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1248 // No placeholder -- create a new instance
1249 folderInfo = new UserFolderInfo();
1250 folders.put(id, folderInfo);
1251 }
1252 return (UserFolderInfo) folderInfo;
1253 }
1254
1255 /**
1256 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1257 * new one.
1258 */
1259 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1260 // See if a placeholder was created for us already
1261 FolderInfo folderInfo = folders.get(id);
1262 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1263 // No placeholder -- create a new instance
1264 folderInfo = new LiveFolderInfo();
1265 folders.put(id, folderInfo);
1266 }
1267 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
1270 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
1271 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -07001272 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
1274 null, null, null);
1275
Romain Guy73b979d2009-06-09 12:57:21 -07001276 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1278 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1279 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1280
1281 // boolean changed = false;
1282
1283 try {
1284 while (c.moveToNext()) {
1285 try {
1286 if (c.getInt(itemTypeIndex) !=
1287 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1288 continue;
1289 }
1290
1291 final String intentUri = c.getString(intentIndex);
1292 if (intentUri != null) {
Romain Guy1ce1a242009-06-23 17:34:54 -07001293 final Intent shortcut = Intent.parseUri(intentUri, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
1295 final ComponentName name = shortcut.getComponent();
1296 if (name != null) {
1297 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
1298 final String title = c.getString(titleIndex);
1299 String label = getLabel(manager, activityInfo);
1300
1301 if (title == null || !title.equals(label)) {
1302 final ContentValues values = new ContentValues();
1303 values.put(LauncherSettings.Favorites.TITLE, label);
1304
Romain Guyfedc4fc2009-03-27 20:48:20 -07001305 resolver.update(
1306 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 values, "_id=?",
1308 new String[] { String.valueOf(c.getLong(idIndex)) });
1309
1310 // changed = true;
1311 }
1312 }
1313 }
1314 }
1315 } catch (URISyntaxException e) {
1316 // Ignore
1317 } catch (PackageManager.NameNotFoundException e) {
1318 // Ignore
1319 }
1320 }
1321 } finally {
1322 c.close();
1323 }
1324
1325 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
1326 }
1327
1328 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1329 String label = activityInfo.loadLabel(manager).toString();
1330 if (label == null) {
1331 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1332 if (label == null) {
1333 label = activityInfo.name;
1334 }
1335 }
1336 return label;
1337 }
1338
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001340 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 = new Comparator<ApplicationInfo>() {
1342 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1343 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 };
Joe Onoratobe386092009-11-17 17:32:16 -08001346
1347 public void dumpState() {
1348 Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad);
1349 Log.d(TAG, "mCallbacks=" + mCallbacks);
1350 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1351 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1352 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1353 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
1354 mLoader.dumpState();
1355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356}