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