blob: 314a502d4e9b1346f2838dac954a6a7c3d922d08 [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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
17package com.android.launcher;
18
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.Intent;
23import android.content.Context;
24import android.content.pm.ActivityInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.content.res.Resources;
28import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070031import android.net.Uri;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070032import android.util.Log;
The Android Open Source Project0e320b22009-01-09 17:51:25 -080033import android.os.Process;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070034
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.HashMap;
38import java.util.List;
39import java.util.Comparator;
40import java.lang.ref.WeakReference;
41import java.text.Collator;
The Android Open Source Projectd097a182008-12-17 18:05:58 -080042import java.net.URISyntaxException;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070043
44/**
45 * Maintains in-memory state of the Launcher. It is expected that there should be only one
46 * LauncherModel object held in a static. Also provide APIs for updating the database state
47 * for the Launcher
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070048 */
49public class LauncherModel {
50 private static final int UI_NOTIFICATION_RATE = 4;
51 private static final int DEFAULT_APPLICATIONS_NUMBER = 42;
52 private static final long APPLICATION_NOT_RESPONDING_TIMEOUT = 5000;
53
54 private final Collator sCollator = Collator.getInstance();
55
56 private boolean mApplicationsLoaded;
57 private boolean mDesktopItemsLoaded;
58
59 private ArrayList<ItemInfo> mDesktopItems;
The Android Open Source Projectd097a182008-12-17 18:05:58 -080060 private HashMap<Long, FolderInfo> mFolders;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070061
62 private ArrayList<ApplicationInfo> mApplications;
63 private ApplicationsAdapter mApplicationsAdapter;
64 private ApplicationsLoader mApplicationsLoader;
65 private DesktopItemsLoader mDesktopItemsLoader;
66 private Thread mLoader;
67 private Thread mDesktopLoader;
68
69 void abortLoaders() {
70 if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
71 mApplicationsLoader.stop();
72 mApplicationsLoaded = false;
73 }
74 if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) {
75 mDesktopItemsLoader.stop();
76 mDesktopItemsLoaded = false;
77 }
78 }
79
80 /**
81 * Loads the list of installed applications in mApplications.
82 */
The Android Open Source Projectd097a182008-12-17 18:05:58 -080083 void loadApplications(boolean isLaunching, Launcher launcher, boolean localeChanged) {
84 if (isLaunching && mApplicationsLoaded && !localeChanged) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070085 mApplicationsAdapter = new ApplicationsAdapter(launcher, mApplications);
86 return;
87 }
88
The Android Open Source Projectd097a182008-12-17 18:05:58 -080089 if (mApplicationsAdapter == null || isLaunching || localeChanged) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070090 mApplicationsAdapter = new ApplicationsAdapter(launcher,
91 mApplications = new ArrayList<ApplicationInfo>(DEFAULT_APPLICATIONS_NUMBER));
92 }
93
94 if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
95 mApplicationsLoader.stop();
96 // Wait for the currently running thread to finish, this can take a little
97 // time but it should be well below the timeout limit
98 try {
99 mLoader.join(APPLICATION_NOT_RESPONDING_TIMEOUT);
100 } catch (InterruptedException e) {
101 // Empty
102 }
103 }
104
105 mApplicationsLoaded = false;
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800106
107 if (!isLaunching) {
108 startApplicationsLoader(launcher);
109 }
110 }
111
112 private void startApplicationsLoader(Launcher launcher) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700113 mApplicationsLoader = new ApplicationsLoader(launcher);
114 mLoader = new Thread(mApplicationsLoader, "Applications Loader");
115 mLoader.start();
116 }
117
118 private class ApplicationsLoader implements Runnable {
119 private final WeakReference<Launcher> mLauncher;
120
121 private volatile boolean mStopped;
122 private volatile boolean mRunning;
123
124 ApplicationsLoader(Launcher launcher) {
125 mLauncher = new WeakReference<Launcher>(launcher);
126 }
127
128 void stop() {
129 mStopped = true;
130 }
131
132 boolean isRunning() {
133 return mRunning;
134 }
135
136 public void run() {
137 mRunning = true;
138
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800139 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
140
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700141 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
142 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
143
144 final Launcher launcher = mLauncher.get();
145 final PackageManager manager = launcher.getPackageManager();
146 final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
147
148 if (apps != null && !mStopped) {
149 final int count = apps.size();
150 final ApplicationsAdapter applicationList = mApplicationsAdapter;
151
152 ChangeNotifier action = new ChangeNotifier(applicationList);
153
154 for (int i = 0; i < count && !mStopped; i++) {
155 ApplicationInfo application = new ApplicationInfo();
156 ResolveInfo info = apps.get(i);
157
158 application.title = info.loadLabel(manager);
159 if (application.title == null) {
160 application.title = info.activityInfo.name;
161 }
162 application.setActivity(new ComponentName(
163 info.activityInfo.applicationInfo.packageName,
164 info.activityInfo.name),
165 Intent.FLAG_ACTIVITY_NEW_TASK |
166 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
167 application.icon = info.activityInfo.loadIcon(manager);
168 application.container = ItemInfo.NO_ID;
169
170 action.add(application);
171 }
172
173 action.sort(new Comparator<ApplicationInfo>() {
174 public final int compare(ApplicationInfo a, ApplicationInfo b) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800175 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700176 }
177 });
178
179 if (!mStopped) {
180 launcher.runOnUiThread(action);
181 }
182 }
183
184 if (!mStopped) {
185 mApplicationsLoaded = true;
186 }
187 mRunning = false;
188 }
189 }
190
191 private static class ChangeNotifier implements Runnable {
192 private final ApplicationsAdapter mApplicationList;
193 private ArrayList<ApplicationInfo> mBuffer;
194
195 ChangeNotifier(ApplicationsAdapter applicationList) {
196 mApplicationList = applicationList;
197 mBuffer = new ArrayList<ApplicationInfo>(UI_NOTIFICATION_RATE);
198 }
199
200 public void run() {
201 final ArrayList<ApplicationInfo> buffer = mBuffer;
202 final ApplicationsAdapter applicationList = mApplicationList;
203 final int count = buffer.size();
204
205 applicationList.clear();
206 for (int i = 0; i < count; i++) {
207 applicationList.setNotifyOnChange(false);
208 applicationList.add(buffer.get(i));
209 }
210
211 applicationList.notifyDataSetChanged();
212 buffer.clear();
213 }
214
215 void add(ApplicationInfo application) {
216 mBuffer.add(application);
217 }
218
219 void sort(Comparator<ApplicationInfo> comparator) {
220 Collections.sort(mBuffer, comparator);
221 }
222 }
223
224 boolean isDesktopLoaded() {
225 return mDesktopItems != null && mDesktopItemsLoaded;
226 }
227
228 /**
229 * Loads all of the items on the desktop, in folders, or in the dock.
230 * These can be apps, shortcuts or widgets
231 */
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800232 void loadUserItems(boolean isLaunching, Launcher launcher, boolean localeChanged,
233 boolean loadApplications) {
234
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700235 if (isLaunching && mDesktopItems != null && mDesktopItemsLoaded) {
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800236 if (loadApplications) startApplicationsLoader(launcher);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700237 // We have already loaded our data from the DB
238 launcher.onDesktopItemsLoaded();
239 return;
240 }
241
242 if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) {
243 mDesktopItemsLoader.stop();
244 // Wait for the currently running thread to finish, this can take a little
245 // time but it should be well below the timeout limit
246 try {
247 mDesktopLoader.join(APPLICATION_NOT_RESPONDING_TIMEOUT);
248 } catch (InterruptedException e) {
249 // Empty
250 }
251 }
252
253 mDesktopItemsLoaded = false;
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800254 mDesktopItemsLoader = new DesktopItemsLoader(launcher, localeChanged, loadApplications);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700255 mDesktopLoader = new Thread(mDesktopItemsLoader, "Desktop Items Loader");
256 mDesktopLoader.start();
257 }
258
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800259 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
260 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
261 new String[] { LauncherSettings.Favorites.ID, LauncherSettings.Favorites.TITLE,
262 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
263 null, null, null);
264
265 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID);
266 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
267 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
268 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
269
270 // boolean changed = false;
271
272 try {
273 while (c.moveToNext()) {
274 try {
275 if (c.getInt(itemTypeIndex) != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
276 continue;
277 }
278
279 final String intentUri = c.getString(intentIndex);
280 if (intentUri != null) {
281 final Intent shortcut = Intent.getIntent(intentUri);
282 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
283 final ComponentName name = shortcut.getComponent();
284 if (name != null) {
285 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
286 final String title = c.getString(titleIndex);
287 String label = getLabel(manager, activityInfo);
288
289 if (title == null || !title.equals(label)) {
290 final ContentValues values = new ContentValues();
291 values.put(LauncherSettings.Favorites.TITLE, label);
292
293 resolver.update(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
294 values, "_id=?",
295 new String[] { String.valueOf(c.getLong(idIndex)) });
296
297 // changed = true;
298 }
299 }
300 }
301 }
302 } catch (URISyntaxException e) {
303 // Ignore
304 } catch (PackageManager.NameNotFoundException e) {
305 // Ignore
306 }
307 }
308 } finally {
309 c.close();
310 }
311
312 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
313 }
314
315 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
316 String label = activityInfo.loadLabel(manager).toString();
317 if (label == null) {
318 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
319 if (label == null) {
320 label = activityInfo.name;
321 }
322 }
323 return label;
324 }
325
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700326 private class DesktopItemsLoader implements Runnable {
327 private volatile boolean mStopped;
328 private volatile boolean mRunning;
329
330 private final WeakReference<Launcher> mLauncher;
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800331 private final boolean mLocaleChanged;
332 private final boolean mLoadApplications;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700333
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800334 DesktopItemsLoader(Launcher launcher, boolean localeChanged, boolean loadApplications) {
335 mLoadApplications = loadApplications;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700336 mLauncher = new WeakReference<Launcher>(launcher);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800337 mLocaleChanged = localeChanged;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700338 }
339
340 void stop() {
341 mStopped = true;
342 }
343
344 boolean isRunning() {
345 return mRunning;
346 }
347
348 public void run() {
349 mRunning = true;
350
351 final Launcher launcher = mLauncher.get();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800352 final ContentResolver contentResolver = launcher.getContentResolver();
353 final PackageManager manager = launcher.getPackageManager();
354
355 if (mLocaleChanged) {
356 updateShortcutLabels(contentResolver, manager);
357 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700358
359 mDesktopItems = new ArrayList<ItemInfo>();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800360 mFolders = new HashMap<Long, FolderInfo>();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700361
362 final ArrayList<ItemInfo> desktopItems = mDesktopItems;
363
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800364 final Cursor c = contentResolver.query(
365 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700366
367 try {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800368 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID);
369 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
370 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
371 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
372 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
373 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
374 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
375 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
376 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
377 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
378 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
379 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
380 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
381 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700382
383 ApplicationInfo info;
384 String intentDescription;
385 Widget widgetInfo = null;
386 int container;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800387 long id;
388 Intent intent;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700389
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800390 final HashMap<Long, FolderInfo> folders = mFolders;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700391
392 while (!mStopped && c.moveToNext()) {
393 try {
394 int itemType = c.getInt(itemTypeIndex);
395
396 switch (itemType) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800397 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
398 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700399 intentDescription = c.getString(intentIndex);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700400 try {
401 intent = Intent.getIntent(intentDescription);
402 } catch (java.net.URISyntaxException e) {
403 continue;
404 }
405
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800406 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700407 info = getApplicationInfo(manager, intent);
408 } else {
409 info = getApplicationInfoShortcut(c, launcher, iconTypeIndex,
410 iconPackageIndex, iconResourceIndex, iconIndex);
411 }
412
413 if (info == null) {
414 info = new ApplicationInfo();
415 info.icon = manager.getDefaultActivityIcon();
416 }
417
418 if (info != null) {
419 info.title = c.getString(titleIndex);
420 info.intent = intent;
421
422 info.id = c.getLong(idIndex);
423 container = c.getInt(containerIndex);
424 info.container = container;
425 info.screen = c.getInt(screenIndex);
426 info.cellX = c.getInt(cellXIndex);
427 info.cellY = c.getInt(cellYIndex);
428
429 switch (container) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800430 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700431 desktopItems.add(info);
432 break;
433 default:
434 // Item is in a user folder
435 UserFolderInfo folderInfo =
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800436 findOrMakeUserFolder(folders, container);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700437 folderInfo.add(info);
438 break;
439 }
440 }
441 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800442 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700443
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800444 id = c.getLong(idIndex);
445 UserFolderInfo folderInfo = findOrMakeUserFolder(folders, id);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700446
447 folderInfo.title = c.getString(titleIndex);
448
449 folderInfo.id = id;
450 container = c.getInt(containerIndex);
451 folderInfo.container = container;
452 folderInfo.screen = c.getInt(screenIndex);
453 folderInfo.cellX = c.getInt(cellXIndex);
454 folderInfo.cellY = c.getInt(cellYIndex);
455
456 switch (container) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800457 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
458 desktopItems.add(folderInfo);
459 break;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700460 }
461 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800462 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
463
464 id = c.getLong(idIndex);
465 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(folders, id);
466
467 intentDescription = c.getString(intentIndex);
468 intent = null;
469 if (intentDescription != null) {
470 try {
471 intent = Intent.getIntent(intentDescription);
472 } catch (java.net.URISyntaxException e) {
473 // Ignore, a live folder might not have a base intent
474 }
475 }
476
477 liveFolderInfo.title = c.getString(titleIndex);
478 liveFolderInfo.id = id;
479 container = c.getInt(containerIndex);
480 liveFolderInfo.container = container;
481 liveFolderInfo.screen = c.getInt(screenIndex);
482 liveFolderInfo.cellX = c.getInt(cellXIndex);
483 liveFolderInfo.cellY = c.getInt(cellYIndex);
484 liveFolderInfo.uri = Uri.parse(c.getString(uriIndex));
485 liveFolderInfo.baseIntent = intent;
486 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
487
488 loadLiveFolderIcon(launcher, c, iconTypeIndex, iconPackageIndex,
489 iconResourceIndex, liveFolderInfo);
490
491 switch (container) {
492 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
493 desktopItems.add(liveFolderInfo);
494 break;
495 }
496 break;
497 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK:
498 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
499 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700500 switch (itemType) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800501 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700502 widgetInfo = Widget.makeClock();
503 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800504 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700505 widgetInfo = Widget.makeSearch();
506 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800507 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700508 widgetInfo = Widget.makePhotoFrame();
509 byte[] data = c.getBlob(iconIndex);
510 if (data != null) {
511 widgetInfo.photo =
512 BitmapFactory.decodeByteArray(data, 0, data.length);
513 }
514 break;
515 }
516
517 if (widgetInfo != null) {
518 container = c.getInt(containerIndex);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800519 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700520 Log.e(Launcher.LOG_TAG, "Widget found where container "
521 + "!= CONTAINER_DESKTOP -- ignoring!");
522 continue;
523 }
524 widgetInfo.id = c.getLong(idIndex);
525 widgetInfo.screen = c.getInt(screenIndex);
526 widgetInfo.container = container;
527 widgetInfo.cellX = c.getInt(cellXIndex);
528 widgetInfo.cellY = c.getInt(cellYIndex);
529
530 desktopItems.add(widgetInfo);
531 }
532 break;
533 }
534 } catch (Exception e) {
535 Log.w(Launcher.LOG_TAG, "Desktop items loading interrupted:", e);
536 }
537 }
538 } finally {
539 c.close();
540 }
541
542 if (!mStopped) {
543 launcher.runOnUiThread(new Runnable() {
544 public void run() {
545 launcher.onDesktopItemsLoaded();
546 }
547 });
The Android Open Source Project0e320b22009-01-09 17:51:25 -0800548 if (mLoadApplications) startApplicationsLoader(launcher);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700549 }
550
551 if (!mStopped) {
552 mDesktopItemsLoaded = true;
553 }
554 mRunning = false;
555 }
556 }
557
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800558 private static void loadLiveFolderIcon(Launcher launcher, Cursor c, int iconTypeIndex,
559 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
560
561 int iconType = c.getInt(iconTypeIndex);
562 switch (iconType) {
563 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
564 String packageName = c.getString(iconPackageIndex);
565 String resourceName = c.getString(iconResourceIndex);
566 PackageManager packageManager = launcher.getPackageManager();
567 try {
568 Resources resources = packageManager.getResourcesForApplication(packageName);
569 final int id = resources.getIdentifier(resourceName, null, null);
570 liveFolderInfo.icon = resources.getDrawable(id);
571 } catch (Exception e) {
572 liveFolderInfo.icon =
573 launcher.getResources().getDrawable(R.drawable.ic_launcher_folder);
574 }
575 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
576 liveFolderInfo.iconResource.packageName = packageName;
577 liveFolderInfo.iconResource.resourceName = resourceName;
578 break;
579 default:
580 liveFolderInfo.icon =
581 launcher.getResources().getDrawable(R.drawable.ic_launcher_folder);
582 }
583 }
584
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700585 /**
586 * Finds the user folder defined by the specified id.
587 *
588 * @param id The id of the folder to look for.
589 *
590 * @return A UserFolderInfo if the folder exists or null otherwise.
591 */
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800592 FolderInfo findFolderById(long id) {
593 return mFolders.get(id);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700594 }
595
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800596 void addFolder(FolderInfo info) {
597 mFolders.put(info.id, info);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700598 }
599
600 /**
601 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
602 * new one.
603 */
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800604 private UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700605 // See if a placeholder was created for us already
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800606 FolderInfo folderInfo = folders.get(id);
607 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700608 // No placeholder -- create a new instance
609 folderInfo = new UserFolderInfo();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800610 folders.put(id, folderInfo);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700611 }
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800612 return (UserFolderInfo) folderInfo;
613 }
614
615 /**
616 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
617 * new one.
618 */
619 private LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
620 // See if a placeholder was created for us already
621 FolderInfo folderInfo = folders.get(id);
622 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
623 // No placeholder -- create a new instance
624 folderInfo = new LiveFolderInfo();
625 folders.put(id, folderInfo);
626 }
627 return (LiveFolderInfo) folderInfo;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700628 }
629
630 /**
631 * Remove the callback for the cached drawables or we leak the previous
632 * Home screen on orientation change.
633 */
634 void unbind() {
635 mApplicationsAdapter = null;
636 unbindAppDrawables(mApplications);
637 unbindDrawables(mDesktopItems);
638 }
639
640 /**
641 * Remove the callback for the cached drawables or we leak the previous
642 * Home screen on orientation change.
643 */
644 private void unbindDrawables(ArrayList<ItemInfo> desktopItems) {
645 if (desktopItems != null) {
646 final int count = desktopItems.size();
647 for (int i = 0; i < count; i++) {
648 ItemInfo item = desktopItems.get(i);
649 switch (item.itemType) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800650 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
651 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700652 ((ApplicationInfo)item).icon.setCallback(null);
653 }
654 }
655 }
656 }
657
658 /**
659 * Remove the callback for the cached drawables or we leak the previous
660 * Home screen on orientation change.
661 */
662 private void unbindAppDrawables(ArrayList<ApplicationInfo> applications) {
663 if (applications != null) {
664 final int count = applications.size();
665 for (int i = 0; i < count; i++) {
666 applications.get(i).icon.setCallback(null);
667 }
668 }
669 }
670
671 /**
672 * @return The current list of applications
673 */
674 public ArrayList<ApplicationInfo> getApplications() {
675 return mApplications;
676 }
677
678 /**
679 * @return The current list of applications
680 */
681 public ApplicationsAdapter getApplicationsAdapter() {
682 return mApplicationsAdapter;
683 }
684
685 /**
686 * @return The current list of desktop items
687 */
688 public ArrayList<ItemInfo> getDesktopItems() {
689 return mDesktopItems;
690 }
691
692 /**
693 * Add an item to the desktop
694 * @param info
695 */
696 public void addDesktopItem(ItemInfo info) {
697 // TODO: write to DB; also check that folder has been added to folders list
698 mDesktopItems.add(info);
699 }
700
701 /**
702 * Remove an item from the desktop
703 * @param info
704 */
705 public void removeDesktopItem(ItemInfo info) {
706 // TODO: write to DB; figure out if we should remove folder from folders list
707 mDesktopItems.remove(info);
708 }
709
710 /**
711 * Make an ApplicationInfo object for an application
712 */
713 private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent) {
714 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
715
716 if (resolveInfo == null) {
717 return null;
718 }
719
720 final ApplicationInfo info = new ApplicationInfo();
721 final ActivityInfo activityInfo = resolveInfo.activityInfo;
722 info.icon = activityInfo.loadIcon(manager);
723 if (info.title == null || info.title.length() == 0) {
724 info.title = activityInfo.loadLabel(manager);
725 }
726 if (info.title == null) {
727 info.title = "";
728 }
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800729 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700730 return info;
731 }
732
733 /**
734 * Make an ApplicationInfo object for a sortcut
735 */
736 private ApplicationInfo getApplicationInfoShortcut(Cursor c, Launcher launcher,
737 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
738
739 final ApplicationInfo info = new ApplicationInfo();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800740 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700741
742 int iconType = c.getInt(iconTypeIndex);
743 switch (iconType) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800744 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700745 String packageName = c.getString(iconPackageIndex);
746 String resourceName = c.getString(iconResourceIndex);
747 PackageManager packageManager = launcher.getPackageManager();
748 try {
749 Resources resources = packageManager.getResourcesForApplication(packageName);
750 final int id = resources.getIdentifier(resourceName, null, null);
751 info.icon = resources.getDrawable(id);
752 } catch (Exception e) {
753 info.icon = packageManager.getDefaultActivityIcon();
754 }
755 info.iconResource = new Intent.ShortcutIconResource();
756 info.iconResource.packageName = packageName;
757 info.iconResource.resourceName = resourceName;
758 info.customIcon = false;
759 break;
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800760 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700761 byte[] data = c.getBlob(iconIndex);
762 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800763 info.icon = new FastBitmapDrawable(
764 Utilities.createBitmapThumbnail(bitmap, launcher));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700765 info.filtered = true;
766 info.customIcon = true;
767 break;
768 default:
769 info.icon = launcher.getPackageManager().getDefaultActivityIcon();
770 info.customIcon = false;
771 break;
772 }
773 return info;
774 }
775
776 /**
777 * Remove an item from the in-memory represention of a user folder. Does not change the DB.
778 */
779 void removeUserFolderItem(UserFolderInfo folder, ItemInfo info) {
780 //noinspection SuspiciousMethodCalls
781 folder.contents.remove(info);
782 }
783
784 /**
785 * Removes a UserFolder from the in-memory list of folders. Does not change the DB.
786 * @param userFolderInfo
787 */
788 void removeUserFolder(UserFolderInfo userFolderInfo) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800789 mFolders.remove(userFolderInfo.id);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700790 }
791
792 /**
793 * Adds an item to the DB if it was not created previously, or move it to a new
794 * <container, screen, cellX, cellY>
795 */
796 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
797 int screen, int cellX, int cellY) {
798 if (item.container == ItemInfo.NO_ID) {
799 // From all apps
800 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
801 } else {
802 // From somewhere else
803 moveItemInDatabase(context, item, container, screen, cellX, cellY);
804 }
805 }
806
807 /**
808 * Move an item in the DB to a new <container, screen, cellX, cellY>
809 */
810 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
811 int cellX, int cellY) {
812 item.container = container;
813 item.screen = screen;
814 item.cellX = cellX;
815 item.cellY = cellY;
816
817 final ContentValues values = new ContentValues();
818 final ContentResolver cr = context.getContentResolver();
819
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800820 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
821 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
822 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
823 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700824
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800825 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700826 }
827
828 /**
829 * Returns true if the shortcuts already exists in the database.
830 * we identify a shortcut by its title and intent.
831 */
832 static boolean shortcutExists(Context context, String title, Intent intent) {
833 final ContentResolver cr = context.getContentResolver();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800834 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700835 new String[] { "title", "intent" }, "title=? and intent=?",
836 new String[] { title, intent.toURI() }, null);
837 boolean result = false;
838 try {
839 result = c.moveToFirst();
840 } finally {
841 c.close();
842 }
843 return result;
844 }
845
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800846 FolderInfo getFolderById(Context context, long id) {
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700847 final ContentResolver cr = context.getContentResolver();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800848 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
849 "_id=? and itemType=? or itemType=?",
850 new String[] { String.valueOf(id),
851 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
852 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700853
854 try {
855 if (c.moveToFirst()) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800856 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
857 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
858 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
859 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
860 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
861 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700862
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800863 FolderInfo folderInfo = null;
864 switch (c.getInt(itemTypeIndex)) {
865 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
866 folderInfo = findOrMakeUserFolder(mFolders, id);
867 break;
868 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
869 folderInfo = findOrMakeLiveFolder(mFolders, id);
870 break;
871 }
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700872
873 folderInfo.title = c.getString(titleIndex);
874 folderInfo.id = id;
875 folderInfo.container = c.getInt(containerIndex);
876 folderInfo.screen = c.getInt(screenIndex);
877 folderInfo.cellX = c.getInt(cellXIndex);
878 folderInfo.cellY = c.getInt(cellYIndex);
879
880 return folderInfo;
881 }
882 } finally {
883 c.close();
884 }
885
886 return null;
887 }
888
889 static Widget getPhotoFrameInfo(Context context, int screen, int cellX, int cellY) {
890 final ContentResolver cr = context.getContentResolver();
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800891 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700892 null, "screen=? and cellX=? and cellY=? and itemType=?",
893 new String[] { String.valueOf(screen), String.valueOf(cellX), String.valueOf(cellY),
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800894 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) }, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700895
896 try {
897 if (c.moveToFirst()) {
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800898 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID);
899 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
900 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
901 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
902 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700903
904 Widget widgetInfo = Widget.makePhotoFrame();
905 widgetInfo.id = c.getLong(idIndex);
906 widgetInfo.screen = c.getInt(screenIndex);
907 widgetInfo.container = c.getInt(containerIndex);
908 widgetInfo.cellX = c.getInt(cellXIndex);
909 widgetInfo.cellY = c.getInt(cellYIndex);
910
911 return widgetInfo;
912 }
913 } finally {
914 c.close();
915 }
916
917 return null;
918 }
919
920 /**
921 * Add an item to the database in a specified container. Sets the container, screen, cellX and
922 * cellY fields of the item. Also assigns an ID to the item.
923 */
924 static void addItemToDatabase(Context context, ItemInfo item, long container,
925 int screen, int cellX, int cellY, boolean notify) {
926 item.container = container;
927 item.screen = screen;
928 item.cellX = cellX;
929 item.cellY = cellY;
930
931 final ContentValues values = new ContentValues();
932 final ContentResolver cr = context.getContentResolver();
933
934 item.onAddToDatabase(values);
935
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800936 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
937 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700938
939 if (result != null) {
940 item.id = Integer.parseInt(result.getPathSegments().get(1));
941 }
942 }
943
944 /**
945 * Update an item to the database in a specified container.
946 */
947 static void updateItemInDatabase(Context context, ItemInfo item) {
948 final ContentValues values = new ContentValues();
949 final ContentResolver cr = context.getContentResolver();
950
951 item.onAddToDatabase(values);
952
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800953 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700954 }
955
956 /**
957 * Removes the specified item from the database
958 * @param context
959 * @param item
960 */
961 static void deleteItemFromDatabase(Context context, ItemInfo item) {
962 final ContentResolver cr = context.getContentResolver();
963
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800964 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700965 }
966
967
968 /**
969 * Remove the contents of the specified folder from the database
970 */
971 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
972 final ContentResolver cr = context.getContentResolver();
973
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800974 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
975 cr.delete(LauncherSettings.Favorites.CONTENT_URI, LauncherSettings.Favorites.CONTAINER + "=" + info.id,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700976 null);
977 }
978}