Launcher.java revision 240420f1980f6fe482057a37708045599b0109d6
1
2/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.launcher2;
19
20import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.AnimatorSet;
23import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
25import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
27import android.app.Activity;
28import android.app.ActivityManager;
29import android.app.SearchManager;
30import android.appwidget.AppWidgetHostView;
31import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
33import android.content.ActivityNotFoundException;
34import android.content.BroadcastReceiver;
35import android.content.ClipData;
36import android.content.ClipDescription;
37import android.content.ComponentCallbacks2;
38import android.content.ComponentName;
39import android.content.ContentResolver;
40import android.content.Context;
41import android.content.Intent;
42import android.content.IntentFilter;
43import android.content.SharedPreferences;
44import android.content.pm.ActivityInfo;
45import android.content.pm.PackageManager;
46import android.content.pm.PackageManager.NameNotFoundException;
47import android.content.res.Configuration;
48import android.content.res.Resources;
49import android.database.ContentObserver;
50import android.graphics.Rect;
51import android.graphics.drawable.Drawable;
52import android.net.Uri;
53import android.os.AsyncTask;
54import android.os.Bundle;
55import android.os.Environment;
56import android.os.Handler;
57import android.os.Message;
58import android.os.StrictMode;
59import android.os.SystemClock;
60import android.os.SystemProperties;
61import android.provider.Settings;
62import android.speech.RecognizerIntent;
63import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.method.TextKeyListener;
66import android.util.Log;
67import android.view.Display;
68import android.view.HapticFeedbackConstants;
69import android.view.KeyEvent;
70import android.view.LayoutInflater;
71import android.view.Menu;
72import android.view.MenuItem;
73import android.view.MotionEvent;
74import android.view.Surface;
75import android.view.View;
76import android.view.View.OnLongClickListener;
77import android.view.ViewGroup;
78import android.view.ViewTreeObserver;
79import android.view.ViewTreeObserver.OnGlobalLayoutListener;
80import android.view.WindowManager;
81import android.view.accessibility.AccessibilityEvent;
82import android.view.animation.AccelerateDecelerateInterpolator;
83import android.view.animation.AccelerateInterpolator;
84import android.view.animation.DecelerateInterpolator;
85import android.view.inputmethod.InputMethodManager;
86import android.widget.Advanceable;
87import android.widget.ImageView;
88import android.widget.TextView;
89import android.widget.Toast;
90
91import com.android.common.Search;
92import com.android.launcher.R;
93import com.android.launcher2.DropTarget.DragObject;
94
95import java.io.DataInputStream;
96import java.io.DataOutputStream;
97import java.io.FileDescriptor;
98import java.io.FileNotFoundException;
99import java.io.IOException;
100import java.io.PrintWriter;
101import java.util.ArrayList;
102import java.util.Collection;
103import java.util.Collections;
104import java.util.Comparator;
105import java.util.HashMap;
106import java.util.HashSet;
107import java.util.Set;
108
109/**
110 * Default launcher application.
111 */
112public final class Launcher extends Activity
113        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114                   AllAppsView.Watcher, View.OnTouchListener {
115    static final String TAG = "Launcher";
116    static final boolean LOGD = false;
117
118    static final boolean PROFILE_STARTUP = false;
119    static final boolean DEBUG_WIDGETS = false;
120    static final boolean DEBUG_STRICT_MODE = false;
121
122    private static final int MENU_GROUP_WALLPAPER = 1;
123    private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
124    private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
125    private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
126    private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
127
128    private static final int REQUEST_CREATE_SHORTCUT = 1;
129    private static final int REQUEST_CREATE_APPWIDGET = 5;
130    private static final int REQUEST_PICK_APPLICATION = 6;
131    private static final int REQUEST_PICK_SHORTCUT = 7;
132    private static final int REQUEST_PICK_APPWIDGET = 9;
133    private static final int REQUEST_PICK_WALLPAPER = 10;
134
135    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
136
137    static final int SCREEN_COUNT = 5;
138    static final int DEFAULT_SCREEN = 2;
139
140    private static final String PREFERENCES = "launcher.preferences";
141    static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
142
143    // Type: int
144    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
145    // Type: int
146    private static final String RUNTIME_STATE = "launcher.state";
147    // Type: int
148    private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
149    // Type: int
150    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
151    // Type: int
152    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
153    // Type: int
154    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
155    // Type: boolean
156    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
157    // Type: long
158    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
159
160    private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
161
162    /** The different states that Launcher can be in. */
163    private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
164    private State mState = State.WORKSPACE;
165    private AnimatorSet mStateAnimation;
166    private AnimatorSet mDividerAnimator;
167
168    static final int APPWIDGET_HOST_ID = 1024;
169    private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
170    private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
171    private static final int SHOW_CLING_DURATION = 550;
172    private static final int DISMISS_CLING_DURATION = 250;
173
174    private static final Object sLock = new Object();
175    private static int sScreen = DEFAULT_SCREEN;
176
177    // How long to wait before the new-shortcut animation automatically pans the workspace
178    private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
179
180    private final BroadcastReceiver mCloseSystemDialogsReceiver
181            = new CloseSystemDialogsIntentReceiver();
182    private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
184    private LayoutInflater mInflater;
185
186    private Workspace mWorkspace;
187    private View mQsbDivider;
188    private View mDockDivider;
189    private DragLayer mDragLayer;
190    private DragController mDragController;
191
192    private AppWidgetManager mAppWidgetManager;
193    private LauncherAppWidgetHost mAppWidgetHost;
194
195    private ItemInfo mPendingAddInfo = new ItemInfo();
196    private int[] mTmpAddItemCellCoordinates = new int[2];
197
198    private FolderInfo mFolderInfo;
199
200    private Hotseat mHotseat;
201    private View mAllAppsButton;
202
203    private SearchDropTargetBar mSearchDropTargetBar;
204    private AppsCustomizeTabHost mAppsCustomizeTabHost;
205    private AppsCustomizePagedView mAppsCustomizeContent;
206    private boolean mAutoAdvanceRunning = false;
207
208    private Bundle mSavedState;
209
210    private SpannableStringBuilder mDefaultKeySsb = null;
211
212    private boolean mWorkspaceLoading = true;
213
214    private boolean mPaused = true;
215    private boolean mRestoring;
216    private boolean mWaitingForResult;
217    private boolean mOnResumeNeedsLoad;
218
219    private Bundle mSavedInstanceState;
220
221    private LauncherModel mModel;
222    private IconCache mIconCache;
223    private boolean mUserPresent = true;
224    private boolean mVisible = false;
225    private boolean mAttached = false;
226
227    private static LocaleConfiguration sLocaleConfiguration = null;
228
229    private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
230
231    private Intent mAppMarketIntent = null;
232
233    // Related to the auto-advancing of widgets
234    private final int ADVANCE_MSG = 1;
235    private final int mAdvanceInterval = 20000;
236    private final int mAdvanceStagger = 250;
237    private long mAutoAdvanceSentTime;
238    private long mAutoAdvanceTimeLeft = -1;
239    private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
240        new HashMap<View, AppWidgetProviderInfo>();
241
242    // Determines how long to wait after a rotation before restoring the screen orientation to
243    // match the sensor state.
244    private final int mRestoreScreenOrientationDelay = 500;
245
246    // External icons saved in case of resource changes, orientation, etc.
247    private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
248    private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
249    private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
250
251    static final ArrayList<String> sDumpLogs = new ArrayList<String>();
252    PendingAddWidgetInfo mWidgetBeingConfigured = null;
253
254    // We only want to get the SharedPreferences once since it does an FS stat each time we get
255    // it from the context.
256    private SharedPreferences mSharedPrefs;
257
258    // Holds the page that we need to animate to, and the icon views that we need to animate up
259    // when we scroll to that page on resume.
260    private int mNewShortcutAnimatePage = -1;
261    private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
262
263    private BubbleTextView mWaitingForResume;
264
265    private Runnable mBuildLayersRunnable = new Runnable() {
266        public void run() {
267            if (mWorkspace != null) {
268                mWorkspace.buildPageHardwareLayers();
269            }
270        }
271    };
272
273    private static ArrayList<PendingAddArguments> sPendingAddList
274            = new ArrayList<PendingAddArguments>();
275
276    private static class PendingAddArguments {
277        int requestCode;
278        Intent intent;
279        long container;
280        int screen;
281        int cellX;
282        int cellY;
283    }
284
285    @Override
286    protected void onCreate(Bundle savedInstanceState) {
287        if (DEBUG_STRICT_MODE) {
288            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
289                    .detectDiskReads()
290                    .detectDiskWrites()
291                    .detectNetwork()   // or .detectAll() for all detectable problems
292                    .penaltyLog()
293                    .build());
294            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
295                    .detectLeakedSqlLiteObjects()
296                    .detectLeakedClosableObjects()
297                    .penaltyLog()
298                    .penaltyDeath()
299                    .build());
300        }
301
302        super.onCreate(savedInstanceState);
303        LauncherApplication app = ((LauncherApplication)getApplication());
304        mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
305                Context.MODE_PRIVATE);
306        mModel = app.setLauncher(this);
307        mIconCache = app.getIconCache();
308        mDragController = new DragController(this);
309        mInflater = getLayoutInflater();
310
311        mAppWidgetManager = AppWidgetManager.getInstance(this);
312        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
313        mAppWidgetHost.startListening();
314
315        if (PROFILE_STARTUP) {
316            android.os.Debug.startMethodTracing(
317                    Environment.getExternalStorageDirectory() + "/launcher");
318        }
319
320        checkForLocaleChange();
321        setContentView(R.layout.launcher);
322        setupViews();
323        showFirstRunWorkspaceCling();
324
325        registerContentObservers();
326
327        lockAllApps();
328
329        mSavedState = savedInstanceState;
330        restoreState(mSavedState);
331
332        // Update customization drawer _after_ restoring the states
333        if (mAppsCustomizeContent != null) {
334            mAppsCustomizeContent.onPackagesUpdated();
335        }
336
337        if (PROFILE_STARTUP) {
338            android.os.Debug.stopMethodTracing();
339        }
340
341        if (!mRestoring) {
342            mModel.startLoader(true);
343        }
344
345        if (!mModel.isAllAppsLoaded()) {
346            ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
347            mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
348        }
349
350        // For handling default keys
351        mDefaultKeySsb = new SpannableStringBuilder();
352        Selection.setSelection(mDefaultKeySsb, 0);
353
354        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
355        registerReceiver(mCloseSystemDialogsReceiver, filter);
356
357        boolean searchVisible = false;
358        boolean voiceVisible = false;
359        // If we have a saved version of these external icons, we load them up immediately
360        int coi = getCurrentOrientationIndexForGlobalIcons();
361        if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
362                sAppMarketIcon[coi] == null) {
363            updateAppMarketIcon();
364            searchVisible = updateGlobalSearchIcon();
365            voiceVisible = updateVoiceSearchIcon(searchVisible);
366        }
367        if (sGlobalSearchIcon[coi] != null) {
368             updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
369             searchVisible = true;
370        }
371        if (sVoiceSearchIcon[coi] != null) {
372            updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
373            voiceVisible = true;
374        }
375        if (sAppMarketIcon[coi] != null) {
376            updateAppMarketIcon(sAppMarketIcon[coi]);
377        }
378        mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
379
380        final String forceEnableRotation =
381                SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
382
383        // On large interfaces, we want the screen to auto-rotate based on the current orientation
384        if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
385            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
386        }
387    }
388
389    private void checkForLocaleChange() {
390        if (sLocaleConfiguration == null) {
391            new AsyncTask<Void, Void, LocaleConfiguration>() {
392                @Override
393                protected LocaleConfiguration doInBackground(Void... unused) {
394                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
395                    readConfiguration(Launcher.this, localeConfiguration);
396                    return localeConfiguration;
397                }
398
399                @Override
400                protected void onPostExecute(LocaleConfiguration result) {
401                    sLocaleConfiguration = result;
402                    checkForLocaleChange();  // recursive, but now with a locale configuration
403                }
404            }.execute();
405            return;
406        }
407
408        final Configuration configuration = getResources().getConfiguration();
409
410        final String previousLocale = sLocaleConfiguration.locale;
411        final String locale = configuration.locale.toString();
412
413        final int previousMcc = sLocaleConfiguration.mcc;
414        final int mcc = configuration.mcc;
415
416        final int previousMnc = sLocaleConfiguration.mnc;
417        final int mnc = configuration.mnc;
418
419        boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
420
421        if (localeChanged) {
422            sLocaleConfiguration.locale = locale;
423            sLocaleConfiguration.mcc = mcc;
424            sLocaleConfiguration.mnc = mnc;
425
426            mIconCache.flush();
427
428            final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
429            new Thread("WriteLocaleConfiguration") {
430                @Override
431                public void run() {
432                    writeConfiguration(Launcher.this, localeConfiguration);
433                }
434            }.start();
435        }
436    }
437
438    private static class LocaleConfiguration {
439        public String locale;
440        public int mcc = -1;
441        public int mnc = -1;
442    }
443
444    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
445        DataInputStream in = null;
446        try {
447            in = new DataInputStream(context.openFileInput(PREFERENCES));
448            configuration.locale = in.readUTF();
449            configuration.mcc = in.readInt();
450            configuration.mnc = in.readInt();
451        } catch (FileNotFoundException e) {
452            // Ignore
453        } catch (IOException e) {
454            // Ignore
455        } finally {
456            if (in != null) {
457                try {
458                    in.close();
459                } catch (IOException e) {
460                    // Ignore
461                }
462            }
463        }
464    }
465
466    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
467        DataOutputStream out = null;
468        try {
469            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
470            out.writeUTF(configuration.locale);
471            out.writeInt(configuration.mcc);
472            out.writeInt(configuration.mnc);
473            out.flush();
474        } catch (FileNotFoundException e) {
475            // Ignore
476        } catch (IOException e) {
477            //noinspection ResultOfMethodCallIgnored
478            context.getFileStreamPath(PREFERENCES).delete();
479        } finally {
480            if (out != null) {
481                try {
482                    out.close();
483                } catch (IOException e) {
484                    // Ignore
485                }
486            }
487        }
488    }
489
490    public DragLayer getDragLayer() {
491        return mDragLayer;
492    }
493
494    static int getScreen() {
495        synchronized (sLock) {
496            return sScreen;
497        }
498    }
499
500    static void setScreen(int screen) {
501        synchronized (sLock) {
502            sScreen = screen;
503        }
504    }
505
506    /**
507     * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
508     * a configuration step, this allows the proper animations to run after other transitions.
509     */
510    private boolean completeAdd(PendingAddArguments args) {
511        boolean result = false;
512        switch (args.requestCode) {
513            case REQUEST_PICK_APPLICATION:
514                completeAddApplication(args.intent, args.container, args.screen, args.cellX,
515                        args.cellY);
516                break;
517            case REQUEST_PICK_SHORTCUT:
518                processShortcut(args.intent);
519                break;
520            case REQUEST_CREATE_SHORTCUT:
521                completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
522                        args.cellY);
523                result = true;
524                break;
525            case REQUEST_PICK_APPWIDGET:
526                addAppWidgetFromPick(args.intent);
527                break;
528            case REQUEST_CREATE_APPWIDGET:
529                int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
530                completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
531                result = true;
532                break;
533            case REQUEST_PICK_WALLPAPER:
534                // We just wanted the activity result here so we can clear mWaitingForResult
535                break;
536        }
537        // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
538        // if you turned the screen off and then back while in All Apps, Launcher would not
539        // return to the workspace. Clearing mAddInfo.container here fixes this issue
540        resetAddInfo();
541        return result;
542    }
543
544    @Override
545    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
546        boolean delayExitSpringLoadedMode = false;
547        boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
548                requestCode == REQUEST_CREATE_APPWIDGET);
549        mWaitingForResult = false;
550
551        // We have special handling for widgets
552        if (isWidgetDrop) {
553            int appWidgetId = data != null ?
554                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555            completeTwoStageWidgetDrop(resultCode, appWidgetId);
556            return;
557        }
558
559        // The pattern used here is that a user PICKs a specific application,
560        // which, depending on the target, might need to CREATE the actual target.
561
562        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
563        // launch over to the Music app to actually CREATE_SHORTCUT.
564        if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
565            final PendingAddArguments args = new PendingAddArguments();
566            args.requestCode = requestCode;
567            args.intent = data;
568            args.container = mPendingAddInfo.container;
569            args.screen = mPendingAddInfo.screen;
570            args.cellX = mPendingAddInfo.cellX;
571            args.cellY = mPendingAddInfo.cellY;
572            if (isWorkspaceLocked()) {
573                sPendingAddList.add(args);
574            } else {
575                delayExitSpringLoadedMode = completeAdd(args);
576            }
577        }
578        mDragLayer.clearAnimatedView();
579        // Exit spring loaded mode if necessary after cancelling the configuration of a widget
580        exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
581                null);
582    }
583
584    private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
585        CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
586        Runnable onCompleteRunnable = null;
587        int animationType = 0;
588
589        if (resultCode == RESULT_OK) {
590            animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
591            final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
592                    mWidgetBeingConfigured.info);
593            mWidgetBeingConfigured.boundWidget = layout;
594            onCompleteRunnable = new Runnable() {
595                @Override
596                public void run() {
597                    completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
598                            mPendingAddInfo.screen, layout, null);
599                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
600                            null);
601                }
602            };
603        } else if (resultCode == RESULT_CANCELED) {
604            animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
605            onCompleteRunnable = new Runnable() {
606                @Override
607                public void run() {
608                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
609                            null);
610                }
611            };
612        }
613        mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
614                (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
615                animationType, mWidgetBeingConfigured.boundWidget, true);
616        mWidgetBeingConfigured = null;
617    }
618
619    @Override
620    protected void onResume() {
621        super.onResume();
622
623        mPaused = false;
624        if (mRestoring || mOnResumeNeedsLoad) {
625            mWorkspaceLoading = true;
626            mModel.startLoader(true);
627            mRestoring = false;
628            mOnResumeNeedsLoad = false;
629        }
630
631        // Reset the pressed state of icons that were locked in the press state while activities
632        // were launching
633        if (mWaitingForResume != null) {
634            // Resets the previous workspace icon press state
635            mWaitingForResume.setStayPressed(false);
636        }
637        if (mAppsCustomizeContent != null) {
638            // Resets the previous all apps icon press state
639            mAppsCustomizeContent.resetDrawableState();
640        }
641    }
642
643    @Override
644    protected void onPause() {
645        super.onPause();
646        mPaused = true;
647        mDragController.cancelDrag();
648        mDragController.resetLastGestureUpTime();
649    }
650
651    @Override
652    public Object onRetainNonConfigurationInstance() {
653        // Flag the loader to stop early before switching
654        mModel.stopLoader();
655        if (mAppsCustomizeContent != null) {
656            mAppsCustomizeContent.surrender();
657        }
658        return Boolean.TRUE;
659    }
660
661    // We can't hide the IME if it was forced open.  So don't bother
662    /*
663    @Override
664    public void onWindowFocusChanged(boolean hasFocus) {
665        super.onWindowFocusChanged(hasFocus);
666
667        if (hasFocus) {
668            final InputMethodManager inputManager = (InputMethodManager)
669                    getSystemService(Context.INPUT_METHOD_SERVICE);
670            WindowManager.LayoutParams lp = getWindow().getAttributes();
671            inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
672                        android.os.Handler()) {
673                        protected void onReceiveResult(int resultCode, Bundle resultData) {
674                            Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
675                        }
676                    });
677            Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
678        }
679    }
680    */
681
682    private boolean acceptFilter() {
683        final InputMethodManager inputManager = (InputMethodManager)
684                getSystemService(Context.INPUT_METHOD_SERVICE);
685        return !inputManager.isFullscreenMode();
686    }
687
688    @Override
689    public boolean onKeyDown(int keyCode, KeyEvent event) {
690        final int uniChar = event.getUnicodeChar();
691        final boolean handled = super.onKeyDown(keyCode, event);
692        final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
693        if (!handled && acceptFilter() && isKeyNotWhitespace) {
694            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
695                    keyCode, event);
696            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
697                // something usable has been typed - start a search
698                // the typed text will be retrieved and cleared by
699                // showSearchDialog()
700                // If there are multiple keystrokes before the search dialog takes focus,
701                // onSearchRequested() will be called for every keystroke,
702                // but it is idempotent, so it's fine.
703                return onSearchRequested();
704            }
705        }
706
707        // Eat the long press event so the keyboard doesn't come up.
708        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
709            return true;
710        }
711
712        return handled;
713    }
714
715    private String getTypedText() {
716        return mDefaultKeySsb.toString();
717    }
718
719    private void clearTypedText() {
720        mDefaultKeySsb.clear();
721        mDefaultKeySsb.clearSpans();
722        Selection.setSelection(mDefaultKeySsb, 0);
723    }
724
725    /**
726     * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
727     * State
728     */
729    private static State intToState(int stateOrdinal) {
730        State state = State.WORKSPACE;
731        final State[] stateValues = State.values();
732        for (int i = 0; i < stateValues.length; i++) {
733            if (stateValues[i].ordinal() == stateOrdinal) {
734                state = stateValues[i];
735                break;
736            }
737        }
738        return state;
739    }
740
741    /**
742     * Restores the previous state, if it exists.
743     *
744     * @param savedState The previous state.
745     */
746    private void restoreState(Bundle savedState) {
747        if (savedState == null) {
748            return;
749        }
750
751        State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
752        if (state == State.APPS_CUSTOMIZE) {
753            showAllApps(false);
754        }
755
756        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
757        if (currentScreen > -1) {
758            mWorkspace.setCurrentPage(currentScreen);
759        }
760
761        final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
762        final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
763
764        if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
765            mPendingAddInfo.container = pendingAddContainer;
766            mPendingAddInfo.screen = pendingAddScreen;
767            mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
768            mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
769            mRestoring = true;
770        }
771
772        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
773        if (renameFolder) {
774            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
775            mFolderInfo = mModel.getFolderById(this, sFolders, id);
776            mRestoring = true;
777        }
778
779
780        // Restore the AppsCustomize tab
781        if (mAppsCustomizeTabHost != null) {
782            String curTab = savedState.getString("apps_customize_currentTab");
783            if (curTab != null) {
784                // We set this directly so that there is no delay before the tab is set
785                mAppsCustomizeContent.setContentType(
786                        mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
787                mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
788                mAppsCustomizeContent.loadAssociatedPages(
789                        mAppsCustomizeContent.getCurrentPage());
790            }
791
792            int currentIndex = savedState.getInt("apps_customize_currentIndex");
793            mAppsCustomizeContent.restorePageForIndex(currentIndex);
794        }
795    }
796
797    /**
798     * Finds all the views we need and configure them properly.
799     */
800    private void setupViews() {
801        final DragController dragController = mDragController;
802
803        mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
804        mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
805        mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
806        mDockDivider = (ImageView) findViewById(R.id.dock_divider);
807
808        // Setup the drag layer
809        mDragLayer.setup(this, dragController);
810
811        // Setup the hotseat
812        mHotseat = (Hotseat) findViewById(R.id.hotseat);
813        if (mHotseat != null) {
814            mHotseat.setup(this);
815        }
816
817        // Setup the workspace
818        mWorkspace.setHapticFeedbackEnabled(false);
819        mWorkspace.setOnLongClickListener(this);
820        mWorkspace.setup(dragController);
821        dragController.addDragListener(mWorkspace);
822
823        // Get the search/delete bar
824        mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
825
826        // Setup AppsCustomize
827        mAppsCustomizeTabHost = (AppsCustomizeTabHost)
828                findViewById(R.id.apps_customize_pane);
829        mAppsCustomizeContent = (AppsCustomizePagedView)
830                mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
831        mAppsCustomizeContent.setup(this, dragController);
832
833        // Get the all apps button
834        mAllAppsButton = findViewById(R.id.all_apps_button);
835        if (mAllAppsButton != null) {
836            mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
837                @Override
838                public boolean onTouch(View v, MotionEvent event) {
839                    if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
840                        onTouchDownAllAppsButton(v);
841                    }
842                    return false;
843                }
844            });
845        }
846        // Setup the drag controller (drop targets have to be added in reverse order in priority)
847        dragController.setDragScoller(mWorkspace);
848        dragController.setScrollView(mDragLayer);
849        dragController.setMoveTarget(mWorkspace);
850        dragController.addDropTarget(mWorkspace);
851        if (mSearchDropTargetBar != null) {
852            mSearchDropTargetBar.setup(this, dragController);
853        }
854    }
855
856    /**
857     * Creates a view representing a shortcut.
858     *
859     * @param info The data structure describing the shortcut.
860     *
861     * @return A View inflated from R.layout.application.
862     */
863    View createShortcut(ShortcutInfo info) {
864        return createShortcut(R.layout.application,
865                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
866    }
867
868    /**
869     * Creates a view representing a shortcut inflated from the specified resource.
870     *
871     * @param layoutResId The id of the XML layout used to create the shortcut.
872     * @param parent The group the shortcut belongs to.
873     * @param info The data structure describing the shortcut.
874     *
875     * @return A View inflated from layoutResId.
876     */
877    View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
878        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
879        favorite.applyFromShortcutInfo(info, mIconCache);
880        favorite.setOnClickListener(this);
881        return favorite;
882    }
883
884    /**
885     * Add an application shortcut to the workspace.
886     *
887     * @param data The intent describing the application.
888     * @param cellInfo The position on screen where to create the shortcut.
889     */
890    void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
891        final int[] cellXY = mTmpAddItemCellCoordinates;
892        final CellLayout layout = getCellLayout(container, screen);
893
894        // First we check if we already know the exact location where we want to add this item.
895        if (cellX >= 0 && cellY >= 0) {
896            cellXY[0] = cellX;
897            cellXY[1] = cellY;
898        } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
899            showOutOfSpaceMessage(isHotseatLayout(layout));
900            return;
901        }
902
903        final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
904
905        if (info != null) {
906            info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
907                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
908            info.container = ItemInfo.NO_ID;
909            mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
910                    isWorkspaceLocked(), cellX, cellY);
911        } else {
912            Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
913        }
914    }
915
916    /**
917     * Add a shortcut to the workspace.
918     *
919     * @param data The intent describing the shortcut.
920     * @param cellInfo The position on screen where to create the shortcut.
921     */
922    private void completeAddShortcut(Intent data, long container, int screen, int cellX,
923            int cellY) {
924        int[] cellXY = mTmpAddItemCellCoordinates;
925        int[] touchXY = mPendingAddInfo.dropPos;
926        CellLayout layout = getCellLayout(container, screen);
927
928        boolean foundCellSpan = false;
929
930        ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
931        if (info == null) {
932            return;
933        }
934        final View view = createShortcut(info);
935
936        // First we check if we already know the exact location where we want to add this item.
937        if (cellX >= 0 && cellY >= 0) {
938            cellXY[0] = cellX;
939            cellXY[1] = cellY;
940            foundCellSpan = true;
941
942            // If appropriate, either create a folder or add to an existing folder
943            if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
944                    true, null,null)) {
945                return;
946            }
947            DragObject dragObject = new DragObject();
948            dragObject.dragInfo = info;
949            if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
950                    true)) {
951                return;
952            }
953        } else if (touchXY != null) {
954            // when dragging and dropping, just find the closest free spot
955            int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
956            foundCellSpan = (result != null);
957        } else {
958            foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
959        }
960
961        if (!foundCellSpan) {
962            showOutOfSpaceMessage(isHotseatLayout(layout));
963            return;
964        }
965
966        LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
967
968        if (!mRestoring) {
969            mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
970                    isWorkspaceLocked());
971        }
972    }
973
974    int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
975        Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
976        // We want to account for the extra amount of padding that we are adding to the widget
977        // to ensure that it gets the full amount of space that it has requested
978        int requiredWidth = minWidth + padding.left + padding.right;
979        int requiredHeight = minHeight + padding.top + padding.bottom;
980        return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
981    }
982
983    int[] getSpanForWidget(AppWidgetProviderInfo info) {
984        return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
985    }
986
987    int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
988        return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
989    }
990
991    int[] getSpanForWidget(PendingAddWidgetInfo info) {
992        return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
993    }
994
995    int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
996        return getSpanForWidget(info.componentName, info.minResizeWidth,
997                info.minResizeHeight);
998    }
999
1000    /**
1001     * Add a widget to the workspace.
1002     *
1003     * @param appWidgetId The app widget id
1004     * @param cellInfo The position on screen where to create the widget.
1005     */
1006    private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1007            AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1008        if (appWidgetInfo == null) {
1009            appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1010        }
1011
1012        // Calculate the grid spans needed to fit this widget
1013        CellLayout layout = getCellLayout(container, screen);
1014
1015        int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1016        int[] spanXY = getSpanForWidget(appWidgetInfo);
1017
1018        // Try finding open space on Launcher screen
1019        // We have saved the position to which the widget was dragged-- this really only matters
1020        // if we are placing widgets on a "spring-loaded" screen
1021        int[] cellXY = mTmpAddItemCellCoordinates;
1022        int[] touchXY = mPendingAddInfo.dropPos;
1023        int[] finalSpan = new int[2];
1024        boolean foundCellSpan = false;
1025        if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1026            cellXY[0] = mPendingAddInfo.cellX;
1027            cellXY[1] = mPendingAddInfo.cellY;
1028            spanXY[0] = mPendingAddInfo.spanX;
1029            spanXY[1] = mPendingAddInfo.spanY;
1030            foundCellSpan = true;
1031        } else if (touchXY != null) {
1032            // when dragging and dropping, just find the closest free spot
1033            int[] result = layout.findNearestVacantArea(
1034                    touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1035                    spanXY[1], cellXY, finalSpan);
1036            spanXY[0] = finalSpan[0];
1037            spanXY[1] = finalSpan[1];
1038            foundCellSpan = (result != null);
1039        } else {
1040            foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
1041        }
1042
1043        if (!foundCellSpan) {
1044            if (appWidgetId != -1) {
1045                // Deleting an app widget ID is a void call but writes to disk before returning
1046                // to the caller...
1047                new Thread("deleteAppWidgetId") {
1048                    public void run() {
1049                        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1050                    }
1051                }.start();
1052            }
1053            showOutOfSpaceMessage(isHotseatLayout(layout));
1054            return;
1055        }
1056
1057        // Build Launcher-specific widget info and save to database
1058        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
1059        launcherInfo.spanX = spanXY[0];
1060        launcherInfo.spanY = spanXY[1];
1061        launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1062        launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
1063
1064        LauncherModel.addItemToDatabase(this, launcherInfo,
1065                container, screen, cellXY[0], cellXY[1], false);
1066
1067        if (!mRestoring) {
1068            if (hostView == null) {
1069                // Perform actual inflation because we're live
1070                launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1071                launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1072            } else {
1073                // The AppWidgetHostView has already been inflated and instantiated
1074                launcherInfo.hostView = hostView;
1075            }
1076
1077            launcherInfo.hostView.setTag(launcherInfo);
1078            launcherInfo.hostView.setVisibility(View.VISIBLE);
1079            mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
1080                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
1081
1082            addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
1083        }
1084        resetAddInfo();
1085    }
1086
1087    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1088        @Override
1089        public void onReceive(Context context, Intent intent) {
1090            final String action = intent.getAction();
1091            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1092                mUserPresent = false;
1093                mDragLayer.clearAllResizeFrames();
1094                updateRunning();
1095
1096                // Reset AllApps to its initial state only if we are not in the middle of
1097                // processing a multi-step drop
1098                if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1099                    mAppsCustomizeTabHost.reset();
1100                    showWorkspace(false);
1101                }
1102            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1103                mUserPresent = true;
1104                updateRunning();
1105            }
1106        }
1107    };
1108
1109    @Override
1110    public void onAttachedToWindow() {
1111        super.onAttachedToWindow();
1112
1113        // Listen for broadcasts related to user-presence
1114        final IntentFilter filter = new IntentFilter();
1115        filter.addAction(Intent.ACTION_SCREEN_OFF);
1116        filter.addAction(Intent.ACTION_USER_PRESENT);
1117        registerReceiver(mReceiver, filter);
1118
1119        mAttached = true;
1120        mVisible = true;
1121    }
1122
1123    @Override
1124    public void onDetachedFromWindow() {
1125        super.onDetachedFromWindow();
1126        mVisible = false;
1127        mDragLayer.clearAllResizeFrames();
1128
1129        if (mAttached) {
1130            unregisterReceiver(mReceiver);
1131            mAttached = false;
1132        }
1133        updateRunning();
1134    }
1135
1136    public void onWindowVisibilityChanged(int visibility) {
1137        mVisible = visibility == View.VISIBLE;
1138        updateRunning();
1139        // The following code used to be in onResume, but it turns out onResume is called when
1140        // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1141        // is a more appropriate event to handle
1142        if (mVisible) {
1143            mAppsCustomizeTabHost.onWindowVisible();
1144            if (!mWorkspaceLoading) {
1145                final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
1146                // We want to let Launcher draw itself at least once before we force it to build
1147                // layers on all the workspace pages, so that transitioning to Launcher from other
1148                // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1149                // a true draw so we wait until the second preDraw call to be safe
1150                observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
1151                    public boolean onPreDraw() {
1152                        // We delay the layer building a bit in order to give
1153                        // other message processing a time to run.  In particular
1154                        // this avoids a delay in hiding the IME if it was
1155                        // currently shown, because doing that may involve
1156                        // some communication back with the app.
1157                        mWorkspace.postDelayed(mBuildLayersRunnable, 500);
1158                        observer.removeOnPreDrawListener(this);
1159                        return true;
1160                    }
1161                });
1162            }
1163            // When Launcher comes back to foreground, a different Activity might be responsible for
1164            // the app market intent, so refresh the icon
1165            updateAppMarketIcon();
1166            clearTypedText();
1167        }
1168    }
1169
1170    private void sendAdvanceMessage(long delay) {
1171        mHandler.removeMessages(ADVANCE_MSG);
1172        Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1173        mHandler.sendMessageDelayed(msg, delay);
1174        mAutoAdvanceSentTime = System.currentTimeMillis();
1175    }
1176
1177    private void updateRunning() {
1178        boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1179        if (autoAdvanceRunning != mAutoAdvanceRunning) {
1180            mAutoAdvanceRunning = autoAdvanceRunning;
1181            if (autoAdvanceRunning) {
1182                long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1183                sendAdvanceMessage(delay);
1184            } else {
1185                if (!mWidgetsToAdvance.isEmpty()) {
1186                    mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1187                            (System.currentTimeMillis() - mAutoAdvanceSentTime));
1188                }
1189                mHandler.removeMessages(ADVANCE_MSG);
1190                mHandler.removeMessages(0); // Remove messages sent using postDelayed()
1191            }
1192        }
1193    }
1194
1195    private final Handler mHandler = new Handler() {
1196        @Override
1197        public void handleMessage(Message msg) {
1198            if (msg.what == ADVANCE_MSG) {
1199                int i = 0;
1200                for (View key: mWidgetsToAdvance.keySet()) {
1201                    final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1202                    final int delay = mAdvanceStagger * i;
1203                    if (v instanceof Advanceable) {
1204                       postDelayed(new Runnable() {
1205                           public void run() {
1206                               ((Advanceable) v).advance();
1207                           }
1208                       }, delay);
1209                    }
1210                    i++;
1211                }
1212                sendAdvanceMessage(mAdvanceInterval);
1213            }
1214        }
1215    };
1216
1217    void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1218        if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
1219        View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1220        if (v instanceof Advanceable) {
1221            mWidgetsToAdvance.put(hostView, appWidgetInfo);
1222            ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
1223            updateRunning();
1224        }
1225    }
1226
1227    void removeWidgetToAutoAdvance(View hostView) {
1228        if (mWidgetsToAdvance.containsKey(hostView)) {
1229            mWidgetsToAdvance.remove(hostView);
1230            updateRunning();
1231        }
1232    }
1233
1234    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1235        removeWidgetToAutoAdvance(launcherInfo.hostView);
1236        launcherInfo.hostView = null;
1237    }
1238
1239    void showOutOfSpaceMessage(boolean isHotseatLayout) {
1240        int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1241        Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
1242    }
1243
1244    public LauncherAppWidgetHost getAppWidgetHost() {
1245        return mAppWidgetHost;
1246    }
1247
1248    public LauncherModel getModel() {
1249        return mModel;
1250    }
1251
1252    void closeSystemDialogs() {
1253        getWindow().closeAllPanels();
1254
1255        // Whatever we were doing is hereby canceled.
1256        mWaitingForResult = false;
1257    }
1258
1259    @Override
1260    protected void onNewIntent(Intent intent) {
1261        super.onNewIntent(intent);
1262
1263        // Close the menu
1264        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
1265            // also will cancel mWaitingForResult.
1266            closeSystemDialogs();
1267
1268            boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1269                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1270
1271            Folder openFolder = mWorkspace.getOpenFolder();
1272            // In all these cases, only animate if we're already on home
1273            mWorkspace.exitWidgetResizeMode();
1274            if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1275                    openFolder == null) {
1276                mWorkspace.moveToDefaultScreen(true);
1277            }
1278
1279            closeFolder();
1280            exitSpringLoadedDragMode();
1281            showWorkspace(alreadyOnHome);
1282
1283            final View v = getWindow().peekDecorView();
1284            if (v != null && v.getWindowToken() != null) {
1285                InputMethodManager imm = (InputMethodManager)getSystemService(
1286                        INPUT_METHOD_SERVICE);
1287                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1288            }
1289
1290            // Reset AllApps to its initial state
1291            if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1292                mAppsCustomizeTabHost.reset();
1293            }
1294        }
1295    }
1296
1297    @Override
1298    protected void onRestoreInstanceState(Bundle savedInstanceState) {
1299        // Do not call super here
1300        mSavedInstanceState = savedInstanceState;
1301    }
1302
1303    @Override
1304    protected void onSaveInstanceState(Bundle outState) {
1305        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
1306        super.onSaveInstanceState(outState);
1307
1308        outState.putInt(RUNTIME_STATE, mState.ordinal());
1309        // We close any open folder since it will not be re-opened, and we need to make sure
1310        // this state is reflected.
1311        closeFolder();
1312
1313        if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1314                mWaitingForResult) {
1315            outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1316            outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1317            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1318            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
1319        }
1320
1321        if (mFolderInfo != null && mWaitingForResult) {
1322            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1323            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1324        }
1325
1326        // Save the current AppsCustomize tab
1327        if (mAppsCustomizeTabHost != null) {
1328            String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1329            if (currentTabTag != null) {
1330                outState.putString("apps_customize_currentTab", currentTabTag);
1331            }
1332            int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1333            outState.putInt("apps_customize_currentIndex", currentIndex);
1334        }
1335    }
1336
1337    @Override
1338    public void onDestroy() {
1339        super.onDestroy();
1340
1341        // Remove all pending runnables
1342        mHandler.removeMessages(ADVANCE_MSG);
1343        mHandler.removeMessages(0);
1344        mWorkspace.removeCallbacks(mBuildLayersRunnable);
1345
1346        // Stop callbacks from LauncherModel
1347        LauncherApplication app = ((LauncherApplication) getApplication());
1348        mModel.stopLoader();
1349        app.setLauncher(null);
1350
1351        try {
1352            mAppWidgetHost.stopListening();
1353        } catch (NullPointerException ex) {
1354            Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
1355        }
1356        mAppWidgetHost = null;
1357
1358        mWidgetsToAdvance.clear();
1359
1360        TextKeyListener.getInstance().release();
1361
1362
1363        unbindWorkspaceAndHotseatItems();
1364
1365        getContentResolver().unregisterContentObserver(mWidgetObserver);
1366        unregisterReceiver(mCloseSystemDialogsReceiver);
1367
1368        ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1369        mWorkspace.removeAllViews();
1370        mWorkspace = null;
1371        mDragController = null;
1372
1373        ValueAnimator.clearAllAnimations();
1374    }
1375
1376    public DragController getDragController() {
1377        return mDragController;
1378    }
1379
1380    @Override
1381    public void startActivityForResult(Intent intent, int requestCode) {
1382        if (requestCode >= 0) mWaitingForResult = true;
1383        super.startActivityForResult(intent, requestCode);
1384    }
1385
1386    /**
1387     * Indicates that we want global search for this activity by setting the globalSearch
1388     * argument for {@link #startSearch} to true.
1389     */
1390    @Override
1391    public void startSearch(String initialQuery, boolean selectInitialQuery,
1392            Bundle appSearchData, boolean globalSearch) {
1393
1394        showWorkspace(true);
1395
1396        if (initialQuery == null) {
1397            // Use any text typed in the launcher as the initial query
1398            initialQuery = getTypedText();
1399        }
1400        if (appSearchData == null) {
1401            appSearchData = new Bundle();
1402            appSearchData.putString(Search.SOURCE, "launcher-search");
1403        }
1404        Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1405
1406        final SearchManager searchManager =
1407                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1408        searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
1409            appSearchData, globalSearch, sourceBounds);
1410    }
1411
1412    @Override
1413    public boolean onCreateOptionsMenu(Menu menu) {
1414        if (isWorkspaceLocked()) {
1415            return false;
1416        }
1417
1418        super.onCreateOptionsMenu(menu);
1419
1420        Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1421        manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1422                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1423        Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1424        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1425                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1426        String helpUrl = getString(R.string.help_url);
1427        Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
1428        help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1429                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1430
1431        menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1432            .setIcon(android.R.drawable.ic_menu_gallery)
1433            .setAlphabeticShortcut('W');
1434        menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1435            .setIcon(android.R.drawable.ic_menu_manage)
1436            .setIntent(manageApps)
1437            .setAlphabeticShortcut('M');
1438        menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1439            .setIcon(android.R.drawable.ic_menu_preferences)
1440            .setIntent(settings)
1441            .setAlphabeticShortcut('P');
1442        if (!helpUrl.isEmpty()) {
1443            menu.add(0, MENU_HELP, 0, R.string.menu_help)
1444                .setIcon(android.R.drawable.ic_menu_help)
1445                .setIntent(help)
1446                .setAlphabeticShortcut('H');
1447        }
1448        return true;
1449    }
1450
1451    @Override
1452    public boolean onPrepareOptionsMenu(Menu menu) {
1453        super.onPrepareOptionsMenu(menu);
1454
1455        if (mAppsCustomizeTabHost.isTransitioning()) {
1456            return false;
1457        }
1458        boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1459        menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1460
1461        return true;
1462    }
1463
1464    @Override
1465    public boolean onOptionsItemSelected(MenuItem item) {
1466        switch (item.getItemId()) {
1467        case MENU_WALLPAPER_SETTINGS:
1468            startWallpaper();
1469            return true;
1470        }
1471
1472        return super.onOptionsItemSelected(item);
1473    }
1474
1475    @Override
1476    public boolean onSearchRequested() {
1477        startSearch(null, false, null, true);
1478        // Use a custom animation for launching search
1479        overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
1480        return true;
1481    }
1482
1483    public boolean isWorkspaceLocked() {
1484        return mWorkspaceLoading || mWaitingForResult;
1485    }
1486
1487    private void resetAddInfo() {
1488        mPendingAddInfo.container = ItemInfo.NO_ID;
1489        mPendingAddInfo.screen = -1;
1490        mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1491        mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1492        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
1493        mPendingAddInfo.dropPos = null;
1494    }
1495
1496    void addAppWidgetFromPick(Intent data) {
1497        // TODO: catch bad widget exception when sent
1498        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
1499        // TODO: Is this log message meaningful?
1500        if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1501        addAppWidgetImpl(appWidgetId, null);
1502    }
1503
1504    void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1505        final AppWidgetProviderInfo appWidget = info.info;
1506        if (appWidget.configure != null) {
1507            // Launch over to configure widget, if needed
1508            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1509            intent.setComponent(appWidget.configure);
1510            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1511            if (info != null) {
1512                if (info.mimeType != null && !info.mimeType.isEmpty()) {
1513                    intent.putExtra(InstallWidgetReceiver.
1514                            EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
1515
1516                    final String mimeType = info.mimeType;
1517                    final ClipData clipData = (ClipData) info.configurationData;
1518                    final ClipDescription clipDesc = clipData.getDescription();
1519                    for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1520                        if (clipDesc.getMimeType(i).equals(mimeType)) {
1521                            final ClipData.Item item = clipData.getItemAt(i);
1522                            final CharSequence stringData = item.getText();
1523                            final Uri uriData = item.getUri();
1524                            final Intent intentData = item.getIntent();
1525                            final String key = InstallWidgetReceiver.
1526                                    EXTRA_APPWIDGET_CONFIGURATION_DATA;
1527                            if (uriData != null) {
1528                                intent.putExtra(key, uriData);
1529                            } else if (intentData != null) {
1530                                intent.putExtra(key, intentData);
1531                            } else if (stringData != null) {
1532                                intent.putExtra(key, stringData);
1533                            }
1534                            break;
1535                        }
1536                    }
1537                }
1538            }
1539            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
1540            mWidgetBeingConfigured = info;
1541        } else {
1542            // Otherwise just add it
1543            completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
1544            // Exit spring loaded mode if necessary after adding the widget
1545            exitSpringLoadedDragModeDelayed(true, false, null);
1546        }
1547    }
1548
1549    /**
1550     * Process a shortcut drop.
1551     *
1552     * @param componentName The name of the component
1553     * @param screen The screen where it should be added
1554     * @param cell The cell it should be added to, optional
1555     * @param position The location on the screen where it was dropped, optional
1556     */
1557    void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1558            int[] cell, int[] loc) {
1559        resetAddInfo();
1560        mPendingAddInfo.container = container;
1561        mPendingAddInfo.screen = screen;
1562        mPendingAddInfo.dropPos = loc;
1563
1564        if (cell != null) {
1565            mPendingAddInfo.cellX = cell[0];
1566            mPendingAddInfo.cellY = cell[1];
1567        }
1568
1569        Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1570        createShortcutIntent.setComponent(componentName);
1571        processShortcut(createShortcutIntent);
1572    }
1573
1574    /**
1575     * Process a widget drop.
1576     *
1577     * @param info The PendingAppWidgetInfo of the widget being added.
1578     * @param screen The screen where it should be added
1579     * @param cell The cell it should be added to, optional
1580     * @param position The location on the screen where it was dropped, optional
1581     */
1582    void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1583            int[] cell, int[] span, int[] loc) {
1584        resetAddInfo();
1585        mPendingAddInfo.container = info.container = container;
1586        mPendingAddInfo.screen = info.screen = screen;
1587        mPendingAddInfo.dropPos = loc;
1588        mPendingAddInfo.minSpanX = info.minSpanX;
1589        mPendingAddInfo.minSpanY = info.minSpanY;
1590
1591        if (cell != null) {
1592            mPendingAddInfo.cellX = cell[0];
1593            mPendingAddInfo.cellY = cell[1];
1594        }
1595        if (span != null) {
1596            mPendingAddInfo.spanX = span[0];
1597            mPendingAddInfo.spanY = span[1];
1598        }
1599
1600        AppWidgetHostView hostView = info.boundWidget;
1601        int appWidgetId;
1602        if (hostView != null) {
1603            appWidgetId = hostView.getAppWidgetId();
1604        } else {
1605            appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1606            AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1607        }
1608        addAppWidgetImpl(appWidgetId, info);
1609    }
1610
1611    void processShortcut(Intent intent) {
1612        // Handle case where user selected "Applications"
1613        String applicationName = getResources().getString(R.string.group_applications);
1614        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1615
1616        if (applicationName != null && applicationName.equals(shortcutName)) {
1617            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1618            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1619
1620            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1621            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
1622            pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
1623            startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
1624        } else {
1625            startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
1626        }
1627    }
1628
1629    void processWallpaper(Intent intent) {
1630        startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1631    }
1632
1633    FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1634            int cellY) {
1635        final FolderInfo folderInfo = new FolderInfo();
1636        folderInfo.title = getText(R.string.folder_name);
1637
1638        // Update the model
1639        LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1640                false);
1641        sFolders.put(folderInfo.id, folderInfo);
1642
1643        // Create the view
1644        FolderIcon newFolder =
1645            FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1646        mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1647                isWorkspaceLocked());
1648        return newFolder;
1649    }
1650
1651    void removeFolder(FolderInfo folder) {
1652        sFolders.remove(folder.id);
1653    }
1654
1655    private void startWallpaper() {
1656        showWorkspace(true);
1657        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
1658        Intent chooser = Intent.createChooser(pickWallpaper,
1659                getText(R.string.chooser_wallpaper));
1660        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1661        //       Removed in Eclair MR1
1662//        WallpaperManager wm = (WallpaperManager)
1663//                getSystemService(Context.WALLPAPER_SERVICE);
1664//        WallpaperInfo wi = wm.getWallpaperInfo();
1665//        if (wi != null && wi.getSettingsActivity() != null) {
1666//            LabeledIntent li = new LabeledIntent(getPackageName(),
1667//                    R.string.configure_wallpaper, 0);
1668//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1669//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1670//        }
1671        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
1672    }
1673
1674    /**
1675     * Registers various content observers. The current implementation registers
1676     * only a favorites observer to keep track of the favorites applications.
1677     */
1678    private void registerContentObservers() {
1679        ContentResolver resolver = getContentResolver();
1680        resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1681                true, mWidgetObserver);
1682    }
1683
1684    @Override
1685    public boolean dispatchKeyEvent(KeyEvent event) {
1686        if (event.getAction() == KeyEvent.ACTION_DOWN) {
1687            switch (event.getKeyCode()) {
1688                case KeyEvent.KEYCODE_HOME:
1689                    return true;
1690                case KeyEvent.KEYCODE_VOLUME_DOWN:
1691                    if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
1692                        dumpState();
1693                        return true;
1694                    }
1695                    break;
1696            }
1697        } else if (event.getAction() == KeyEvent.ACTION_UP) {
1698            switch (event.getKeyCode()) {
1699                case KeyEvent.KEYCODE_HOME:
1700                    return true;
1701            }
1702        }
1703
1704        return super.dispatchKeyEvent(event);
1705    }
1706
1707    @Override
1708    public void onBackPressed() {
1709        if (mState == State.APPS_CUSTOMIZE) {
1710            showWorkspace(true);
1711        } else if (mWorkspace.getOpenFolder() != null) {
1712            Folder openFolder = mWorkspace.getOpenFolder();
1713            if (openFolder.isEditingName()) {
1714                openFolder.dismissEditingName();
1715            } else {
1716                closeFolder();
1717            }
1718        } else {
1719            mWorkspace.exitWidgetResizeMode();
1720
1721            // Back button is a no-op here, but give at least some feedback for the button press
1722            mWorkspace.showOutlinesTemporarily();
1723        }
1724    }
1725
1726    /**
1727     * Re-listen when widgets are reset.
1728     */
1729    private void onAppWidgetReset() {
1730        if (mAppWidgetHost != null) {
1731            mAppWidgetHost.startListening();
1732        }
1733    }
1734
1735    /**
1736     * Go through the and disconnect any of the callbacks in the drawables and the views or we
1737     * leak the previous Home screen on orientation change.
1738     */
1739    private void unbindWorkspaceAndHotseatItems() {
1740        if (mModel != null) {
1741            mModel.unbindWorkspaceItems();
1742        }
1743    }
1744
1745    /**
1746     * Launches the intent referred by the clicked shortcut.
1747     *
1748     * @param v The view representing the clicked shortcut.
1749     */
1750    public void onClick(View v) {
1751        // Make sure that rogue clicks don't get through while allapps is launching, or after the
1752        // view has detached (it's possible for this to happen if the view is removed mid touch).
1753        if (v.getWindowToken() == null) {
1754            return;
1755        }
1756
1757        if (!mWorkspace.isFinishedSwitchingState()) {
1758            return;
1759        }
1760
1761        Object tag = v.getTag();
1762        if (tag instanceof ShortcutInfo) {
1763            // Open shortcut
1764            final Intent intent = ((ShortcutInfo) tag).intent;
1765            int[] pos = new int[2];
1766            v.getLocationOnScreen(pos);
1767            intent.setSourceBounds(new Rect(pos[0], pos[1],
1768                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
1769            boolean success = startActivitySafely(intent, tag);
1770
1771            if (success && v instanceof BubbleTextView) {
1772                mWaitingForResume = (BubbleTextView) v;
1773                mWaitingForResume.setStayPressed(true);
1774            }
1775        } else if (tag instanceof FolderInfo) {
1776            if (v instanceof FolderIcon) {
1777                FolderIcon fi = (FolderIcon) v;
1778                handleFolderClick(fi);
1779            }
1780        } else if (v == mAllAppsButton) {
1781            if (mState == State.APPS_CUSTOMIZE) {
1782                showWorkspace(true);
1783            } else {
1784                onClickAllAppsButton(v);
1785            }
1786        }
1787    }
1788
1789    public boolean onTouch(View v, MotionEvent event) {
1790        // this is an intercepted event being forwarded from mWorkspace;
1791        // clicking anywhere on the workspace causes the customization drawer to slide down
1792        showWorkspace(true);
1793        return false;
1794    }
1795
1796    /**
1797     * Event handler for the search button
1798     *
1799     * @param v The view that was clicked.
1800     */
1801    public void onClickSearchButton(View v) {
1802        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1803
1804        onSearchRequested();
1805    }
1806
1807    /**
1808     * Event handler for the voice button
1809     *
1810     * @param v The view that was clicked.
1811     */
1812    public void onClickVoiceButton(View v) {
1813        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1814
1815        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1816        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1817        startActivity(intent);
1818    }
1819
1820    /**
1821     * Event handler for the "grid" button that appears on the home screen, which
1822     * enters all apps mode.
1823     *
1824     * @param v The view that was clicked.
1825     */
1826    public void onClickAllAppsButton(View v) {
1827        showAllApps(true);
1828    }
1829
1830    public void onTouchDownAllAppsButton(View v) {
1831        // Provide the same haptic feedback that the system offers for virtual keys.
1832        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1833    }
1834
1835    public void onClickAppMarketButton(View v) {
1836        if (mAppMarketIntent != null) {
1837            startActivitySafely(mAppMarketIntent, "app market");
1838        } else {
1839            Log.e(TAG, "Invalid app market intent.");
1840        }
1841    }
1842
1843    void startApplicationDetailsActivity(ComponentName componentName) {
1844        String packageName = componentName.getPackageName();
1845        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1846                Uri.fromParts("package", packageName, null));
1847        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1848        startActivity(intent);
1849    }
1850
1851    void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1852        if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1853            // System applications cannot be installed. For now, show a toast explaining that.
1854            // We may give them the option of disabling apps this way.
1855            int messageId = R.string.uninstall_system_app_text;
1856            Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1857        } else {
1858            String packageName = appInfo.componentName.getPackageName();
1859            String className = appInfo.componentName.getClassName();
1860            Intent intent = new Intent(
1861                    Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1862            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1863                    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1864            startActivity(intent);
1865        }
1866    }
1867
1868    boolean startActivitySafely(Intent intent, Object tag) {
1869        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1870        try {
1871            startActivity(intent);
1872            return true;
1873        } catch (ActivityNotFoundException e) {
1874            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1875            Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1876        } catch (SecurityException e) {
1877            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1878            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1879                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1880                    "or use the exported attribute for this activity. "
1881                    + "tag="+ tag + " intent=" + intent, e);
1882        }
1883        return false;
1884    }
1885
1886    void startActivityForResultSafely(Intent intent, int requestCode) {
1887        try {
1888            startActivityForResult(intent, requestCode);
1889        } catch (ActivityNotFoundException e) {
1890            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1891        } catch (SecurityException e) {
1892            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1893            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1894                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1895                    "or use the exported attribute for this activity.", e);
1896        }
1897    }
1898
1899    private void handleFolderClick(FolderIcon folderIcon) {
1900        final FolderInfo info = folderIcon.mInfo;
1901        Folder openFolder = mWorkspace.getFolderForTag(info);
1902
1903        // If the folder info reports that the associated folder is open, then verify that
1904        // it is actually opened. There have been a few instances where this gets out of sync.
1905        if (info.opened && openFolder == null) {
1906            Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1907                    + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1908            info.opened = false;
1909        }
1910
1911        if (!info.opened) {
1912            // Close any open folder
1913            closeFolder();
1914            // Open the requested folder
1915            openFolder(folderIcon);
1916        } else {
1917            // Find the open folder...
1918            int folderScreen;
1919            if (openFolder != null) {
1920                folderScreen = mWorkspace.getPageForView(openFolder);
1921                // .. and close it
1922                closeFolder(openFolder);
1923                if (folderScreen != mWorkspace.getCurrentPage()) {
1924                    // Close any folder open on the current screen
1925                    closeFolder();
1926                    // Pull the folder onto this screen
1927                    openFolder(folderIcon);
1928                }
1929            }
1930        }
1931    }
1932
1933    private void growAndFadeOutFolderIcon(FolderIcon fi) {
1934        if (fi == null) return;
1935        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1936        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1937        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1938
1939        FolderInfo info = (FolderInfo) fi.getTag();
1940        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1941            CellLayout cl = (CellLayout) fi.getParent().getParent();
1942            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1943            cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
1944        }
1945
1946        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1947        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1948        oa.start();
1949    }
1950
1951    private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1952        if (fi == null) return;
1953        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1954        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1955        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1956
1957        FolderInfo info = (FolderInfo) fi.getTag();
1958        CellLayout cl = null;
1959        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1960            cl = (CellLayout) fi.getParent().getParent();
1961        }
1962
1963        final CellLayout layout = cl;
1964        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1965        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1966        oa.addListener(new AnimatorListenerAdapter() {
1967            @Override
1968            public void onAnimationEnd(Animator animation) {
1969                if (layout != null) {
1970                    layout.clearFolderLeaveBehind();
1971                }
1972            }
1973        });
1974        oa.start();
1975    }
1976
1977    /**
1978     * Opens the user folder described by the specified tag. The opening of the folder
1979     * is animated relative to the specified View. If the View is null, no animation
1980     * is played.
1981     *
1982     * @param folderInfo The FolderInfo describing the folder to open.
1983     */
1984    public void openFolder(FolderIcon folderIcon) {
1985        Folder folder = folderIcon.mFolder;
1986        FolderInfo info = folder.mInfo;
1987
1988        growAndFadeOutFolderIcon(folderIcon);
1989        info.opened = true;
1990
1991        // Just verify that the folder hasn't already been added to the DragLayer.
1992        // There was a one-off crash where the folder had a parent already.
1993        if (folder.getParent() == null) {
1994            mDragLayer.addView(folder);
1995            mDragController.addDropTarget((DropTarget) folder);
1996        } else {
1997            Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1998                    folder.getParent() + ").");
1999        }
2000        folder.animateOpen();
2001    }
2002
2003    public void closeFolder() {
2004        Folder folder = mWorkspace.getOpenFolder();
2005        if (folder != null) {
2006            if (folder.isEditingName()) {
2007                folder.dismissEditingName();
2008            }
2009            closeFolder(folder);
2010
2011            // Dismiss the folder cling
2012            dismissFolderCling(null);
2013        }
2014    }
2015
2016    void closeFolder(Folder folder) {
2017        folder.getInfo().opened = false;
2018
2019        ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2020        if (parent != null) {
2021            FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2022            shrinkAndFadeInFolderIcon(fi);
2023        }
2024        folder.animateClosed();
2025    }
2026
2027    public boolean onLongClick(View v) {
2028
2029        if (mState != State.WORKSPACE) {
2030            return false;
2031        }
2032
2033        if (isWorkspaceLocked()) {
2034            return false;
2035        }
2036
2037        if (!(v instanceof CellLayout)) {
2038            v = (View) v.getParent().getParent();
2039        }
2040
2041        resetAddInfo();
2042        CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
2043        // This happens when long clicking an item with the dpad/trackball
2044        if (longClickCellInfo == null) {
2045            return true;
2046        }
2047
2048        // The hotseat touch handling does not go through Workspace, and we always allow long press
2049        // on hotseat items.
2050        final View itemUnderLongClick = longClickCellInfo.cell;
2051        boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2052        if (allowLongPress && !mDragController.isDragging()) {
2053            if (itemUnderLongClick == null) {
2054                // User long pressed on empty space
2055                mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2056                        HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2057                startWallpaper();
2058            } else {
2059                if (!(itemUnderLongClick instanceof Folder)) {
2060                    // User long pressed on an item
2061                    mWorkspace.startDrag(longClickCellInfo);
2062                }
2063            }
2064        }
2065        return true;
2066    }
2067
2068    boolean isHotseatLayout(View layout) {
2069        return mHotseat != null && layout != null &&
2070                (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2071    }
2072    Hotseat getHotseat() {
2073        return mHotseat;
2074    }
2075    SearchDropTargetBar getSearchBar() {
2076        return mSearchDropTargetBar;
2077    }
2078
2079    /**
2080     * Returns the CellLayout of the specified container at the specified screen.
2081     */
2082    CellLayout getCellLayout(long container, int screen) {
2083        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2084            if (mHotseat != null) {
2085                return mHotseat.getLayout();
2086            } else {
2087                return null;
2088            }
2089        } else {
2090            return (CellLayout) mWorkspace.getChildAt(screen);
2091        }
2092    }
2093
2094    Workspace getWorkspace() {
2095        return mWorkspace;
2096    }
2097
2098    // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2099    public boolean isAllAppsVisible() {
2100        return (mState == State.APPS_CUSTOMIZE);
2101    }
2102
2103    public boolean isAllAppsButtonRank(int rank) {
2104        return mHotseat.isAllAppsButtonRank(rank);
2105    }
2106
2107    // AllAppsView.Watcher
2108    public void zoomed(float zoom) {
2109        if (zoom == 1.0f) {
2110            mWorkspace.setVisibility(View.GONE);
2111        }
2112    }
2113
2114    /**
2115     * Helper method for the cameraZoomIn/cameraZoomOut animations
2116     * @param view The view being animated
2117     * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
2118     * @param scaleFactor The scale factor used for the zoom
2119     */
2120    private void setPivotsForZoom(View view, float scaleFactor) {
2121        view.setPivotX(view.getWidth() / 2.0f);
2122        view.setPivotY(view.getHeight() / 2.0f);
2123    }
2124
2125    void updateWallpaperVisibility(boolean visible) {
2126        int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2127        int curflags = getWindow().getAttributes().flags
2128                & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2129        if (wpflags != curflags) {
2130            getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2131        }
2132    }
2133
2134    private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2135        if (v instanceof LauncherTransitionable) {
2136            ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2137        }
2138
2139        // Update the workspace transition step as well
2140        dispatchOnLauncherTransitionStep(v, 0f);
2141    }
2142
2143    private void dispatchOnLauncherTransitionStep(View v, float t) {
2144        if (v instanceof LauncherTransitionable) {
2145            ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2146        }
2147    }
2148
2149    private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2150        if (v instanceof LauncherTransitionable) {
2151            ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2152        }
2153
2154        // Update the workspace transition step as well
2155        dispatchOnLauncherTransitionStep(v, 1f);
2156    }
2157
2158    /**
2159     * Things to test when changing the following seven functions.
2160     *   - Home from workspace
2161     *          - from center screen
2162     *          - from other screens
2163     *   - Home from all apps
2164     *          - from center screen
2165     *          - from other screens
2166     *   - Back from all apps
2167     *          - from center screen
2168     *          - from other screens
2169     *   - Launch app from workspace and quit
2170     *          - with back
2171     *          - with home
2172     *   - Launch app from all apps and quit
2173     *          - with back
2174     *          - with home
2175     *   - Go to a screen that's not the default, then all
2176     *     apps, and launch and app, and go back
2177     *          - with back
2178     *          -with home
2179     *   - On workspace, long press power and go back
2180     *          - with back
2181     *          - with home
2182     *   - On all apps, long press power and go back
2183     *          - with back
2184     *          - with home
2185     *   - On workspace, power off
2186     *   - On all apps, power off
2187     *   - Launch an app and turn off the screen while in that app
2188     *          - Go back with home key
2189     *          - Go back with back key  TODO: make this not go to workspace
2190     *          - From all apps
2191     *          - From workspace
2192     *   - Enter and exit car mode (becuase it causes an extra configuration changed)
2193     *          - From all apps
2194     *          - From the center workspace
2195     *          - From another workspace
2196     */
2197
2198    /**
2199     * Zoom the camera out from the workspace to reveal 'toView'.
2200     * Assumes that the view to show is anchored at either the very top or very bottom
2201     * of the screen.
2202     */
2203    private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
2204        if (mStateAnimation != null) {
2205            mStateAnimation.cancel();
2206            mStateAnimation = null;
2207        }
2208        final Resources res = getResources();
2209
2210        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2211        final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2212        final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2213        final View fromView = mWorkspace;
2214        final View toView = mAppsCustomizeTabHost;
2215        final int startDelay =
2216                res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
2217
2218        setPivotsForZoom(toView, scale);
2219
2220        // Shrink workspaces away if going to AppsCustomize from workspace
2221        Animator workspaceAnim =
2222                mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
2223
2224        if (animated) {
2225            toView.setScaleX(scale);
2226            toView.setScaleY(scale);
2227            final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2228            scaleAnim.
2229                scaleX(1f).scaleY(1f).
2230                setDuration(duration).
2231                setInterpolator(new Workspace.ZoomOutInterpolator());
2232
2233            toView.setVisibility(View.VISIBLE);
2234            toView.setAlpha(0f);
2235            final ObjectAnimator alphaAnim = ObjectAnimator
2236                .ofFloat(toView, "alpha", 0f, 1f)
2237                .setDuration(fadeDuration);
2238            alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2239            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2240                @Override
2241                public void onAnimationUpdate(ValueAnimator animation) {
2242                    float t = (Float) animation.getAnimatedValue();
2243                    dispatchOnLauncherTransitionStep(fromView, t);
2244                    dispatchOnLauncherTransitionStep(toView, t);
2245                }
2246            });
2247
2248            // toView should appear right at the end of the workspace shrink
2249            // animation
2250            mStateAnimation = new AnimatorSet();
2251            mStateAnimation.play(scaleAnim).after(startDelay);
2252            mStateAnimation.play(alphaAnim).after(startDelay);
2253
2254            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2255                boolean animationCancelled = false;
2256
2257                @Override
2258                public void onAnimationStart(Animator animation) {
2259                    updateWallpaperVisibility(true);
2260                    // Prepare the position
2261                    toView.setTranslationX(0.0f);
2262                    toView.setTranslationY(0.0f);
2263                    toView.setVisibility(View.VISIBLE);
2264                    toView.bringToFront();
2265                }
2266                @Override
2267                public void onAnimationEnd(Animator animation) {
2268                    dispatchOnLauncherTransitionEnd(fromView, animated, false);
2269                    dispatchOnLauncherTransitionEnd(toView, animated, false);
2270
2271                    if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2272                        // Hide the workspace scrollbar
2273                        mWorkspace.hideScrollingIndicator(true);
2274                        hideDockDivider();
2275                    }
2276                    if (!animationCancelled) {
2277                        updateWallpaperVisibility(false);
2278                    }
2279                }
2280
2281                @Override
2282                public void onAnimationCancel(Animator animation) {
2283                    animationCancelled = true;
2284                }
2285            });
2286
2287            if (workspaceAnim != null) {
2288                mStateAnimation.play(workspaceAnim);
2289            }
2290
2291            boolean delayAnim = false;
2292            final ViewTreeObserver observer;
2293
2294            dispatchOnLauncherTransitionStart(fromView, animated, false);
2295            dispatchOnLauncherTransitionStart(toView, animated, false);
2296
2297            // If any of the objects being animated haven't been measured/laid out
2298            // yet, delay the animation until we get a layout pass
2299            if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
2300                    (mWorkspace.getMeasuredWidth() == 0) ||
2301                    (toView.getMeasuredWidth() == 0)) {
2302                observer = mWorkspace.getViewTreeObserver();
2303                delayAnim = true;
2304            } else {
2305                observer = null;
2306            }
2307
2308            if (delayAnim) {
2309                final AnimatorSet stateAnimation = mStateAnimation;
2310                final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2311                    public void onGlobalLayout() {
2312                        mWorkspace.post(new Runnable() {
2313                            public void run() {
2314                                // Check that mStateAnimation hasn't changed while
2315                                // we waited for a layout pass
2316                                if (mStateAnimation == stateAnimation) {
2317                                    // Need to update pivots for zoom if layout changed
2318                                    setPivotsForZoom(toView, scale);
2319                                    mStateAnimation.start();
2320                                }
2321                            }
2322                        });
2323                        observer.removeOnGlobalLayoutListener(this);
2324                    }
2325                };
2326                observer.addOnGlobalLayoutListener(delayedStart);
2327            } else {
2328                setPivotsForZoom(toView, scale);
2329                mStateAnimation.start();
2330            }
2331        } else {
2332            toView.setTranslationX(0.0f);
2333            toView.setTranslationY(0.0f);
2334            toView.setScaleX(1.0f);
2335            toView.setScaleY(1.0f);
2336            toView.setVisibility(View.VISIBLE);
2337            toView.bringToFront();
2338
2339            if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2340                // Hide the workspace scrollbar
2341                mWorkspace.hideScrollingIndicator(true);
2342                hideDockDivider();
2343            }
2344            dispatchOnLauncherTransitionStart(fromView, animated, false);
2345            dispatchOnLauncherTransitionEnd(fromView, animated, false);
2346            dispatchOnLauncherTransitionStart(toView, animated, false);
2347            dispatchOnLauncherTransitionEnd(toView, animated, false);
2348            updateWallpaperVisibility(false);
2349        }
2350    }
2351
2352    /**
2353     * Zoom the camera back into the workspace, hiding 'fromView'.
2354     * This is the opposite of showAppsCustomizeHelper.
2355     * @param animated If true, the transition will be animated.
2356     */
2357    private void hideAppsCustomizeHelper(State toState, final boolean animated,
2358            final boolean springLoaded, final Runnable onCompleteRunnable) {
2359
2360        if (mStateAnimation != null) {
2361            mStateAnimation.cancel();
2362            mStateAnimation = null;
2363        }
2364        Resources res = getResources();
2365
2366        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2367        final int fadeOutDuration =
2368                res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
2369        final float scaleFactor = (float)
2370                res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2371        final View fromView = mAppsCustomizeTabHost;
2372        final View toView = mWorkspace;
2373        Animator workspaceAnim = null;
2374
2375        if (toState == State.WORKSPACE) {
2376            int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2377            workspaceAnim = mWorkspace.getChangeStateAnimation(
2378                    Workspace.State.NORMAL, animated, stagger);
2379        } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2380            workspaceAnim = mWorkspace.getChangeStateAnimation(
2381                    Workspace.State.SPRING_LOADED, animated);
2382        }
2383
2384        setPivotsForZoom(fromView, scaleFactor);
2385        updateWallpaperVisibility(true);
2386        showHotseat(animated);
2387        if (animated) {
2388            final LauncherViewPropertyAnimator scaleAnim =
2389                    new LauncherViewPropertyAnimator(fromView);
2390            scaleAnim.
2391                scaleX(scaleFactor).scaleY(scaleFactor).
2392                setDuration(duration).
2393                setInterpolator(new Workspace.ZoomInInterpolator());
2394
2395            final ObjectAnimator alphaAnim = ObjectAnimator
2396                .ofFloat(fromView, "alpha", 1f, 0f)
2397                .setDuration(fadeOutDuration);
2398            alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
2399            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2400                @Override
2401                public void onAnimationUpdate(ValueAnimator animation) {
2402                    float t = 1f - (Float) animation.getAnimatedValue();
2403                    dispatchOnLauncherTransitionStep(fromView, t);
2404                    dispatchOnLauncherTransitionStep(toView, t);
2405                }
2406            });
2407
2408            mStateAnimation = new AnimatorSet();
2409
2410            dispatchOnLauncherTransitionStart(fromView, animated, true);
2411            dispatchOnLauncherTransitionStart(toView, animated, true);
2412
2413            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2414                @Override
2415                public void onAnimationEnd(Animator animation) {
2416                    updateWallpaperVisibility(true);
2417                    fromView.setVisibility(View.GONE);
2418                    dispatchOnLauncherTransitionEnd(fromView, animated, true);
2419                    dispatchOnLauncherTransitionEnd(toView, animated, true);
2420                    mWorkspace.hideScrollingIndicator(false);
2421                    if (onCompleteRunnable != null) {
2422                        onCompleteRunnable.run();
2423                    }
2424                }
2425            });
2426
2427            mStateAnimation.playTogether(scaleAnim, alphaAnim);
2428            if (workspaceAnim != null) {
2429                mStateAnimation.play(workspaceAnim);
2430            }
2431            mStateAnimation.start();
2432        } else {
2433            fromView.setVisibility(View.GONE);
2434            dispatchOnLauncherTransitionStart(fromView, animated, true);
2435            dispatchOnLauncherTransitionEnd(fromView, animated, true);
2436            dispatchOnLauncherTransitionStart(toView, animated, true);
2437            dispatchOnLauncherTransitionEnd(toView, animated, true);
2438            mWorkspace.hideScrollingIndicator(false);
2439        }
2440    }
2441
2442    @Override
2443    public void onTrimMemory(int level) {
2444        super.onTrimMemory(level);
2445        if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2446            mAppsCustomizeTabHost.onTrimMemory();
2447        }
2448    }
2449
2450    void showWorkspace(boolean animated) {
2451        showWorkspace(animated, null);
2452    }
2453
2454    void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
2455        if (mState != State.WORKSPACE) {
2456            mWorkspace.setVisibility(View.VISIBLE);
2457            hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
2458
2459            // Show the search bar and hotseat
2460            mSearchDropTargetBar.showSearchBar(animated);
2461            // We only need to animate in the dock divider if we're going from spring loaded mode
2462            showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
2463
2464            // Set focus to the AppsCustomize button
2465            if (mAllAppsButton != null) {
2466                mAllAppsButton.requestFocus();
2467            }
2468        }
2469
2470        mWorkspace.flashScrollingIndicator(animated);
2471
2472        // Change the state *after* we've called all the transition code
2473        mState = State.WORKSPACE;
2474
2475        // Resume the auto-advance of widgets
2476        mUserPresent = true;
2477        updateRunning();
2478
2479        // send an accessibility event to announce the context change
2480        getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2481    }
2482
2483    void showAllApps(boolean animated) {
2484        if (mState != State.WORKSPACE) return;
2485
2486        showAppsCustomizeHelper(animated, false);
2487        mAppsCustomizeTabHost.requestFocus();
2488
2489        // Hide the search bar and hotseat
2490        mSearchDropTargetBar.hideSearchBar(animated);
2491
2492        // Change the state *after* we've called all the transition code
2493        mState = State.APPS_CUSTOMIZE;
2494
2495        // Pause the auto-advance of widgets until we are out of AllApps
2496        mUserPresent = false;
2497        updateRunning();
2498        closeFolder();
2499
2500        // Send an accessibility event to announce the context change
2501        getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2502    }
2503
2504    void enterSpringLoadedDragMode() {
2505        if (mState == State.APPS_CUSTOMIZE) {
2506            hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
2507            hideDockDivider();
2508            mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2509        }
2510    }
2511
2512    void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2513            final Runnable onCompleteRunnable) {
2514        if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2515
2516        mHandler.postDelayed(new Runnable() {
2517            @Override
2518            public void run() {
2519                if (successfulDrop) {
2520                    // Before we show workspace, hide all apps again because
2521                    // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2522                    // clean up our state transition functions
2523                    mAppsCustomizeTabHost.setVisibility(View.GONE);
2524                    mSearchDropTargetBar.showSearchBar(true);
2525                    showWorkspace(true, onCompleteRunnable);
2526                } else {
2527                    exitSpringLoadedDragMode();
2528                }
2529            }
2530        }, (extendedDelay ?
2531                EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2532                EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2533    }
2534
2535    void exitSpringLoadedDragMode() {
2536        if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2537            final boolean animated = true;
2538            final boolean springLoaded = true;
2539            showAppsCustomizeHelper(animated, springLoaded);
2540            mState = State.APPS_CUSTOMIZE;
2541        }
2542        // Otherwise, we are not in spring loaded mode, so don't do anything.
2543    }
2544
2545    void hideDockDivider() {
2546        if (mQsbDivider != null && mDockDivider != null) {
2547            mQsbDivider.setVisibility(View.INVISIBLE);
2548            mDockDivider.setVisibility(View.INVISIBLE);
2549        }
2550    }
2551
2552    void showDockDivider(boolean animated) {
2553        if (mQsbDivider != null && mDockDivider != null) {
2554            mQsbDivider.setVisibility(View.VISIBLE);
2555            mDockDivider.setVisibility(View.VISIBLE);
2556            if (mDividerAnimator != null) {
2557                mDividerAnimator.cancel();
2558                mQsbDivider.setAlpha(1f);
2559                mDockDivider.setAlpha(1f);
2560                mDividerAnimator = null;
2561            }
2562            if (animated) {
2563                mDividerAnimator = new AnimatorSet();
2564                mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2565                        ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2566                mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2567                mDividerAnimator.start();
2568            }
2569        }
2570    }
2571
2572    void lockAllApps() {
2573        // TODO
2574    }
2575
2576    void unlockAllApps() {
2577        // TODO
2578    }
2579
2580    public boolean isAllAppsCustomizeOpen() {
2581        return mState == State.APPS_CUSTOMIZE;
2582    }
2583
2584    /**
2585     * Shows the hotseat area.
2586     */
2587    void showHotseat(boolean animated) {
2588        if (!LauncherApplication.isScreenLarge()) {
2589            if (animated) {
2590                if (mHotseat.getAlpha() != 1f) {
2591                    int duration = mSearchDropTargetBar.getTransitionInDuration();
2592                    mHotseat.animate().alpha(1f).setDuration(duration);
2593                }
2594            } else {
2595                mHotseat.setAlpha(1f);
2596            }
2597        }
2598    }
2599
2600    /**
2601     * Hides the hotseat area.
2602     */
2603    void hideHotseat(boolean animated) {
2604        if (!LauncherApplication.isScreenLarge()) {
2605            if (animated) {
2606                if (mHotseat.getAlpha() != 0f) {
2607                    int duration = mSearchDropTargetBar.getTransitionOutDuration();
2608                    mHotseat.animate().alpha(0f).setDuration(duration);
2609                }
2610            } else {
2611                mHotseat.setAlpha(0f);
2612            }
2613        }
2614    }
2615
2616    /**
2617     * Add an item from all apps or customize onto the given workspace screen.
2618     * If layout is null, add to the current screen.
2619     */
2620    void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
2621        if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2622            showOutOfSpaceMessage(isHotseatLayout(layout));
2623        }
2624    }
2625
2626    /** Maps the current orientation to an index for referencing orientation correct global icons */
2627    private int getCurrentOrientationIndexForGlobalIcons() {
2628        // default - 0, landscape - 1
2629        switch (getResources().getConfiguration().orientation) {
2630        case Configuration.ORIENTATION_LANDSCAPE:
2631            return 1;
2632        default:
2633            return 0;
2634        }
2635    }
2636
2637    private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
2638        try {
2639            PackageManager packageManager = getPackageManager();
2640            // Look for the toolbar icon specified in the activity meta-data
2641            Bundle metaData = packageManager.getActivityInfo(
2642                    activityName, PackageManager.GET_META_DATA).metaData;
2643            if (metaData != null) {
2644                int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2645                if (iconResId != 0) {
2646                    Resources res = packageManager.getResourcesForActivity(activityName);
2647                    return res.getDrawable(iconResId);
2648                }
2649            }
2650        } catch (NameNotFoundException e) {
2651            // This can happen if the activity defines an invalid drawable
2652            Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2653                    " not found", e);
2654        } catch (Resources.NotFoundException nfe) {
2655            // This can happen if the activity defines an invalid drawable
2656            Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2657                    nfe);
2658        }
2659        return null;
2660    }
2661
2662    // if successful in getting icon, return it; otherwise, set button to use default drawable
2663    private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2664            int buttonId, ComponentName activityName, int fallbackDrawableId) {
2665        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2666        Resources r = getResources();
2667        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2668        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
2669
2670        TextView button = (TextView) findViewById(buttonId);
2671        // If we were unable to find the icon via the meta-data, use a generic one
2672        if (toolbarIcon == null) {
2673            toolbarIcon = r.getDrawable(fallbackDrawableId);
2674            toolbarIcon.setBounds(0, 0, w, h);
2675            if (button != null) {
2676                button.setCompoundDrawables(toolbarIcon, null, null, null);
2677            }
2678            return null;
2679        } else {
2680            toolbarIcon.setBounds(0, 0, w, h);
2681            if (button != null) {
2682                button.setCompoundDrawables(toolbarIcon, null, null, null);
2683            }
2684            return toolbarIcon.getConstantState();
2685        }
2686    }
2687
2688    // if successful in getting icon, return it; otherwise, set button to use default drawable
2689    private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2690            int buttonId, ComponentName activityName, int fallbackDrawableId) {
2691        ImageView button = (ImageView) findViewById(buttonId);
2692        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2693
2694        if (button != null) {
2695            // If we were unable to find the icon via the meta-data, use a
2696            // generic one
2697            if (toolbarIcon == null) {
2698                button.setImageResource(fallbackDrawableId);
2699            } else {
2700                button.setImageDrawable(toolbarIcon);
2701            }
2702        }
2703
2704        return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2705
2706    }
2707
2708    private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2709        TextView button = (TextView) findViewById(buttonId);
2710        button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2711    }
2712
2713    private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2714        ImageView button = (ImageView) findViewById(buttonId);
2715        button.setImageDrawable(d.newDrawable(getResources()));
2716    }
2717
2718    private void invalidatePressedFocusedStates(View container, View button) {
2719        if (container instanceof HolographicLinearLayout) {
2720            HolographicLinearLayout layout = (HolographicLinearLayout) container;
2721            layout.invalidatePressedFocusedStates();
2722        } else if (button instanceof HolographicImageView) {
2723            HolographicImageView view = (HolographicImageView) button;
2724            view.invalidatePressedFocusedStates();
2725        }
2726    }
2727
2728    private boolean updateGlobalSearchIcon() {
2729        final View searchButtonContainer = findViewById(R.id.search_button_container);
2730        final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2731        final View searchDivider = findViewById(R.id.search_divider);
2732        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2733        final View voiceButton = findViewById(R.id.voice_button);
2734
2735        final SearchManager searchManager =
2736                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2737        ComponentName activityName = searchManager.getGlobalSearchActivity();
2738        if (activityName != null) {
2739            int coi = getCurrentOrientationIndexForGlobalIcons();
2740            sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2741                    R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
2742            if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
2743            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2744            searchButton.setVisibility(View.VISIBLE);
2745            invalidatePressedFocusedStates(searchButtonContainer, searchButton);
2746            return true;
2747        } else {
2748            // We disable both search and voice search when there is no global search provider
2749            if (searchDivider != null) searchDivider.setVisibility(View.GONE);
2750            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2751            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2752            searchButton.setVisibility(View.GONE);
2753            voiceButton.setVisibility(View.GONE);
2754            return false;
2755        }
2756    }
2757
2758    private void updateGlobalSearchIcon(Drawable.ConstantState d) {
2759        final View searchButtonContainer = findViewById(R.id.search_button_container);
2760        final View searchButton = (ImageView) findViewById(R.id.search_button);
2761        updateButtonWithDrawable(R.id.search_button, d);
2762        invalidatePressedFocusedStates(searchButtonContainer, searchButton);
2763    }
2764
2765    private boolean updateVoiceSearchIcon(boolean searchVisible) {
2766        final View searchDivider = findViewById(R.id.search_divider);
2767        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2768        final View voiceButton = findViewById(R.id.voice_button);
2769
2770        // We only show/update the voice search icon if the search icon is enabled as well
2771        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2772        ComponentName activityName = intent.resolveActivity(getPackageManager());
2773        if (searchVisible && activityName != null) {
2774            int coi = getCurrentOrientationIndexForGlobalIcons();
2775            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2776                    R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
2777            if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
2778            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
2779            voiceButton.setVisibility(View.VISIBLE);
2780            invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
2781            return true;
2782        } else {
2783            if (searchDivider != null) searchDivider.setVisibility(View.GONE);
2784            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2785            voiceButton.setVisibility(View.GONE);
2786            return false;
2787        }
2788    }
2789
2790    private void updateVoiceSearchIcon(Drawable.ConstantState d) {
2791        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2792        final View voiceButton = findViewById(R.id.voice_button);
2793        updateButtonWithDrawable(R.id.voice_button, d);
2794        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
2795    }
2796
2797    /**
2798     * Sets the app market icon
2799     */
2800    private void updateAppMarketIcon() {
2801        final View marketButton = findViewById(R.id.market_button);
2802        Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2803        // Find the app market activity by resolving an intent.
2804        // (If multiple app markets are installed, it will return the ResolverActivity.)
2805        ComponentName activityName = intent.resolveActivity(getPackageManager());
2806        if (activityName != null) {
2807            int coi = getCurrentOrientationIndexForGlobalIcons();
2808            mAppMarketIntent = intent;
2809            sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
2810                    R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
2811            marketButton.setVisibility(View.VISIBLE);
2812        } else {
2813            // We should hide and disable the view so that we don't try and restore the visibility
2814            // of it when we swap between drag & normal states from IconDropTarget subclasses.
2815            marketButton.setVisibility(View.GONE);
2816            marketButton.setEnabled(false);
2817        }
2818    }
2819
2820    private void updateAppMarketIcon(Drawable.ConstantState d) {
2821        updateTextButtonWithDrawable(R.id.market_button, d);
2822    }
2823
2824    /**
2825     * Receives notifications when system dialogs are to be closed.
2826     */
2827    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2828        @Override
2829        public void onReceive(Context context, Intent intent) {
2830            closeSystemDialogs();
2831        }
2832    }
2833
2834    /**
2835     * Receives notifications whenever the appwidgets are reset.
2836     */
2837    private class AppWidgetResetObserver extends ContentObserver {
2838        public AppWidgetResetObserver() {
2839            super(new Handler());
2840        }
2841
2842        @Override
2843        public void onChange(boolean selfChange) {
2844            onAppWidgetReset();
2845        }
2846    }
2847
2848    /**
2849     * If the activity is currently paused, signal that we need to re-run the loader
2850     * in onResume.
2851     *
2852     * This needs to be called from incoming places where resources might have been loaded
2853     * while we are paused.  That is becaues the Configuration might be wrong
2854     * when we're not running, and if it comes back to what it was when we
2855     * were paused, we are not restarted.
2856     *
2857     * Implementation of the method from LauncherModel.Callbacks.
2858     *
2859     * @return true if we are currently paused.  The caller might be able to
2860     * skip some work in that case since we will come back again.
2861     */
2862    public boolean setLoadOnResume() {
2863        if (mPaused) {
2864            Log.i(TAG, "setLoadOnResume");
2865            mOnResumeNeedsLoad = true;
2866            return true;
2867        } else {
2868            return false;
2869        }
2870    }
2871
2872    /**
2873     * Implementation of the method from LauncherModel.Callbacks.
2874     */
2875    public int getCurrentWorkspaceScreen() {
2876        if (mWorkspace != null) {
2877            return mWorkspace.getCurrentPage();
2878        } else {
2879            return SCREEN_COUNT / 2;
2880        }
2881    }
2882
2883    /**
2884     * Refreshes the shortcuts shown on the workspace.
2885     *
2886     * Implementation of the method from LauncherModel.Callbacks.
2887     */
2888    public void startBinding() {
2889        final Workspace workspace = mWorkspace;
2890
2891        mNewShortcutAnimatePage = -1;
2892        mNewShortcutAnimateViews.clear();
2893        mWorkspace.clearDropTargets();
2894        int count = workspace.getChildCount();
2895        for (int i = 0; i < count; i++) {
2896            // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
2897            final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2898            layoutParent.removeAllViewsInLayout();
2899        }
2900        mWidgetsToAdvance.clear();
2901        if (mHotseat != null) {
2902            mHotseat.resetLayout();
2903        }
2904    }
2905
2906    /**
2907     * Bind the items start-end from the list.
2908     *
2909     * Implementation of the method from LauncherModel.Callbacks.
2910     */
2911    public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2912        setLoadOnResume();
2913
2914        // Get the list of added shortcuts and intersect them with the set of shortcuts here
2915        Set<String> newApps = new HashSet<String>();
2916        newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2917
2918        Workspace workspace = mWorkspace;
2919        for (int i = start; i < end; i++) {
2920            final ItemInfo item = shortcuts.get(i);
2921
2922            // Short circuit if we are loading dock items for a configuration which has no dock
2923            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2924                    mHotseat == null) {
2925                continue;
2926            }
2927
2928            switch (item.itemType) {
2929                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2930                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2931                    ShortcutInfo info = (ShortcutInfo) item;
2932                    String uri = info.intent.toUri(0).toString();
2933                    View shortcut = createShortcut(info);
2934                    workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2935                            item.cellY, 1, 1, false);
2936                    if (newApps.contains(uri)) {
2937                        newApps.remove(uri);
2938
2939                        // Prepare the view to be animated up
2940                        shortcut.setAlpha(0f);
2941                        shortcut.setScaleX(0f);
2942                        shortcut.setScaleY(0f);
2943                        mNewShortcutAnimatePage = item.screen;
2944                        if (!mNewShortcutAnimateViews.contains(shortcut)) {
2945                            mNewShortcutAnimateViews.add(shortcut);
2946                        }
2947                    }
2948                    break;
2949                case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2950                    FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2951                            (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
2952                            (FolderInfo) item, mIconCache);
2953                    workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2954                            item.cellY, 1, 1, false);
2955                    break;
2956            }
2957        }
2958
2959        workspace.requestLayout();
2960    }
2961
2962    /**
2963     * Implementation of the method from LauncherModel.Callbacks.
2964     */
2965    public void bindFolders(HashMap<Long, FolderInfo> folders) {
2966        setLoadOnResume();
2967        sFolders.clear();
2968        sFolders.putAll(folders);
2969    }
2970
2971    /**
2972     * Add the views for a widget to the workspace.
2973     *
2974     * Implementation of the method from LauncherModel.Callbacks.
2975     */
2976    public void bindAppWidget(LauncherAppWidgetInfo item) {
2977        setLoadOnResume();
2978
2979        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2980        if (DEBUG_WIDGETS) {
2981            Log.d(TAG, "bindAppWidget: " + item);
2982        }
2983        final Workspace workspace = mWorkspace;
2984
2985        final int appWidgetId = item.appWidgetId;
2986        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2987        if (DEBUG_WIDGETS) {
2988            Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2989        }
2990
2991        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2992
2993        item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2994        item.hostView.setTag(item);
2995
2996        workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
2997                item.cellY, item.spanX, item.spanY, false);
2998
2999        addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3000
3001        workspace.requestLayout();
3002
3003        if (DEBUG_WIDGETS) {
3004            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3005                    + (SystemClock.uptimeMillis()-start) + "ms");
3006        }
3007    }
3008
3009    /**
3010     * Callback saying that there aren't any more items to bind.
3011     *
3012     * Implementation of the method from LauncherModel.Callbacks.
3013     */
3014    public void finishBindingItems() {
3015        setLoadOnResume();
3016
3017        if (mSavedState != null) {
3018            if (!mWorkspace.hasFocus()) {
3019                mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
3020            }
3021            mSavedState = null;
3022        }
3023
3024        if (mSavedInstanceState != null) {
3025            super.onRestoreInstanceState(mSavedInstanceState);
3026            mSavedInstanceState = null;
3027        }
3028
3029        // If we received the result of any pending adds while the loader was running (e.g. the
3030        // widget configuration forced an orientation change), process them now.
3031        for (int i = 0; i < sPendingAddList.size(); i++) {
3032            completeAdd(sPendingAddList.get(i));
3033        }
3034        sPendingAddList.clear();
3035
3036        // Update the market app icon as necessary (the other icons will be managed in response to
3037        // package changes in bindSearchablesChanged()
3038        updateAppMarketIcon();
3039
3040        // Animate up any icons as necessary
3041        if (mVisible || mWorkspaceLoading) {
3042            Runnable newAppsRunnable = new Runnable() {
3043                @Override
3044                public void run() {
3045                    runNewAppsAnimation(false);
3046                }
3047            };
3048
3049            boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3050                    mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3051            if (canRunNewAppsAnimation()) {
3052                // If the user has not interacted recently, then either snap to the new page to show
3053                // the new-apps animation or just run them if they are to appear on the current page
3054                if (willSnapPage) {
3055                    mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3056                } else {
3057                    runNewAppsAnimation(false);
3058                }
3059            } else {
3060                // If the user has interacted recently, then just add the items in place if they
3061                // are on another page (or just normally if they are added to the current page)
3062                runNewAppsAnimation(willSnapPage);
3063            }
3064        }
3065
3066        mWorkspaceLoading = false;
3067    }
3068
3069    private boolean canRunNewAppsAnimation() {
3070        long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3071        return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3072    }
3073
3074    /**
3075     * Runs a new animation that scales up icons that were added while Launcher was in the
3076     * background.
3077     *
3078     * @param immediate whether to run the animation or show the results immediately
3079     */
3080    private void runNewAppsAnimation(boolean immediate) {
3081        AnimatorSet anim = new AnimatorSet();
3082        Collection<Animator> bounceAnims = new ArrayList<Animator>();
3083
3084        // Order these new views spatially so that they animate in order
3085        Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3086            @Override
3087            public int compare(View a, View b) {
3088                CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3089                CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3090                int cellCountX = LauncherModel.getCellCountX();
3091                return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3092            }
3093        });
3094
3095        // Animate each of the views in place (or show them immediately if requested)
3096        if (immediate) {
3097            for (View v : mNewShortcutAnimateViews) {
3098                v.setAlpha(1f);
3099                v.setScaleX(1f);
3100                v.setScaleY(1f);
3101            }
3102        } else {
3103            for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3104                View v = mNewShortcutAnimateViews.get(i);
3105                ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3106                        PropertyValuesHolder.ofFloat("alpha", 1f),
3107                        PropertyValuesHolder.ofFloat("scaleX", 1f),
3108                        PropertyValuesHolder.ofFloat("scaleY", 1f));
3109                bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3110                bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3111                bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3112                bounceAnims.add(bounceAnim);
3113            }
3114            anim.playTogether(bounceAnims);
3115            anim.addListener(new AnimatorListenerAdapter() {
3116                @Override
3117                public void onAnimationEnd(Animator animation) {
3118                    mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3119                }
3120            });
3121            anim.start();
3122        }
3123
3124        // Clean up
3125        mNewShortcutAnimatePage = -1;
3126        mNewShortcutAnimateViews.clear();
3127        new Thread("clearNewAppsThread") {
3128            public void run() {
3129                mSharedPrefs.edit()
3130                            .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3131                            .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3132                            .commit();
3133            }
3134        }.start();
3135    }
3136
3137    @Override
3138    public void bindSearchablesChanged() {
3139        boolean searchVisible = updateGlobalSearchIcon();
3140        boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3141        mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3142    }
3143
3144    /**
3145     * Add the icons for all apps.
3146     *
3147     * Implementation of the method from LauncherModel.Callbacks.
3148     */
3149    public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3150        // Remove the progress bar entirely; we could also make it GONE
3151        // but better to remove it since we know it's not going to be used
3152        View progressBar = mAppsCustomizeTabHost.
3153            findViewById(R.id.apps_customize_progress_bar);
3154        if (progressBar != null) {
3155            ((ViewGroup)progressBar.getParent()).removeView(progressBar);
3156        }
3157        // We just post the call to setApps so the user sees the progress bar
3158        // disappear-- otherwise, it just looks like the progress bar froze
3159        // which doesn't look great
3160        mAppsCustomizeTabHost.post(new Runnable() {
3161            public void run() {
3162                if (mAppsCustomizeContent != null) {
3163                    mAppsCustomizeContent.setApps(apps);
3164                }
3165            }
3166        });
3167    }
3168
3169    /**
3170     * A package was installed.
3171     *
3172     * Implementation of the method from LauncherModel.Callbacks.
3173     */
3174    public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
3175        setLoadOnResume();
3176
3177        if (mAppsCustomizeContent != null) {
3178            mAppsCustomizeContent.addApps(apps);
3179        }
3180    }
3181
3182    /**
3183     * A package was updated.
3184     *
3185     * Implementation of the method from LauncherModel.Callbacks.
3186     */
3187    public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
3188        setLoadOnResume();
3189        if (mWorkspace != null) {
3190            mWorkspace.updateShortcuts(apps);
3191        }
3192
3193        if (mAppsCustomizeContent != null) {
3194            mAppsCustomizeContent.updateApps(apps);
3195        }
3196    }
3197
3198    /**
3199     * A package was uninstalled.
3200     *
3201     * Implementation of the method from LauncherModel.Callbacks.
3202     */
3203    public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
3204        if (permanent) {
3205            mWorkspace.removeItems(apps);
3206        }
3207
3208        if (mAppsCustomizeContent != null) {
3209            mAppsCustomizeContent.removeApps(apps);
3210        }
3211
3212        // Notify the drag controller
3213        mDragController.onAppsRemoved(apps, this);
3214    }
3215
3216    /**
3217     * A number of packages were updated.
3218     */
3219    public void bindPackagesUpdated() {
3220        if (mAppsCustomizeContent != null) {
3221            mAppsCustomizeContent.onPackagesUpdated();
3222        }
3223    }
3224
3225    private int mapConfigurationOriActivityInfoOri(int configOri) {
3226        final Display d = getWindowManager().getDefaultDisplay();
3227        int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3228        switch (d.getRotation()) {
3229        case Surface.ROTATION_0:
3230        case Surface.ROTATION_180:
3231            // We are currently in the same basic orientation as the natural orientation
3232            naturalOri = configOri;
3233            break;
3234        case Surface.ROTATION_90:
3235        case Surface.ROTATION_270:
3236            // We are currently in the other basic orientation to the natural orientation
3237            naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3238                    Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3239            break;
3240        }
3241
3242        int[] oriMap = {
3243                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3244                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3245                ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3246                ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3247        };
3248        // Since the map starts at portrait, we need to offset if this device's natural orientation
3249        // is landscape.
3250        int indexOffset = 0;
3251        if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3252            indexOffset = 1;
3253        }
3254        return oriMap[(d.getRotation() + indexOffset) % 4];
3255    }
3256
3257    public void lockScreenOrientationOnLargeUI() {
3258        if (LauncherApplication.isScreenLarge()) {
3259            setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3260                    .getConfiguration().orientation));
3261        }
3262    }
3263    public void unlockScreenOrientationOnLargeUI() {
3264        if (LauncherApplication.isScreenLarge()) {
3265            mHandler.postDelayed(new Runnable() {
3266                public void run() {
3267                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3268                }
3269            }, mRestoreScreenOrientationDelay);
3270        }
3271    }
3272
3273    /* Cling related */
3274    private boolean isClingsEnabled() {
3275        // disable clings when running in a test harness
3276        if(ActivityManager.isRunningInTestHarness()) return false;
3277
3278        return true;
3279    }
3280    private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3281        Cling cling = (Cling) findViewById(clingId);
3282        if (cling != null) {
3283            cling.init(this, positionData);
3284            cling.setVisibility(View.VISIBLE);
3285            cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3286            if (animate) {
3287                cling.buildLayer();
3288                cling.setAlpha(0f);
3289                cling.animate()
3290                    .alpha(1f)
3291                    .setInterpolator(new AccelerateInterpolator())
3292                    .setDuration(SHOW_CLING_DURATION)
3293                    .setStartDelay(delay)
3294                    .start();
3295            } else {
3296                cling.setAlpha(1f);
3297            }
3298        }
3299        return cling;
3300    }
3301    private void dismissCling(final Cling cling, final String flag, int duration) {
3302        if (cling != null) {
3303            ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3304            anim.setDuration(duration);
3305            anim.addListener(new AnimatorListenerAdapter() {
3306                public void onAnimationEnd(Animator animation) {
3307                    cling.setVisibility(View.GONE);
3308                    cling.cleanup();
3309                    // We should update the shared preferences on a background thread
3310                    new Thread("dismissClingThread") {
3311                        public void run() {
3312                            SharedPreferences.Editor editor = mSharedPrefs.edit();
3313                            editor.putBoolean(flag, true);
3314                            editor.commit();
3315                        }
3316                    }.start();
3317                };
3318            });
3319            anim.start();
3320        }
3321    }
3322    private void removeCling(int id) {
3323        final View cling = findViewById(id);
3324        if (cling != null) {
3325            final ViewGroup parent = (ViewGroup) cling.getParent();
3326            parent.post(new Runnable() {
3327                @Override
3328                public void run() {
3329                    parent.removeView(cling);
3330                }
3331            });
3332        }
3333    }
3334    public void showFirstRunWorkspaceCling() {
3335        // Enable the clings only if they have not been dismissed before
3336        if (isClingsEnabled() &&
3337                !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
3338            initCling(R.id.workspace_cling, null, false, 0);
3339        } else {
3340            removeCling(R.id.workspace_cling);
3341        }
3342    }
3343    public void showFirstRunAllAppsCling(int[] position) {
3344        // Enable the clings only if they have not been dismissed before
3345        if (isClingsEnabled() &&
3346                !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
3347            initCling(R.id.all_apps_cling, position, true, 0);
3348        } else {
3349            removeCling(R.id.all_apps_cling);
3350        }
3351    }
3352    public Cling showFirstRunFoldersCling() {
3353        // Enable the clings only if they have not been dismissed before
3354        if (isClingsEnabled() &&
3355                !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3356            return initCling(R.id.folder_cling, null, true, 0);
3357        } else {
3358            removeCling(R.id.folder_cling);
3359            return null;
3360        }
3361    }
3362    public boolean isFolderClingVisible() {
3363        Cling cling = (Cling) findViewById(R.id.folder_cling);
3364        if (cling != null) {
3365            return cling.getVisibility() == View.VISIBLE;
3366        }
3367        return false;
3368    }
3369    public void dismissWorkspaceCling(View v) {
3370        Cling cling = (Cling) findViewById(R.id.workspace_cling);
3371        dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3372    }
3373    public void dismissAllAppsCling(View v) {
3374        Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3375        dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3376    }
3377    public void dismissFolderCling(View v) {
3378        Cling cling = (Cling) findViewById(R.id.folder_cling);
3379        dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3380    }
3381
3382    /**
3383     * Prints out out state for debugging.
3384     */
3385    public void dumpState() {
3386        Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
3387        Log.d(TAG, "mSavedState=" + mSavedState);
3388        Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3389        Log.d(TAG, "mRestoring=" + mRestoring);
3390        Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3391        Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3392        Log.d(TAG, "sFolders.size=" + sFolders.size());
3393        mModel.dumpState();
3394
3395        if (mAppsCustomizeContent != null) {
3396            mAppsCustomizeContent.dumpState();
3397        }
3398        Log.d(TAG, "END launcher2 dump state");
3399    }
3400
3401    @Override
3402    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3403        super.dump(prefix, fd, writer, args);
3404        writer.println(" ");
3405        writer.println("Debug logs: ");
3406        for (int i = 0; i < sDumpLogs.size(); i++) {
3407            writer.println("  " + sDumpLogs.get(i));
3408        }
3409    }
3410}
3411
3412interface LauncherTransitionable {
3413    View getContent();
3414    void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
3415    void onLauncherTransitionStep(Launcher l, float t);
3416    void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
3417}
3418