Launcher.java revision 4a79a04bcd5f8c4d4fab7bcc46eea9e19a3dc63e
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 com.android.common.Search;
20
21import android.app.Activity;
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
26import android.app.WallpaperManager;
27import android.content.ActivityNotFoundException;
28import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.Intent.ShortcutIconResource;
35import android.content.IntentFilter;
36import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.ResolveInfo;
39import android.content.res.Configuration;
40import android.content.res.Resources;
41import android.content.res.TypedArray;
42import android.database.ContentObserver;
43import android.graphics.Bitmap;
44import android.graphics.Rect;
45import android.graphics.Canvas;
46import android.graphics.drawable.Drawable;
47import android.graphics.drawable.ColorDrawable;
48import android.net.Uri;
49import android.os.AsyncTask;
50import android.os.Bundle;
51import android.os.Handler;
52import android.os.Parcelable;
53import android.os.SystemClock;
54import android.os.SystemProperties;
55import android.provider.LiveFolders;
56import android.text.Selection;
57import android.text.SpannableStringBuilder;
58import android.text.TextUtils;
59import android.text.method.TextKeyListener;
60import android.util.Log;
61import android.view.Display;
62import android.view.HapticFeedbackConstants;
63import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuItem;
67import android.view.View;
68import android.view.ViewGroup;
69import android.view.View.OnLongClickListener;
70import android.view.inputmethod.InputMethodManager;
71import android.widget.EditText;
72import android.widget.TextView;
73import android.widget.Toast;
74import android.widget.ImageView;
75import android.widget.PopupWindow;
76import android.widget.LinearLayout;
77import android.appwidget.AppWidgetManager;
78import android.appwidget.AppWidgetProviderInfo;
79
80import java.util.ArrayList;
81import java.util.List;
82import java.util.HashMap;
83import java.io.DataOutputStream;
84import java.io.FileNotFoundException;
85import java.io.IOException;
86import java.io.DataInputStream;
87
88import com.android.launcher.R;
89
90/**
91 * Default launcher application.
92 */
93public final class Launcher extends Activity
94        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
95    static final String TAG = "Launcher";
96    static final boolean LOGD = false;
97
98    static final boolean PROFILE_STARTUP = false;
99    static final boolean DEBUG_WIDGETS = false;
100    static final boolean DEBUG_USER_INTERFACE = false;
101
102    private static final int WALLPAPER_SCREENS_SPAN = 2;
103
104    private static final int MENU_GROUP_ADD = 1;
105    private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
106
107    private static final int MENU_ADD = Menu.FIRST + 1;
108    private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
109    private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
110    private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
111    private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
112
113    private static final int REQUEST_CREATE_SHORTCUT = 1;
114    private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
115    private static final int REQUEST_CREATE_APPWIDGET = 5;
116    private static final int REQUEST_PICK_APPLICATION = 6;
117    private static final int REQUEST_PICK_SHORTCUT = 7;
118    private static final int REQUEST_PICK_LIVE_FOLDER = 8;
119    private static final int REQUEST_PICK_APPWIDGET = 9;
120    private static final int REQUEST_PICK_WALLPAPER = 10;
121
122    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
123
124    static final int SCREEN_COUNT = 5;
125    static final int DEFAULT_SCREEN = 2;
126    static final int NUMBER_CELLS_X = 4;
127    static final int NUMBER_CELLS_Y = 4;
128
129    static final int DIALOG_CREATE_SHORTCUT = 1;
130    static final int DIALOG_RENAME_FOLDER = 2;
131
132    private static final String PREFERENCES = "launcher.preferences";
133
134    // Type: int
135    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
136    // Type: boolean
137    private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
138    // Type: long
139    private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
140    // Type: int
141    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
142    // Type: int
143    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
144    // Type: int
145    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
146    // Type: int
147    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
148    // Type: int
149    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
150    // Type: int
151    private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
152    // Type: int
153    private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
154    // Type: int[]
155    private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
156    // Type: boolean
157    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158    // Type: long
159    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
161    static final int APPWIDGET_HOST_ID = 1024;
162
163    private static final Object sLock = new Object();
164    private static int sScreen = DEFAULT_SCREEN;
165
166    private final BroadcastReceiver mCloseSystemDialogsReceiver
167            = new CloseSystemDialogsIntentReceiver();
168    private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
169
170    private LayoutInflater mInflater;
171
172    private DragController mDragController;
173    private Workspace mWorkspace;
174
175    private AppWidgetManager mAppWidgetManager;
176    private LauncherAppWidgetHost mAppWidgetHost;
177
178    private CellLayout.CellInfo mAddItemCellInfo;
179    private CellLayout.CellInfo mMenuAddInfo;
180    private final int[] mCellCoordinates = new int[2];
181    private FolderInfo mFolderInfo;
182
183    private DeleteZone mDeleteZone;
184    private HandleView mHandleView;
185    private AllAppsView mAllAppsGrid;
186
187    private Bundle mSavedState;
188
189    private SpannableStringBuilder mDefaultKeySsb = null;
190
191    private boolean mWorkspaceLoading = true;
192
193    private boolean mPaused = true;
194    private boolean mRestoring;
195    private boolean mWaitingForResult;
196
197    private Bundle mSavedInstanceState;
198
199    private LauncherModel mModel;
200    private IconCache mIconCache;
201
202    private static LocaleConfiguration sLocaleConfiguration = null;
203
204    private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
205    private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
206
207    private ImageView mPreviousView;
208    private ImageView mNextView;
209
210    // Hotseats (quick-launch icons next to AllApps)
211    private static final int NUM_HOTSEATS = 2;
212    private String[] mHotseatConfig = null;
213    private Intent[] mHotseats = null;
214    private Drawable[] mHotseatIcons = null;
215    private CharSequence[] mHotseatLabels = null;
216
217    @Override
218    protected void onCreate(Bundle savedInstanceState) {
219        super.onCreate(savedInstanceState);
220
221        LauncherApplication app = ((LauncherApplication)getApplication());
222        mModel = app.setLauncher(this);
223        mIconCache = app.getIconCache();
224        mDragController = new DragController(this);
225        mInflater = getLayoutInflater();
226
227        mAppWidgetManager = AppWidgetManager.getInstance(this);
228        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
229        mAppWidgetHost.startListening();
230
231        if (PROFILE_STARTUP) {
232            android.os.Debug.startMethodTracing("/sdcard/launcher");
233        }
234
235        loadHotseats();
236        checkForLocaleChange();
237        setWallpaperDimension();
238
239        setContentView(R.layout.launcher);
240        setupViews();
241
242        registerContentObservers();
243
244        lockAllApps();
245
246        mSavedState = savedInstanceState;
247        restoreState(mSavedState);
248
249        if (PROFILE_STARTUP) {
250            android.os.Debug.stopMethodTracing();
251        }
252
253        if (!mRestoring) {
254            mModel.startLoader(this, true);
255        }
256
257        // For handling default keys
258        mDefaultKeySsb = new SpannableStringBuilder();
259        Selection.setSelection(mDefaultKeySsb, 0);
260
261        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
262        registerReceiver(mCloseSystemDialogsReceiver, filter);
263    }
264
265    private void checkForLocaleChange() {
266        if (sLocaleConfiguration == null) {
267            new AsyncTask<Void, Void, LocaleConfiguration>() {
268                @Override
269                protected LocaleConfiguration doInBackground(Void... unused) {
270                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
271                    readConfiguration(Launcher.this, localeConfiguration);
272                    return localeConfiguration;
273                }
274
275                @Override
276                protected void onPostExecute(LocaleConfiguration result) {
277                    sLocaleConfiguration = result;
278                    checkForLocaleChange();  // recursive, but now with a locale configuration
279                }
280            }.execute();
281            return;
282        }
283
284        final Configuration configuration = getResources().getConfiguration();
285
286        final String previousLocale = sLocaleConfiguration.locale;
287        final String locale = configuration.locale.toString();
288
289        final int previousMcc = sLocaleConfiguration.mcc;
290        final int mcc = configuration.mcc;
291
292        final int previousMnc = sLocaleConfiguration.mnc;
293        final int mnc = configuration.mnc;
294
295        boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
296
297        if (localeChanged) {
298            sLocaleConfiguration.locale = locale;
299            sLocaleConfiguration.mcc = mcc;
300            sLocaleConfiguration.mnc = mnc;
301
302            mIconCache.flush();
303            loadHotseats();
304
305            final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
306            new Thread("WriteLocaleConfiguration") {
307                public void run() {
308                    writeConfiguration(Launcher.this, localeConfiguration);
309                }
310            }.start();
311        }
312    }
313
314    private static class LocaleConfiguration {
315        public String locale;
316        public int mcc = -1;
317        public int mnc = -1;
318    }
319
320    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
321        DataInputStream in = null;
322        try {
323            in = new DataInputStream(context.openFileInput(PREFERENCES));
324            configuration.locale = in.readUTF();
325            configuration.mcc = in.readInt();
326            configuration.mnc = in.readInt();
327        } catch (FileNotFoundException e) {
328            // Ignore
329        } catch (IOException e) {
330            // Ignore
331        } finally {
332            if (in != null) {
333                try {
334                    in.close();
335                } catch (IOException e) {
336                    // Ignore
337                }
338            }
339        }
340    }
341
342    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
343        DataOutputStream out = null;
344        try {
345            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
346            out.writeUTF(configuration.locale);
347            out.writeInt(configuration.mcc);
348            out.writeInt(configuration.mnc);
349            out.flush();
350        } catch (FileNotFoundException e) {
351            // Ignore
352        } catch (IOException e) {
353            //noinspection ResultOfMethodCallIgnored
354            context.getFileStreamPath(PREFERENCES).delete();
355        } finally {
356            if (out != null) {
357                try {
358                    out.close();
359                } catch (IOException e) {
360                    // Ignore
361                }
362            }
363        }
364    }
365
366    static int getScreen() {
367        synchronized (sLock) {
368            return sScreen;
369        }
370    }
371
372    static void setScreen(int screen) {
373        synchronized (sLock) {
374            sScreen = screen;
375        }
376    }
377
378    private void setWallpaperDimension() {
379        WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
380
381        Display display = getWindowManager().getDefaultDisplay();
382        boolean isPortrait = display.getWidth() < display.getHeight();
383
384        final int width = isPortrait ? display.getWidth() : display.getHeight();
385        final int height = isPortrait ? display.getHeight() : display.getWidth();
386        wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
387    }
388
389    // Note: This doesn't do all the client-id magic that BrowserProvider does
390    // in Browser. (http://b/2425179)
391    private Uri getDefaultBrowserUri() {
392        String url = getString(R.string.default_browser_url);
393        if (url.indexOf("{CID}") != -1) {
394            url = url.replace("{CID}", "android-google");
395        }
396        return Uri.parse(url);
397    }
398
399    // Load the Intent templates from arrays.xml to populate the hotseats. For
400    // each Intent, if it resolves to a single app, use that as the launch
401    // intent & use that app's label as the contentDescription. Otherwise,
402    // retain the ResolveActivity so the user can pick an app.
403    private void loadHotseats() {
404        if (mHotseatConfig == null) {
405            mHotseatConfig = getResources().getStringArray(R.array.hotseats);
406            if (mHotseatConfig.length > 0) {
407                mHotseats = new Intent[mHotseatConfig.length];
408                mHotseatLabels = new CharSequence[mHotseatConfig.length];
409                mHotseatIcons = new Drawable[mHotseatConfig.length];
410            } else {
411                mHotseats = null;
412                mHotseatIcons = null;
413                mHotseatLabels = null;
414            }
415
416            TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
417            for (int i=0; i<mHotseatConfig.length; i++) {
418                // load icon for this slot; currently unrelated to the actual activity
419                try {
420                    mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
421                } catch (ArrayIndexOutOfBoundsException ex) {
422                    Log.w(TAG, "Missing hotseat_icons array item #" + i);
423                    mHotseatIcons[i] = null;
424                }
425            }
426            hotseatIconDrawables.recycle();
427        }
428
429        PackageManager pm = getPackageManager();
430        for (int i=0; i<mHotseatConfig.length; i++) {
431            Intent intent = null;
432            if (mHotseatConfig[i].equals("*BROWSER*")) {
433                // magic value meaning "launch user's default web browser"
434                // replace it with a generic web request so we can see if there is indeed a default
435                String defaultUri = getString(R.string.default_browser_url);
436                intent = new Intent(
437                        Intent.ACTION_VIEW,
438                        ((defaultUri != null)
439                            ? Uri.parse(defaultUri)
440                            : getDefaultBrowserUri())
441                    ).addCategory(Intent.CATEGORY_BROWSABLE);
442                // note: if the user launches this without a default set, she
443                // will always be taken to the default URL above; this is
444                // unavoidable as we must specify a valid URL in order for the
445                // chooser to appear, and once the user selects something, that
446                // URL is unavoidably sent to the chosen app.
447            } else {
448                try {
449                    intent = Intent.parseUri(mHotseatConfig[i], 0);
450                } catch (java.net.URISyntaxException ex) {
451                    Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
452                    // bogus; leave intent=null
453                }
454            }
455
456            if (intent == null) {
457                mHotseats[i] = null;
458                mHotseatLabels[i] = getText(R.string.activity_not_found);
459                continue;
460            }
461
462            if (LOGD) {
463                Log.d(TAG, "loadHotseats: hotseat " + i
464                    + " initial intent=["
465                    + intent.toUri(Intent.URI_INTENT_SCHEME)
466                    + "]");
467            }
468
469            ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
470            List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
471            if (LOGD) {
472                Log.d(TAG, "Best match for intent: " + bestMatch);
473                Log.d(TAG, "All matches: ");
474                for (ResolveInfo ri : allMatches) {
475                    Log.d(TAG, "  --> " + ri);
476                }
477            }
478            // did this resolve to a single app, or the resolver?
479            if (allMatches.size() == 0 || bestMatch == null) {
480                // can't find any activity to handle this. let's leave the
481                // intent as-is and let Launcher show a toast when it fails
482                // to launch.
483                mHotseats[i] = intent;
484
485                // set accessibility text to "Not installed"
486                mHotseatLabels[i] = getText(R.string.activity_not_found);
487            } else {
488                boolean found = false;
489                for (ResolveInfo ri : allMatches) {
490                    if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
491                        && bestMatch.activityInfo.applicationInfo.packageName
492                            .equals(ri.activityInfo.applicationInfo.packageName)) {
493                        found = true;
494                        break;
495                    }
496                }
497
498                if (!found) {
499                    if (LOGD) Log.d(TAG, "Multiple options, no default yet");
500                    // the bestMatch is probably the ResolveActivity, meaning the
501                    // user has not yet selected a default
502                    // so: we'll keep the original intent for now
503                    mHotseats[i] = intent;
504
505                    // set the accessibility text to "Select shortcut"
506                    mHotseatLabels[i] = getText(R.string.title_select_shortcut);
507                } else {
508                    // we have an app!
509                    // now reconstruct the intent to launch it through the front
510                    // door
511                    ComponentName com = new ComponentName(
512                        bestMatch.activityInfo.applicationInfo.packageName,
513                        bestMatch.activityInfo.name);
514                    mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
515
516                    // load the app label for accessibility
517                    mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
518                }
519            }
520
521            if (LOGD) {
522                Log.d(TAG, "loadHotseats: hotseat " + i
523                    + " final intent=["
524                    + ((mHotseats[i] == null)
525                        ? "null"
526                        : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
527                    + "] label=[" + mHotseatLabels[i]
528                    + "]"
529                    );
530            }
531        }
532    }
533
534    @Override
535    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
536        mWaitingForResult = false;
537
538        // The pattern used here is that a user PICKs a specific application,
539        // which, depending on the target, might need to CREATE the actual target.
540
541        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
542        // launch over to the Music app to actually CREATE_SHORTCUT.
543
544        if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
545            switch (requestCode) {
546                case REQUEST_PICK_APPLICATION:
547                    completeAddApplication(this, data, mAddItemCellInfo);
548                    break;
549                case REQUEST_PICK_SHORTCUT:
550                    processShortcut(data);
551                    break;
552                case REQUEST_CREATE_SHORTCUT:
553                    completeAddShortcut(data, mAddItemCellInfo);
554                    break;
555                case REQUEST_PICK_LIVE_FOLDER:
556                    addLiveFolder(data);
557                    break;
558                case REQUEST_CREATE_LIVE_FOLDER:
559                    completeAddLiveFolder(data, mAddItemCellInfo);
560                    break;
561                case REQUEST_PICK_APPWIDGET:
562                    addAppWidget(data);
563                    break;
564                case REQUEST_CREATE_APPWIDGET:
565                    completeAddAppWidget(data, mAddItemCellInfo);
566                    break;
567                case REQUEST_PICK_WALLPAPER:
568                    // We just wanted the activity result here so we can clear mWaitingForResult
569                    break;
570            }
571        } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
572                requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
573                data != null) {
574            // Clean up the appWidgetId if we canceled
575            int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
576            if (appWidgetId != -1) {
577                mAppWidgetHost.deleteAppWidgetId(appWidgetId);
578            }
579        }
580    }
581
582    @Override
583    protected void onResume() {
584        super.onResume();
585
586        mPaused = false;
587
588        if (mRestoring) {
589            mWorkspaceLoading = true;
590            mModel.startLoader(this, true);
591            mRestoring = false;
592        }
593    }
594
595    @Override
596    protected void onPause() {
597        super.onPause();
598        dismissPreview(mPreviousView);
599        dismissPreview(mNextView);
600        mDragController.cancelDrag();
601    }
602
603    @Override
604    public Object onRetainNonConfigurationInstance() {
605        // Flag the loader to stop early before switching
606        mModel.stopLoader();
607        mAllAppsGrid.surrender();
608        return Boolean.TRUE;
609    }
610
611    // We can't hide the IME if it was forced open.  So don't bother
612    /*
613    @Override
614    public void onWindowFocusChanged(boolean hasFocus) {
615        super.onWindowFocusChanged(hasFocus);
616
617        if (hasFocus) {
618            final InputMethodManager inputManager = (InputMethodManager)
619                    getSystemService(Context.INPUT_METHOD_SERVICE);
620            WindowManager.LayoutParams lp = getWindow().getAttributes();
621            inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
622                        android.os.Handler()) {
623                        protected void onReceiveResult(int resultCode, Bundle resultData) {
624                            Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
625                        }
626                    });
627            Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
628        }
629    }
630    */
631
632    private boolean acceptFilter() {
633        final InputMethodManager inputManager = (InputMethodManager)
634                getSystemService(Context.INPUT_METHOD_SERVICE);
635        return !inputManager.isFullscreenMode();
636    }
637
638    @Override
639    public boolean onKeyDown(int keyCode, KeyEvent event) {
640        boolean handled = super.onKeyDown(keyCode, event);
641        if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
642            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
643                    keyCode, event);
644            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
645                // something usable has been typed - start a search
646                // the typed text will be retrieved and cleared by
647                // showSearchDialog()
648                // If there are multiple keystrokes before the search dialog takes focus,
649                // onSearchRequested() will be called for every keystroke,
650                // but it is idempotent, so it's fine.
651                return onSearchRequested();
652            }
653        }
654
655        // Eat the long press event so the keyboard doesn't come up.
656        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
657            return true;
658        }
659
660        return handled;
661    }
662
663    private String getTypedText() {
664        return mDefaultKeySsb.toString();
665    }
666
667    private void clearTypedText() {
668        mDefaultKeySsb.clear();
669        mDefaultKeySsb.clearSpans();
670        Selection.setSelection(mDefaultKeySsb, 0);
671    }
672
673    /**
674     * Restores the previous state, if it exists.
675     *
676     * @param savedState The previous state.
677     */
678    private void restoreState(Bundle savedState) {
679        if (savedState == null) {
680            return;
681        }
682
683        final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
684        if (allApps) {
685            showAllApps(false);
686        }
687
688        final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
689        if (currentScreen > -1) {
690            mWorkspace.setCurrentScreen(currentScreen);
691        }
692
693        final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
694        if (addScreen > -1) {
695            mAddItemCellInfo = new CellLayout.CellInfo();
696            final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
697            addItemCellInfo.valid = true;
698            addItemCellInfo.screen = addScreen;
699            addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
700            addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
701            addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
702            addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
703            addItemCellInfo.findVacantCellsFromOccupied(
704                    savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
705                    savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
706                    savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
707            mRestoring = true;
708        }
709
710        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
711        if (renameFolder) {
712            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
713            mFolderInfo = mModel.getFolderById(this, sFolders, id);
714            mRestoring = true;
715        }
716    }
717
718    /**
719     * Finds all the views we need and configure them properly.
720     */
721    private void setupViews() {
722        DragController dragController = mDragController;
723
724        DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
725        dragLayer.setDragController(dragController);
726
727        mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
728        mAllAppsGrid.setLauncher(this);
729        mAllAppsGrid.setDragController(dragController);
730        ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
731        // Manage focusability manually since this thing is always visible
732        ((View) mAllAppsGrid).setFocusable(false);
733
734        mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
735        final Workspace workspace = mWorkspace;
736        workspace.setHapticFeedbackEnabled(false);
737
738        DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
739        mDeleteZone = deleteZone;
740
741        mHandleView = (HandleView) findViewById(R.id.all_apps_button);
742        mHandleView.setLauncher(this);
743        mHandleView.setOnClickListener(this);
744        mHandleView.setOnLongClickListener(this);
745
746        ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
747        hotseatLeft.setContentDescription(mHotseatLabels[0]);
748        hotseatLeft.setImageDrawable(mHotseatIcons[0]);
749        ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
750        hotseatRight.setContentDescription(mHotseatLabels[1]);
751        hotseatRight.setImageDrawable(mHotseatIcons[1]);
752
753        mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
754        mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
755
756        Drawable previous = mPreviousView.getDrawable();
757        Drawable next = mNextView.getDrawable();
758        mWorkspace.setIndicators(previous, next);
759
760        mPreviousView.setHapticFeedbackEnabled(false);
761        mPreviousView.setOnLongClickListener(this);
762        mNextView.setHapticFeedbackEnabled(false);
763        mNextView.setOnLongClickListener(this);
764
765        workspace.setOnLongClickListener(this);
766        workspace.setDragController(dragController);
767        workspace.setLauncher(this);
768
769        deleteZone.setLauncher(this);
770        deleteZone.setDragController(dragController);
771        deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
772
773        dragController.setDragScoller(workspace);
774        dragController.setDragListener(deleteZone);
775        dragController.setScrollView(dragLayer);
776        dragController.setMoveTarget(workspace);
777
778        // The order here is bottom to top.
779        dragController.addDropTarget(workspace);
780        dragController.addDropTarget(deleteZone);
781    }
782
783    @SuppressWarnings({"UnusedDeclaration"})
784    public void previousScreen(View v) {
785        if (!isAllAppsVisible()) {
786            mWorkspace.scrollLeft();
787        }
788    }
789
790    @SuppressWarnings({"UnusedDeclaration"})
791    public void nextScreen(View v) {
792        if (!isAllAppsVisible()) {
793            mWorkspace.scrollRight();
794        }
795    }
796
797    @SuppressWarnings({"UnusedDeclaration"})
798    public void launchHotSeat(View v) {
799        if (isAllAppsVisible()) return;
800
801        int index = -1;
802        if (v.getId() == R.id.hotseat_left) {
803            index = 0;
804        } else if (v.getId() == R.id.hotseat_right) {
805            index = 1;
806        }
807
808        // reload these every tap; you never know when they might change
809        loadHotseats();
810        if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
811            Intent intent = mHotseats[index];
812            startActivitySafely(
813                mHotseats[index],
814                "hotseat"
815            );
816        }
817    }
818
819    /**
820     * Creates a view representing a shortcut.
821     *
822     * @param info The data structure describing the shortcut.
823     *
824     * @return A View inflated from R.layout.application.
825     */
826    View createShortcut(ShortcutInfo info) {
827        return createShortcut(R.layout.application,
828                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
829    }
830
831    /**
832     * Creates a view representing a shortcut inflated from the specified resource.
833     *
834     * @param layoutResId The id of the XML layout used to create the shortcut.
835     * @param parent The group the shortcut belongs to.
836     * @param info The data structure describing the shortcut.
837     *
838     * @return A View inflated from layoutResId.
839     */
840    View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
841        TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
842
843        favorite.setCompoundDrawablesWithIntrinsicBounds(null,
844                new FastBitmapDrawable(info.getIcon(mIconCache)),
845                null, null);
846        favorite.setText(info.title);
847        favorite.setTag(info);
848        favorite.setOnClickListener(this);
849
850        return favorite;
851    }
852
853    /**
854     * Add an application shortcut to the workspace.
855     *
856     * @param data The intent describing the application.
857     * @param cellInfo The position on screen where to create the shortcut.
858     */
859    void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
860        cellInfo.screen = mWorkspace.getCurrentScreen();
861        if (!findSingleSlot(cellInfo)) return;
862
863        final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
864                data, context);
865
866        if (info != null) {
867            info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
868                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
869            info.container = ItemInfo.NO_ID;
870            mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
871        } else {
872            Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
873        }
874    }
875
876    /**
877     * Add a shortcut to the workspace.
878     *
879     * @param data The intent describing the shortcut.
880     * @param cellInfo The position on screen where to create the shortcut.
881     */
882    private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
883        cellInfo.screen = mWorkspace.getCurrentScreen();
884        if (!findSingleSlot(cellInfo)) return;
885
886        final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
887
888        if (!mRestoring) {
889            final View view = createShortcut(info);
890            mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
891                    isWorkspaceLocked());
892        }
893    }
894
895
896    /**
897     * Add a widget to the workspace.
898     *
899     * @param data The intent describing the appWidgetId.
900     * @param cellInfo The position on screen where to create the widget.
901     */
902    private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
903        Bundle extras = data.getExtras();
904        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
905
906        if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
907
908        AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
909
910        // Calculate the grid spans needed to fit this widget
911        CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
912        int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
913
914        // Try finding open space on Launcher screen
915        final int[] xy = mCellCoordinates;
916        if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
917            if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
918            return;
919        }
920
921        // Build Launcher-specific widget info and save to database
922        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
923        launcherInfo.spanX = spans[0];
924        launcherInfo.spanY = spans[1];
925
926        LauncherModel.addItemToDatabase(this, launcherInfo,
927                LauncherSettings.Favorites.CONTAINER_DESKTOP,
928                mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
929
930        if (!mRestoring) {
931            mDesktopItems.add(launcherInfo);
932
933            // Perform actual inflation because we're live
934            launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
935
936            launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
937            launcherInfo.hostView.setTag(launcherInfo);
938
939            mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
940                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
941        }
942    }
943
944    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
945        mDesktopItems.remove(launcherInfo);
946        launcherInfo.hostView = null;
947    }
948
949    public LauncherAppWidgetHost getAppWidgetHost() {
950        return mAppWidgetHost;
951    }
952
953    void closeSystemDialogs() {
954        getWindow().closeAllPanels();
955
956        try {
957            dismissDialog(DIALOG_CREATE_SHORTCUT);
958            // Unlock the workspace if the dialog was showing
959        } catch (Exception e) {
960            // An exception is thrown if the dialog is not visible, which is fine
961        }
962
963        try {
964            dismissDialog(DIALOG_RENAME_FOLDER);
965            // Unlock the workspace if the dialog was showing
966        } catch (Exception e) {
967            // An exception is thrown if the dialog is not visible, which is fine
968        }
969
970        // Whatever we were doing is hereby canceled.
971        mWaitingForResult = false;
972    }
973
974    @Override
975    protected void onNewIntent(Intent intent) {
976        super.onNewIntent(intent);
977
978        // Close the menu
979        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
980            // also will cancel mWaitingForResult.
981            closeSystemDialogs();
982
983            boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
984                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
985            boolean allAppsVisible = isAllAppsVisible();
986            if (!mWorkspace.isDefaultScreenShowing()) {
987                mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
988            }
989            closeAllApps(alreadyOnHome && allAppsVisible);
990
991            final View v = getWindow().peekDecorView();
992            if (v != null && v.getWindowToken() != null) {
993                InputMethodManager imm = (InputMethodManager)getSystemService(
994                        INPUT_METHOD_SERVICE);
995                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
996            }
997        }
998    }
999
1000    @Override
1001    protected void onRestoreInstanceState(Bundle savedInstanceState) {
1002        // Do not call super here
1003        mSavedInstanceState = savedInstanceState;
1004    }
1005
1006    @Override
1007    protected void onSaveInstanceState(Bundle outState) {
1008        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1009
1010        final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1011        if (folders.size() > 0) {
1012            final int count = folders.size();
1013            long[] ids = new long[count];
1014            for (int i = 0; i < count; i++) {
1015                final FolderInfo info = folders.get(i).getInfo();
1016                ids[i] = info.id;
1017            }
1018            outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1019        } else {
1020            super.onSaveInstanceState(outState);
1021        }
1022
1023        // TODO should not do this if the drawer is currently closing.
1024        if (isAllAppsVisible()) {
1025            outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
1026        }
1027
1028        if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1029            final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1030            final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1031
1032            outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1033            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1034            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1035            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1036            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1037            outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1038            outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1039            outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1040                   layout.getOccupiedCells());
1041        }
1042
1043        if (mFolderInfo != null && mWaitingForResult) {
1044            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1045            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1046        }
1047    }
1048
1049    @Override
1050    public void onDestroy() {
1051        super.onDestroy();
1052
1053        try {
1054            mAppWidgetHost.stopListening();
1055        } catch (NullPointerException ex) {
1056            Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
1057        }
1058
1059        TextKeyListener.getInstance().release();
1060
1061        mModel.stopLoader();
1062
1063        unbindDesktopItems();
1064
1065        getContentResolver().unregisterContentObserver(mWidgetObserver);
1066
1067        dismissPreview(mPreviousView);
1068        dismissPreview(mNextView);
1069
1070        unregisterReceiver(mCloseSystemDialogsReceiver);
1071    }
1072
1073    @Override
1074    public void startActivityForResult(Intent intent, int requestCode) {
1075        if (requestCode >= 0) mWaitingForResult = true;
1076        super.startActivityForResult(intent, requestCode);
1077    }
1078
1079    @Override
1080    public void startSearch(String initialQuery, boolean selectInitialQuery,
1081            Bundle appSearchData, boolean globalSearch) {
1082
1083        closeAllApps(true);
1084
1085        if (initialQuery == null) {
1086            // Use any text typed in the launcher as the initial query
1087            initialQuery = getTypedText();
1088            clearTypedText();
1089        }
1090        if (appSearchData == null) {
1091            appSearchData = new Bundle();
1092            appSearchData.putString(Search.SOURCE, "launcher-search");
1093        }
1094
1095        final SearchManager searchManager =
1096                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1097        searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
1098            appSearchData, globalSearch);
1099    }
1100
1101    @Override
1102    public boolean onCreateOptionsMenu(Menu menu) {
1103        if (isWorkspaceLocked()) {
1104            return false;
1105        }
1106
1107        super.onCreateOptionsMenu(menu);
1108
1109        menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1110                .setIcon(android.R.drawable.ic_menu_add)
1111                .setAlphabeticShortcut('A');
1112        menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1113                 .setIcon(android.R.drawable.ic_menu_gallery)
1114                 .setAlphabeticShortcut('W');
1115        menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1116                .setIcon(android.R.drawable.ic_search_category_default)
1117                .setAlphabeticShortcut(SearchManager.MENU_KEY);
1118        menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1119                .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1120                .setAlphabeticShortcut('N');
1121
1122        final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1123        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1124                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1125
1126        menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1127                .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1128                .setIntent(settings);
1129
1130        return true;
1131    }
1132
1133    @Override
1134    public boolean onPrepareOptionsMenu(Menu menu) {
1135        super.onPrepareOptionsMenu(menu);
1136
1137        // If all apps is animating, don't show the menu, because we don't know
1138        // which one to show.
1139        if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1140            return false;
1141        }
1142
1143        // Only show the add and wallpaper options when we're not in all apps.
1144        boolean visible = !mAllAppsGrid.isOpaque();
1145        menu.setGroupVisible(MENU_GROUP_ADD, visible);
1146        menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1147
1148        // Disable add if the workspace is full.
1149        if (visible) {
1150            mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1151            menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1152        }
1153
1154        return true;
1155    }
1156
1157    @Override
1158    public boolean onOptionsItemSelected(MenuItem item) {
1159        switch (item.getItemId()) {
1160            case MENU_ADD:
1161                addItems();
1162                return true;
1163            case MENU_WALLPAPER_SETTINGS:
1164                startWallpaper();
1165                return true;
1166            case MENU_SEARCH:
1167                onSearchRequested();
1168                return true;
1169            case MENU_NOTIFICATIONS:
1170                showNotifications();
1171                return true;
1172        }
1173
1174        return super.onOptionsItemSelected(item);
1175    }
1176
1177    /**
1178     * Indicates that we want global search for this activity by setting the globalSearch
1179     * argument for {@link #startSearch} to true.
1180     */
1181
1182    @Override
1183    public boolean onSearchRequested() {
1184        startSearch(null, false, null, true);
1185        return true;
1186    }
1187
1188    public boolean isWorkspaceLocked() {
1189        return mWorkspaceLoading || mWaitingForResult;
1190    }
1191
1192    private void addItems() {
1193        closeAllApps(true);
1194        showAddDialog(mMenuAddInfo);
1195    }
1196
1197    void addAppWidget(Intent data) {
1198        // TODO: catch bad widget exception when sent
1199        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
1200        AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1201
1202        if (appWidget.configure != null) {
1203            // Launch over to configure widget, if needed
1204            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1205            intent.setComponent(appWidget.configure);
1206            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1207
1208            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
1209        } else {
1210            // Otherwise just add it
1211            onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1212        }
1213    }
1214
1215    void processShortcut(Intent intent) {
1216        // Handle case where user selected "Applications"
1217        String applicationName = getResources().getString(R.string.group_applications);
1218        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1219
1220        if (applicationName != null && applicationName.equals(shortcutName)) {
1221            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1222            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1223
1224            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1225            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
1226            startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
1227        } else {
1228            startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
1229        }
1230    }
1231
1232    void addLiveFolder(Intent intent) {
1233        // Handle case where user selected "Folder"
1234        String folderName = getResources().getString(R.string.group_folder);
1235        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1236
1237        if (folderName != null && folderName.equals(shortcutName)) {
1238            addFolder();
1239        } else {
1240            startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
1241        }
1242    }
1243
1244    void addFolder() {
1245        UserFolderInfo folderInfo = new UserFolderInfo();
1246        folderInfo.title = getText(R.string.folder_name);
1247
1248        CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1249        cellInfo.screen = mWorkspace.getCurrentScreen();
1250        if (!findSingleSlot(cellInfo)) return;
1251
1252        // Update the model
1253        LauncherModel.addItemToDatabase(this, folderInfo,
1254                LauncherSettings.Favorites.CONTAINER_DESKTOP,
1255                mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
1256        sFolders.put(folderInfo.id, folderInfo);
1257
1258        // Create the view
1259        FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1260                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1261        mWorkspace.addInCurrentScreen(newFolder,
1262                cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
1263    }
1264
1265    void removeFolder(FolderInfo folder) {
1266        sFolders.remove(folder.id);
1267    }
1268
1269    private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
1270        cellInfo.screen = mWorkspace.getCurrentScreen();
1271        if (!findSingleSlot(cellInfo)) return;
1272
1273        final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1274
1275        if (!mRestoring) {
1276            final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1277                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
1278            mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1279                    isWorkspaceLocked());
1280        }
1281    }
1282
1283    static LiveFolderInfo addLiveFolder(Context context, Intent data,
1284            CellLayout.CellInfo cellInfo, boolean notify) {
1285
1286        Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1287        String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1288
1289        Drawable icon = null;
1290        Intent.ShortcutIconResource iconResource = null;
1291
1292        Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1293        if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1294            try {
1295                iconResource = (Intent.ShortcutIconResource) extra;
1296                final PackageManager packageManager = context.getPackageManager();
1297                Resources resources = packageManager.getResourcesForApplication(
1298                        iconResource.packageName);
1299                final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1300                icon = resources.getDrawable(id);
1301            } catch (Exception e) {
1302                Log.w(TAG, "Could not load live folder icon: " + extra);
1303            }
1304        }
1305
1306        if (icon == null) {
1307            icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1308        }
1309
1310        final LiveFolderInfo info = new LiveFolderInfo();
1311        info.icon = Utilities.createIconBitmap(icon, context);
1312        info.title = name;
1313        info.iconResource = iconResource;
1314        info.uri = data.getData();
1315        info.baseIntent = baseIntent;
1316        info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1317                LiveFolders.DISPLAY_MODE_GRID);
1318
1319        LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1320                cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1321        sFolders.put(info.id, info);
1322
1323        return info;
1324    }
1325
1326    private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1327        final int[] xy = new int[2];
1328        if (findSlot(cellInfo, xy, 1, 1)) {
1329            cellInfo.cellX = xy[0];
1330            cellInfo.cellY = xy[1];
1331            return true;
1332        }
1333        return false;
1334    }
1335
1336    private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1337        if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1338            boolean[] occupied = mSavedState != null ?
1339                    mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1340            cellInfo = mWorkspace.findAllVacantCells(occupied);
1341            if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1342                Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1343                return false;
1344            }
1345        }
1346        return true;
1347    }
1348
1349    private void showNotifications() {
1350        final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1351        if (statusBar != null) {
1352            statusBar.expand();
1353        }
1354    }
1355
1356    private void startWallpaper() {
1357        closeAllApps(true);
1358        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1359        Intent chooser = Intent.createChooser(pickWallpaper,
1360                getText(R.string.chooser_wallpaper));
1361        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1362        //       Removed in Eclair MR1
1363//        WallpaperManager wm = (WallpaperManager)
1364//                getSystemService(Context.WALLPAPER_SERVICE);
1365//        WallpaperInfo wi = wm.getWallpaperInfo();
1366//        if (wi != null && wi.getSettingsActivity() != null) {
1367//            LabeledIntent li = new LabeledIntent(getPackageName(),
1368//                    R.string.configure_wallpaper, 0);
1369//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1370//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1371//        }
1372        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
1373    }
1374
1375    /**
1376     * Registers various content observers. The current implementation registers
1377     * only a favorites observer to keep track of the favorites applications.
1378     */
1379    private void registerContentObservers() {
1380        ContentResolver resolver = getContentResolver();
1381        resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1382                true, mWidgetObserver);
1383    }
1384
1385    @Override
1386    public boolean dispatchKeyEvent(KeyEvent event) {
1387        if (event.getAction() == KeyEvent.ACTION_DOWN) {
1388            switch (event.getKeyCode()) {
1389                case KeyEvent.KEYCODE_HOME:
1390                    return true;
1391                case KeyEvent.KEYCODE_VOLUME_DOWN:
1392                    if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
1393                        dumpState();
1394                        return true;
1395                    }
1396                    break;
1397            }
1398        } else if (event.getAction() == KeyEvent.ACTION_UP) {
1399            switch (event.getKeyCode()) {
1400                case KeyEvent.KEYCODE_HOME:
1401                    return true;
1402            }
1403        }
1404
1405        return super.dispatchKeyEvent(event);
1406    }
1407
1408    @Override
1409    public void onBackPressed() {
1410        if (isAllAppsVisible()) {
1411            closeAllApps(true);
1412        } else {
1413            closeFolder();
1414        }
1415        dismissPreview(mPreviousView);
1416        dismissPreview(mNextView);
1417    }
1418
1419    private void closeFolder() {
1420        Folder folder = mWorkspace.getOpenFolder();
1421        if (folder != null) {
1422            closeFolder(folder);
1423        }
1424    }
1425
1426    void closeFolder(Folder folder) {
1427        folder.getInfo().opened = false;
1428        ViewGroup parent = (ViewGroup) folder.getParent();
1429        if (parent != null) {
1430            parent.removeView(folder);
1431            if (folder instanceof DropTarget) {
1432                // Live folders aren't DropTargets.
1433                mDragController.removeDropTarget((DropTarget)folder);
1434            }
1435        }
1436        folder.onClose();
1437    }
1438
1439    /**
1440     * Re-listen when widgets are reset.
1441     */
1442    private void onAppWidgetReset() {
1443        mAppWidgetHost.startListening();
1444    }
1445
1446    /**
1447     * Go through the and disconnect any of the callbacks in the drawables and the views or we
1448     * leak the previous Home screen on orientation change.
1449     */
1450    private void unbindDesktopItems() {
1451        for (ItemInfo item: mDesktopItems) {
1452            item.unbind();
1453        }
1454    }
1455
1456    /**
1457     * Launches the intent referred by the clicked shortcut.
1458     *
1459     * @param v The view representing the clicked shortcut.
1460     */
1461    public void onClick(View v) {
1462        Object tag = v.getTag();
1463        if (tag instanceof ShortcutInfo) {
1464            // Open shortcut
1465            final Intent intent = ((ShortcutInfo) tag).intent;
1466            int[] pos = new int[2];
1467            v.getLocationOnScreen(pos);
1468            intent.setSourceBounds(new Rect(pos[0], pos[1],
1469                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
1470            startActivitySafely(intent, tag);
1471        } else if (tag instanceof FolderInfo) {
1472            handleFolderClick((FolderInfo) tag);
1473        } else if (v == mHandleView) {
1474            if (isAllAppsVisible()) {
1475                closeAllApps(true);
1476            } else {
1477                showAllApps(true);
1478            }
1479        }
1480    }
1481
1482    void startActivitySafely(Intent intent, Object tag) {
1483        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1484        try {
1485            startActivity(intent);
1486        } catch (ActivityNotFoundException e) {
1487            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1488            Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1489        } catch (SecurityException e) {
1490            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1491            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1492                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1493                    "or use the exported attribute for this activity. "
1494                    + "tag="+ tag + " intent=" + intent, e);
1495        }
1496    }
1497
1498    void startActivityForResultSafely(Intent intent, int requestCode) {
1499        try {
1500            startActivityForResult(intent, requestCode);
1501        } catch (ActivityNotFoundException e) {
1502            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1503        } catch (SecurityException e) {
1504            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1505            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1506                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1507                    "or use the exported attribute for this activity.", e);
1508        }
1509    }
1510
1511    private void handleFolderClick(FolderInfo folderInfo) {
1512        if (!folderInfo.opened) {
1513            // Close any open folder
1514            closeFolder();
1515            // Open the requested folder
1516            openFolder(folderInfo);
1517        } else {
1518            // Find the open folder...
1519            Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1520            int folderScreen;
1521            if (openFolder != null) {
1522                folderScreen = mWorkspace.getScreenForView(openFolder);
1523                // .. and close it
1524                closeFolder(openFolder);
1525                if (folderScreen != mWorkspace.getCurrentScreen()) {
1526                    // Close any folder open on the current screen
1527                    closeFolder();
1528                    // Pull the folder onto this screen
1529                    openFolder(folderInfo);
1530                }
1531            }
1532        }
1533    }
1534
1535    /**
1536     * Opens the user fodler described by the specified tag. The opening of the folder
1537     * is animated relative to the specified View. If the View is null, no animation
1538     * is played.
1539     *
1540     * @param folderInfo The FolderInfo describing the folder to open.
1541     */
1542    private void openFolder(FolderInfo folderInfo) {
1543        Folder openFolder;
1544
1545        if (folderInfo instanceof UserFolderInfo) {
1546            openFolder = UserFolder.fromXml(this);
1547        } else if (folderInfo instanceof LiveFolderInfo) {
1548            openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
1549        } else {
1550            return;
1551        }
1552
1553        openFolder.setDragController(mDragController);
1554        openFolder.setLauncher(this);
1555
1556        openFolder.bind(folderInfo);
1557        folderInfo.opened = true;
1558
1559        mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1560        openFolder.onOpen();
1561    }
1562
1563    public boolean onLongClick(View v) {
1564        switch (v.getId()) {
1565            case R.id.previous_screen:
1566                if (!isAllAppsVisible()) {
1567                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1568                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1569                    showPreviews(v);
1570                }
1571                return true;
1572            case R.id.next_screen:
1573                if (!isAllAppsVisible()) {
1574                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1575                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1576                    showPreviews(v);
1577                }
1578                return true;
1579            case R.id.all_apps_button:
1580                if (!isAllAppsVisible()) {
1581                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1582                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1583                    showPreviews(v);
1584                }
1585                return true;
1586        }
1587
1588        if (isWorkspaceLocked()) {
1589            return false;
1590        }
1591
1592        if (!(v instanceof CellLayout)) {
1593            v = (View) v.getParent();
1594        }
1595
1596        CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1597
1598        // This happens when long clicking an item with the dpad/trackball
1599        if (cellInfo == null) {
1600            return true;
1601        }
1602
1603        if (mWorkspace.allowLongPress()) {
1604            if (cellInfo.cell == null) {
1605                if (cellInfo.valid) {
1606                    // User long pressed on empty space
1607                    mWorkspace.setAllowLongPress(false);
1608                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1609                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1610                    showAddDialog(cellInfo);
1611                }
1612            } else {
1613                if (!(cellInfo.cell instanceof Folder)) {
1614                    // User long pressed on an item
1615                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1616                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1617                    mWorkspace.startDrag(cellInfo);
1618                }
1619            }
1620        }
1621        return true;
1622    }
1623
1624    @SuppressWarnings({"unchecked"})
1625    private void dismissPreview(final View v) {
1626        final PopupWindow window = (PopupWindow) v.getTag();
1627        if (window != null) {
1628            window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1629                public void onDismiss() {
1630                    ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1631                    int count = group.getChildCount();
1632                    for (int i = 0; i < count; i++) {
1633                        ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1634                    }
1635                    ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1636                    for (Bitmap bitmap : bitmaps) bitmap.recycle();
1637
1638                    v.setTag(R.id.workspace, null);
1639                    v.setTag(R.id.icon, null);
1640                    window.setOnDismissListener(null);
1641                }
1642            });
1643            window.dismiss();
1644        }
1645        v.setTag(null);
1646    }
1647
1648    private void showPreviews(View anchor) {
1649        showPreviews(anchor, 0, mWorkspace.getChildCount());
1650    }
1651
1652    private void showPreviews(final View anchor, int start, int end) {
1653        final Resources resources = getResources();
1654        final Workspace workspace = mWorkspace;
1655
1656        CellLayout cell = ((CellLayout) workspace.getChildAt(start));
1657
1658        float max = workspace.getChildCount();
1659
1660        final Rect r = new Rect();
1661        resources.getDrawable(R.drawable.preview_background).getPadding(r);
1662        int extraW = (int) ((r.left + r.right) * max);
1663        int extraH = r.top + r.bottom;
1664
1665        int aW = cell.getWidth() - extraW;
1666        float w = aW / max;
1667
1668        int width = cell.getWidth();
1669        int height = cell.getHeight();
1670        int x = cell.getLeftPadding();
1671        int y = cell.getTopPadding();
1672        width -= (x + cell.getRightPadding());
1673        height -= (y + cell.getBottomPadding());
1674
1675        float scale = w / width;
1676
1677        int count = end - start;
1678
1679        final float sWidth = width * scale;
1680        float sHeight = height * scale;
1681
1682        LinearLayout preview = new LinearLayout(this);
1683
1684        PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1685        ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
1686
1687        for (int i = start; i < end; i++) {
1688            ImageView image = new ImageView(this);
1689            cell = (CellLayout) workspace.getChildAt(i);
1690
1691            final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1692                    Bitmap.Config.ARGB_8888);
1693
1694            final Canvas c = new Canvas(bitmap);
1695            c.scale(scale, scale);
1696            c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1697            cell.dispatchDraw(c);
1698
1699            image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
1700            image.setImageBitmap(bitmap);
1701            image.setTag(i);
1702            image.setOnClickListener(handler);
1703            image.setOnFocusChangeListener(handler);
1704            image.setFocusable(true);
1705            if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
1706
1707            preview.addView(image,
1708                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1709
1710            bitmaps.add(bitmap);
1711        }
1712
1713        final PopupWindow p = new PopupWindow(this);
1714        p.setContentView(preview);
1715        p.setWidth((int) (sWidth * count + extraW));
1716        p.setHeight((int) (sHeight + extraH));
1717        p.setAnimationStyle(R.style.AnimationPreview);
1718        p.setOutsideTouchable(true);
1719        p.setFocusable(true);
1720        p.setBackgroundDrawable(new ColorDrawable(0));
1721        p.showAsDropDown(anchor, 0, 0);
1722
1723        p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1724            public void onDismiss() {
1725                dismissPreview(anchor);
1726            }
1727        });
1728
1729        anchor.setTag(p);
1730        anchor.setTag(R.id.workspace, preview);
1731        anchor.setTag(R.id.icon, bitmaps);
1732    }
1733
1734    class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
1735        private final View mAnchor;
1736
1737        public PreviewTouchHandler(View anchor) {
1738            mAnchor = anchor;
1739        }
1740
1741        public void onClick(View v) {
1742            mWorkspace.snapToScreen((Integer) v.getTag());
1743            v.post(this);
1744        }
1745
1746        public void run() {
1747            dismissPreview(mAnchor);
1748        }
1749
1750        public void onFocusChange(View v, boolean hasFocus) {
1751            if (hasFocus) {
1752                mWorkspace.snapToScreen((Integer) v.getTag());
1753            }
1754        }
1755    }
1756
1757    Workspace getWorkspace() {
1758        return mWorkspace;
1759    }
1760
1761    @Override
1762    protected Dialog onCreateDialog(int id) {
1763        switch (id) {
1764            case DIALOG_CREATE_SHORTCUT:
1765                return new CreateShortcut().createDialog();
1766            case DIALOG_RENAME_FOLDER:
1767                return new RenameFolder().createDialog();
1768        }
1769
1770        return super.onCreateDialog(id);
1771    }
1772
1773    @Override
1774    protected void onPrepareDialog(int id, Dialog dialog) {
1775        switch (id) {
1776            case DIALOG_CREATE_SHORTCUT:
1777                break;
1778            case DIALOG_RENAME_FOLDER:
1779                if (mFolderInfo != null) {
1780                    EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1781                    final CharSequence text = mFolderInfo.title;
1782                    input.setText(text);
1783                    input.setSelection(0, text.length());
1784                }
1785                break;
1786        }
1787    }
1788
1789    void showRenameDialog(FolderInfo info) {
1790        mFolderInfo = info;
1791        mWaitingForResult = true;
1792        showDialog(DIALOG_RENAME_FOLDER);
1793    }
1794
1795    private void showAddDialog(CellLayout.CellInfo cellInfo) {
1796        mAddItemCellInfo = cellInfo;
1797        mWaitingForResult = true;
1798        showDialog(DIALOG_CREATE_SHORTCUT);
1799    }
1800
1801    private void pickShortcut() {
1802        Bundle bundle = new Bundle();
1803
1804        ArrayList<String> shortcutNames = new ArrayList<String>();
1805        shortcutNames.add(getString(R.string.group_applications));
1806        bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1807
1808        ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1809        shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1810                        R.drawable.ic_launcher_application));
1811        bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1812
1813        Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1814        pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1815        pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
1816        pickIntent.putExtras(bundle);
1817
1818        startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
1819    }
1820
1821    private class RenameFolder {
1822        private EditText mInput;
1823
1824        Dialog createDialog() {
1825            final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1826            mInput = (EditText) layout.findViewById(R.id.folder_name);
1827
1828            AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1829            builder.setIcon(0);
1830            builder.setTitle(getString(R.string.rename_folder_title));
1831            builder.setCancelable(true);
1832            builder.setOnCancelListener(new Dialog.OnCancelListener() {
1833                public void onCancel(DialogInterface dialog) {
1834                    cleanup();
1835                }
1836            });
1837            builder.setNegativeButton(getString(R.string.cancel_action),
1838                new Dialog.OnClickListener() {
1839                    public void onClick(DialogInterface dialog, int which) {
1840                        cleanup();
1841                    }
1842                }
1843            );
1844            builder.setPositiveButton(getString(R.string.rename_action),
1845                new Dialog.OnClickListener() {
1846                    public void onClick(DialogInterface dialog, int which) {
1847                        changeFolderName();
1848                    }
1849                }
1850            );
1851            builder.setView(layout);
1852
1853            final AlertDialog dialog = builder.create();
1854            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1855                public void onShow(DialogInterface dialog) {
1856                    mWaitingForResult = true;
1857                    mInput.requestFocus();
1858                    InputMethodManager inputManager = (InputMethodManager)
1859                            getSystemService(Context.INPUT_METHOD_SERVICE);
1860                    inputManager.showSoftInput(mInput, 0);
1861                }
1862            });
1863
1864            return dialog;
1865        }
1866
1867        private void changeFolderName() {
1868            final String name = mInput.getText().toString();
1869            if (!TextUtils.isEmpty(name)) {
1870                // Make sure we have the right folder info
1871                mFolderInfo = sFolders.get(mFolderInfo.id);
1872                mFolderInfo.title = name;
1873                LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1874
1875                if (mWorkspaceLoading) {
1876                    lockAllApps();
1877                    mModel.startLoader(Launcher.this, false);
1878                } else {
1879                    final FolderIcon folderIcon = (FolderIcon)
1880                            mWorkspace.getViewForTag(mFolderInfo);
1881                    if (folderIcon != null) {
1882                        folderIcon.setText(name);
1883                        getWorkspace().requestLayout();
1884                    } else {
1885                        lockAllApps();
1886                        mWorkspaceLoading = true;
1887                        mModel.startLoader(Launcher.this, false);
1888                    }
1889                }
1890            }
1891            cleanup();
1892        }
1893
1894        private void cleanup() {
1895            dismissDialog(DIALOG_RENAME_FOLDER);
1896            mWaitingForResult = false;
1897            mFolderInfo = null;
1898        }
1899    }
1900
1901    // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1902    public boolean isAllAppsVisible() {
1903        return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
1904    }
1905
1906    // AllAppsView.Watcher
1907    public void zoomed(float zoom) {
1908        if (zoom == 1.0f) {
1909            mWorkspace.setVisibility(View.GONE);
1910        }
1911    }
1912
1913    void showAllApps(boolean animated) {
1914        mAllAppsGrid.zoom(1.0f, animated);
1915
1916        ((View) mAllAppsGrid).setFocusable(true);
1917        ((View) mAllAppsGrid).requestFocus();
1918
1919        // TODO: fade these two too
1920        mDeleteZone.setVisibility(View.GONE);
1921    }
1922
1923    /**
1924     * Things to test when changing this code.
1925     *   - Home from workspace
1926     *          - from center screen
1927     *          - from other screens
1928     *   - Home from all apps
1929     *          - from center screen
1930     *          - from other screens
1931     *   - Back from all apps
1932     *          - from center screen
1933     *          - from other screens
1934     *   - Launch app from workspace and quit
1935     *          - with back
1936     *          - with home
1937     *   - Launch app from all apps and quit
1938     *          - with back
1939     *          - with home
1940     *   - Go to a screen that's not the default, then all
1941     *     apps, and launch and app, and go back
1942     *          - with back
1943     *          -with home
1944     *   - On workspace, long press power and go back
1945     *          - with back
1946     *          - with home
1947     *   - On all apps, long press power and go back
1948     *          - with back
1949     *          - with home
1950     *   - On workspace, power off
1951     *   - On all apps, power off
1952     *   - Launch an app and turn off the screen while in that app
1953     *          - Go back with home key
1954     *          - Go back with back key  TODO: make this not go to workspace
1955     *          - From all apps
1956     *          - From workspace
1957     *   - Enter and exit car mode (becuase it causes an extra configuration changed)
1958     *          - From all apps
1959     *          - From the center workspace
1960     *          - From another workspace
1961     */
1962    void closeAllApps(boolean animated) {
1963        if (mAllAppsGrid.isVisible()) {
1964            mWorkspace.setVisibility(View.VISIBLE);
1965            mAllAppsGrid.zoom(0.0f, animated);
1966            ((View)mAllAppsGrid).setFocusable(false);
1967            mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1968        }
1969    }
1970
1971    void lockAllApps() {
1972        // TODO
1973    }
1974
1975    void unlockAllApps() {
1976        // TODO
1977    }
1978
1979    /**
1980     * Displays the shortcut creation dialog and launches, if necessary, the
1981     * appropriate activity.
1982     */
1983    private class CreateShortcut implements DialogInterface.OnClickListener,
1984            DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1985            DialogInterface.OnShowListener {
1986
1987        private AddAdapter mAdapter;
1988
1989        Dialog createDialog() {
1990            mAdapter = new AddAdapter(Launcher.this);
1991
1992            final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1993            builder.setTitle(getString(R.string.menu_item_add_item));
1994            builder.setAdapter(mAdapter, this);
1995
1996            builder.setInverseBackgroundForced(true);
1997
1998            AlertDialog dialog = builder.create();
1999            dialog.setOnCancelListener(this);
2000            dialog.setOnDismissListener(this);
2001            dialog.setOnShowListener(this);
2002
2003            return dialog;
2004        }
2005
2006        public void onCancel(DialogInterface dialog) {
2007            mWaitingForResult = false;
2008            cleanup();
2009        }
2010
2011        public void onDismiss(DialogInterface dialog) {
2012        }
2013
2014        private void cleanup() {
2015            try {
2016                dismissDialog(DIALOG_CREATE_SHORTCUT);
2017            } catch (Exception e) {
2018                // An exception is thrown if the dialog is not visible, which is fine
2019            }
2020        }
2021
2022        /**
2023         * Handle the action clicked in the "Add to home" dialog.
2024         */
2025        public void onClick(DialogInterface dialog, int which) {
2026            Resources res = getResources();
2027            cleanup();
2028
2029            switch (which) {
2030                case AddAdapter.ITEM_SHORTCUT: {
2031                    // Insert extra item to handle picking application
2032                    pickShortcut();
2033                    break;
2034                }
2035
2036                case AddAdapter.ITEM_APPWIDGET: {
2037                    int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
2038
2039                    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2040                    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2041                    // start the pick activity
2042                    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2043                    break;
2044                }
2045
2046                case AddAdapter.ITEM_LIVE_FOLDER: {
2047                    // Insert extra item to handle inserting folder
2048                    Bundle bundle = new Bundle();
2049
2050                    ArrayList<String> shortcutNames = new ArrayList<String>();
2051                    shortcutNames.add(res.getString(R.string.group_folder));
2052                    bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2053
2054                    ArrayList<ShortcutIconResource> shortcutIcons =
2055                            new ArrayList<ShortcutIconResource>();
2056                    shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2057                            R.drawable.ic_launcher_folder));
2058                    bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2059
2060                    Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2061                    pickIntent.putExtra(Intent.EXTRA_INTENT,
2062                            new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2063                    pickIntent.putExtra(Intent.EXTRA_TITLE,
2064                            getText(R.string.title_select_live_folder));
2065                    pickIntent.putExtras(bundle);
2066
2067                    startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2068                    break;
2069                }
2070
2071                case AddAdapter.ITEM_WALLPAPER: {
2072                    startWallpaper();
2073                    break;
2074                }
2075            }
2076        }
2077
2078        public void onShow(DialogInterface dialog) {
2079            mWaitingForResult = true;
2080        }
2081    }
2082
2083    /**
2084     * Receives notifications when applications are added/removed.
2085     */
2086    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2087        @Override
2088        public void onReceive(Context context, Intent intent) {
2089            closeSystemDialogs();
2090            String reason = intent.getStringExtra("reason");
2091            if (!"homekey".equals(reason)) {
2092                boolean animate = true;
2093                if (mPaused || "lock".equals(reason)) {
2094                    animate = false;
2095                }
2096                closeAllApps(animate);
2097            }
2098        }
2099    }
2100
2101    /**
2102     * Receives notifications whenever the appwidgets are reset.
2103     */
2104    private class AppWidgetResetObserver extends ContentObserver {
2105        public AppWidgetResetObserver() {
2106            super(new Handler());
2107        }
2108
2109        @Override
2110        public void onChange(boolean selfChange) {
2111            onAppWidgetReset();
2112        }
2113    }
2114
2115    /**
2116     * Implementation of the method from LauncherModel.Callbacks.
2117     */
2118    public int getCurrentWorkspaceScreen() {
2119        if (mWorkspace != null) {
2120            return mWorkspace.getCurrentScreen();
2121        } else {
2122            return SCREEN_COUNT / 2;
2123        }
2124    }
2125
2126    /**
2127     * Refreshes the shortcuts shown on the workspace.
2128     *
2129     * Implementation of the method from LauncherModel.Callbacks.
2130     */
2131    public void startBinding() {
2132        final Workspace workspace = mWorkspace;
2133        int count = workspace.getChildCount();
2134        for (int i = 0; i < count; i++) {
2135            // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
2136            ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2137        }
2138
2139        if (DEBUG_USER_INTERFACE) {
2140            android.widget.Button finishButton = new android.widget.Button(this);
2141            finishButton.setText("Finish");
2142            workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2143
2144            finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2145                public void onClick(View v) {
2146                    finish();
2147                }
2148            });
2149        }
2150    }
2151
2152    /**
2153     * Bind the items start-end from the list.
2154     *
2155     * Implementation of the method from LauncherModel.Callbacks.
2156     */
2157    public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2158
2159        final Workspace workspace = mWorkspace;
2160
2161        for (int i=start; i<end; i++) {
2162            final ItemInfo item = shortcuts.get(i);
2163            mDesktopItems.add(item);
2164            switch (item.itemType) {
2165                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2166                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2167                    final View shortcut = createShortcut((ShortcutInfo)item);
2168                    workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2169                            false);
2170                    break;
2171                case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2172                    final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2173                            (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2174                            (UserFolderInfo) item);
2175                    workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2176                            false);
2177                    break;
2178                case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2179                    final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2180                            R.layout.live_folder_icon, this,
2181                            (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2182                            (LiveFolderInfo) item);
2183                    workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2184                            false);
2185                    break;
2186            }
2187        }
2188
2189        workspace.requestLayout();
2190    }
2191
2192    /**
2193     * Implementation of the method from LauncherModel.Callbacks.
2194     */
2195    public void bindFolders(HashMap<Long, FolderInfo> folders) {
2196        sFolders.clear();
2197        sFolders.putAll(folders);
2198    }
2199
2200    /**
2201     * Add the views for a widget to the workspace.
2202     *
2203     * Implementation of the method from LauncherModel.Callbacks.
2204     */
2205    public void bindAppWidget(LauncherAppWidgetInfo item) {
2206        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2207        if (DEBUG_WIDGETS) {
2208            Log.d(TAG, "bindAppWidget: " + item);
2209        }
2210        final Workspace workspace = mWorkspace;
2211
2212        final int appWidgetId = item.appWidgetId;
2213        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2214        if (DEBUG_WIDGETS) {
2215            Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2216        }
2217
2218        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2219
2220        item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2221        item.hostView.setTag(item);
2222
2223        workspace.addInScreen(item.hostView, item.screen, item.cellX,
2224                item.cellY, item.spanX, item.spanY, false);
2225
2226        workspace.requestLayout();
2227
2228        mDesktopItems.add(item);
2229
2230        if (DEBUG_WIDGETS) {
2231            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2232                    + (SystemClock.uptimeMillis()-start) + "ms");
2233        }
2234    }
2235
2236    /**
2237     * Callback saying that there aren't any more items to bind.
2238     *
2239     * Implementation of the method from LauncherModel.Callbacks.
2240     */
2241    public void finishBindingItems() {
2242        if (mSavedState != null) {
2243            if (!mWorkspace.hasFocus()) {
2244                mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2245            }
2246
2247            final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2248            if (userFolders != null) {
2249                for (long folderId : userFolders) {
2250                    final FolderInfo info = sFolders.get(folderId);
2251                    if (info != null) {
2252                        openFolder(info);
2253                    }
2254                }
2255                final Folder openFolder = mWorkspace.getOpenFolder();
2256                if (openFolder != null) {
2257                    openFolder.requestFocus();
2258                }
2259            }
2260
2261            mSavedState = null;
2262        }
2263
2264        if (mSavedInstanceState != null) {
2265            super.onRestoreInstanceState(mSavedInstanceState);
2266            mSavedInstanceState = null;
2267        }
2268
2269        mWorkspaceLoading = false;
2270    }
2271
2272    /**
2273     * Add the icons for all apps.
2274     *
2275     * Implementation of the method from LauncherModel.Callbacks.
2276     */
2277    public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
2278        mAllAppsGrid.setApps(apps);
2279    }
2280
2281    /**
2282     * A package was installed.
2283     *
2284     * Implementation of the method from LauncherModel.Callbacks.
2285     */
2286    public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
2287        removeDialog(DIALOG_CREATE_SHORTCUT);
2288        mAllAppsGrid.addApps(apps);
2289    }
2290
2291    /**
2292     * A package was updated.
2293     *
2294     * Implementation of the method from LauncherModel.Callbacks.
2295     */
2296    public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
2297        removeDialog(DIALOG_CREATE_SHORTCUT);
2298        mWorkspace.updateShortcuts(apps);
2299        mAllAppsGrid.updateApps(apps);
2300    }
2301
2302    /**
2303     * A package was uninstalled.
2304     *
2305     * Implementation of the method from LauncherModel.Callbacks.
2306     */
2307    public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
2308        removeDialog(DIALOG_CREATE_SHORTCUT);
2309        if (permanent) {
2310            mWorkspace.removeItems(apps);
2311        }
2312        mAllAppsGrid.removeApps(apps);
2313    }
2314
2315    /**
2316     * Prints out out state for debugging.
2317     */
2318    public void dumpState() {
2319        Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
2320        Log.d(TAG, "mSavedState=" + mSavedState);
2321        Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2322        Log.d(TAG, "mRestoring=" + mRestoring);
2323        Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2324        Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2325        Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2326        Log.d(TAG, "sFolders.size=" + sFolders.size());
2327        mModel.dumpState();
2328        mAllAppsGrid.dumpState();
2329        Log.d(TAG, "END launcher2 dump state");
2330    }
2331}
2332