Launcher.java revision 742966ba0eb3106202b57709f3cc9c2ecfb2a41d
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.ISearchManager;
23import android.app.SearchManager;
24import android.app.StatusBarManager;
25import android.app.WallpaperInfo;
26import android.app.WallpaperManager;
27import android.content.ActivityNotFoundException;
28import android.content.ComponentName;
29import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
32import android.content.Intent.ShortcutIconResource;
33import android.content.pm.ActivityInfo;
34import android.content.pm.LabeledIntent;
35import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.res.Configuration;
38import android.content.res.Resources;
39import android.graphics.Bitmap;
40import android.graphics.drawable.Drawable;
41import android.graphics.drawable.TransitionDrawable;
42import android.os.Bundle;
43import android.os.Looper;
44import android.os.Message;
45import android.os.MessageQueue;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
49import android.provider.LiveFolders;
50import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
54import static android.util.Log.*;
55import android.util.Log;
56import android.view.Display;
57import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
63import android.view.WindowManager;
64import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
66import android.widget.EditText;
67import android.widget.TextView;
68import android.widget.Toast;
69import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
71
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
74import java.util.HashMap;
75import java.util.LinkedList;
76import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
80
81/**
82 * Default launcher application.
83 */
84public final class Launcher extends Activity
85        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
86    static final String LOG_TAG = "Launcher";
87    static final String TAG = LOG_TAG;
88    static final boolean LOGD = false;
89
90    static final boolean PROFILE_STARTUP = false;
91    static final boolean PROFILE_ROTATE = false;
92    static final boolean DEBUG_USER_INTERFACE = false;
93
94    private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96    private static final int MENU_GROUP_ADD = 1;
97    private static final int MENU_ADD = Menu.FIRST + 1;
98    private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99    private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100    private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
101    private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
102
103    private static final int REQUEST_CREATE_SHORTCUT = 1;
104    private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
105    private static final int REQUEST_CREATE_APPWIDGET = 5;
106    private static final int REQUEST_PICK_APPLICATION = 6;
107    private static final int REQUEST_PICK_SHORTCUT = 7;
108    private static final int REQUEST_PICK_LIVE_FOLDER = 8;
109    private static final int REQUEST_PICK_APPWIDGET = 9;
110
111    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
112
113    static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
114    static final String SEARCH_WIDGET = "search_widget";
115
116    static final int SCREEN_COUNT = 3;
117    static final int DEFAULT_SCREN = 1;
118    static final int NUMBER_CELLS_X = 4;
119    static final int NUMBER_CELLS_Y = 4;
120
121    static final int DIALOG_CREATE_SHORTCUT = 1;
122    static final int DIALOG_RENAME_FOLDER = 2;
123
124    private static final String PREFERENCES = "launcher.preferences";
125
126    // Type: int
127    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
128    // Type: boolean
129    private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
130    // Type: long
131    private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
132    // Type: int
133    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
134    // Type: int
135    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
136    // Type: int
137    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
138    // Type: int
139    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
140    // Type: int
141    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
142    // Type: int
143    private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
144    // Type: int
145    private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
146    // Type: int[]
147    private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
148    // Type: boolean
149    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150    // Type: long
151    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
153    static final int APPWIDGET_HOST_ID = 1024;
154
155    private static final Object sLock = new Object();
156    private static int sScreen = DEFAULT_SCREN;
157
158    private LayoutInflater mInflater;
159
160    private DragController mDragController;
161    private Workspace mWorkspace;
162
163    private AppWidgetManager mAppWidgetManager;
164    private LauncherAppWidgetHost mAppWidgetHost;
165
166    private CellLayout.CellInfo mAddItemCellInfo;
167    private CellLayout.CellInfo mMenuAddInfo;
168    private final int[] mCellCoordinates = new int[2];
169    private FolderInfo mFolderInfo;
170
171    private DeleteZone mDeleteZone;
172    private HandleView mHandleView;
173    private AllAppsView mAllAppsGrid;
174
175    private Bundle mSavedState;
176
177    private SpannableStringBuilder mDefaultKeySsb = null;
178
179    private boolean mIsNewIntent;
180
181    private boolean mWorkspaceLoading = true;
182
183    private boolean mRestoring;
184    private boolean mWaitingForResult;
185    private boolean mLocaleChanged;
186
187    private boolean mHomeDown;
188    private boolean mBackDown;
189
190    private Bundle mSavedInstanceState;
191
192    private LauncherModel mModel;
193
194    private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
195    private static HashMap<Long, FolderInfo> mFolders = new HashMap();
196    private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
197
198    @Override
199    protected void onCreate(Bundle savedInstanceState) {
200        super.onCreate(savedInstanceState);
201
202        mModel = ((LauncherApplication)getApplication()).setLauncher(this);
203        mInflater = getLayoutInflater();
204
205        mAppWidgetManager = AppWidgetManager.getInstance(this);
206
207        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
208        mAppWidgetHost.startListening();
209
210        if (PROFILE_STARTUP) {
211            android.os.Debug.startMethodTracing("/sdcard/launcher");
212        }
213
214        checkForLocaleChange();
215        setWallpaperDimension();
216
217        setContentView(R.layout.launcher);
218        setupViews();
219
220        lockAllApps();
221
222        mSavedState = savedInstanceState;
223        restoreState(mSavedState);
224
225        if (PROFILE_STARTUP) {
226            android.os.Debug.stopMethodTracing();
227        }
228
229        if (!mRestoring) {
230            mModel.startLoader(this, true);
231        }
232
233        // For handling default keys
234        mDefaultKeySsb = new SpannableStringBuilder();
235        Selection.setSelection(mDefaultKeySsb, 0);
236    }
237
238    private void checkForLocaleChange() {
239        final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
240        readConfiguration(this, localeConfiguration);
241
242        final Configuration configuration = getResources().getConfiguration();
243
244        final String previousLocale = localeConfiguration.locale;
245        final String locale = configuration.locale.toString();
246
247        final int previousMcc = localeConfiguration.mcc;
248        final int mcc = configuration.mcc;
249
250        final int previousMnc = localeConfiguration.mnc;
251        final int mnc = configuration.mnc;
252
253        mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
254
255        if (mLocaleChanged) {
256            localeConfiguration.locale = locale;
257            localeConfiguration.mcc = mcc;
258            localeConfiguration.mnc = mnc;
259
260            writeConfiguration(this, localeConfiguration);
261            AppInfoCache.flush();
262        }
263    }
264
265    private static class LocaleConfiguration {
266        public String locale;
267        public int mcc = -1;
268        public int mnc = -1;
269    }
270
271    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
272        DataInputStream in = null;
273        try {
274            in = new DataInputStream(context.openFileInput(PREFERENCES));
275            configuration.locale = in.readUTF();
276            configuration.mcc = in.readInt();
277            configuration.mnc = in.readInt();
278        } catch (FileNotFoundException e) {
279            // Ignore
280        } catch (IOException e) {
281            // Ignore
282        } finally {
283            if (in != null) {
284                try {
285                    in.close();
286                } catch (IOException e) {
287                    // Ignore
288                }
289            }
290        }
291    }
292
293    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
294        DataOutputStream out = null;
295        try {
296            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
297            out.writeUTF(configuration.locale);
298            out.writeInt(configuration.mcc);
299            out.writeInt(configuration.mnc);
300            out.flush();
301        } catch (FileNotFoundException e) {
302            // Ignore
303        } catch (IOException e) {
304            //noinspection ResultOfMethodCallIgnored
305            context.getFileStreamPath(PREFERENCES).delete();
306        } finally {
307            if (out != null) {
308                try {
309                    out.close();
310                } catch (IOException e) {
311                    // Ignore
312                }
313            }
314        }
315    }
316
317    static int getScreen() {
318        synchronized (sLock) {
319            return sScreen;
320        }
321    }
322
323    static void setScreen(int screen) {
324        synchronized (sLock) {
325            sScreen = screen;
326        }
327    }
328
329    private void setWallpaperDimension() {
330        WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
331
332        Display display = getWindowManager().getDefaultDisplay();
333        boolean isPortrait = display.getWidth() < display.getHeight();
334
335        final int width = isPortrait ? display.getWidth() : display.getHeight();
336        final int height = isPortrait ? display.getHeight() : display.getWidth();
337        wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
338    }
339
340    @Override
341    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
342        mWaitingForResult = false;
343
344        // The pattern used here is that a user PICKs a specific application,
345        // which, depending on the target, might need to CREATE the actual target.
346
347        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
348        // launch over to the Music app to actually CREATE_SHORTCUT.
349
350        if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
351            switch (requestCode) {
352                case REQUEST_PICK_APPLICATION:
353                    completeAddApplication(this, data, mAddItemCellInfo);
354                    break;
355                case REQUEST_PICK_SHORTCUT:
356                    processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
357                    break;
358                case REQUEST_CREATE_SHORTCUT:
359                    completeAddShortcut(data, mAddItemCellInfo);
360                    break;
361                case REQUEST_PICK_LIVE_FOLDER:
362                    addLiveFolder(data);
363                    break;
364                case REQUEST_CREATE_LIVE_FOLDER:
365                    completeAddLiveFolder(data, mAddItemCellInfo);
366                    break;
367                case REQUEST_PICK_APPWIDGET:
368                    addAppWidget(data);
369                    break;
370                case REQUEST_CREATE_APPWIDGET:
371                    completeAddAppWidget(data, mAddItemCellInfo);
372                    break;
373            }
374        } else if (requestCode == REQUEST_PICK_APPWIDGET &&
375                resultCode == RESULT_CANCELED && data != null) {
376            // Clean up the appWidgetId if we canceled
377            int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
378            if (appWidgetId != -1) {
379                mAppWidgetHost.deleteAppWidgetId(appWidgetId);
380            }
381        }
382    }
383
384    @Override
385    protected void onResume() {
386        super.onResume();
387
388        if (mRestoring) {
389            mWorkspaceLoading = true;
390            mModel.startLoader(this, true);
391            mRestoring = false;
392        }
393
394        // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
395        // onNewIntent), then close the search dialog if needed, because it probably
396        // came from the user pressing 'home' (rather than, for example, pressing 'back').
397        if (mIsNewIntent) {
398            // Post to a handler so that this happens after the search dialog tries to open
399            // itself again.
400            mWorkspace.post(new Runnable() {
401                public void run() {
402                    ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
403                            ServiceManager.getService(Context.SEARCH_SERVICE));
404                    try {
405                        searchManagerService.stopSearch();
406                    } catch (RemoteException e) {
407                        e(LOG_TAG, "error stopping search", e);
408                    }
409                }
410            });
411        }
412
413        mIsNewIntent = false;
414    }
415
416    @Override
417    protected void onPause() {
418        super.onPause();
419        closeAllApps(false);
420    }
421
422    @Override
423    public Object onRetainNonConfigurationInstance() {
424        // Flag the loader to stop early before switching
425        mModel.stopLoader();
426
427        if (PROFILE_ROTATE) {
428            android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
429        }
430        return null;
431    }
432
433    private boolean acceptFilter() {
434        final InputMethodManager inputManager = (InputMethodManager)
435                getSystemService(Context.INPUT_METHOD_SERVICE);
436        return !inputManager.isFullscreenMode();
437    }
438
439    @Override
440    public boolean onKeyDown(int keyCode, KeyEvent event) {
441        boolean handled = super.onKeyDown(keyCode, event);
442        if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
443            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
444                    keyCode, event);
445            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
446                // something usable has been typed - start a search
447                // the typed text will be retrieved and cleared by
448                // showSearchDialog()
449                // If there are multiple keystrokes before the search dialog takes focus,
450                // onSearchRequested() will be called for every keystroke,
451                // but it is idempotent, so it's fine.
452                return onSearchRequested();
453            }
454        }
455
456        return handled;
457    }
458
459    private String getTypedText() {
460        return mDefaultKeySsb.toString();
461    }
462
463    private void clearTypedText() {
464        mDefaultKeySsb.clear();
465        mDefaultKeySsb.clearSpans();
466        Selection.setSelection(mDefaultKeySsb, 0);
467    }
468
469    /**
470     * Restores the previous state, if it exists.
471     *
472     * @param savedState The previous state.
473     */
474    private void restoreState(Bundle savedState) {
475        if (savedState == null) {
476            return;
477        }
478
479        final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
480        if (currentScreen > -1) {
481            mWorkspace.setCurrentScreen(currentScreen);
482        }
483
484        final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
485        if (addScreen > -1) {
486            mAddItemCellInfo = new CellLayout.CellInfo();
487            final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
488            addItemCellInfo.valid = true;
489            addItemCellInfo.screen = addScreen;
490            addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
491            addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
492            addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
493            addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
494            addItemCellInfo.findVacantCellsFromOccupied(
495                    savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
496                    savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
497                    savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
498            mRestoring = true;
499        }
500
501        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
502        if (renameFolder) {
503            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
504            mFolderInfo = mModel.getFolderById(this, mFolders, id);
505            mRestoring = true;
506        }
507    }
508
509    /**
510     * Finds all the views we need and configure them properly.
511     */
512    private void setupViews() {
513        mDragController = new DragController(this);
514        DragController dragController = mDragController;
515
516        DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
517        dragLayer.setDragController(dragController);
518
519        mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
520        mAllAppsGrid.setLauncher(this);
521        mAllAppsGrid.setDragController(dragController);
522        mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
523
524        mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
525        final Workspace workspace = mWorkspace;
526
527        DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
528        mDeleteZone = deleteZone;
529
530        mHandleView = (HandleView) findViewById(R.id.all_apps_button);
531        mHandleView.setLauncher(this);
532        mHandleView.setOnClickListener(this);
533        /* TODO
534        TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
535        handleIocon.setCrossFadeEnabled(true);
536        */
537
538        workspace.setOnLongClickListener(this);
539        workspace.setDragController(dragController);
540        workspace.setLauncher(this);
541
542        deleteZone.setLauncher(this);
543        deleteZone.setDragController(dragController);
544        deleteZone.setHandle(mHandleView);
545
546        dragController.setDragScoller(workspace);
547        dragController.setDragListener(deleteZone);
548        dragController.setScrollView(dragLayer);
549
550        // The order here is bottom to top.
551        dragController.addDropTarget(workspace);
552        dragController.addDropTarget(deleteZone);
553    }
554
555    /**
556     * Creates a view representing a shortcut.
557     *
558     * @param info The data structure describing the shortcut.
559     *
560     * @return A View inflated from R.layout.application.
561     */
562    View createShortcut(ApplicationInfo info) {
563        return createShortcut(R.layout.application,
564                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
565    }
566
567    /**
568     * Creates a view representing a shortcut inflated from the specified resource.
569     *
570     * @param layoutResId The id of the XML layout used to create the shortcut.
571     * @param parent The group the shortcut belongs to.
572     * @param info The data structure describing the shortcut.
573     *
574     * @return A View inflated from layoutResId.
575     */
576    View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
577        TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
578
579        if (info.icon == null) {
580            info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
581        }
582        if (!info.filtered) {
583            info.icon = Utilities.createIconThumbnail(info.icon, this);
584            info.filtered = true;
585        }
586
587        favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
588        favorite.setText(info.title);
589        favorite.setTag(info);
590        favorite.setOnClickListener(this);
591
592        return favorite;
593    }
594
595    /**
596     * Add an application shortcut to the workspace.
597     *
598     * @param data The intent describing the application.
599     * @param cellInfo The position on screen where to create the shortcut.
600     */
601    void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
602        cellInfo.screen = mWorkspace.getCurrentScreen();
603        if (!findSingleSlot(cellInfo)) return;
604
605        final ApplicationInfo info = infoFromApplicationIntent(context, data);
606        if (info != null) {
607            mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
608        }
609    }
610
611    private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
612        ComponentName component = data.getComponent();
613        PackageManager packageManager = context.getPackageManager();
614        ActivityInfo activityInfo = null;
615        try {
616            activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
617        } catch (NameNotFoundException e) {
618            e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
619        }
620
621        if (activityInfo != null) {
622            ApplicationInfo itemInfo = new ApplicationInfo();
623
624            itemInfo.title = activityInfo.loadLabel(packageManager);
625            if (itemInfo.title == null) {
626                itemInfo.title = activityInfo.name;
627            }
628
629            itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
630                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
631            itemInfo.icon = activityInfo.loadIcon(packageManager);
632            itemInfo.container = ItemInfo.NO_ID;
633
634            return itemInfo;
635        }
636
637        return null;
638    }
639
640    /**
641     * Add a shortcut to the workspace.
642     *
643     * @param data The intent describing the shortcut.
644     * @param cellInfo The position on screen where to create the shortcut.
645     */
646    private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
647        cellInfo.screen = mWorkspace.getCurrentScreen();
648        if (!findSingleSlot(cellInfo)) return;
649
650        final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
651
652        if (!mRestoring) {
653            final View view = createShortcut(info);
654            mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
655                    isWorkspaceLocked());
656        }
657    }
658
659
660    /**
661     * Add a widget to the workspace.
662     *
663     * @param data The intent describing the appWidgetId.
664     * @param cellInfo The position on screen where to create the widget.
665     */
666    private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
667        Bundle extras = data.getExtras();
668        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
669
670        d(LOG_TAG, "dumping extras content="+extras.toString());
671
672        AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
673
674        // Calculate the grid spans needed to fit this widget
675        CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
676        int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
677
678        // Try finding open space on Launcher screen
679        final int[] xy = mCellCoordinates;
680        if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
681
682        // Build Launcher-specific widget info and save to database
683        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
684        launcherInfo.spanX = spans[0];
685        launcherInfo.spanY = spans[1];
686
687        LauncherModel.addItemToDatabase(this, launcherInfo,
688                LauncherSettings.Favorites.CONTAINER_DESKTOP,
689                mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
690
691        if (!mRestoring) {
692            mDesktopItems.add(launcherInfo);
693
694            // Perform actual inflation because we're live
695            launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
696
697            launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
698            launcherInfo.hostView.setTag(launcherInfo);
699
700            mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
701                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
702        }
703    }
704
705    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
706        mDesktopItems.remove(launcherInfo);
707        launcherInfo.hostView = null;
708    }
709
710    public LauncherAppWidgetHost getAppWidgetHost() {
711        return mAppWidgetHost;
712    }
713
714    static ApplicationInfo addShortcut(Context context, Intent data,
715            CellLayout.CellInfo cellInfo, boolean notify) {
716
717        final ApplicationInfo info = infoFromShortcutIntent(context, data);
718        LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
719                cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
720
721        return info;
722    }
723
724    private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
725        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
726        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
727        Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
728
729        Drawable icon = null;
730        boolean filtered = false;
731        boolean customIcon = false;
732        ShortcutIconResource iconResource = null;
733
734        if (bitmap != null) {
735            icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
736            filtered = true;
737            customIcon = true;
738        } else {
739            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
740            if (extra != null && extra instanceof ShortcutIconResource) {
741                try {
742                    iconResource = (ShortcutIconResource) extra;
743                    final PackageManager packageManager = context.getPackageManager();
744                    Resources resources = packageManager.getResourcesForApplication(
745                            iconResource.packageName);
746                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
747                    icon = resources.getDrawable(id);
748                } catch (Exception e) {
749                    w(LOG_TAG, "Could not load shortcut icon: " + extra);
750                }
751            }
752        }
753
754        if (icon == null) {
755            icon = context.getPackageManager().getDefaultActivityIcon();
756        }
757
758        final ApplicationInfo info = new ApplicationInfo();
759        info.icon = icon;
760        info.filtered = filtered;
761        info.title = name;
762        info.intent = intent;
763        info.customIcon = customIcon;
764        info.iconResource = iconResource;
765
766        return info;
767    }
768
769    @Override
770    protected void onNewIntent(Intent intent) {
771        super.onNewIntent(intent);
772
773        // Close the menu
774        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
775            getWindow().closeAllPanels();
776
777            // Whatever we were doing is hereby canceled.
778            mWaitingForResult = false;
779
780            // Set this flag so that onResume knows to close the search dialog if it's open,
781            // because this was a new intent (thus a press of 'home' or some such) rather than
782            // for example onResume being called when the user pressed the 'back' button.
783            mIsNewIntent = true;
784
785            try {
786                dismissDialog(DIALOG_CREATE_SHORTCUT);
787                // Unlock the workspace if the dialog was showing
788            } catch (Exception e) {
789                // An exception is thrown if the dialog is not visible, which is fine
790            }
791
792            try {
793                dismissDialog(DIALOG_RENAME_FOLDER);
794                // Unlock the workspace if the dialog was showing
795            } catch (Exception e) {
796                // An exception is thrown if the dialog is not visible, which is fine
797            }
798
799            if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
800                    Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
801
802                if (!mWorkspace.isDefaultScreenShowing()) {
803                    mWorkspace.moveToDefaultScreen();
804                }
805
806                closeAllApps(true);
807
808                final View v = getWindow().peekDecorView();
809                if (v != null && v.getWindowToken() != null) {
810                    InputMethodManager imm = (InputMethodManager)getSystemService(
811                            INPUT_METHOD_SERVICE);
812                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
813                }
814            } else {
815                closeAllApps(false);
816            }
817        }
818    }
819
820    @Override
821    protected void onRestoreInstanceState(Bundle savedInstanceState) {
822        // Do not call super here
823        mSavedInstanceState = savedInstanceState;
824    }
825
826    @Override
827    protected void onSaveInstanceState(Bundle outState) {
828        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
829
830        final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
831        if (folders.size() > 0) {
832            final int count = folders.size();
833            long[] ids = new long[count];
834            for (int i = 0; i < count; i++) {
835                final FolderInfo info = folders.get(i).getInfo();
836                ids[i] = info.id;
837            }
838            outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
839        } else {
840            super.onSaveInstanceState(outState);
841        }
842
843        final boolean isConfigurationChange = getChangingConfigurations() != 0;
844
845        // When the drawer is opened and we are saving the state because of a
846        // configuration change
847        // TODO should not do this if the drawer is currently closing.
848        if (isAllAppsVisible() && isConfigurationChange) {
849            outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
850        }
851
852        if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
853            final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
854            final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
855
856            outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
857            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
858            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
859            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
860            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
861            outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
862            outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
863            outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
864                   layout.getOccupiedCells());
865        }
866
867        if (mFolderInfo != null && mWaitingForResult) {
868            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
869            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
870        }
871    }
872
873    @Override
874    public void onDestroy() {
875        super.onDestroy();
876
877        try {
878            mAppWidgetHost.stopListening();
879        } catch (NullPointerException ex) {
880            w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
881        }
882
883        TextKeyListener.getInstance().release();
884
885        mModel.stopLoader();
886
887        unbindDesktopItems();
888        AppInfoCache.unbindDrawables();
889    }
890
891    @Override
892    public void startActivityForResult(Intent intent, int requestCode) {
893        if (requestCode >= 0) mWaitingForResult = true;
894        super.startActivityForResult(intent, requestCode);
895    }
896
897    @Override
898    public void startSearch(String initialQuery, boolean selectInitialQuery,
899            Bundle appSearchData, boolean globalSearch) {
900
901        closeAllApps(true);
902
903        // Slide the search widget to the top, if it's on the current screen,
904        // otherwise show the search dialog immediately.
905        Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
906        if (searchWidget == null) {
907            showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
908        } else {
909            searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
910            // show the currently typed text in the search widget while sliding
911            searchWidget.setQuery(getTypedText());
912        }
913    }
914
915    /**
916     * Show the search dialog immediately, without changing the search widget.
917     *
918     * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
919     */
920    void showSearchDialog(String initialQuery, boolean selectInitialQuery,
921            Bundle appSearchData, boolean globalSearch) {
922
923        if (initialQuery == null) {
924            // Use any text typed in the launcher as the initial query
925            initialQuery = getTypedText();
926            clearTypedText();
927        }
928        if (appSearchData == null) {
929            appSearchData = new Bundle();
930            appSearchData.putString(SearchManager.SOURCE, "launcher-search");
931        }
932
933        final SearchManager searchManager =
934                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
935
936        final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
937        if (searchWidget != null) {
938            // This gets called when the user leaves the search dialog to go back to
939            // the Launcher.
940            searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
941                public void onCancel() {
942                    searchManager.setOnCancelListener(null);
943                    stopSearch();
944                }
945            });
946        }
947
948        searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
949            appSearchData, globalSearch);
950    }
951
952    /**
953     * Cancel search dialog if it is open.
954     */
955    void stopSearch() {
956        // Close search dialog
957        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
958        searchManager.stopSearch();
959        // Restore search widget to its normal position
960        Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
961        if (searchWidget != null) {
962            searchWidget.stopSearch(false);
963        }
964    }
965
966    @Override
967    public boolean onCreateOptionsMenu(Menu menu) {
968        if (isWorkspaceLocked()) {
969            return false;
970        }
971
972        super.onCreateOptionsMenu(menu);
973        menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
974                .setIcon(android.R.drawable.ic_menu_add)
975                .setAlphabeticShortcut('A');
976        menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
977                 .setIcon(android.R.drawable.ic_menu_gallery)
978                 .setAlphabeticShortcut('W');
979        menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
980                .setIcon(android.R.drawable.ic_search_category_default)
981                .setAlphabeticShortcut(SearchManager.MENU_KEY);
982        menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
983                .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
984                .setAlphabeticShortcut('N');
985
986        final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
987        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
988                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
989
990        menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
991                .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
992                .setIntent(settings);
993
994        return true;
995    }
996
997    @Override
998    public boolean onPrepareOptionsMenu(Menu menu) {
999        super.onPrepareOptionsMenu(menu);
1000
1001        mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1002        menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1003
1004        return true;
1005    }
1006
1007    @Override
1008    public boolean onOptionsItemSelected(MenuItem item) {
1009        switch (item.getItemId()) {
1010            case MENU_ADD:
1011                addItems();
1012                return true;
1013            case MENU_WALLPAPER_SETTINGS:
1014                startWallpaper();
1015                return true;
1016            case MENU_SEARCH:
1017                onSearchRequested();
1018                return true;
1019            case MENU_NOTIFICATIONS:
1020                showNotifications();
1021                return true;
1022        }
1023
1024        return super.onOptionsItemSelected(item);
1025    }
1026
1027    /**
1028     * Indicates that we want global search for this activity by setting the globalSearch
1029     * argument for {@link #startSearch} to true.
1030     */
1031
1032    @Override
1033    public boolean onSearchRequested() {
1034        startSearch(null, false, null, true);
1035        return true;
1036    }
1037
1038    public boolean isWorkspaceLocked() {
1039        return mWorkspaceLoading || mWaitingForResult;
1040    }
1041
1042    private void addItems() {
1043        showAddDialog(mMenuAddInfo);
1044    }
1045
1046    void addAppWidget(Intent data) {
1047        // TODO: catch bad widget exception when sent
1048        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
1049
1050        String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1051        if (SEARCH_WIDGET.equals(customWidget)) {
1052            // We don't need this any more, since this isn't a real app widget.
1053            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1054            // add the search widget
1055            addSearch();
1056        } else {
1057            AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1058
1059            if (appWidget.configure != null) {
1060                // Launch over to configure widget, if needed
1061                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1062                intent.setComponent(appWidget.configure);
1063                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1064
1065                startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1066            } else {
1067                // Otherwise just add it
1068                onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1069            }
1070        }
1071    }
1072
1073    void addSearch() {
1074        final Widget info = Widget.makeSearch();
1075        final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1076
1077        final int[] xy = mCellCoordinates;
1078        final int spanX = info.spanX;
1079        final int spanY = info.spanY;
1080
1081        if (!findSlot(cellInfo, xy, spanX, spanY)) return;
1082
1083        LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1084        mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
1085
1086        final View view = mInflater.inflate(info.layoutResource, null);
1087        view.setTag(info);
1088        Search search = (Search) view.findViewById(R.id.widget_search);
1089        search.setLauncher(this);
1090
1091        mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1092    }
1093
1094    void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
1095        // Handle case where user selected "Applications"
1096        String applicationName = getResources().getString(R.string.group_applications);
1097        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1098
1099        if (applicationName != null && applicationName.equals(shortcutName)) {
1100            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1101            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1102
1103            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1104            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
1105            startActivityForResult(pickIntent, requestCodeApplication);
1106        } else {
1107            startActivityForResult(intent, requestCodeShortcut);
1108        }
1109    }
1110
1111    void addLiveFolder(Intent intent) {
1112        // Handle case where user selected "Folder"
1113        String folderName = getResources().getString(R.string.group_folder);
1114        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1115
1116        if (folderName != null && folderName.equals(shortcutName)) {
1117            addFolder();
1118        } else {
1119            startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1120        }
1121    }
1122
1123    void addFolder() {
1124        UserFolderInfo folderInfo = new UserFolderInfo();
1125        folderInfo.title = getText(R.string.folder_name);
1126
1127        CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1128        cellInfo.screen = mWorkspace.getCurrentScreen();
1129        if (!findSingleSlot(cellInfo)) return;
1130
1131        // Update the model
1132        LauncherModel.addItemToDatabase(this, folderInfo,
1133                LauncherSettings.Favorites.CONTAINER_DESKTOP,
1134                mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1135        mFolders.put(folderInfo.id, folderInfo);
1136
1137        // Create the view
1138        FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1139                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1140        mWorkspace.addInCurrentScreen(newFolder,
1141                cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
1142    }
1143
1144    void removeFolder(FolderInfo folder) {
1145        mFolders.remove(folder.id);
1146    }
1147
1148    private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
1149        cellInfo.screen = mWorkspace.getCurrentScreen();
1150        if (!findSingleSlot(cellInfo)) return;
1151
1152        final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1153
1154        if (!mRestoring) {
1155            final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1156                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1157            mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1158                    isWorkspaceLocked());
1159        }
1160    }
1161
1162    static LiveFolderInfo addLiveFolder(Context context, Intent data,
1163            CellLayout.CellInfo cellInfo, boolean notify) {
1164
1165        Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1166        String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1167
1168        Drawable icon = null;
1169        boolean filtered = false;
1170        Intent.ShortcutIconResource iconResource = null;
1171
1172        Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1173        if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1174            try {
1175                iconResource = (Intent.ShortcutIconResource) extra;
1176                final PackageManager packageManager = context.getPackageManager();
1177                Resources resources = packageManager.getResourcesForApplication(
1178                        iconResource.packageName);
1179                final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1180                icon = resources.getDrawable(id);
1181            } catch (Exception e) {
1182                w(LOG_TAG, "Could not load live folder icon: " + extra);
1183            }
1184        }
1185
1186        if (icon == null) {
1187            icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1188        }
1189
1190        final LiveFolderInfo info = new LiveFolderInfo();
1191        info.icon = icon;
1192        info.filtered = filtered;
1193        info.title = name;
1194        info.iconResource = iconResource;
1195        info.uri = data.getData();
1196        info.baseIntent = baseIntent;
1197        info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1198                LiveFolders.DISPLAY_MODE_GRID);
1199
1200        LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1201                cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1202        mFolders.put(info.id, info);
1203
1204        return info;
1205    }
1206
1207    private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1208        final int[] xy = new int[2];
1209        if (findSlot(cellInfo, xy, 1, 1)) {
1210            cellInfo.cellX = xy[0];
1211            cellInfo.cellY = xy[1];
1212            return true;
1213        }
1214        return false;
1215    }
1216
1217    private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1218        if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1219            boolean[] occupied = mSavedState != null ?
1220                    mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1221            cellInfo = mWorkspace.findAllVacantCells(occupied);
1222            if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1223                Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1224                return false;
1225            }
1226        }
1227        return true;
1228    }
1229
1230    private void showNotifications() {
1231        final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1232        if (statusBar != null) {
1233            statusBar.expand();
1234        }
1235    }
1236
1237    private void startWallpaper() {
1238        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1239        Intent chooser = Intent.createChooser(pickWallpaper,
1240                getText(R.string.chooser_wallpaper));
1241        WallpaperManager wm = (WallpaperManager)
1242                getSystemService(Context.WALLPAPER_SERVICE);
1243        WallpaperInfo wi = wm.getWallpaperInfo();
1244        if (wi != null && wi.getSettingsActivity() != null) {
1245            LabeledIntent li = new LabeledIntent(getPackageName(),
1246                    R.string.configure_wallpaper, 0);
1247            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1248            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1249        }
1250        startActivity(chooser);
1251    }
1252
1253    @Override
1254    public void onWindowFocusChanged(boolean hasFocus) {
1255        super.onWindowFocusChanged(hasFocus);
1256        if (!hasFocus) {
1257            mBackDown = mHomeDown = false;
1258        }
1259    }
1260
1261    @Override
1262    public boolean dispatchKeyEvent(KeyEvent event) {
1263        if (event.getAction() == KeyEvent.ACTION_DOWN) {
1264            switch (event.getKeyCode()) {
1265                case KeyEvent.KEYCODE_BACK:
1266                    mBackDown = true;
1267                    return true;
1268                case KeyEvent.KEYCODE_HOME:
1269                    mHomeDown = true;
1270                    return true;
1271            }
1272        } else if (event.getAction() == KeyEvent.ACTION_UP) {
1273            switch (event.getKeyCode()) {
1274                case KeyEvent.KEYCODE_BACK:
1275                    if (!event.isCanceled()) {
1276                        mWorkspace.dispatchKeyEvent(event);
1277                        if (isAllAppsVisible()) {
1278                            closeAllApps(true);
1279                        } else {
1280                            closeFolder();
1281                        }
1282                    }
1283                    mBackDown = false;
1284                    return true;
1285                case KeyEvent.KEYCODE_HOME:
1286                    mHomeDown = false;
1287                    return true;
1288            }
1289        }
1290
1291        return super.dispatchKeyEvent(event);
1292    }
1293
1294    private void closeFolder() {
1295        Folder folder = mWorkspace.getOpenFolder();
1296        if (folder != null) {
1297            closeFolder(folder);
1298        }
1299    }
1300
1301    void closeFolder(Folder folder) {
1302        folder.getInfo().opened = false;
1303        ViewGroup parent = (ViewGroup) folder.getParent();
1304        if (parent != null) {
1305            parent.removeView(folder);
1306            // TODO: this line crashes.
1307            //mDragController.removeDropTarget((DropTarget)folder);
1308        }
1309        folder.onClose();
1310    }
1311
1312    /**
1313     * Go through the and disconnect any of the callbacks in the drawables and the views or we
1314     * leak the previous Home screen on orientation change.
1315     */
1316    private void unbindDesktopItems() {
1317        for (ItemInfo item: mDesktopItems) {
1318            item.unbind();
1319        }
1320    }
1321
1322    /**
1323     * Launches the intent referred by the clicked shortcut.
1324     *
1325     * @param v The view representing the clicked shortcut.
1326     */
1327    public void onClick(View v) {
1328        Object tag = v.getTag();
1329        if (tag instanceof ApplicationInfo) {
1330            // Open shortcut
1331            final Intent intent = ((ApplicationInfo) tag).intent;
1332            startActivitySafely(intent);
1333        } else if (tag instanceof FolderInfo) {
1334            handleFolderClick((FolderInfo) tag);
1335        } else if (v == mHandleView) {
1336            Log.d(TAG, "onClick");
1337            if (isAllAppsVisible()) {
1338                closeAllApps(true);
1339            } else {
1340                showAllApps();
1341            }
1342        }
1343    }
1344
1345    void startActivitySafely(Intent intent) {
1346        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1347        try {
1348            startActivity(intent);
1349        } catch (ActivityNotFoundException e) {
1350            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1351        } catch (SecurityException e) {
1352            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1353            e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
1354                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1355                    "or use the exported attribute for this activity.", e);
1356        }
1357    }
1358
1359    private void handleFolderClick(FolderInfo folderInfo) {
1360        if (!folderInfo.opened) {
1361            // Close any open folder
1362            closeFolder();
1363            // Open the requested folder
1364            openFolder(folderInfo);
1365        } else {
1366            // Find the open folder...
1367            Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1368            int folderScreen;
1369            if (openFolder != null) {
1370                folderScreen = mWorkspace.getScreenForView(openFolder);
1371                // .. and close it
1372                closeFolder(openFolder);
1373                if (folderScreen != mWorkspace.getCurrentScreen()) {
1374                    // Close any folder open on the current screen
1375                    closeFolder();
1376                    // Pull the folder onto this screen
1377                    openFolder(folderInfo);
1378                }
1379            }
1380        }
1381    }
1382
1383    /**
1384     * Opens the user fodler described by the specified tag. The opening of the folder
1385     * is animated relative to the specified View. If the View is null, no animation
1386     * is played.
1387     *
1388     * @param folderInfo The FolderInfo describing the folder to open.
1389     */
1390    private void openFolder(FolderInfo folderInfo) {
1391        Folder openFolder;
1392
1393        if (folderInfo instanceof UserFolderInfo) {
1394            openFolder = UserFolder.fromXml(this);
1395        } else if (folderInfo instanceof LiveFolderInfo) {
1396            openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
1397        } else {
1398            return;
1399        }
1400
1401        openFolder.setDragController(mDragController);
1402        openFolder.setLauncher(this);
1403
1404        openFolder.bind(folderInfo);
1405        folderInfo.opened = true;
1406
1407        mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1408        openFolder.onOpen();
1409    }
1410
1411    public boolean onLongClick(View v) {
1412        if (isWorkspaceLocked()) {
1413            return false;
1414        }
1415
1416        if (!(v instanceof CellLayout)) {
1417            v = (View) v.getParent();
1418        }
1419
1420        CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1421
1422        // This happens when long clicking an item with the dpad/trackball
1423        if (cellInfo == null) {
1424            return true;
1425        }
1426
1427        if (mWorkspace.allowLongPress()) {
1428            if (cellInfo.cell == null) {
1429                if (cellInfo.valid) {
1430                    // User long pressed on empty space
1431                    mWorkspace.setAllowLongPress(false);
1432                    showAddDialog(cellInfo);
1433                }
1434            } else {
1435                if (!(cellInfo.cell instanceof Folder)) {
1436                    // User long pressed on an item
1437                    mWorkspace.startDrag(cellInfo);
1438                }
1439            }
1440        }
1441        return true;
1442    }
1443
1444    View getDrawerHandle() {
1445        return mHandleView;
1446    }
1447
1448    Workspace getWorkspace() {
1449        return mWorkspace;
1450    }
1451
1452    /* TODO
1453    GridView getApplicationsGrid() {
1454        return mAllAppsGrid;
1455    }
1456    */
1457
1458    @Override
1459    protected Dialog onCreateDialog(int id) {
1460        switch (id) {
1461            case DIALOG_CREATE_SHORTCUT:
1462                return new CreateShortcut().createDialog();
1463            case DIALOG_RENAME_FOLDER:
1464                return new RenameFolder().createDialog();
1465        }
1466
1467        return super.onCreateDialog(id);
1468    }
1469
1470    @Override
1471    protected void onPrepareDialog(int id, Dialog dialog) {
1472        switch (id) {
1473            case DIALOG_CREATE_SHORTCUT:
1474                break;
1475            case DIALOG_RENAME_FOLDER:
1476                if (mFolderInfo != null) {
1477                    EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1478                    final CharSequence text = mFolderInfo.title;
1479                    input.setText(text);
1480                    input.setSelection(0, text.length());
1481                }
1482                break;
1483        }
1484    }
1485
1486    void showRenameDialog(FolderInfo info) {
1487        mFolderInfo = info;
1488        mWaitingForResult = true;
1489        showDialog(DIALOG_RENAME_FOLDER);
1490    }
1491
1492    private void showAddDialog(CellLayout.CellInfo cellInfo) {
1493        mAddItemCellInfo = cellInfo;
1494        mWaitingForResult = true;
1495        showDialog(DIALOG_CREATE_SHORTCUT);
1496    }
1497
1498    private void pickShortcut(int requestCode, int title) {
1499        Bundle bundle = new Bundle();
1500
1501        ArrayList<String> shortcutNames = new ArrayList<String>();
1502        shortcutNames.add(getString(R.string.group_applications));
1503        bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1504
1505        ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1506        shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1507                        R.drawable.ic_launcher_application));
1508        bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1509
1510        Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1511        pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1512        pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1513        pickIntent.putExtras(bundle);
1514
1515        startActivityForResult(pickIntent, requestCode);
1516    }
1517
1518    private class RenameFolder {
1519        private EditText mInput;
1520
1521        Dialog createDialog() {
1522            mWaitingForResult = true;
1523            final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1524            mInput = (EditText) layout.findViewById(R.id.folder_name);
1525
1526            AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1527            builder.setIcon(0);
1528            builder.setTitle(getString(R.string.rename_folder_title));
1529            builder.setCancelable(true);
1530            builder.setOnCancelListener(new Dialog.OnCancelListener() {
1531                public void onCancel(DialogInterface dialog) {
1532                    cleanup();
1533                }
1534            });
1535            builder.setNegativeButton(getString(R.string.cancel_action),
1536                new Dialog.OnClickListener() {
1537                    public void onClick(DialogInterface dialog, int which) {
1538                        cleanup();
1539                    }
1540                }
1541            );
1542            builder.setPositiveButton(getString(R.string.rename_action),
1543                new Dialog.OnClickListener() {
1544                    public void onClick(DialogInterface dialog, int which) {
1545                        changeFolderName();
1546                    }
1547                }
1548            );
1549            builder.setView(layout);
1550
1551            final AlertDialog dialog = builder.create();
1552            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1553                public void onShow(DialogInterface dialog) {
1554                }
1555            });
1556
1557            return dialog;
1558        }
1559
1560        private void changeFolderName() {
1561            final String name = mInput.getText().toString();
1562            if (!TextUtils.isEmpty(name)) {
1563                // Make sure we have the right folder info
1564                mFolderInfo = mFolders.get(mFolderInfo.id);
1565                mFolderInfo.title = name;
1566                LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1567
1568                if (mWorkspaceLoading) {
1569                    lockAllApps();
1570                    mModel.setWorkspaceDirty();
1571                    mModel.startLoader(Launcher.this, false);
1572                } else {
1573                    final FolderIcon folderIcon = (FolderIcon)
1574                            mWorkspace.getViewForTag(mFolderInfo);
1575                    if (folderIcon != null) {
1576                        folderIcon.setText(name);
1577                        getWorkspace().requestLayout();
1578                    } else {
1579                        lockAllApps();
1580                        mModel.setWorkspaceDirty();
1581                        mWorkspaceLoading = true;
1582                        mModel.startLoader(Launcher.this, false);
1583                    }
1584                }
1585            }
1586            cleanup();
1587        }
1588
1589        private void cleanup() {
1590            dismissDialog(DIALOG_RENAME_FOLDER);
1591            mWaitingForResult = false;
1592            mFolderInfo = null;
1593        }
1594    }
1595
1596    boolean isAllAppsVisible() {
1597        return mAllAppsGrid.isVisible();
1598    }
1599
1600    void showAllApps() {
1601        mAllAppsGrid.zoom(1.0f);
1602        //mWorkspace.hide();
1603
1604        // TODO: fade these two too
1605        mDeleteZone.setVisibility(View.GONE);
1606        //mHandleView.setVisibility(View.GONE);
1607    }
1608
1609    void closeAllApps(boolean animated) {
1610        if (mAllAppsGrid.isVisible()) {
1611            mAllAppsGrid.zoom(0.0f);
1612            mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1613
1614            // TODO: fade these two too
1615            /*
1616            mDeleteZone.setVisibility(View.VISIBLE);
1617            mHandleView.setVisibility(View.VISIBLE);
1618            */
1619        }
1620    }
1621
1622    void lockAllApps() {
1623        // TODO
1624    }
1625
1626    void unlockAllApps() {
1627        // TODO
1628    }
1629
1630    /**
1631     * Displays the shortcut creation dialog and launches, if necessary, the
1632     * appropriate activity.
1633     */
1634    private class CreateShortcut implements DialogInterface.OnClickListener,
1635            DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1636            DialogInterface.OnShowListener {
1637
1638        private AddAdapter mAdapter;
1639
1640        Dialog createDialog() {
1641            mWaitingForResult = true;
1642
1643            mAdapter = new AddAdapter(Launcher.this);
1644
1645            final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1646            builder.setTitle(getString(R.string.menu_item_add_item));
1647            builder.setAdapter(mAdapter, this);
1648
1649            builder.setInverseBackgroundForced(true);
1650
1651            AlertDialog dialog = builder.create();
1652            dialog.setOnCancelListener(this);
1653            dialog.setOnDismissListener(this);
1654            dialog.setOnShowListener(this);
1655
1656            return dialog;
1657        }
1658
1659        public void onCancel(DialogInterface dialog) {
1660            mWaitingForResult = false;
1661            cleanup();
1662        }
1663
1664        public void onDismiss(DialogInterface dialog) {
1665        }
1666
1667        private void cleanup() {
1668            dismissDialog(DIALOG_CREATE_SHORTCUT);
1669        }
1670
1671        /**
1672         * Handle the action clicked in the "Add to home" dialog.
1673         */
1674        public void onClick(DialogInterface dialog, int which) {
1675            Resources res = getResources();
1676            cleanup();
1677
1678            switch (which) {
1679                case AddAdapter.ITEM_SHORTCUT: {
1680                    // Insert extra item to handle picking application
1681                    pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
1682                    break;
1683                }
1684
1685                case AddAdapter.ITEM_APPWIDGET: {
1686                    int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
1687
1688                    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1689                    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1690                    // add the search widget
1691                    ArrayList<AppWidgetProviderInfo> customInfo =
1692                            new ArrayList<AppWidgetProviderInfo>();
1693                    AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1694                    info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1695                    info.label = getString(R.string.group_search);
1696                    info.icon = R.drawable.ic_search_widget;
1697                    customInfo.add(info);
1698                    pickIntent.putParcelableArrayListExtra(
1699                            AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1700                    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1701                    Bundle b = new Bundle();
1702                    b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1703                    customExtras.add(b);
1704                    pickIntent.putParcelableArrayListExtra(
1705                            AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1706                    // start the pick activity
1707                    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1708                    break;
1709                }
1710
1711                case AddAdapter.ITEM_LIVE_FOLDER: {
1712                    // Insert extra item to handle inserting folder
1713                    Bundle bundle = new Bundle();
1714
1715                    ArrayList<String> shortcutNames = new ArrayList<String>();
1716                    shortcutNames.add(res.getString(R.string.group_folder));
1717                    bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1718
1719                    ArrayList<ShortcutIconResource> shortcutIcons =
1720                            new ArrayList<ShortcutIconResource>();
1721                    shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1722                            R.drawable.ic_launcher_folder));
1723                    bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1724
1725                    Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1726                    pickIntent.putExtra(Intent.EXTRA_INTENT,
1727                            new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1728                    pickIntent.putExtra(Intent.EXTRA_TITLE,
1729                            getText(R.string.title_select_live_folder));
1730                    pickIntent.putExtras(bundle);
1731
1732                    startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1733                    break;
1734                }
1735
1736                case AddAdapter.ITEM_WALLPAPER: {
1737                    startWallpaper();
1738                    break;
1739                }
1740            }
1741        }
1742
1743        public void onShow(DialogInterface dialog) {
1744        }
1745    }
1746
1747    /**
1748     * Implementation of the method from LauncherModel.Callbacks.
1749     */
1750    public int getCurrentWorkspaceScreen() {
1751        return mWorkspace.getCurrentScreen();
1752    }
1753
1754    /**
1755     * Refreshes the shortcuts shown on the workspace.
1756     *
1757     * Implementation of the method from LauncherModel.Callbacks.
1758     */
1759    public void startBinding() {
1760        final Workspace workspace = mWorkspace;
1761        int count = workspace.getChildCount();
1762        for (int i = 0; i < count; i++) {
1763            ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1764        }
1765
1766        if (DEBUG_USER_INTERFACE) {
1767            android.widget.Button finishButton = new android.widget.Button(this);
1768            finishButton.setText("Finish");
1769            workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1770
1771            finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1772                public void onClick(View v) {
1773                    finish();
1774                }
1775            });
1776        }
1777    }
1778
1779    /**
1780     * Bind the items start-end from the list.
1781     *
1782     * Implementation of the method from LauncherModel.Callbacks.
1783     */
1784    public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1785
1786        final Workspace workspace = mWorkspace;
1787
1788        for (int i=start; i<end; i++) {
1789            final ItemInfo item = shortcuts.get(i);
1790            mDesktopItems.add(item);
1791            switch (item.itemType) {
1792                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1793                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1794                    final View shortcut = createShortcut((ApplicationInfo) item);
1795                    workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1796                            false);
1797                    break;
1798                case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1799                    final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1800                            (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1801                            (UserFolderInfo) item);
1802                    workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1803                            false);
1804                    break;
1805                case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1806                    final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1807                            R.layout.live_folder_icon, this,
1808                            (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1809                            (LiveFolderInfo) item);
1810                    workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1811                            false);
1812                    break;
1813                case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1814                    final int screen = workspace.getCurrentScreen();
1815                    final View view = mInflater.inflate(R.layout.widget_search,
1816                            (ViewGroup) workspace.getChildAt(screen), false);
1817
1818                    Search search = (Search) view.findViewById(R.id.widget_search);
1819                    search.setLauncher(this);
1820
1821                    final Widget widget = (Widget) item;
1822                    view.setTag(widget);
1823
1824                    workspace.addWidget(view, widget, false);
1825                    break;
1826            }
1827        }
1828
1829        workspace.requestLayout();
1830    }
1831
1832    /**
1833     * Implementation of the method from LauncherModel.Callbacks.
1834     */
1835    void bindFolders(HashMap<Long, FolderInfo> folders) {
1836        mFolders.putAll(folders);
1837    }
1838
1839    /**
1840     * Add the views for a widget to the workspace.
1841     *
1842     * Implementation of the method from LauncherModel.Callbacks.
1843     */
1844    public void bindAppWidget(LauncherAppWidgetInfo item) {
1845        final Workspace workspace = mWorkspace;
1846
1847        final int appWidgetId = item.appWidgetId;
1848        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1849        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1850
1851        if (true) {
1852            Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1853                    appWidgetId, appWidgetInfo));
1854        }
1855
1856        item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1857        item.hostView.setTag(item);
1858
1859        workspace.addInScreen(item.hostView, item.screen, item.cellX,
1860                item.cellY, item.spanX, item.spanY, false);
1861
1862        workspace.requestLayout();
1863
1864        mDesktopItems.add(item);
1865    }
1866
1867    /**
1868     * Callback saying that there aren't any more items to bind.
1869     *
1870     * Implementation of the method from LauncherModel.Callbacks.
1871     */
1872    public void finishBindingItems() {
1873        if (mSavedState != null) {
1874            if (!mWorkspace.hasFocus()) {
1875                mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1876            }
1877
1878            final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1879            if (userFolders != null) {
1880                for (long folderId : userFolders) {
1881                    final FolderInfo info = mFolders.get(folderId);
1882                    if (info != null) {
1883                        openFolder(info);
1884                    }
1885                }
1886                final Folder openFolder = mWorkspace.getOpenFolder();
1887                if (openFolder != null) {
1888                    openFolder.requestFocus();
1889                }
1890            }
1891
1892            final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1893            if (allApps) {
1894                showAllApps();
1895            }
1896
1897            mSavedState = null;
1898        }
1899
1900        if (mSavedInstanceState != null) {
1901            super.onRestoreInstanceState(mSavedInstanceState);
1902            mSavedInstanceState = null;
1903        }
1904
1905        /* TODO
1906        if (mAllAppsVisible && !mDrawer.hasFocus()) {
1907            mDrawer.requestFocus();
1908        }
1909        */
1910
1911        Log.d(TAG, "finishBindingItems done");
1912        mWorkspaceLoading = false;
1913    }
1914
1915    /**
1916     * Add the icons for all apps.
1917     *
1918     * Implementation of the method from LauncherModel.Callbacks.
1919     */
1920    public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
1921        Log.d(LOG_TAG, "got info for " + apps.size() + " apps");
1922        mAllAppsList = apps;
1923        mAllAppsGrid.setApps(mAllAppsList);
1924    }
1925
1926    /**
1927     * A package was installed.
1928     *
1929     * Implementation of the method from LauncherModel.Callbacks.
1930     */
1931    public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1932        removeDialog(DIALOG_CREATE_SHORTCUT);
1933        mAllAppsGrid.addApps(apps);
1934    }
1935
1936    /**
1937     * A package was updated.
1938     *
1939     * Implementation of the method from LauncherModel.Callbacks.
1940     */
1941    public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1942        removeDialog(DIALOG_CREATE_SHORTCUT);
1943        mWorkspace.updateShortcutsForPackage(packageName);
1944    }
1945
1946    /**
1947     * A package was uninstalled.
1948     *
1949     * Implementation of the method from LauncherModel.Callbacks.
1950     */
1951    public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1952        removeDialog(DIALOG_CREATE_SHORTCUT);
1953        mWorkspace.removeShortcutsForPackage(packageName);
1954        mAllAppsGrid.removeApps(apps);
1955    }
1956}
1957