Launcher.java revision d805837a877bb503d7890c4f92cfd183dec8c5ee
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.launcher3;
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.ActivityOptions;
30import android.app.SearchManager;
31import android.appwidget.AppWidgetHostView;
32import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
34import android.content.ActivityNotFoundException;
35import android.content.BroadcastReceiver;
36import android.content.ComponentCallbacks2;
37import android.content.ComponentName;
38import android.content.ContentResolver;
39import android.content.Context;
40import android.content.Intent;
41import android.content.IntentFilter;
42import android.content.SharedPreferences;
43import android.content.pm.ActivityInfo;
44import android.content.pm.ApplicationInfo;
45import android.content.pm.PackageManager;
46import android.content.pm.PackageManager.NameNotFoundException;
47import android.content.pm.ResolveInfo;
48import android.content.res.Configuration;
49import android.content.res.Resources;
50import android.database.ContentObserver;
51import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.Point;
54import android.graphics.PorterDuff;
55import android.graphics.Rect;
56import android.graphics.drawable.Drawable;
57import android.net.Uri;
58import android.os.AsyncTask;
59import android.os.Bundle;
60import android.os.Environment;
61import android.os.Handler;
62import android.os.Message;
63import android.os.StrictMode;
64import android.os.SystemClock;
65import android.provider.Settings;
66import android.speech.RecognizerIntent;
67import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
71import android.util.DisplayMetrics;
72import android.util.Log;
73import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
82import android.view.View.OnClickListener;
83import android.view.View.OnLongClickListener;
84import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
86import android.view.ViewTreeObserver.OnGlobalLayoutListener;
87import android.view.WindowManager;
88import android.view.accessibility.AccessibilityEvent;
89import android.view.animation.AccelerateDecelerateInterpolator;
90import android.view.animation.DecelerateInterpolator;
91import android.view.inputmethod.InputMethodManager;
92import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
97
98import com.android.launcher3.DropTarget.DragObject;
99
100import java.io.DataInputStream;
101import java.io.DataOutputStream;
102import java.io.File;
103import java.io.FileDescriptor;
104import java.io.FileNotFoundException;
105import java.io.FileOutputStream;
106import java.io.IOException;
107import java.io.PrintWriter;
108import java.text.DateFormat;
109import java.util.ArrayList;
110import java.util.Collection;
111import java.util.Date;
112import java.util.HashMap;
113import java.util.List;
114import java.util.concurrent.atomic.AtomicInteger;
115
116
117/**
118 * Default launcher application.
119 */
120public class Launcher extends Activity
121        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
122                   View.OnTouchListener {
123    static final String TAG = "Launcher";
124    static final boolean LOGD = false;
125
126    static final boolean PROFILE_STARTUP = false;
127    static final boolean DEBUG_WIDGETS = false;
128    static final boolean DEBUG_STRICT_MODE = false;
129    static final boolean DEBUG_RESUME_TIME = false;
130    static final boolean DEBUG_DUMP_LOG = false;
131
132    static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
133
134    private static final int REQUEST_CREATE_SHORTCUT = 1;
135    private static final int REQUEST_CREATE_APPWIDGET = 5;
136    private static final int REQUEST_PICK_APPLICATION = 6;
137    private static final int REQUEST_PICK_SHORTCUT = 7;
138    private static final int REQUEST_PICK_APPWIDGET = 9;
139    private static final int REQUEST_PICK_WALLPAPER = 10;
140
141    private static final int REQUEST_BIND_APPWIDGET = 11;
142
143    /**
144     * IntentStarter uses request codes starting with this. This must be greater than all activity
145     * request codes used internally.
146     */
147    protected static final int REQUEST_LAST = 100;
148
149    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
151    static final int SCREEN_COUNT = 5;
152    static final int DEFAULT_SCREEN = 2;
153
154    private static final String PREFERENCES = "launcher.preferences";
155    // To turn on these properties, type
156    // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
157    static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
158    static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
159    static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
160
161    // The Intent extra that defines whether to ignore the launch animation
162    static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
163            "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
164
165    // Type: int
166    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
167    // Type: int
168    private static final String RUNTIME_STATE = "launcher.state";
169    // Type: int
170    private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171    // Type: int
172    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173    // Type: int
174    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
175    // Type: int
176    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
177    // Type: boolean
178    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179    // Type: long
180    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
181    // Type: int
182    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183    // Type: int
184    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185    // Type: parcelable
186    private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
187    // Type: parcelable
188    private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
189    // Type: int[]
190    private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
191
192
193    static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
194
195    private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
196    private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
197            "com.android.launcher.toolbar_search_icon";
198    private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
199            "com.android.launcher.toolbar_voice_search_icon";
200
201    public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
202    public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
203
204    public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
206    /** The different states that Launcher can be in. */
207    private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
208    private State mState = State.WORKSPACE;
209    private AnimatorSet mStateAnimation;
210
211    static final int APPWIDGET_HOST_ID = 1024;
212    public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
213    private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
214
215    private static final Object sLock = new Object();
216    private static int sScreen = DEFAULT_SCREEN;
217
218    private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
219    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
220
221    // How long to wait before the new-shortcut animation automatically pans the workspace
222    private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
223    private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
224    private static int NEW_APPS_ANIMATION_DELAY = 500;
225
226    private final BroadcastReceiver mCloseSystemDialogsReceiver
227            = new CloseSystemDialogsIntentReceiver();
228    private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
229
230    private LayoutInflater mInflater;
231
232    private Workspace mWorkspace;
233    private View mLauncherView;
234    private View mPageIndicators;
235    private DragLayer mDragLayer;
236    private DragController mDragController;
237    private View mWeightWatcher;
238    private LauncherClings mLauncherClings;
239
240    private AppWidgetManager mAppWidgetManager;
241    private LauncherAppWidgetHost mAppWidgetHost;
242
243    private ItemInfo mPendingAddInfo = new ItemInfo();
244    private AppWidgetProviderInfo mPendingAddWidgetInfo;
245    private int mPendingAddWidgetId = -1;
246
247    private int[] mTmpAddItemCellCoordinates = new int[2];
248
249    private FolderInfo mFolderInfo;
250
251    private Hotseat mHotseat;
252    private ViewGroup mOverviewPanel;
253
254    private View mAllAppsButton;
255
256    private SearchDropTargetBar mSearchDropTargetBar;
257    private AppsCustomizeTabHost mAppsCustomizeTabHost;
258    private AppsCustomizePagedView mAppsCustomizeContent;
259    private boolean mAutoAdvanceRunning = false;
260    private View mQsb;
261
262    private Bundle mSavedState;
263    // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264    // scroll issues (because the workspace may not have been measured yet) and extra work.
265    // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266    private State mOnResumeState = State.NONE;
267
268    private SpannableStringBuilder mDefaultKeySsb = null;
269
270    private boolean mWorkspaceLoading = true;
271
272    private boolean mPaused = true;
273    private boolean mRestoring;
274    private boolean mWaitingForResult;
275    private boolean mOnResumeNeedsLoad;
276
277    private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
278    private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
279
280    // Keep track of whether the user has left launcher
281    private static boolean sPausedFromUserAction = false;
282
283    private Bundle mSavedInstanceState;
284
285    private LauncherModel mModel;
286    private IconCache mIconCache;
287    private boolean mUserPresent = true;
288    private boolean mVisible = false;
289    private boolean mHasFocus = false;
290    private boolean mAttached = false;
291
292    private static LocaleConfiguration sLocaleConfiguration = null;
293
294    private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
295
296    private View.OnTouchListener mHapticFeedbackTouchListener;
297
298    // Related to the auto-advancing of widgets
299    private final int ADVANCE_MSG = 1;
300    private final int mAdvanceInterval = 20000;
301    private final int mAdvanceStagger = 250;
302    private long mAutoAdvanceSentTime;
303    private long mAutoAdvanceTimeLeft = -1;
304    private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
305        new HashMap<View, AppWidgetProviderInfo>();
306
307    // Determines how long to wait after a rotation before restoring the screen orientation to
308    // match the sensor state.
309    private final int mRestoreScreenOrientationDelay = 500;
310
311    // External icons saved in case of resource changes, orientation, etc.
312    private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
313    private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
314    private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
315
316    private Intent mAppMarketIntent = null;
317    private static final boolean DISABLE_MARKET_BUTTON = true;
318
319    private Drawable mWorkspaceBackgroundDrawable;
320
321    private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
322    private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
323
324    static final ArrayList<String> sDumpLogs = new ArrayList<String>();
325    static Date sDateStamp = new Date();
326    static DateFormat sDateFormat =
327            DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
328    static long sRunStart = System.currentTimeMillis();
329    static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
330
331    // We only want to get the SharedPreferences once since it does an FS stat each time we get
332    // it from the context.
333    private SharedPreferences mSharedPrefs;
334
335    private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
336
337    // Holds the page that we need to animate to, and the icon views that we need to animate up
338    // when we scroll to that page on resume.
339    private ImageView mFolderIconImageView;
340    private Bitmap mFolderIconBitmap;
341    private Canvas mFolderIconCanvas;
342    private Rect mRectForFolderAnimation = new Rect();
343
344    private BubbleTextView mWaitingForResume;
345
346    private Runnable mBuildLayersRunnable = new Runnable() {
347        public void run() {
348            if (mWorkspace != null) {
349                mWorkspace.buildPageHardwareLayers();
350            }
351        }
352    };
353
354    private static ArrayList<PendingAddArguments> sPendingAddList
355            = new ArrayList<PendingAddArguments>();
356
357    public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
358
359    private static class PendingAddArguments {
360        int requestCode;
361        Intent intent;
362        long container;
363        long screenId;
364        int cellX;
365        int cellY;
366    }
367
368    private Stats mStats;
369
370    static boolean isPropertyEnabled(String propertyName) {
371        return Log.isLoggable(propertyName, Log.VERBOSE);
372    }
373
374    @Override
375    protected void onCreate(Bundle savedInstanceState) {
376        if (DEBUG_STRICT_MODE) {
377            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378                    .detectDiskReads()
379                    .detectDiskWrites()
380                    .detectNetwork()   // or .detectAll() for all detectable problems
381                    .penaltyLog()
382                    .build());
383            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384                    .detectLeakedSqlLiteObjects()
385                    .detectLeakedClosableObjects()
386                    .penaltyLog()
387                    .penaltyDeath()
388                    .build());
389        }
390
391        super.onCreate(savedInstanceState);
392
393        LauncherAppState.setApplicationContext(getApplicationContext());
394        LauncherAppState app = LauncherAppState.getInstance();
395
396        // Determine the dynamic grid properties
397        Point smallestSize = new Point();
398        Point largestSize = new Point();
399        Point realSize = new Point();
400        Display display = getWindowManager().getDefaultDisplay();
401        display.getCurrentSizeRange(smallestSize, largestSize);
402        display.getRealSize(realSize);
403        DisplayMetrics dm = new DisplayMetrics();
404        display.getMetrics(dm);
405
406        // Lazy-initialize the dynamic grid
407        DeviceProfile grid = app.initDynamicGrid(this,
408                Math.min(smallestSize.x, smallestSize.y),
409                Math.min(largestSize.x, largestSize.y),
410                realSize.x, realSize.y,
411                dm.widthPixels, dm.heightPixels);
412
413        // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
414        mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
415                Context.MODE_PRIVATE);
416        mModel = app.setLauncher(this);
417        mIconCache = app.getIconCache();
418        mIconCache.flushInvalidIcons(grid);
419        mDragController = new DragController(this);
420        mLauncherClings = new LauncherClings(this);
421        mInflater = getLayoutInflater();
422
423        mStats = new Stats(this);
424
425        mAppWidgetManager = AppWidgetManager.getInstance(this);
426
427        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428        mAppWidgetHost.startListening();
429
430        // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431        // this also ensures that any synchronous binding below doesn't re-trigger another
432        // LauncherModel load.
433        mPaused = false;
434
435        if (PROFILE_STARTUP) {
436            android.os.Debug.startMethodTracing(
437                    Environment.getExternalStorageDirectory() + "/launcher");
438        }
439
440
441        checkForLocaleChange();
442        setContentView(R.layout.launcher);
443
444        setupViews();
445        grid.layout(this);
446
447        registerContentObservers();
448
449        lockAllApps();
450
451        mSavedState = savedInstanceState;
452        restoreState(mSavedState);
453
454        if (PROFILE_STARTUP) {
455            android.os.Debug.stopMethodTracing();
456        }
457
458        if (!mRestoring) {
459            if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
460                // If the user leaves launcher, then we should just load items asynchronously when
461                // they return.
462                mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
463            } else {
464                // We only load the page synchronously if the user rotates (or triggers a
465                // configuration change) while launcher is in the foreground
466                mModel.startLoader(true, mWorkspace.getRestorePage());
467            }
468        }
469
470        // For handling default keys
471        mDefaultKeySsb = new SpannableStringBuilder();
472        Selection.setSelection(mDefaultKeySsb, 0);
473
474        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
475        registerReceiver(mCloseSystemDialogsReceiver, filter);
476
477        updateGlobalIcons();
478
479        // On large interfaces, we want the screen to auto-rotate based on the current orientation
480        unlockScreenOrientation(true);
481
482        // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
483        // on the device, then we always show the first run cling experience (or if there is no
484        // launcher2). Otherwise, we prompt the user upon started for migration
485        showFirstRunActivity();
486        if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
487            if (mModel.canMigrateFromOldLauncherDb(this)) {
488                mLauncherClings.showMigrationCling();
489            } else {
490                mLauncherClings.showFirstRunCling();
491            }
492        } else {
493            mLauncherClings.removeFirstRunAndMigrationClings();
494        }
495    }
496
497    protected void onUserLeaveHint() {
498        super.onUserLeaveHint();
499        sPausedFromUserAction = true;
500    }
501
502    /** To be overriden by subclasses to hint to Launcher that we have custom content */
503    protected boolean hasCustomContentToLeft() {
504        return false;
505    }
506
507    /**
508     * To be overridden by subclasses to create the custom content and call
509     * {@link #addToCustomContentPage}. This will only be invoked if
510     * {@link #hasCustomContentToLeft()} is {@code true}.
511     */
512    protected void addCustomContentToLeft() {
513    }
514
515    /**
516     * To be overridden by subclasses to indicate that there is an activity to launch
517     * before showing the standard launcher experience.
518     */
519    protected boolean hasFirstRunActivity() {
520        return false;
521    }
522
523    /**
524     * To be overridden by subclasses to launch any first run activity
525     */
526    protected Intent getFirstRunActivity() {
527        return null;
528    }
529
530    /**
531     * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532     * ensure the custom content page is added or removed if necessary.
533     */
534    protected void invalidateHasCustomContentToLeft() {
535        if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
536            // Not bound yet, wait for bindScreens to be called.
537            return;
538        }
539
540        if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541            // Create the custom content page and call the subclass to populate it.
542            mWorkspace.createCustomContentPage();
543            addCustomContentToLeft();
544        } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545            mWorkspace.removeCustomContentPage();
546        }
547    }
548
549    private void updateGlobalIcons() {
550        boolean searchVisible = false;
551        boolean voiceVisible = false;
552        // If we have a saved version of these external icons, we load them up immediately
553        int coi = getCurrentOrientationIndexForGlobalIcons();
554        if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555                sAppMarketIcon[coi] == null) {
556            if (!DISABLE_MARKET_BUTTON) {
557                updateAppMarketIcon();
558            }
559            searchVisible = updateGlobalSearchIcon();
560            voiceVisible = updateVoiceSearchIcon(searchVisible);
561        }
562        if (sGlobalSearchIcon[coi] != null) {
563             updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
564             searchVisible = true;
565        }
566        if (sVoiceSearchIcon[coi] != null) {
567            updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
568            voiceVisible = true;
569        }
570        if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
571            updateAppMarketIcon(sAppMarketIcon[coi]);
572        }
573        if (mSearchDropTargetBar != null) {
574            mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575        }
576    }
577
578    private void checkForLocaleChange() {
579        if (sLocaleConfiguration == null) {
580            new AsyncTask<Void, Void, LocaleConfiguration>() {
581                @Override
582                protected LocaleConfiguration doInBackground(Void... unused) {
583                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584                    readConfiguration(Launcher.this, localeConfiguration);
585                    return localeConfiguration;
586                }
587
588                @Override
589                protected void onPostExecute(LocaleConfiguration result) {
590                    sLocaleConfiguration = result;
591                    checkForLocaleChange();  // recursive, but now with a locale configuration
592                }
593            }.execute();
594            return;
595        }
596
597        final Configuration configuration = getResources().getConfiguration();
598
599        final String previousLocale = sLocaleConfiguration.locale;
600        final String locale = configuration.locale.toString();
601
602        final int previousMcc = sLocaleConfiguration.mcc;
603        final int mcc = configuration.mcc;
604
605        final int previousMnc = sLocaleConfiguration.mnc;
606        final int mnc = configuration.mnc;
607
608        boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
609
610        if (localeChanged) {
611            sLocaleConfiguration.locale = locale;
612            sLocaleConfiguration.mcc = mcc;
613            sLocaleConfiguration.mnc = mnc;
614
615            mIconCache.flush();
616
617            final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
618            new AsyncTask<Void, Void, Void>() {
619                public Void doInBackground(Void ... args) {
620                    writeConfiguration(Launcher.this, localeConfiguration);
621                    return null;
622                }
623            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
624        }
625    }
626
627    private static class LocaleConfiguration {
628        public String locale;
629        public int mcc = -1;
630        public int mnc = -1;
631    }
632
633    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634        DataInputStream in = null;
635        try {
636            in = new DataInputStream(context.openFileInput(PREFERENCES));
637            configuration.locale = in.readUTF();
638            configuration.mcc = in.readInt();
639            configuration.mnc = in.readInt();
640        } catch (FileNotFoundException e) {
641            // Ignore
642        } catch (IOException e) {
643            // Ignore
644        } finally {
645            if (in != null) {
646                try {
647                    in.close();
648                } catch (IOException e) {
649                    // Ignore
650                }
651            }
652        }
653    }
654
655    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656        DataOutputStream out = null;
657        try {
658            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659            out.writeUTF(configuration.locale);
660            out.writeInt(configuration.mcc);
661            out.writeInt(configuration.mnc);
662            out.flush();
663        } catch (FileNotFoundException e) {
664            // Ignore
665        } catch (IOException e) {
666            //noinspection ResultOfMethodCallIgnored
667            context.getFileStreamPath(PREFERENCES).delete();
668        } finally {
669            if (out != null) {
670                try {
671                    out.close();
672                } catch (IOException e) {
673                    // Ignore
674                }
675            }
676        }
677    }
678
679    public Stats getStats() {
680        return mStats;
681    }
682
683    public LayoutInflater getInflater() {
684        return mInflater;
685    }
686
687    boolean isDraggingEnabled() {
688        // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689        // that is subsequently removed from the workspace in startBinding().
690        return !mModel.isLoadingWorkspace();
691    }
692
693    static int getScreen() {
694        synchronized (sLock) {
695            return sScreen;
696        }
697    }
698
699    static void setScreen(int screen) {
700        synchronized (sLock) {
701            sScreen = screen;
702        }
703    }
704
705    /**
706     * Copied from View -- the View version of the method isn't called
707     * anywhere else in our process and only exists for API level 17+,
708     * so it's ok to keep our own version with no API requirement.
709     */
710    public static int generateViewId() {
711        for (;;) {
712            final int result = sNextGeneratedId.get();
713            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714            int newValue = result + 1;
715            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716            if (sNextGeneratedId.compareAndSet(result, newValue)) {
717                return result;
718            }
719        }
720    }
721
722    public int getViewIdForItem(ItemInfo info) {
723        // This cast is safe given the > 2B range for int.
724        int itemId = (int) info.id;
725        if (mItemIdToViewId.containsKey(itemId)) {
726            return mItemIdToViewId.get(itemId);
727        }
728        int viewId = generateViewId();
729        mItemIdToViewId.put(itemId, viewId);
730        return viewId;
731    }
732
733    /**
734     * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735     * a configuration step, this allows the proper animations to run after other transitions.
736     */
737    private boolean completeAdd(PendingAddArguments args) {
738        boolean result = false;
739        switch (args.requestCode) {
740            case REQUEST_PICK_APPLICATION:
741                completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
742                        args.cellY);
743                break;
744            case REQUEST_PICK_SHORTCUT:
745                processShortcut(args.intent);
746                break;
747            case REQUEST_CREATE_SHORTCUT:
748                completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
749                        args.cellY);
750                result = true;
751                break;
752            case REQUEST_CREATE_APPWIDGET:
753                int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
754                completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
755                result = true;
756                break;
757        }
758        // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
759        // if you turned the screen off and then back while in All Apps, Launcher would not
760        // return to the workspace. Clearing mAddInfo.container here fixes this issue
761        resetAddInfo();
762        return result;
763    }
764
765    @Override
766    protected void onActivityResult(
767            final int requestCode, final int resultCode, final Intent data) {
768        // Reset the startActivity waiting flag
769        mWaitingForResult = false;
770        int pendingAddWidgetId = mPendingAddWidgetId;
771        mPendingAddWidgetId = -1;
772
773        Runnable exitSpringLoaded = new Runnable() {
774            @Override
775            public void run() {
776                exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
777                        EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
778            }
779        };
780
781        if (requestCode == REQUEST_BIND_APPWIDGET) {
782            final int appWidgetId = data != null ?
783                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
784            if (resultCode == RESULT_CANCELED) {
785                completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
786                mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
787                        ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
788            } else if (resultCode == RESULT_OK) {
789                addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
790                        mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
791            }
792            return;
793        } else if (requestCode == REQUEST_PICK_WALLPAPER) {
794            if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
795                mWorkspace.exitOverviewMode(false);
796            }
797            return;
798        }
799
800        boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
801                requestCode == REQUEST_CREATE_APPWIDGET);
802
803        // We have special handling for widgets
804        if (isWidgetDrop) {
805            final int appWidgetId;
806            int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
807                    : -1;
808            if (widgetId < 0) {
809                appWidgetId = pendingAddWidgetId;
810            } else {
811                appWidgetId = widgetId;
812            }
813
814            final int result;
815            final Runnable onComplete;
816            if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
817                Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
818                        "widget configuration activity.");
819                result = RESULT_CANCELED;
820                completeTwoStageWidgetDrop(result, appWidgetId);
821                onComplete = new Runnable() {
822                    @Override
823                    public void run() {
824                        exitSpringLoadedDragModeDelayed(false, 0, null);
825                    }
826                };
827            } else {
828                result = resultCode;
829                final CellLayout dropLayout =
830                        (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
831                dropLayout.setDropPending(true);
832                onComplete = new Runnable() {
833                    @Override
834                    public void run() {
835                        completeTwoStageWidgetDrop(result, appWidgetId);
836                        dropLayout.setDropPending(false);
837                    }
838                };
839            }
840            mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
841                    false);
842            return;
843        }
844
845        // The pattern used here is that a user PICKs a specific application,
846        // which, depending on the target, might need to CREATE the actual target.
847
848        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
849        // launch over to the Music app to actually CREATE_SHORTCUT.
850        if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
851            final PendingAddArguments args = new PendingAddArguments();
852            args.requestCode = requestCode;
853            args.intent = data;
854            args.container = mPendingAddInfo.container;
855            args.screenId = mPendingAddInfo.screenId;
856            args.cellX = mPendingAddInfo.cellX;
857            args.cellY = mPendingAddInfo.cellY;
858            if (isWorkspaceLocked()) {
859                sPendingAddList.add(args);
860            } else {
861                completeAdd(args);
862            }
863            mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
864                    ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
865        } else if (resultCode == RESULT_CANCELED) {
866            mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
867                    ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
868        }
869        mDragLayer.clearAnimatedView();
870    }
871
872    private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
873        CellLayout cellLayout =
874                (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
875        Runnable onCompleteRunnable = null;
876        int animationType = 0;
877
878        AppWidgetHostView boundWidget = null;
879        if (resultCode == RESULT_OK) {
880            animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
881            final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
882                    mPendingAddWidgetInfo);
883            boundWidget = layout;
884            onCompleteRunnable = new Runnable() {
885                @Override
886                public void run() {
887                    completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
888                            mPendingAddInfo.screenId, layout, null);
889                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
890                            EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
891                }
892            };
893        } else if (resultCode == RESULT_CANCELED) {
894            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
895            animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
896        }
897        if (mDragLayer.getAnimatedView() != null) {
898            mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
899                    (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
900                    animationType, boundWidget, true);
901        } else if (onCompleteRunnable != null) {
902            // The animated view may be null in the case of a rotation during widget configuration
903            onCompleteRunnable.run();
904        }
905    }
906
907    @Override
908    protected void onStop() {
909        super.onStop();
910        FirstFrameAnimatorHelper.setIsVisible(false);
911    }
912
913    @Override
914    protected void onStart() {
915        super.onStart();
916        FirstFrameAnimatorHelper.setIsVisible(true);
917    }
918
919    @Override
920    protected void onResume() {
921        long startTime = 0;
922        if (DEBUG_RESUME_TIME) {
923            startTime = System.currentTimeMillis();
924            Log.v(TAG, "Launcher.onResume()");
925        }
926        super.onResume();
927
928        // Restore the previous launcher state
929        if (mOnResumeState == State.WORKSPACE) {
930            showWorkspace(false);
931        } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
932            showAllApps(false, mAppsCustomizeContent.getContentType(), false);
933        }
934        mOnResumeState = State.NONE;
935
936        // Background was set to gradient in onPause(), restore to black if in all apps.
937        setWorkspaceBackground(mState == State.WORKSPACE);
938
939        mPaused = false;
940        sPausedFromUserAction = false;
941        if (mRestoring || mOnResumeNeedsLoad) {
942            mWorkspaceLoading = true;
943            mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
944            mRestoring = false;
945            mOnResumeNeedsLoad = false;
946        }
947        if (mBindOnResumeCallbacks.size() > 0) {
948            // We might have postponed some bind calls until onResume (see waitUntilResume) --
949            // execute them here
950            long startTimeCallbacks = 0;
951            if (DEBUG_RESUME_TIME) {
952                startTimeCallbacks = System.currentTimeMillis();
953            }
954
955            if (mAppsCustomizeContent != null) {
956                mAppsCustomizeContent.setBulkBind(true);
957            }
958            for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
959                mBindOnResumeCallbacks.get(i).run();
960            }
961            if (mAppsCustomizeContent != null) {
962                mAppsCustomizeContent.setBulkBind(false);
963            }
964            mBindOnResumeCallbacks.clear();
965            if (DEBUG_RESUME_TIME) {
966                Log.d(TAG, "Time spent processing callbacks in onResume: " +
967                    (System.currentTimeMillis() - startTimeCallbacks));
968            }
969        }
970        if (mOnResumeCallbacks.size() > 0) {
971            for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
972                mOnResumeCallbacks.get(i).run();
973            }
974            mOnResumeCallbacks.clear();
975        }
976
977        // Reset the pressed state of icons that were locked in the press state while activities
978        // were launching
979        if (mWaitingForResume != null) {
980            // Resets the previous workspace icon press state
981            mWaitingForResume.setStayPressed(false);
982        }
983        if (mAppsCustomizeContent != null) {
984            // Resets the previous all apps icon press state
985            mAppsCustomizeContent.resetDrawableState();
986        }
987
988        // It is possible that widgets can receive updates while launcher is not in the foreground.
989        // Consequently, the widgets will be inflated in the orientation of the foreground activity
990        // (framework issue). On resuming, we ensure that any widgets are inflated for the current
991        // orientation.
992        getWorkspace().reinflateWidgetsIfNecessary();
993
994        // Process any items that were added while Launcher was away.
995        InstallShortcutReceiver.disableAndFlushInstallQueue(this);
996
997        // Update the voice search button proxy
998        updateVoiceButtonProxyVisible(false);
999
1000        // Again, as with the above scenario, it's possible that one or more of the global icons
1001        // were updated in the wrong orientation.
1002        updateGlobalIcons();
1003        if (DEBUG_RESUME_TIME) {
1004            Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1005        }
1006
1007        if (mWorkspace.getCustomContentCallbacks() != null) {
1008            // If we are resuming and the custom content is the current page, we call onShow().
1009            // It is also poassible that onShow will instead be called slightly after first layout
1010            // if PagedView#setRestorePage was set to the custom content page in onCreate().
1011            if (mWorkspace.isOnOrMovingToCustomContent()) {
1012                mWorkspace.getCustomContentCallbacks().onShow();
1013            }
1014        }
1015        mWorkspace.updateInteractionForState();
1016        mWorkspace.onResume();
1017    }
1018
1019    @Override
1020    protected void onPause() {
1021        // Ensure that items added to Launcher are queued until Launcher returns
1022        InstallShortcutReceiver.enableInstallQueue();
1023
1024        super.onPause();
1025        mPaused = true;
1026        mDragController.cancelDrag();
1027        mDragController.resetLastGestureUpTime();
1028
1029        // We call onHide() aggressively. The custom content callbacks should be able to
1030        // debounce excess onHide calls.
1031        if (mWorkspace.getCustomContentCallbacks() != null) {
1032            mWorkspace.getCustomContentCallbacks().onHide();
1033        }
1034    }
1035
1036    protected void onFinishBindingItems() {
1037        if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
1038            addCustomContentToLeft();
1039        }
1040    }
1041
1042    QSBScroller mQsbScroller = new QSBScroller() {
1043        int scrollY = 0;
1044
1045        @Override
1046        public void setScrollY(int scroll) {
1047            scrollY = scroll;
1048
1049            if (mWorkspace.isOnOrMovingToCustomContent()) {
1050                mSearchDropTargetBar.setTranslationY(- scrollY);
1051                getQsbBar().setTranslationY(-scrollY);
1052            }
1053        }
1054    };
1055
1056    public void resetQSBScroll() {
1057        mSearchDropTargetBar.animate().translationY(0).start();
1058        getQsbBar().animate().translationY(0).start();
1059    }
1060
1061    public interface CustomContentCallbacks {
1062        // Custom content is completely shown
1063        public void onShow();
1064
1065        // Custom content is completely hidden
1066        public void onHide();
1067
1068        // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1069        public void onScrollProgressChanged(float progress);
1070    }
1071
1072    protected boolean hasSettings() {
1073        return false;
1074    }
1075
1076    protected void startSettings() {
1077    }
1078
1079    public interface QSBScroller {
1080        public void setScrollY(int scrollY);
1081    }
1082
1083    public QSBScroller addToCustomContentPage(View customContent,
1084            CustomContentCallbacks callbacks, String description) {
1085        mWorkspace.addToCustomContentPage(customContent, callbacks, description);
1086        return mQsbScroller;
1087    }
1088
1089    // The custom content needs to offset its content to account for the QSB
1090    public int getTopOffsetForCustomContent() {
1091        return mWorkspace.getPaddingTop();
1092    }
1093
1094    @Override
1095    public Object onRetainNonConfigurationInstance() {
1096        // Flag the loader to stop early before switching
1097        mModel.stopLoader();
1098        if (mAppsCustomizeContent != null) {
1099            mAppsCustomizeContent.surrender();
1100        }
1101        return Boolean.TRUE;
1102    }
1103
1104    // We can't hide the IME if it was forced open.  So don't bother
1105    @Override
1106    public void onWindowFocusChanged(boolean hasFocus) {
1107        super.onWindowFocusChanged(hasFocus);
1108        mHasFocus = hasFocus;
1109    }
1110
1111    private boolean acceptFilter() {
1112        final InputMethodManager inputManager = (InputMethodManager)
1113                getSystemService(Context.INPUT_METHOD_SERVICE);
1114        return !inputManager.isFullscreenMode();
1115    }
1116
1117    @Override
1118    public boolean onKeyDown(int keyCode, KeyEvent event) {
1119        final int uniChar = event.getUnicodeChar();
1120        final boolean handled = super.onKeyDown(keyCode, event);
1121        final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1122        if (!handled && acceptFilter() && isKeyNotWhitespace) {
1123            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1124                    keyCode, event);
1125            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
1126                // something usable has been typed - start a search
1127                // the typed text will be retrieved and cleared by
1128                // showSearchDialog()
1129                // If there are multiple keystrokes before the search dialog takes focus,
1130                // onSearchRequested() will be called for every keystroke,
1131                // but it is idempotent, so it's fine.
1132                return onSearchRequested();
1133            }
1134        }
1135
1136        // Eat the long press event so the keyboard doesn't come up.
1137        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1138            return true;
1139        }
1140
1141        return handled;
1142    }
1143
1144    private String getTypedText() {
1145        return mDefaultKeySsb.toString();
1146    }
1147
1148    private void clearTypedText() {
1149        mDefaultKeySsb.clear();
1150        mDefaultKeySsb.clearSpans();
1151        Selection.setSelection(mDefaultKeySsb, 0);
1152    }
1153
1154    /**
1155     * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1156     * State
1157     */
1158    private static State intToState(int stateOrdinal) {
1159        State state = State.WORKSPACE;
1160        final State[] stateValues = State.values();
1161        for (int i = 0; i < stateValues.length; i++) {
1162            if (stateValues[i].ordinal() == stateOrdinal) {
1163                state = stateValues[i];
1164                break;
1165            }
1166        }
1167        return state;
1168    }
1169
1170    /**
1171     * Restores the previous state, if it exists.
1172     *
1173     * @param savedState The previous state.
1174     */
1175    @SuppressWarnings("unchecked")
1176    private void restoreState(Bundle savedState) {
1177        if (savedState == null) {
1178            return;
1179        }
1180
1181        State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
1182        if (state == State.APPS_CUSTOMIZE) {
1183            mOnResumeState = State.APPS_CUSTOMIZE;
1184        }
1185
1186        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1187                PagedView.INVALID_RESTORE_PAGE);
1188        if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1189            mWorkspace.setRestorePage(currentScreen);
1190        }
1191
1192        final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
1193        final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
1194
1195        if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1196            mPendingAddInfo.container = pendingAddContainer;
1197            mPendingAddInfo.screenId = pendingAddScreen;
1198            mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1199            mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
1200            mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1201            mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1202            mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1203            mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
1204            mWaitingForResult = true;
1205            mRestoring = true;
1206        }
1207
1208        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1209        if (renameFolder) {
1210            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
1211            mFolderInfo = mModel.getFolderById(this, sFolders, id);
1212            mRestoring = true;
1213        }
1214
1215        // Restore the AppsCustomize tab
1216        if (mAppsCustomizeTabHost != null) {
1217            String curTab = savedState.getString("apps_customize_currentTab");
1218            if (curTab != null) {
1219                mAppsCustomizeTabHost.setContentTypeImmediate(
1220                        mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
1221                mAppsCustomizeContent.loadAssociatedPages(
1222                        mAppsCustomizeContent.getCurrentPage());
1223            }
1224
1225            int currentIndex = savedState.getInt("apps_customize_currentIndex");
1226            mAppsCustomizeContent.restorePageForIndex(currentIndex);
1227        }
1228        mItemIdToViewId = (HashMap<Integer, Integer>)
1229                savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
1230    }
1231
1232    /**
1233     * Finds all the views we need and configure them properly.
1234     */
1235    private void setupViews() {
1236        final DragController dragController = mDragController;
1237
1238        mLauncherView = findViewById(R.id.launcher);
1239        mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1240        mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
1241        mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
1242
1243        mLauncherView.setSystemUiVisibility(
1244                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1245        mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
1246
1247        // Setup the drag layer
1248        mDragLayer.setup(this, dragController);
1249
1250        // Setup the hotseat
1251        mHotseat = (Hotseat) findViewById(R.id.hotseat);
1252        if (mHotseat != null) {
1253            mHotseat.setup(this);
1254            mHotseat.setOnLongClickListener(this);
1255        }
1256
1257        mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1258        View widgetButton = findViewById(R.id.widget_button);
1259        widgetButton.setOnClickListener(new OnClickListener() {
1260            @Override
1261            public void onClick(View arg0) {
1262                if (!mWorkspace.isSwitchingState()) {
1263                    showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1264                }
1265            }
1266        });
1267        widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1268
1269        View wallpaperButton = findViewById(R.id.wallpaper_button);
1270        wallpaperButton.setOnClickListener(new OnClickListener() {
1271            @Override
1272            public void onClick(View arg0) {
1273                if (!mWorkspace.isSwitchingState()) {
1274                    startWallpaper();
1275                }
1276            }
1277        });
1278        wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1279
1280        View settingsButton = findViewById(R.id.settings_button);
1281        if (hasSettings()) {
1282            settingsButton.setOnClickListener(new OnClickListener() {
1283                @Override
1284                public void onClick(View arg0) {
1285                    if (!mWorkspace.isSwitchingState()) {
1286                        startSettings();
1287                    }
1288                    }
1289            });
1290            settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1291        } else {
1292            settingsButton.setVisibility(View.GONE);
1293            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1294            lp.gravity = Gravity.END | Gravity.TOP;
1295            widgetButton.requestLayout();
1296        }
1297
1298        mOverviewPanel.setAlpha(0f);
1299
1300        // Setup the workspace
1301        mWorkspace.setHapticFeedbackEnabled(false);
1302        mWorkspace.setOnLongClickListener(this);
1303        mWorkspace.setup(dragController);
1304        dragController.addDragListener(mWorkspace);
1305
1306        // Get the search/delete bar
1307        mSearchDropTargetBar = (SearchDropTargetBar)
1308                mDragLayer.findViewById(R.id.search_drop_target_bar);
1309
1310        // Setup AppsCustomize
1311        mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
1312        mAppsCustomizeContent = (AppsCustomizePagedView)
1313                mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1314        mAppsCustomizeContent.setup(this, dragController);
1315
1316        // Setup the drag controller (drop targets have to be added in reverse order in priority)
1317        dragController.setDragScoller(mWorkspace);
1318        dragController.setScrollView(mDragLayer);
1319        dragController.setMoveTarget(mWorkspace);
1320        dragController.addDropTarget(mWorkspace);
1321        if (mSearchDropTargetBar != null) {
1322            mSearchDropTargetBar.setup(this, dragController);
1323        }
1324
1325        if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
1326            Log.v(TAG, "adding WeightWatcher");
1327            mWeightWatcher = new WeightWatcher(this);
1328            mWeightWatcher.setAlpha(0.5f);
1329            ((FrameLayout) mLauncherView).addView(mWeightWatcher,
1330                    new FrameLayout.LayoutParams(
1331                            FrameLayout.LayoutParams.MATCH_PARENT,
1332                            FrameLayout.LayoutParams.WRAP_CONTENT,
1333                            Gravity.BOTTOM)
1334            );
1335
1336            boolean show = shouldShowWeightWatcher();
1337            mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
1338        }
1339    }
1340
1341    /**
1342     * Creates a view representing a shortcut.
1343     *
1344     * @param info The data structure describing the shortcut.
1345     *
1346     * @return A View inflated from R.layout.application.
1347     */
1348    View createShortcut(ShortcutInfo info) {
1349        return createShortcut(R.layout.application,
1350                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
1351    }
1352
1353    /**
1354     * Creates a view representing a shortcut inflated from the specified resource.
1355     *
1356     * @param layoutResId The id of the XML layout used to create the shortcut.
1357     * @param parent The group the shortcut belongs to.
1358     * @param info The data structure describing the shortcut.
1359     *
1360     * @return A View inflated from layoutResId.
1361     */
1362    View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
1363        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1364        favorite.applyFromShortcutInfo(info, mIconCache);
1365        favorite.setOnClickListener(this);
1366        return favorite;
1367    }
1368
1369    /**
1370     * Add an application shortcut to the workspace.
1371     *
1372     * @param data The intent describing the application.
1373     * @param cellInfo The position on screen where to create the shortcut.
1374     */
1375    void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
1376        final int[] cellXY = mTmpAddItemCellCoordinates;
1377        final CellLayout layout = getCellLayout(container, screenId);
1378
1379        // First we check if we already know the exact location where we want to add this item.
1380        if (cellX >= 0 && cellY >= 0) {
1381            cellXY[0] = cellX;
1382            cellXY[1] = cellY;
1383        } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
1384            showOutOfSpaceMessage(isHotseatLayout(layout));
1385            return;
1386        }
1387
1388        final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
1389
1390        if (info != null) {
1391            info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
1392                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1393            info.container = ItemInfo.NO_ID;
1394            mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
1395                    isWorkspaceLocked(), cellX, cellY);
1396        } else {
1397            Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
1398        }
1399    }
1400
1401    /**
1402     * Add a shortcut to the workspace.
1403     *
1404     * @param data The intent describing the shortcut.
1405     * @param cellInfo The position on screen where to create the shortcut.
1406     */
1407    private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
1408            int cellY) {
1409        int[] cellXY = mTmpAddItemCellCoordinates;
1410        int[] touchXY = mPendingAddInfo.dropPos;
1411        CellLayout layout = getCellLayout(container, screenId);
1412
1413        boolean foundCellSpan = false;
1414
1415        ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
1416        if (info == null) {
1417            return;
1418        }
1419        final View view = createShortcut(info);
1420
1421        // First we check if we already know the exact location where we want to add this item.
1422        if (cellX >= 0 && cellY >= 0) {
1423            cellXY[0] = cellX;
1424            cellXY[1] = cellY;
1425            foundCellSpan = true;
1426
1427            // If appropriate, either create a folder or add to an existing folder
1428            if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1429                    true, null,null)) {
1430                return;
1431            }
1432            DragObject dragObject = new DragObject();
1433            dragObject.dragInfo = info;
1434            if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1435                    true)) {
1436                return;
1437            }
1438        } else if (touchXY != null) {
1439            // when dragging and dropping, just find the closest free spot
1440            int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
1441            foundCellSpan = (result != null);
1442        } else {
1443            foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1444        }
1445
1446        if (!foundCellSpan) {
1447            showOutOfSpaceMessage(isHotseatLayout(layout));
1448            return;
1449        }
1450
1451        LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
1452
1453        if (!mRestoring) {
1454            mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1455                    isWorkspaceLocked());
1456        }
1457    }
1458
1459    static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1460            int minHeight) {
1461        Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
1462        // We want to account for the extra amount of padding that we are adding to the widget
1463        // to ensure that it gets the full amount of space that it has requested
1464        int requiredWidth = minWidth + padding.left + padding.right;
1465        int requiredHeight = minHeight + padding.top + padding.bottom;
1466        return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
1467    }
1468
1469    static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1470        return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
1471    }
1472
1473    static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1474        return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
1475    }
1476
1477    static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1478        return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
1479    }
1480
1481    static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1482        return getSpanForWidget(context, info.componentName, info.minResizeWidth,
1483                info.minResizeHeight);
1484    }
1485
1486    /**
1487     * Add a widget to the workspace.
1488     *
1489     * @param appWidgetId The app widget id
1490     * @param cellInfo The position on screen where to create the widget.
1491     */
1492    private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
1493            AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1494        if (appWidgetInfo == null) {
1495            appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1496        }
1497
1498        // Calculate the grid spans needed to fit this widget
1499        CellLayout layout = getCellLayout(container, screenId);
1500
1501        int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1502        int[] spanXY = getSpanForWidget(this, appWidgetInfo);
1503
1504        // Try finding open space on Launcher screen
1505        // We have saved the position to which the widget was dragged-- this really only matters
1506        // if we are placing widgets on a "spring-loaded" screen
1507        int[] cellXY = mTmpAddItemCellCoordinates;
1508        int[] touchXY = mPendingAddInfo.dropPos;
1509        int[] finalSpan = new int[2];
1510        boolean foundCellSpan = false;
1511        if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1512            cellXY[0] = mPendingAddInfo.cellX;
1513            cellXY[1] = mPendingAddInfo.cellY;
1514            spanXY[0] = mPendingAddInfo.spanX;
1515            spanXY[1] = mPendingAddInfo.spanY;
1516            foundCellSpan = true;
1517        } else if (touchXY != null) {
1518            // when dragging and dropping, just find the closest free spot
1519            int[] result = layout.findNearestVacantArea(
1520                    touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1521                    spanXY[1], cellXY, finalSpan);
1522            spanXY[0] = finalSpan[0];
1523            spanXY[1] = finalSpan[1];
1524            foundCellSpan = (result != null);
1525        } else {
1526            foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
1527        }
1528
1529        if (!foundCellSpan) {
1530            if (appWidgetId != -1) {
1531                // Deleting an app widget ID is a void call but writes to disk before returning
1532                // to the caller...
1533                new AsyncTask<Void, Void, Void>() {
1534                    public Void doInBackground(Void ... args) {
1535                        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1536                        return null;
1537                    }
1538                }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
1539            }
1540            showOutOfSpaceMessage(isHotseatLayout(layout));
1541            return;
1542        }
1543
1544        // Build Launcher-specific widget info and save to database
1545        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1546                appWidgetInfo.provider);
1547        launcherInfo.spanX = spanXY[0];
1548        launcherInfo.spanY = spanXY[1];
1549        launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1550        launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
1551
1552        LauncherModel.addItemToDatabase(this, launcherInfo,
1553                container, screenId, cellXY[0], cellXY[1], false);
1554
1555        if (!mRestoring) {
1556            if (hostView == null) {
1557                // Perform actual inflation because we're live
1558                launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1559                launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1560            } else {
1561                // The AppWidgetHostView has already been inflated and instantiated
1562                launcherInfo.hostView = hostView;
1563            }
1564
1565            launcherInfo.hostView.setTag(launcherInfo);
1566            launcherInfo.hostView.setVisibility(View.VISIBLE);
1567            launcherInfo.notifyWidgetSizeChanged(this);
1568
1569            mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
1570                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
1571
1572            addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
1573        }
1574        resetAddInfo();
1575    }
1576
1577    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1578        @Override
1579        public void onReceive(Context context, Intent intent) {
1580            final String action = intent.getAction();
1581            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1582                mUserPresent = false;
1583                mDragLayer.clearAllResizeFrames();
1584                updateRunning();
1585
1586                // Reset AllApps to its initial state only if we are not in the middle of
1587                // processing a multi-step drop
1588                if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1589                    showWorkspace(false);
1590                }
1591            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1592                mUserPresent = true;
1593                updateRunning();
1594            } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1595                mModel.resetLoadedState(false, true);
1596                mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1597                        LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1598            } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1599                mModel.resetLoadedState(false, true);
1600                mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1601                        LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1602                                | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
1603            }
1604        }
1605    };
1606
1607    @Override
1608    public void onAttachedToWindow() {
1609        super.onAttachedToWindow();
1610
1611        // Listen for broadcasts related to user-presence
1612        final IntentFilter filter = new IntentFilter();
1613        filter.addAction(Intent.ACTION_SCREEN_OFF);
1614        filter.addAction(Intent.ACTION_USER_PRESENT);
1615        if (ENABLE_DEBUG_INTENTS) {
1616            filter.addAction(DebugIntents.DELETE_DATABASE);
1617            filter.addAction(DebugIntents.MIGRATE_DATABASE);
1618        }
1619        registerReceiver(mReceiver, filter);
1620        FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
1621        mAttached = true;
1622        mVisible = true;
1623    }
1624
1625    @Override
1626    public void onDetachedFromWindow() {
1627        super.onDetachedFromWindow();
1628        mVisible = false;
1629
1630        if (mAttached) {
1631            unregisterReceiver(mReceiver);
1632            mAttached = false;
1633        }
1634        updateRunning();
1635    }
1636
1637    public void onWindowVisibilityChanged(int visibility) {
1638        mVisible = visibility == View.VISIBLE;
1639        updateRunning();
1640        // The following code used to be in onResume, but it turns out onResume is called when
1641        // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1642        // is a more appropriate event to handle
1643        if (mVisible) {
1644            mAppsCustomizeTabHost.onWindowVisible();
1645            if (!mWorkspaceLoading) {
1646                final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
1647                // We want to let Launcher draw itself at least once before we force it to build
1648                // layers on all the workspace pages, so that transitioning to Launcher from other
1649                // apps is nice and speedy.
1650                observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
1651                    private boolean mStarted = false;
1652                    public void onDraw() {
1653                        if (mStarted) return;
1654                        mStarted = true;
1655                        // We delay the layer building a bit in order to give
1656                        // other message processing a time to run.  In particular
1657                        // this avoids a delay in hiding the IME if it was
1658                        // currently shown, because doing that may involve
1659                        // some communication back with the app.
1660                        mWorkspace.postDelayed(mBuildLayersRunnable, 500);
1661                        final ViewTreeObserver.OnDrawListener listener = this;
1662                        mWorkspace.post(new Runnable() {
1663                                public void run() {
1664                                    if (mWorkspace != null &&
1665                                            mWorkspace.getViewTreeObserver() != null) {
1666                                        mWorkspace.getViewTreeObserver().
1667                                                removeOnDrawListener(listener);
1668                                    }
1669                                }
1670                            });
1671                        return;
1672                    }
1673                });
1674            }
1675            // When Launcher comes back to foreground, a different Activity might be responsible for
1676            // the app market intent, so refresh the icon
1677            if (!DISABLE_MARKET_BUTTON) {
1678                updateAppMarketIcon();
1679            }
1680            clearTypedText();
1681        }
1682    }
1683
1684    private void sendAdvanceMessage(long delay) {
1685        mHandler.removeMessages(ADVANCE_MSG);
1686        Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1687        mHandler.sendMessageDelayed(msg, delay);
1688        mAutoAdvanceSentTime = System.currentTimeMillis();
1689    }
1690
1691    private void updateRunning() {
1692        boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1693        if (autoAdvanceRunning != mAutoAdvanceRunning) {
1694            mAutoAdvanceRunning = autoAdvanceRunning;
1695            if (autoAdvanceRunning) {
1696                long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1697                sendAdvanceMessage(delay);
1698            } else {
1699                if (!mWidgetsToAdvance.isEmpty()) {
1700                    mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1701                            (System.currentTimeMillis() - mAutoAdvanceSentTime));
1702                }
1703                mHandler.removeMessages(ADVANCE_MSG);
1704                mHandler.removeMessages(0); // Remove messages sent using postDelayed()
1705            }
1706        }
1707    }
1708
1709    private final Handler mHandler = new Handler() {
1710        @Override
1711        public void handleMessage(Message msg) {
1712            if (msg.what == ADVANCE_MSG) {
1713                int i = 0;
1714                for (View key: mWidgetsToAdvance.keySet()) {
1715                    final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1716                    final int delay = mAdvanceStagger * i;
1717                    if (v instanceof Advanceable) {
1718                       postDelayed(new Runnable() {
1719                           public void run() {
1720                               ((Advanceable) v).advance();
1721                           }
1722                       }, delay);
1723                    }
1724                    i++;
1725                }
1726                sendAdvanceMessage(mAdvanceInterval);
1727            }
1728        }
1729    };
1730
1731    void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1732        if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
1733        View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1734        if (v instanceof Advanceable) {
1735            mWidgetsToAdvance.put(hostView, appWidgetInfo);
1736            ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
1737            updateRunning();
1738        }
1739    }
1740
1741    void removeWidgetToAutoAdvance(View hostView) {
1742        if (mWidgetsToAdvance.containsKey(hostView)) {
1743            mWidgetsToAdvance.remove(hostView);
1744            updateRunning();
1745        }
1746    }
1747
1748    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1749        removeWidgetToAutoAdvance(launcherInfo.hostView);
1750        launcherInfo.hostView = null;
1751    }
1752
1753    void showOutOfSpaceMessage(boolean isHotseatLayout) {
1754        int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1755        Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
1756    }
1757
1758    public DragLayer getDragLayer() {
1759        return mDragLayer;
1760    }
1761
1762    public Workspace getWorkspace() {
1763        return mWorkspace;
1764    }
1765
1766    public Hotseat getHotseat() {
1767        return mHotseat;
1768    }
1769
1770    public ViewGroup getOverviewPanel() {
1771        return mOverviewPanel;
1772    }
1773
1774    public SearchDropTargetBar getSearchBar() {
1775        return mSearchDropTargetBar;
1776    }
1777
1778    public LauncherAppWidgetHost getAppWidgetHost() {
1779        return mAppWidgetHost;
1780    }
1781
1782    public LauncherModel getModel() {
1783        return mModel;
1784    }
1785
1786    public LauncherClings getLauncherClings() {
1787        return mLauncherClings;
1788    }
1789
1790    protected SharedPreferences getSharedPrefs() {
1791        return mSharedPrefs;
1792    }
1793
1794    public void closeSystemDialogs() {
1795        getWindow().closeAllPanels();
1796
1797        // Whatever we were doing is hereby canceled.
1798        mWaitingForResult = false;
1799    }
1800
1801    @Override
1802    protected void onNewIntent(Intent intent) {
1803        long startTime = 0;
1804        if (DEBUG_RESUME_TIME) {
1805            startTime = System.currentTimeMillis();
1806        }
1807        super.onNewIntent(intent);
1808
1809        // Close the menu
1810        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
1811            // also will cancel mWaitingForResult.
1812            closeSystemDialogs();
1813
1814            final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1815                    Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1816                    != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1817
1818            if (mWorkspace == null) {
1819                // Can be cases where mWorkspace is null, this prevents a NPE
1820                return;
1821            }
1822            Folder openFolder = mWorkspace.getOpenFolder();
1823            // In all these cases, only animate if we're already on home
1824            mWorkspace.exitWidgetResizeMode();
1825            if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1826                    openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
1827                mWorkspace.moveToDefaultScreen(true);
1828            }
1829
1830            closeFolder();
1831            exitSpringLoadedDragMode();
1832
1833            // If we are already on home, then just animate back to the workspace,
1834            // otherwise, just wait until onResume to set the state back to Workspace
1835            if (alreadyOnHome) {
1836                showWorkspace(true);
1837            } else {
1838                mOnResumeState = State.WORKSPACE;
1839            }
1840
1841            final View v = getWindow().peekDecorView();
1842            if (v != null && v.getWindowToken() != null) {
1843                InputMethodManager imm = (InputMethodManager)getSystemService(
1844                        INPUT_METHOD_SERVICE);
1845                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1846            }
1847
1848            // Reset the apps customize page
1849            if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1850                mAppsCustomizeTabHost.reset();
1851            }
1852
1853            onHomeIntent();
1854        }
1855
1856        if (DEBUG_RESUME_TIME) {
1857            Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1858        }
1859    }
1860
1861    /**
1862     * Override point for subclasses to prevent movement to the default screen when the home
1863     * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1864     */
1865    protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1866        return true;
1867    }
1868
1869    /**
1870     * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1871     */
1872    protected void onHomeIntent() {
1873        // Do nothing
1874    }
1875
1876    @Override
1877    public void onRestoreInstanceState(Bundle state) {
1878        super.onRestoreInstanceState(state);
1879        for (int page: mSynchronouslyBoundPages) {
1880            mWorkspace.restoreInstanceStateForChild(page);
1881        }
1882    }
1883
1884    @Override
1885    protected void onSaveInstanceState(Bundle outState) {
1886        if (mWorkspace.getChildCount() > 0) {
1887            outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1888                    mWorkspace.getCurrentPageOffsetFromCustomContent());
1889        }
1890        super.onSaveInstanceState(outState);
1891
1892        outState.putInt(RUNTIME_STATE, mState.ordinal());
1893        // We close any open folder since it will not be re-opened, and we need to make sure
1894        // this state is reflected.
1895        closeFolder();
1896
1897        if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
1898                mWaitingForResult) {
1899            outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1900            outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
1901            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1902            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
1903            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1904            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1905            outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
1906            outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
1907        }
1908
1909        if (mFolderInfo != null && mWaitingForResult) {
1910            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1911            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1912        }
1913
1914        // Save the current AppsCustomize tab
1915        if (mAppsCustomizeTabHost != null) {
1916            AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1917            String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
1918            if (currentTabTag != null) {
1919                outState.putString("apps_customize_currentTab", currentTabTag);
1920            }
1921            int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1922            outState.putInt("apps_customize_currentIndex", currentIndex);
1923        }
1924        outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
1925    }
1926
1927    @Override
1928    public void onDestroy() {
1929        super.onDestroy();
1930
1931        // Remove all pending runnables
1932        mHandler.removeMessages(ADVANCE_MSG);
1933        mHandler.removeMessages(0);
1934        mWorkspace.removeCallbacks(mBuildLayersRunnable);
1935
1936        // Stop callbacks from LauncherModel
1937        LauncherAppState app = (LauncherAppState.getInstance());
1938        mModel.stopLoader();
1939        app.setLauncher(null);
1940
1941        try {
1942            mAppWidgetHost.stopListening();
1943        } catch (NullPointerException ex) {
1944            Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
1945        }
1946        mAppWidgetHost = null;
1947
1948        mWidgetsToAdvance.clear();
1949
1950        TextKeyListener.getInstance().release();
1951
1952        // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1953        // to prevent leaking Launcher activities on orientation change.
1954        if (mModel != null) {
1955            mModel.unbindItemInfosAndClearQueuedBindRunnables();
1956        }
1957
1958        getContentResolver().unregisterContentObserver(mWidgetObserver);
1959        unregisterReceiver(mCloseSystemDialogsReceiver);
1960
1961        mDragLayer.clearAllResizeFrames();
1962        ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1963        mWorkspace.removeAllWorkspaceScreens();
1964        mWorkspace = null;
1965        mDragController = null;
1966
1967        LauncherAnimUtils.onDestroyActivity();
1968    }
1969
1970    public DragController getDragController() {
1971        return mDragController;
1972    }
1973
1974    @Override
1975    public void startActivityForResult(Intent intent, int requestCode) {
1976        if (requestCode >= 0) mWaitingForResult = true;
1977        super.startActivityForResult(intent, requestCode);
1978    }
1979
1980    /**
1981     * Indicates that we want global search for this activity by setting the globalSearch
1982     * argument for {@link #startSearch} to true.
1983     */
1984    @Override
1985    public void startSearch(String initialQuery, boolean selectInitialQuery,
1986            Bundle appSearchData, boolean globalSearch) {
1987
1988        showWorkspace(true);
1989
1990        if (initialQuery == null) {
1991            // Use any text typed in the launcher as the initial query
1992            initialQuery = getTypedText();
1993        }
1994        if (appSearchData == null) {
1995            appSearchData = new Bundle();
1996            appSearchData.putString("source", "launcher-search");
1997        }
1998        Rect sourceBounds = new Rect();
1999        if (mSearchDropTargetBar != null) {
2000            sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2001        }
2002
2003        startSearch(initialQuery, selectInitialQuery,
2004                appSearchData, sourceBounds);
2005    }
2006
2007    public void startSearch(String initialQuery,
2008            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
2009        startGlobalSearch(initialQuery, selectInitialQuery,
2010                appSearchData, sourceBounds);
2011    }
2012
2013    /**
2014     * Starts the global search activity. This code is a copied from SearchManager
2015     */
2016    private void startGlobalSearch(String initialQuery,
2017            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
2018        final SearchManager searchManager =
2019            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2020        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2021        if (globalSearchActivity == null) {
2022            Log.w(TAG, "No global search activity found.");
2023            return;
2024        }
2025        Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2026        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2027        intent.setComponent(globalSearchActivity);
2028        // Make sure that we have a Bundle to put source in
2029        if (appSearchData == null) {
2030            appSearchData = new Bundle();
2031        } else {
2032            appSearchData = new Bundle(appSearchData);
2033        }
2034        // Set source to package name of app that starts global search, if not set already.
2035        if (!appSearchData.containsKey("source")) {
2036            appSearchData.putString("source", getPackageName());
2037        }
2038        intent.putExtra(SearchManager.APP_DATA, appSearchData);
2039        if (!TextUtils.isEmpty(initialQuery)) {
2040            intent.putExtra(SearchManager.QUERY, initialQuery);
2041        }
2042        if (selectInitialQuery) {
2043            intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2044        }
2045        intent.setSourceBounds(sourceBounds);
2046        try {
2047            startActivity(intent);
2048        } catch (ActivityNotFoundException ex) {
2049            Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2050        }
2051    }
2052
2053    @Override
2054    public boolean onPrepareOptionsMenu(Menu menu) {
2055        super.onPrepareOptionsMenu(menu);
2056        // Close any open folders
2057        closeFolder();
2058        // Stop resizing any widgets
2059        mWorkspace.exitWidgetResizeMode();
2060        if (!mWorkspace.isInOverviewMode()) {
2061            // Show the overview mode
2062            showOverviewMode(true);
2063        } else {
2064            showWorkspace(true);
2065        }
2066        return false;
2067    }
2068
2069    @Override
2070    public boolean onSearchRequested() {
2071        startSearch(null, false, null, true);
2072        // Use a custom animation for launching search
2073        return true;
2074    }
2075
2076    public boolean isWorkspaceLocked() {
2077        return mWorkspaceLoading || mWaitingForResult;
2078    }
2079
2080    private void resetAddInfo() {
2081        mPendingAddInfo.container = ItemInfo.NO_ID;
2082        mPendingAddInfo.screenId = -1;
2083        mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2084        mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
2085        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
2086        mPendingAddInfo.dropPos = null;
2087    }
2088
2089    void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2090            final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2091        addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2092    }
2093
2094    void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2095            final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2096            delay) {
2097        if (appWidgetInfo.configure != null) {
2098            mPendingAddWidgetInfo = appWidgetInfo;
2099            mPendingAddWidgetId = appWidgetId;
2100
2101            // Launch over to configure widget, if needed
2102            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
2103            intent.setComponent(appWidgetInfo.configure);
2104            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2105            Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
2106        } else {
2107            // Otherwise just add it
2108            Runnable onComplete = new Runnable() {
2109                @Override
2110                public void run() {
2111                    // Exit spring loaded mode if necessary after adding the widget
2112                    exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2113                            null);
2114                }
2115            };
2116            completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
2117                    appWidgetInfo);
2118            mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
2119        }
2120    }
2121
2122    protected void moveToCustomContentScreen(boolean animate) {
2123        // Close any folders that may be open.
2124        closeFolder();
2125        mWorkspace.moveToCustomContentScreen(animate);
2126    }
2127    /**
2128     * Process a shortcut drop.
2129     *
2130     * @param componentName The name of the component
2131     * @param screenId The ID of the screen where it should be added
2132     * @param cell The cell it should be added to, optional
2133     * @param position The location on the screen where it was dropped, optional
2134     */
2135    void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2136            int[] cell, int[] loc) {
2137        resetAddInfo();
2138        mPendingAddInfo.container = container;
2139        mPendingAddInfo.screenId = screenId;
2140        mPendingAddInfo.dropPos = loc;
2141
2142        if (cell != null) {
2143            mPendingAddInfo.cellX = cell[0];
2144            mPendingAddInfo.cellY = cell[1];
2145        }
2146
2147        Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2148        createShortcutIntent.setComponent(componentName);
2149        processShortcut(createShortcutIntent);
2150    }
2151
2152    /**
2153     * Process a widget drop.
2154     *
2155     * @param info The PendingAppWidgetInfo of the widget being added.
2156     * @param screenId The ID of the screen where it should be added
2157     * @param cell The cell it should be added to, optional
2158     * @param position The location on the screen where it was dropped, optional
2159     */
2160    void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2161            int[] cell, int[] span, int[] loc) {
2162        resetAddInfo();
2163        mPendingAddInfo.container = info.container = container;
2164        mPendingAddInfo.screenId = info.screenId = screenId;
2165        mPendingAddInfo.dropPos = loc;
2166        mPendingAddInfo.minSpanX = info.minSpanX;
2167        mPendingAddInfo.minSpanY = info.minSpanY;
2168
2169        if (cell != null) {
2170            mPendingAddInfo.cellX = cell[0];
2171            mPendingAddInfo.cellY = cell[1];
2172        }
2173        if (span != null) {
2174            mPendingAddInfo.spanX = span[0];
2175            mPendingAddInfo.spanY = span[1];
2176        }
2177
2178        AppWidgetHostView hostView = info.boundWidget;
2179        int appWidgetId;
2180        if (hostView != null) {
2181            appWidgetId = hostView.getAppWidgetId();
2182            addAppWidgetImpl(appWidgetId, info, hostView, info.info);
2183        } else {
2184            // In this case, we either need to start an activity to get permission to bind
2185            // the widget, or we need to start an activity to configure the widget, or both.
2186            appWidgetId = getAppWidgetHost().allocateAppWidgetId();
2187            Bundle options = info.bindOptions;
2188
2189            boolean success = false;
2190            if (options != null) {
2191                success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2192                        info.componentName, options);
2193            } else {
2194                success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2195                        info.componentName);
2196            }
2197            if (success) {
2198                addAppWidgetImpl(appWidgetId, info, null, info.info);
2199            } else {
2200                mPendingAddWidgetInfo = info.info;
2201                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2202                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2203                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
2204                // TODO: we need to make sure that this accounts for the options bundle.
2205                // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
2206                startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2207            }
2208        }
2209    }
2210
2211    void processShortcut(Intent intent) {
2212        // Handle case where user selected "Applications"
2213        String applicationName = getResources().getString(R.string.group_applications);
2214        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2215
2216        if (applicationName != null && applicationName.equals(shortcutName)) {
2217            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2218            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2219
2220            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2221            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
2222            pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
2223            Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
2224        } else {
2225            Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
2226        }
2227    }
2228
2229    void processWallpaper(Intent intent) {
2230        startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2231    }
2232
2233    FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
2234            int cellY) {
2235        final FolderInfo folderInfo = new FolderInfo();
2236        folderInfo.title = getText(R.string.folder_name);
2237
2238        // Update the model
2239        LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
2240                false);
2241        sFolders.put(folderInfo.id, folderInfo);
2242
2243        // Create the view
2244        FolderIcon newFolder =
2245            FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
2246        mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
2247                isWorkspaceLocked());
2248        // Force measure the new folder icon
2249        CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2250        parent.getShortcutsAndWidgets().measureChild(newFolder);
2251        return newFolder;
2252    }
2253
2254    void removeFolder(FolderInfo folder) {
2255        sFolders.remove(folder.id);
2256    }
2257
2258    protected void startWallpaper() {
2259        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2260        pickWallpaper.setComponent(getWallpaperPickerComponent());
2261        startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2262    }
2263
2264    protected ComponentName getWallpaperPickerComponent() {
2265        return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
2266    }
2267
2268    /**
2269     * Registers various content observers. The current implementation registers
2270     * only a favorites observer to keep track of the favorites applications.
2271     */
2272    private void registerContentObservers() {
2273        ContentResolver resolver = getContentResolver();
2274        resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2275                true, mWidgetObserver);
2276    }
2277
2278    @Override
2279    public boolean dispatchKeyEvent(KeyEvent event) {
2280        if (event.getAction() == KeyEvent.ACTION_DOWN) {
2281            switch (event.getKeyCode()) {
2282                case KeyEvent.KEYCODE_HOME:
2283                    return true;
2284                case KeyEvent.KEYCODE_VOLUME_DOWN:
2285                    if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
2286                        dumpState();
2287                        return true;
2288                    }
2289                    break;
2290            }
2291        } else if (event.getAction() == KeyEvent.ACTION_UP) {
2292            switch (event.getKeyCode()) {
2293                case KeyEvent.KEYCODE_HOME:
2294                    return true;
2295            }
2296        }
2297
2298        return super.dispatchKeyEvent(event);
2299    }
2300
2301    @Override
2302    public void onBackPressed() {
2303        if (isAllAppsVisible()) {
2304            if (mAppsCustomizeContent.getContentType() ==
2305                    AppsCustomizePagedView.ContentType.Applications) {
2306                showWorkspace(true);
2307            } else {
2308                showOverviewMode(true);
2309            }
2310        } else if (mWorkspace.isInOverviewMode()) {
2311            mWorkspace.exitOverviewMode(true);
2312        } else if (mWorkspace.getOpenFolder() != null) {
2313            Folder openFolder = mWorkspace.getOpenFolder();
2314            if (openFolder.isEditingName()) {
2315                openFolder.dismissEditingName();
2316            } else {
2317                closeFolder();
2318            }
2319        } else {
2320            mWorkspace.exitWidgetResizeMode();
2321
2322            // Back button is a no-op here, but give at least some feedback for the button press
2323            mWorkspace.showOutlinesTemporarily();
2324        }
2325    }
2326
2327    /**
2328     * Re-listen when widgets are reset.
2329     */
2330    private void onAppWidgetReset() {
2331        if (mAppWidgetHost != null) {
2332            mAppWidgetHost.startListening();
2333        }
2334    }
2335
2336    /**
2337     * Launches the intent referred by the clicked shortcut.
2338     *
2339     * @param v The view representing the clicked shortcut.
2340     */
2341    public void onClick(View v) {
2342        // Make sure that rogue clicks don't get through while allapps is launching, or after the
2343        // view has detached (it's possible for this to happen if the view is removed mid touch).
2344        if (v.getWindowToken() == null) {
2345            return;
2346        }
2347
2348        if (!mWorkspace.isFinishedSwitchingState()) {
2349            return;
2350        }
2351
2352        if (v instanceof Workspace) {
2353            if (mWorkspace.isInOverviewMode()) {
2354                mWorkspace.exitOverviewMode(true);
2355            }
2356            return;
2357        }
2358
2359        if (v instanceof CellLayout) {
2360            if (mWorkspace.isInOverviewMode()) {
2361                mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
2362            }
2363        }
2364
2365        Object tag = v.getTag();
2366        if (tag instanceof ShortcutInfo) {
2367            // Open shortcut
2368            final ShortcutInfo shortcut = (ShortcutInfo) tag;
2369            final Intent intent = shortcut.intent;
2370
2371            // Check for special shortcuts
2372            if (intent.getComponent() != null) {
2373                final String shortcutClass = intent.getComponent().getClassName();
2374
2375                if (shortcutClass.equals(WidgetAdder.class.getName())) {
2376                    onClickAddWidgetButton();
2377                    return;
2378                } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2379                    MemoryDumpActivity.startDump(this);
2380                    return;
2381                } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2382                    toggleShowWeightWatcher();
2383                    return;
2384                }
2385            }
2386
2387            // Start activities
2388            int[] pos = new int[2];
2389            v.getLocationOnScreen(pos);
2390            intent.setSourceBounds(new Rect(pos[0], pos[1],
2391                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2392
2393            boolean success = startActivitySafely(v, intent, tag);
2394
2395            mStats.recordLaunch(intent, shortcut);
2396
2397            if (success && v instanceof BubbleTextView) {
2398                mWaitingForResume = (BubbleTextView) v;
2399                mWaitingForResume.setStayPressed(true);
2400            }
2401        } else if (tag instanceof FolderInfo) {
2402            if (v instanceof FolderIcon) {
2403                FolderIcon fi = (FolderIcon) v;
2404                handleFolderClick(fi);
2405            }
2406        } else if (v == mAllAppsButton) {
2407            if (isAllAppsVisible()) {
2408                showWorkspace(true);
2409            } else {
2410                onClickAllAppsButton(v);
2411            }
2412        }
2413    }
2414
2415    public boolean onTouch(View v, MotionEvent event) {
2416        return false;
2417    }
2418
2419    /**
2420     * Event handler for the search button
2421     *
2422     * @param v The view that was clicked.
2423     */
2424    public void onClickSearchButton(View v) {
2425        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2426
2427        onSearchRequested();
2428    }
2429
2430    /**
2431     * Event handler for the voice button
2432     *
2433     * @param v The view that was clicked.
2434     */
2435    public void onClickVoiceButton(View v) {
2436        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2437
2438        startVoice();
2439    }
2440
2441    public void startVoice() {
2442        try {
2443            final SearchManager searchManager =
2444                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2445            ComponentName activityName = searchManager.getGlobalSearchActivity();
2446            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2447            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2448            if (activityName != null) {
2449                intent.setPackage(activityName.getPackageName());
2450            }
2451            startActivity(null, intent, "onClickVoiceButton");
2452        } catch (ActivityNotFoundException e) {
2453            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2454            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2455            startActivitySafely(null, intent, "onClickVoiceButton");
2456        }
2457    }
2458
2459    /**
2460     * Event handler for the "grid" button that appears on the home screen, which
2461     * enters all apps mode.
2462     *
2463     * @param v The view that was clicked.
2464     */
2465    public void onClickAllAppsButton(View v) {
2466        showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2467    }
2468
2469    /**
2470     * Event handler for the (Add) Widgets button that appears after a long press
2471     * on the home screen.
2472     */
2473    protected void onClickAddWidgetButton() {
2474        showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2475    }
2476
2477    public void onTouchDownAllAppsButton(View v) {
2478        // Provide the same haptic feedback that the system offers for virtual keys.
2479        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2480    }
2481
2482    public void performHapticFeedbackOnTouchDown(View v) {
2483        // Provide the same haptic feedback that the system offers for virtual keys.
2484        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2485    }
2486
2487    public View.OnTouchListener getHapticFeedbackTouchListener() {
2488        if (mHapticFeedbackTouchListener == null) {
2489            mHapticFeedbackTouchListener = new View.OnTouchListener() {
2490                @Override
2491                public boolean onTouch(View v, MotionEvent event) {
2492                    if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2493                        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2494                    }
2495                    return false;
2496                }
2497            };
2498        }
2499        return mHapticFeedbackTouchListener;
2500    }
2501
2502    public void onClickAppMarketButton(View v) {
2503        if (!DISABLE_MARKET_BUTTON) {
2504            if (mAppMarketIntent != null) {
2505                startActivitySafely(v, mAppMarketIntent, "app market");
2506            } else {
2507                Log.e(TAG, "Invalid app market intent.");
2508            }
2509        }
2510    }
2511
2512    /**
2513     * Called when the user stops interacting with the launcher.
2514     * This implies that the user is now on the homescreen and is not doing housekeeping.
2515     */
2516    protected void onInteractionEnd() {}
2517
2518    /**
2519     * Called when the user starts interacting with the launcher.
2520     * The possible interactions are:
2521     *  - open all apps
2522     *  - reorder an app shortcut, or a widget
2523     *  - open the overview mode.
2524     * This is a good time to stop doing things that only make sense
2525     * when the user is on the homescreen and not doing housekeeping.
2526     */
2527    protected void onInteractionBegin() {}
2528
2529    void startApplicationDetailsActivity(ComponentName componentName) {
2530        String packageName = componentName.getPackageName();
2531        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2532                Uri.fromParts("package", packageName, null));
2533        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2534                Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
2535        startActivitySafely(null, intent, "startApplicationDetailsActivity");
2536    }
2537
2538    // returns true if the activity was started
2539    boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2540        if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
2541            // System applications cannot be installed. For now, show a toast explaining that.
2542            // We may give them the option of disabling apps this way.
2543            int messageId = R.string.uninstall_system_app_text;
2544            Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2545            return false;
2546        } else {
2547            String packageName = componentName.getPackageName();
2548            String className = componentName.getClassName();
2549            Intent intent = new Intent(
2550                    Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2551            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2552                    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
2553            startActivity(intent);
2554            return true;
2555        }
2556    }
2557
2558    boolean startActivity(View v, Intent intent, Object tag) {
2559        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2560
2561        try {
2562            // Only launch using the new animation if the shortcut has not opted out (this is a
2563            // private contract between launcher and may be ignored in the future).
2564            boolean useLaunchAnimation = (v != null) &&
2565                    !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2566            if (useLaunchAnimation) {
2567                ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2568                        v.getMeasuredWidth(), v.getMeasuredHeight());
2569
2570                startActivity(intent, opts.toBundle());
2571            } else {
2572                startActivity(intent);
2573            }
2574            return true;
2575        } catch (SecurityException e) {
2576            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2577            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2578                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2579                    "or use the exported attribute for this activity. "
2580                    + "tag="+ tag + " intent=" + intent, e);
2581        }
2582        return false;
2583    }
2584
2585    boolean startActivitySafely(View v, Intent intent, Object tag) {
2586        boolean success = false;
2587        try {
2588            success = startActivity(v, intent, tag);
2589        } catch (ActivityNotFoundException e) {
2590            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2591            Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2592        }
2593        return success;
2594    }
2595
2596    private void handleFolderClick(FolderIcon folderIcon) {
2597        final FolderInfo info = folderIcon.getFolderInfo();
2598        Folder openFolder = mWorkspace.getFolderForTag(info);
2599
2600        // If the folder info reports that the associated folder is open, then verify that
2601        // it is actually opened. There have been a few instances where this gets out of sync.
2602        if (info.opened && openFolder == null) {
2603            Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2604                    + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2605            info.opened = false;
2606        }
2607
2608        if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2609            // Close any open folder
2610            closeFolder();
2611            // Open the requested folder
2612            openFolder(folderIcon);
2613        } else {
2614            // Find the open folder...
2615            int folderScreen;
2616            if (openFolder != null) {
2617                folderScreen = mWorkspace.getPageForView(openFolder);
2618                // .. and close it
2619                closeFolder(openFolder);
2620                if (folderScreen != mWorkspace.getCurrentPage()) {
2621                    // Close any folder open on the current screen
2622                    closeFolder();
2623                    // Pull the folder onto this screen
2624                    openFolder(folderIcon);
2625                }
2626            }
2627        }
2628    }
2629
2630    /**
2631     * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2632     * in the DragLayer in the exact absolute location of the original FolderIcon.
2633     */
2634    private void copyFolderIconToImage(FolderIcon fi) {
2635        final int width = fi.getMeasuredWidth();
2636        final int height = fi.getMeasuredHeight();
2637
2638        // Lazy load ImageView, Bitmap and Canvas
2639        if (mFolderIconImageView == null) {
2640            mFolderIconImageView = new ImageView(this);
2641        }
2642        if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2643                mFolderIconBitmap.getHeight() != height) {
2644            mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2645            mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2646        }
2647
2648        DragLayer.LayoutParams lp;
2649        if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2650            lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2651        } else {
2652            lp = new DragLayer.LayoutParams(width, height);
2653        }
2654
2655        // The layout from which the folder is being opened may be scaled, adjust the starting
2656        // view size by this scale factor.
2657        float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
2658        lp.customPosition = true;
2659        lp.x = mRectForFolderAnimation.left;
2660        lp.y = mRectForFolderAnimation.top;
2661        lp.width = (int) (scale * width);
2662        lp.height = (int) (scale * height);
2663
2664        mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2665        fi.draw(mFolderIconCanvas);
2666        mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
2667        if (fi.getFolder() != null) {
2668            mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2669            mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
2670        }
2671        // Just in case this image view is still in the drag layer from a previous animation,
2672        // we remove it and re-add it.
2673        if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2674            mDragLayer.removeView(mFolderIconImageView);
2675        }
2676        mDragLayer.addView(mFolderIconImageView, lp);
2677        if (fi.getFolder() != null) {
2678            fi.getFolder().bringToFront();
2679        }
2680    }
2681
2682    private void growAndFadeOutFolderIcon(FolderIcon fi) {
2683        if (fi == null) return;
2684        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2685        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2686        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2687
2688        FolderInfo info = (FolderInfo) fi.getTag();
2689        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2690            CellLayout cl = (CellLayout) fi.getParent().getParent();
2691            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2692            cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
2693        }
2694
2695        // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2696        copyFolderIconToImage(fi);
2697        fi.setVisibility(View.INVISIBLE);
2698
2699        ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2700                scaleX, scaleY);
2701        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2702        oa.start();
2703    }
2704
2705    private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
2706        if (fi == null) return;
2707        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2708        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2709        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2710
2711        final CellLayout cl = (CellLayout) fi.getParent().getParent();
2712
2713        // We remove and re-draw the FolderIcon in-case it has changed
2714        mDragLayer.removeView(mFolderIconImageView);
2715        copyFolderIconToImage(fi);
2716        ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2717                scaleX, scaleY);
2718        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2719        oa.addListener(new AnimatorListenerAdapter() {
2720            @Override
2721            public void onAnimationEnd(Animator animation) {
2722                if (cl != null) {
2723                    cl.clearFolderLeaveBehind();
2724                    // Remove the ImageView copy of the FolderIcon and make the original visible.
2725                    mDragLayer.removeView(mFolderIconImageView);
2726                    fi.setVisibility(View.VISIBLE);
2727                }
2728            }
2729        });
2730        oa.start();
2731    }
2732
2733    /**
2734     * Opens the user folder described by the specified tag. The opening of the folder
2735     * is animated relative to the specified View. If the View is null, no animation
2736     * is played.
2737     *
2738     * @param folderInfo The FolderInfo describing the folder to open.
2739     */
2740    public void openFolder(FolderIcon folderIcon) {
2741        Folder folder = folderIcon.getFolder();
2742        FolderInfo info = folder.mInfo;
2743
2744        info.opened = true;
2745
2746        // Just verify that the folder hasn't already been added to the DragLayer.
2747        // There was a one-off crash where the folder had a parent already.
2748        if (folder.getParent() == null) {
2749            mDragLayer.addView(folder);
2750            mDragController.addDropTarget((DropTarget) folder);
2751        } else {
2752            Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2753                    folder.getParent() + ").");
2754        }
2755        folder.animateOpen();
2756        growAndFadeOutFolderIcon(folderIcon);
2757
2758        // Notify the accessibility manager that this folder "window" has appeared and occluded
2759        // the workspace items
2760        folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2761        getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
2762    }
2763
2764    public void closeFolder() {
2765        Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2766        if (folder != null) {
2767            if (folder.isEditingName()) {
2768                folder.dismissEditingName();
2769            }
2770            closeFolder(folder);
2771
2772            // Dismiss the folder cling
2773            mLauncherClings.dismissFolderCling(null);
2774        }
2775    }
2776
2777    void closeFolder(Folder folder) {
2778        folder.getInfo().opened = false;
2779
2780        ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2781        if (parent != null) {
2782            FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2783            shrinkAndFadeInFolderIcon(fi);
2784        }
2785        folder.animateClosed();
2786
2787        // Notify the accessibility manager that this folder "window" has disappeard and no
2788        // longer occludeds the workspace items
2789        getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2790    }
2791
2792    public boolean onLongClick(View v) {
2793        if (!isDraggingEnabled()) return false;
2794        if (isWorkspaceLocked()) return false;
2795        if (mState != State.WORKSPACE) return false;
2796
2797        if (v instanceof Workspace) {
2798            if (!mWorkspace.isInOverviewMode()) {
2799                if (mWorkspace.enterOverviewMode()) {
2800                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2801                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2802                    return true;
2803                } else {
2804                    return false;
2805                }
2806            }
2807        }
2808
2809        if (!(v instanceof CellLayout)) {
2810            v = (View) v.getParent().getParent();
2811        }
2812
2813        resetAddInfo();
2814        CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
2815        // This happens when long clicking an item with the dpad/trackball
2816        if (longClickCellInfo == null) {
2817            return true;
2818        }
2819
2820        // The hotseat touch handling does not go through Workspace, and we always allow long press
2821        // on hotseat items.
2822        final View itemUnderLongClick = longClickCellInfo.cell;
2823        final boolean inHotseat = isHotseatLayout(v);
2824        boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
2825        if (allowLongPress && !mDragController.isDragging()) {
2826            if (itemUnderLongClick == null) {
2827                // User long pressed on empty space
2828                mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2829                        HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2830                // Disabling reordering until we sort out some issues.
2831                if (mWorkspace.isInOverviewMode()) {
2832                    mWorkspace.startReordering(v);
2833                } else {
2834                    mWorkspace.enterOverviewMode();
2835                }
2836            } else {
2837                final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2838                        mHotseat.getOrderInHotseat(
2839                                longClickCellInfo.cellX,
2840                                longClickCellInfo.cellY));
2841                if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
2842                    // User long pressed on an item
2843                    mWorkspace.startDrag(longClickCellInfo);
2844                }
2845            }
2846        }
2847        return true;
2848    }
2849
2850    boolean isHotseatLayout(View layout) {
2851        return mHotseat != null && layout != null &&
2852                (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2853    }
2854
2855    /**
2856     * Returns the CellLayout of the specified container at the specified screen.
2857     */
2858    CellLayout getCellLayout(long container, long screenId) {
2859        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2860            if (mHotseat != null) {
2861                return mHotseat.getLayout();
2862            } else {
2863                return null;
2864            }
2865        } else {
2866            return (CellLayout) mWorkspace.getScreenWithId(screenId);
2867        }
2868    }
2869
2870    public boolean isAllAppsVisible() {
2871        return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
2872    }
2873
2874    /**
2875     * Helper method for the cameraZoomIn/cameraZoomOut animations
2876     * @param view The view being animated
2877     * @param scaleFactor The scale factor used for the zoom
2878     */
2879    private void setPivotsForZoom(View view, float scaleFactor) {
2880        view.setPivotX(view.getWidth() / 2.0f);
2881        view.setPivotY(view.getHeight() / 2.0f);
2882    }
2883
2884    private void setWorkspaceBackground(boolean workspace) {
2885        mLauncherView.setBackground(workspace ?
2886                mWorkspaceBackgroundDrawable : null);
2887    }
2888
2889    protected void changeWallpaperVisiblity(boolean visible) {
2890        int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2891        int curflags = getWindow().getAttributes().flags
2892                & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2893        if (wpflags != curflags) {
2894            getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2895        }
2896        setWorkspaceBackground(visible);
2897    }
2898
2899    private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2900        if (v instanceof LauncherTransitionable) {
2901            ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2902        }
2903    }
2904
2905    private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2906        if (v instanceof LauncherTransitionable) {
2907            ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2908        }
2909
2910        // Update the workspace transition step as well
2911        dispatchOnLauncherTransitionStep(v, 0f);
2912    }
2913
2914    private void dispatchOnLauncherTransitionStep(View v, float t) {
2915        if (v instanceof LauncherTransitionable) {
2916            ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2917        }
2918    }
2919
2920    private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2921        if (v instanceof LauncherTransitionable) {
2922            ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2923        }
2924
2925        // Update the workspace transition step as well
2926        dispatchOnLauncherTransitionStep(v, 1f);
2927    }
2928
2929    /**
2930     * Things to test when changing the following seven functions.
2931     *   - Home from workspace
2932     *          - from center screen
2933     *          - from other screens
2934     *   - Home from all apps
2935     *          - from center screen
2936     *          - from other screens
2937     *   - Back from all apps
2938     *          - from center screen
2939     *          - from other screens
2940     *   - Launch app from workspace and quit
2941     *          - with back
2942     *          - with home
2943     *   - Launch app from all apps and quit
2944     *          - with back
2945     *          - with home
2946     *   - Go to a screen that's not the default, then all
2947     *     apps, and launch and app, and go back
2948     *          - with back
2949     *          -with home
2950     *   - On workspace, long press power and go back
2951     *          - with back
2952     *          - with home
2953     *   - On all apps, long press power and go back
2954     *          - with back
2955     *          - with home
2956     *   - On workspace, power off
2957     *   - On all apps, power off
2958     *   - Launch an app and turn off the screen while in that app
2959     *          - Go back with home key
2960     *          - Go back with back key  TODO: make this not go to workspace
2961     *          - From all apps
2962     *          - From workspace
2963     *   - Enter and exit car mode (becuase it causes an extra configuration changed)
2964     *          - From all apps
2965     *          - From the center workspace
2966     *          - From another workspace
2967     */
2968
2969    /**
2970     * Zoom the camera out from the workspace to reveal 'toView'.
2971     * Assumes that the view to show is anchored at either the very top or very bottom
2972     * of the screen.
2973     */
2974    private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
2975        AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2976        showAppsCustomizeHelper(animated, springLoaded, contentType);
2977    }
2978    private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2979                                         final AppsCustomizePagedView.ContentType contentType) {
2980        if (mStateAnimation != null) {
2981            mStateAnimation.setDuration(0);
2982            mStateAnimation.cancel();
2983            mStateAnimation = null;
2984        }
2985        final Resources res = getResources();
2986
2987        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2988        final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2989        final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2990        final View fromView = mWorkspace;
2991        final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
2992        final int startDelay =
2993                res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
2994
2995        setPivotsForZoom(toView, scale);
2996
2997        // Shrink workspaces away if going to AppsCustomize from workspace
2998        Animator workspaceAnim =
2999                mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
3000        if (!LauncherAppState.isDisableAllApps()
3001                || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3002            // Set the content type for the all apps/widgets space
3003            mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3004        }
3005
3006        if (animated) {
3007            toView.setScaleX(scale);
3008            toView.setScaleY(scale);
3009            final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3010            scaleAnim.
3011                scaleX(1f).scaleY(1f).
3012                setDuration(duration).
3013                setInterpolator(new Workspace.ZoomOutInterpolator());
3014
3015            toView.setVisibility(View.VISIBLE);
3016            toView.setAlpha(0f);
3017            final ObjectAnimator alphaAnim = LauncherAnimUtils
3018                .ofFloat(toView, "alpha", 0f, 1f)
3019                .setDuration(fadeDuration);
3020            alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
3021            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3022                @Override
3023                public void onAnimationUpdate(ValueAnimator animation) {
3024                    if (animation == null) {
3025                        throw new RuntimeException("animation is null");
3026                    }
3027                    float t = (Float) animation.getAnimatedValue();
3028                    dispatchOnLauncherTransitionStep(fromView, t);
3029                    dispatchOnLauncherTransitionStep(toView, t);
3030                }
3031            });
3032
3033            // toView should appear right at the end of the workspace shrink
3034            // animation
3035            mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3036            mStateAnimation.play(scaleAnim).after(startDelay);
3037            mStateAnimation.play(alphaAnim).after(startDelay);
3038
3039            mStateAnimation.addListener(new AnimatorListenerAdapter() {
3040                @Override
3041                public void onAnimationStart(Animator animation) {
3042                    // Prepare the position
3043                    toView.setTranslationX(0.0f);
3044                    toView.setTranslationY(0.0f);
3045                    toView.setVisibility(View.VISIBLE);
3046                    toView.bringToFront();
3047                }
3048                @Override
3049                public void onAnimationEnd(Animator animation) {
3050                    dispatchOnLauncherTransitionEnd(fromView, animated, false);
3051                    dispatchOnLauncherTransitionEnd(toView, animated, false);
3052
3053                    // Hide the search bar
3054                    if (mSearchDropTargetBar != null) {
3055                        mSearchDropTargetBar.hideSearchBar(false);
3056                    }
3057                }
3058            });
3059
3060            if (workspaceAnim != null) {
3061                mStateAnimation.play(workspaceAnim);
3062            }
3063
3064            boolean delayAnim = false;
3065
3066            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3067            dispatchOnLauncherTransitionPrepare(toView, animated, false);
3068
3069            // If any of the objects being animated haven't been measured/laid out
3070            // yet, delay the animation until we get a layout pass
3071            if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
3072                    (mWorkspace.getMeasuredWidth() == 0) ||
3073                    (toView.getMeasuredWidth() == 0)) {
3074                delayAnim = true;
3075            }
3076
3077            final AnimatorSet stateAnimation = mStateAnimation;
3078            final Runnable startAnimRunnable = new Runnable() {
3079                public void run() {
3080                    // Check that mStateAnimation hasn't changed while
3081                    // we waited for a layout/draw pass
3082                    if (mStateAnimation != stateAnimation)
3083                        return;
3084                    setPivotsForZoom(toView, scale);
3085                    dispatchOnLauncherTransitionStart(fromView, animated, false);
3086                    dispatchOnLauncherTransitionStart(toView, animated, false);
3087                    LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
3088                }
3089            };
3090            if (delayAnim) {
3091                final ViewTreeObserver observer = toView.getViewTreeObserver();
3092                observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3093                        public void onGlobalLayout() {
3094                            startAnimRunnable.run();
3095                            toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3096                        }
3097                    });
3098            } else {
3099                startAnimRunnable.run();
3100            }
3101        } else {
3102            toView.setTranslationX(0.0f);
3103            toView.setTranslationY(0.0f);
3104            toView.setScaleX(1.0f);
3105            toView.setScaleY(1.0f);
3106            toView.setVisibility(View.VISIBLE);
3107            toView.bringToFront();
3108
3109            if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
3110                // Hide the search bar
3111                if (mSearchDropTargetBar != null) {
3112                    mSearchDropTargetBar.hideSearchBar(false);
3113                }
3114            }
3115            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3116            dispatchOnLauncherTransitionStart(fromView, animated, false);
3117            dispatchOnLauncherTransitionEnd(fromView, animated, false);
3118            dispatchOnLauncherTransitionPrepare(toView, animated, false);
3119            dispatchOnLauncherTransitionStart(toView, animated, false);
3120            dispatchOnLauncherTransitionEnd(toView, animated, false);
3121        }
3122    }
3123
3124    /**
3125     * Zoom the camera back into the workspace, hiding 'fromView'.
3126     * This is the opposite of showAppsCustomizeHelper.
3127     * @param animated If true, the transition will be animated.
3128     */
3129    private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
3130            final boolean springLoaded, final Runnable onCompleteRunnable) {
3131
3132        if (mStateAnimation != null) {
3133            mStateAnimation.setDuration(0);
3134            mStateAnimation.cancel();
3135            mStateAnimation = null;
3136        }
3137        Resources res = getResources();
3138
3139        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
3140        final int fadeOutDuration =
3141                res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
3142        final float scaleFactor = (float)
3143                res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3144        final View fromView = mAppsCustomizeTabHost;
3145        final View toView = mWorkspace;
3146        Animator workspaceAnim = null;
3147        if (toState == Workspace.State.NORMAL) {
3148            int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3149            workspaceAnim = mWorkspace.getChangeStateAnimation(
3150                    toState, animated, stagger, -1);
3151        } else if (toState == Workspace.State.SPRING_LOADED ||
3152                toState == Workspace.State.OVERVIEW) {
3153            workspaceAnim = mWorkspace.getChangeStateAnimation(
3154                    toState, animated);
3155        }
3156
3157        setPivotsForZoom(fromView, scaleFactor);
3158        showHotseat(animated);
3159        if (animated) {
3160            final LauncherViewPropertyAnimator scaleAnim =
3161                    new LauncherViewPropertyAnimator(fromView);
3162            scaleAnim.
3163                scaleX(scaleFactor).scaleY(scaleFactor).
3164                setDuration(duration).
3165                setInterpolator(new Workspace.ZoomInInterpolator());
3166
3167            final ObjectAnimator alphaAnim = LauncherAnimUtils
3168                .ofFloat(fromView, "alpha", 1f, 0f)
3169                .setDuration(fadeOutDuration);
3170            alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
3171            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3172                @Override
3173                public void onAnimationUpdate(ValueAnimator animation) {
3174                    float t = 1f - (Float) animation.getAnimatedValue();
3175                    dispatchOnLauncherTransitionStep(fromView, t);
3176                    dispatchOnLauncherTransitionStep(toView, t);
3177                }
3178            });
3179
3180            mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3181
3182            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3183            dispatchOnLauncherTransitionPrepare(toView, animated, true);
3184            mAppsCustomizeContent.stopScrolling();
3185
3186            mStateAnimation.addListener(new AnimatorListenerAdapter() {
3187                @Override
3188                public void onAnimationEnd(Animator animation) {
3189                    fromView.setVisibility(View.GONE);
3190                    dispatchOnLauncherTransitionEnd(fromView, animated, true);
3191                    dispatchOnLauncherTransitionEnd(toView, animated, true);
3192                    if (onCompleteRunnable != null) {
3193                        onCompleteRunnable.run();
3194                    }
3195                    mAppsCustomizeContent.updateCurrentPageScroll();
3196                }
3197            });
3198
3199            mStateAnimation.playTogether(scaleAnim, alphaAnim);
3200            if (workspaceAnim != null) {
3201                mStateAnimation.play(workspaceAnim);
3202            }
3203            dispatchOnLauncherTransitionStart(fromView, animated, true);
3204            dispatchOnLauncherTransitionStart(toView, animated, true);
3205            LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
3206        } else {
3207            fromView.setVisibility(View.GONE);
3208            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3209            dispatchOnLauncherTransitionStart(fromView, animated, true);
3210            dispatchOnLauncherTransitionEnd(fromView, animated, true);
3211            dispatchOnLauncherTransitionPrepare(toView, animated, true);
3212            dispatchOnLauncherTransitionStart(toView, animated, true);
3213            dispatchOnLauncherTransitionEnd(toView, animated, true);
3214        }
3215    }
3216
3217    @Override
3218    public void onTrimMemory(int level) {
3219        super.onTrimMemory(level);
3220        if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
3221            mAppsCustomizeTabHost.onTrimMemory();
3222        }
3223    }
3224
3225    protected void showWorkspace(boolean animated) {
3226        showWorkspace(animated, null);
3227    }
3228
3229    protected void showWorkspace() {
3230        showWorkspace(true);
3231    }
3232
3233    void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3234        if (mWorkspace.isInOverviewMode()) {
3235            mWorkspace.exitOverviewMode(animated);
3236        }
3237        if (mState != State.WORKSPACE) {
3238            boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
3239            mWorkspace.setVisibility(View.VISIBLE);
3240            hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
3241
3242            // Show the search bar (only animate if we were showing the drop target bar in spring
3243            // loaded mode)
3244            if (mSearchDropTargetBar != null) {
3245                mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
3246            }
3247
3248            // Set focus to the AppsCustomize button
3249            if (mAllAppsButton != null) {
3250                mAllAppsButton.requestFocus();
3251            }
3252        }
3253
3254        // Change the state *after* we've called all the transition code
3255        mState = State.WORKSPACE;
3256
3257        // Resume the auto-advance of widgets
3258        mUserPresent = true;
3259        updateRunning();
3260
3261        // Send an accessibility event to announce the context change
3262        getWindow().getDecorView()
3263                .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3264
3265        onWorkspaceShown(animated);
3266    }
3267
3268    void showOverviewMode(boolean animated) {
3269        mWorkspace.setVisibility(View.VISIBLE);
3270        hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3271        mState = State.WORKSPACE;
3272        onWorkspaceShown(animated);
3273    }
3274
3275    public void onWorkspaceShown(boolean animated) {
3276    }
3277
3278    void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3279                     boolean resetPageToZero) {
3280        if (mState != State.WORKSPACE) return;
3281
3282        if (resetPageToZero) {
3283            mAppsCustomizeTabHost.reset();
3284        }
3285        showAppsCustomizeHelper(animated, false, contentType);
3286        mAppsCustomizeTabHost.requestFocus();
3287
3288        // Change the state *after* we've called all the transition code
3289        mState = State.APPS_CUSTOMIZE;
3290
3291        // Pause the auto-advance of widgets until we are out of AllApps
3292        mUserPresent = false;
3293        updateRunning();
3294        closeFolder();
3295
3296        // Send an accessibility event to announce the context change
3297        getWindow().getDecorView()
3298                .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3299    }
3300
3301    void enterSpringLoadedDragMode() {
3302        if (isAllAppsVisible()) {
3303            hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
3304            mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
3305        }
3306    }
3307
3308    void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
3309            final Runnable onCompleteRunnable) {
3310        if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3311
3312        mHandler.postDelayed(new Runnable() {
3313            @Override
3314            public void run() {
3315                if (successfulDrop) {
3316                    // Before we show workspace, hide all apps again because
3317                    // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3318                    // clean up our state transition functions
3319                    mAppsCustomizeTabHost.setVisibility(View.GONE);
3320                    showWorkspace(true, onCompleteRunnable);
3321                } else {
3322                    exitSpringLoadedDragMode();
3323                }
3324            }
3325        }, delay);
3326
3327    }
3328
3329    void exitSpringLoadedDragMode() {
3330        if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
3331            final boolean animated = true;
3332            final boolean springLoaded = true;
3333            showAppsCustomizeHelper(animated, springLoaded);
3334            mState = State.APPS_CUSTOMIZE;
3335        }
3336        // Otherwise, we are not in spring loaded mode, so don't do anything.
3337    }
3338
3339    void lockAllApps() {
3340        // TODO
3341    }
3342
3343    void unlockAllApps() {
3344        // TODO
3345    }
3346
3347    /**
3348     * Shows the hotseat area.
3349     */
3350    void showHotseat(boolean animated) {
3351        if (!LauncherAppState.getInstance().isScreenLarge()) {
3352            if (animated) {
3353                if (mHotseat.getAlpha() != 1f) {
3354                    int duration = 0;
3355                    if (mSearchDropTargetBar != null) {
3356                        duration = mSearchDropTargetBar.getTransitionInDuration();
3357                    }
3358                    mHotseat.animate().alpha(1f).setDuration(duration);
3359                }
3360            } else {
3361                mHotseat.setAlpha(1f);
3362            }
3363        }
3364    }
3365
3366    /**
3367     * Hides the hotseat area.
3368     */
3369    void hideHotseat(boolean animated) {
3370        if (!LauncherAppState.getInstance().isScreenLarge()) {
3371            if (animated) {
3372                if (mHotseat.getAlpha() != 0f) {
3373                    int duration = 0;
3374                    if (mSearchDropTargetBar != null) {
3375                        duration = mSearchDropTargetBar.getTransitionOutDuration();
3376                    }
3377                    mHotseat.animate().alpha(0f).setDuration(duration);
3378                }
3379            } else {
3380                mHotseat.setAlpha(0f);
3381            }
3382        }
3383    }
3384
3385    /**
3386     * Add an item from all apps or customize onto the given workspace screen.
3387     * If layout is null, add to the current screen.
3388     */
3389    void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
3390        if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3391            showOutOfSpaceMessage(isHotseatLayout(layout));
3392        }
3393    }
3394
3395    /** Maps the current orientation to an index for referencing orientation correct global icons */
3396    private int getCurrentOrientationIndexForGlobalIcons() {
3397        // default - 0, landscape - 1
3398        switch (getResources().getConfiguration().orientation) {
3399        case Configuration.ORIENTATION_LANDSCAPE:
3400            return 1;
3401        default:
3402            return 0;
3403        }
3404    }
3405
3406    private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
3407        try {
3408            PackageManager packageManager = getPackageManager();
3409            // Look for the toolbar icon specified in the activity meta-data
3410            Bundle metaData = packageManager.getActivityInfo(
3411                    activityName, PackageManager.GET_META_DATA).metaData;
3412            if (metaData != null) {
3413                int iconResId = metaData.getInt(resourceName);
3414                if (iconResId != 0) {
3415                    Resources res = packageManager.getResourcesForActivity(activityName);
3416                    return res.getDrawable(iconResId);
3417                }
3418            }
3419        } catch (NameNotFoundException e) {
3420            // This can happen if the activity defines an invalid drawable
3421            Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3422                    " not found", e);
3423        } catch (Resources.NotFoundException nfe) {
3424            // This can happen if the activity defines an invalid drawable
3425            Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3426                    nfe);
3427        }
3428        return null;
3429    }
3430
3431    // if successful in getting icon, return it; otherwise, set button to use default drawable
3432    private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3433            int buttonId, ComponentName activityName, int fallbackDrawableId,
3434            String toolbarResourceName) {
3435        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
3436        Resources r = getResources();
3437        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3438        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3439
3440        TextView button = (TextView) findViewById(buttonId);
3441        // If we were unable to find the icon via the meta-data, use a generic one
3442        if (toolbarIcon == null) {
3443            toolbarIcon = r.getDrawable(fallbackDrawableId);
3444            toolbarIcon.setBounds(0, 0, w, h);
3445            if (button != null) {
3446                button.setCompoundDrawables(toolbarIcon, null, null, null);
3447            }
3448            return null;
3449        } else {
3450            toolbarIcon.setBounds(0, 0, w, h);
3451            if (button != null) {
3452                button.setCompoundDrawables(toolbarIcon, null, null, null);
3453            }
3454            return toolbarIcon.getConstantState();
3455        }
3456    }
3457
3458    // if successful in getting icon, return it; otherwise, set button to use default drawable
3459    private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3460            int buttonId, ComponentName activityName, int fallbackDrawableId,
3461            String toolbarResourceName) {
3462        ImageView button = (ImageView) findViewById(buttonId);
3463        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
3464
3465        if (button != null) {
3466            // If we were unable to find the icon via the meta-data, use a
3467            // generic one
3468            if (toolbarIcon == null) {
3469                button.setImageResource(fallbackDrawableId);
3470            } else {
3471                button.setImageDrawable(toolbarIcon);
3472            }
3473        }
3474
3475        return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3476
3477    }
3478
3479    private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
3480        TextView button = (TextView) findViewById(buttonId);
3481        button.setCompoundDrawables(d, null, null, null);
3482    }
3483
3484    private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3485        ImageView button = (ImageView) findViewById(buttonId);
3486        button.setImageDrawable(d.newDrawable(getResources()));
3487    }
3488
3489    private void invalidatePressedFocusedStates(View container, View button) {
3490        if (container instanceof HolographicLinearLayout) {
3491            HolographicLinearLayout layout = (HolographicLinearLayout) container;
3492            layout.invalidatePressedFocusedStates();
3493        } else if (button instanceof HolographicImageView) {
3494            HolographicImageView view = (HolographicImageView) button;
3495            view.invalidatePressedFocusedStates();
3496        }
3497    }
3498
3499    public View getQsbBar() {
3500        if (mQsb == null) {
3501            mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3502            mSearchDropTargetBar.addView(mQsb);
3503        }
3504        return mQsb;
3505    }
3506
3507    protected boolean updateGlobalSearchIcon() {
3508        final View searchButtonContainer = findViewById(R.id.search_button_container);
3509        final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3510        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3511        final View voiceButton = findViewById(R.id.voice_button);
3512
3513        final SearchManager searchManager =
3514                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3515        ComponentName activityName = searchManager.getGlobalSearchActivity();
3516        if (activityName != null) {
3517            int coi = getCurrentOrientationIndexForGlobalIcons();
3518            sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3519                    R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3520                    TOOLBAR_SEARCH_ICON_METADATA_NAME);
3521            if (sGlobalSearchIcon[coi] == null) {
3522                sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3523                        R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3524                        TOOLBAR_ICON_METADATA_NAME);
3525            }
3526
3527            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3528            searchButton.setVisibility(View.VISIBLE);
3529            invalidatePressedFocusedStates(searchButtonContainer, searchButton);
3530            return true;
3531        } else {
3532            // We disable both search and voice search when there is no global search provider
3533            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3534            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3535            if (searchButton != null) searchButton.setVisibility(View.GONE);
3536            if (voiceButton != null) voiceButton.setVisibility(View.GONE);
3537            updateVoiceButtonProxyVisible(false);
3538            return false;
3539        }
3540    }
3541
3542    protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
3543        final View searchButtonContainer = findViewById(R.id.search_button_container);
3544        final View searchButton = (ImageView) findViewById(R.id.search_button);
3545        updateButtonWithDrawable(R.id.search_button, d);
3546        invalidatePressedFocusedStates(searchButtonContainer, searchButton);
3547    }
3548
3549    protected boolean updateVoiceSearchIcon(boolean searchVisible) {
3550        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3551        final View voiceButton = findViewById(R.id.voice_button);
3552
3553        // We only show/update the voice search icon if the search icon is enabled as well
3554        final SearchManager searchManager =
3555                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3556        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3557
3558        ComponentName activityName = null;
3559        if (globalSearchActivity != null) {
3560            // Check if the global search activity handles voice search
3561            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3562            intent.setPackage(globalSearchActivity.getPackageName());
3563            activityName = intent.resolveActivity(getPackageManager());
3564        }
3565
3566        if (activityName == null) {
3567            // Fallback: check if an activity other than the global search activity
3568            // resolves this
3569            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3570            activityName = intent.resolveActivity(getPackageManager());
3571        }
3572        if (searchVisible && activityName != null) {
3573            int coi = getCurrentOrientationIndexForGlobalIcons();
3574            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3575                    R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3576                    TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3577            if (sVoiceSearchIcon[coi] == null) {
3578                sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3579                        R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3580                        TOOLBAR_ICON_METADATA_NAME);
3581            }
3582            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
3583            voiceButton.setVisibility(View.VISIBLE);
3584            updateVoiceButtonProxyVisible(false);
3585            invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
3586            return true;
3587        } else {
3588            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3589            if (voiceButton != null) voiceButton.setVisibility(View.GONE);
3590            updateVoiceButtonProxyVisible(false);
3591            return false;
3592        }
3593    }
3594
3595    protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
3596        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3597        final View voiceButton = findViewById(R.id.voice_button);
3598        updateButtonWithDrawable(R.id.voice_button, d);
3599        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
3600    }
3601
3602    public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
3603        final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3604        if (voiceButtonProxy != null) {
3605            boolean visible = !forceDisableVoiceButtonProxy &&
3606                    mWorkspace.shouldVoiceButtonProxyBeVisible();
3607            voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3608            voiceButtonProxy.bringToFront();
3609        }
3610    }
3611
3612    /**
3613     * This is an overrid eot disable the voice button proxy.  If disabled is true, then the voice button proxy
3614     * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3615     */
3616    public void disableVoiceButtonProxy(boolean disabled) {
3617        updateVoiceButtonProxyVisible(disabled);
3618    }
3619    /**
3620     * Sets the app market icon
3621     */
3622    private void updateAppMarketIcon() {
3623        if (!DISABLE_MARKET_BUTTON) {
3624            final View marketButton = findViewById(R.id.market_button);
3625            Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3626            // Find the app market activity by resolving an intent.
3627            // (If multiple app markets are installed, it will return the ResolverActivity.)
3628            ComponentName activityName = intent.resolveActivity(getPackageManager());
3629            if (activityName != null) {
3630                int coi = getCurrentOrientationIndexForGlobalIcons();
3631                mAppMarketIntent = intent;
3632                sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3633                        R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3634                        TOOLBAR_ICON_METADATA_NAME);
3635                marketButton.setVisibility(View.VISIBLE);
3636            } else {
3637                // We should hide and disable the view so that we don't try and restore the visibility
3638                // of it when we swap between drag & normal states from IconDropTarget subclasses.
3639                marketButton.setVisibility(View.GONE);
3640                marketButton.setEnabled(false);
3641            }
3642        }
3643    }
3644
3645    private void updateAppMarketIcon(Drawable.ConstantState d) {
3646        if (!DISABLE_MARKET_BUTTON) {
3647            // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3648            Resources r = getResources();
3649            Drawable marketIconDrawable = d.newDrawable(r);
3650            int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3651            int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3652            marketIconDrawable.setBounds(0, 0, w, h);
3653
3654            updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3655        }
3656    }
3657
3658    @Override
3659    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3660        final boolean result = super.dispatchPopulateAccessibilityEvent(event);
3661        final List<CharSequence> text = event.getText();
3662        text.clear();
3663        // Populate event with a fake title based on the current state.
3664        if (mState == State.APPS_CUSTOMIZE) {
3665            text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
3666        } else {
3667            text.add(getString(R.string.all_apps_home_button_label));
3668        }
3669        return result;
3670    }
3671
3672    /**
3673     * Receives notifications when system dialogs are to be closed.
3674     */
3675    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3676        @Override
3677        public void onReceive(Context context, Intent intent) {
3678            closeSystemDialogs();
3679        }
3680    }
3681
3682    /**
3683     * Receives notifications whenever the appwidgets are reset.
3684     */
3685    private class AppWidgetResetObserver extends ContentObserver {
3686        public AppWidgetResetObserver() {
3687            super(new Handler());
3688        }
3689
3690        @Override
3691        public void onChange(boolean selfChange) {
3692            onAppWidgetReset();
3693        }
3694    }
3695
3696    /**
3697     * If the activity is currently paused, signal that we need to run the passed Runnable
3698     * in onResume.
3699     *
3700     * This needs to be called from incoming places where resources might have been loaded
3701     * while we are paused.  That is becaues the Configuration might be wrong
3702     * when we're not running, and if it comes back to what it was when we
3703     * were paused, we are not restarted.
3704     *
3705     * Implementation of the method from LauncherModel.Callbacks.
3706     *
3707     * @return true if we are currently paused.  The caller might be able to
3708     * skip some work in that case since we will come back again.
3709     */
3710    private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
3711        if (mPaused) {
3712            Log.i(TAG, "Deferring update until onResume");
3713            if (deletePreviousRunnables) {
3714                while (mBindOnResumeCallbacks.remove(run)) {
3715                }
3716            }
3717            mBindOnResumeCallbacks.add(run);
3718            return true;
3719        } else {
3720            return false;
3721        }
3722    }
3723
3724    private boolean waitUntilResume(Runnable run) {
3725        return waitUntilResume(run, false);
3726    }
3727
3728    public void addOnResumeCallback(Runnable run) {
3729        mOnResumeCallbacks.add(run);
3730    }
3731
3732    /**
3733     * If the activity is currently paused, signal that we need to re-run the loader
3734     * in onResume.
3735     *
3736     * This needs to be called from incoming places where resources might have been loaded
3737     * while we are paused.  That is becaues the Configuration might be wrong
3738     * when we're not running, and if it comes back to what it was when we
3739     * were paused, we are not restarted.
3740     *
3741     * Implementation of the method from LauncherModel.Callbacks.
3742     *
3743     * @return true if we are currently paused.  The caller might be able to
3744     * skip some work in that case since we will come back again.
3745     */
3746    public boolean setLoadOnResume() {
3747        if (mPaused) {
3748            Log.i(TAG, "setLoadOnResume");
3749            mOnResumeNeedsLoad = true;
3750            return true;
3751        } else {
3752            return false;
3753        }
3754    }
3755
3756    /**
3757     * Implementation of the method from LauncherModel.Callbacks.
3758     */
3759    public int getCurrentWorkspaceScreen() {
3760        if (mWorkspace != null) {
3761            return mWorkspace.getCurrentPage();
3762        } else {
3763            return SCREEN_COUNT / 2;
3764        }
3765    }
3766
3767    /**
3768     * Refreshes the shortcuts shown on the workspace.
3769     *
3770     * Implementation of the method from LauncherModel.Callbacks.
3771     */
3772    public void startBinding() {
3773        // If we're starting binding all over again, clear any bind calls we'd postponed in
3774        // the past (see waitUntilResume) -- we don't need them since we're starting binding
3775        // from scratch again
3776        mBindOnResumeCallbacks.clear();
3777
3778        // Clear the workspace because it's going to be rebound
3779        mWorkspace.clearDropTargets();
3780        mWorkspace.removeAllWorkspaceScreens();
3781
3782        mWidgetsToAdvance.clear();
3783        if (mHotseat != null) {
3784            mHotseat.resetLayout();
3785        }
3786    }
3787
3788    @Override
3789    public void bindScreens(ArrayList<Long> orderedScreenIds) {
3790        bindAddScreens(orderedScreenIds);
3791
3792        // If there are no screens, we need to have an empty screen
3793        if (orderedScreenIds.size() == 0) {
3794            mWorkspace.addExtraEmptyScreen();
3795        }
3796
3797        // Create the custom content page (this call updates mDefaultScreen which calls
3798        // setCurrentPage() so ensure that all pages are added before calling this).
3799        // The actual content of the custom page will be added during onFinishBindingItems().
3800        if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3801            mWorkspace.createCustomContentPage();
3802        }
3803    }
3804
3805    @Override
3806    public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
3807        // Log to disk
3808        Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3809        Launcher.addDumpLog(TAG, "11683562 -   orderedScreenIds: " +
3810                TextUtils.join(", ", orderedScreenIds), true);
3811        int count = orderedScreenIds.size();
3812        for (int i = 0; i < count; i++) {
3813            mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
3814        }
3815    }
3816
3817    private boolean shouldShowWeightWatcher() {
3818        String spKey = LauncherAppState.getSharedPreferencesKey();
3819        SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3820        boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
3821
3822        return show;
3823    }
3824
3825    private void toggleShowWeightWatcher() {
3826        String spKey = LauncherAppState.getSharedPreferencesKey();
3827        SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3828        boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3829
3830        show = !show;
3831
3832        SharedPreferences.Editor editor = sp.edit();
3833        editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3834        editor.commit();
3835
3836        if (mWeightWatcher != null) {
3837            mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3838        }
3839    }
3840
3841    public void bindAppsAdded(final ArrayList<Long> newScreens,
3842                              final ArrayList<ItemInfo> addNotAnimated,
3843                              final ArrayList<ItemInfo> addAnimated,
3844                              final ArrayList<AppInfo> addedApps) {
3845        Runnable r = new Runnable() {
3846            public void run() {
3847                bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
3848            }
3849        };
3850        if (waitUntilResume(r)) {
3851            return;
3852        }
3853
3854        // Add the new screens
3855        bindAddScreens(newScreens);
3856
3857        // We add the items without animation on non-visible pages, and with
3858        // animations on the new page (which we will try and snap to).
3859        if (!addNotAnimated.isEmpty()) {
3860            bindItems(addNotAnimated, 0,
3861                    addNotAnimated.size(), false);
3862        }
3863        if (!addAnimated.isEmpty()) {
3864            bindItems(addAnimated, 0,
3865                    addAnimated.size(), true);
3866        }
3867
3868        // Remove the extra empty screen
3869        mWorkspace.removeExtraEmptyScreen(false, null);
3870
3871        if (!LauncherAppState.isDisableAllApps() &&
3872                addedApps != null && mAppsCustomizeContent != null) {
3873            mAppsCustomizeContent.addApps(addedApps);
3874        }
3875    }
3876
3877    /**
3878     * Bind the items start-end from the list.
3879     *
3880     * Implementation of the method from LauncherModel.Callbacks.
3881     */
3882    public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3883                          final boolean forceAnimateIcons) {
3884        Runnable r = new Runnable() {
3885            public void run() {
3886                bindItems(shortcuts, start, end, forceAnimateIcons);
3887            }
3888        };
3889        if (waitUntilResume(r)) {
3890            return;
3891        }
3892
3893        // Get the list of added shortcuts and intersect them with the set of shortcuts here
3894        final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3895        final Collection<Animator> bounceAnims = new ArrayList<Animator>();
3896        final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
3897        Workspace workspace = mWorkspace;
3898        long newShortcutsScreenId = -1;
3899        for (int i = start; i < end; i++) {
3900            final ItemInfo item = shortcuts.get(i);
3901
3902            // Short circuit if we are loading dock items for a configuration which has no dock
3903            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3904                    mHotseat == null) {
3905                continue;
3906            }
3907
3908            switch (item.itemType) {
3909                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3910                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3911                    ShortcutInfo info = (ShortcutInfo) item;
3912                    View shortcut = createShortcut(info);
3913
3914                    /*
3915                     * TODO: FIX collision case
3916                     */
3917                    if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3918                        CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3919                        if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3920                            throw new RuntimeException("OCCUPIED");
3921                        }
3922                    }
3923
3924                    workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3925                            item.cellY, 1, 1);
3926                    if (animateIcons) {
3927                        // Animate all the applications up now
3928                        shortcut.setAlpha(0f);
3929                        shortcut.setScaleX(0f);
3930                        shortcut.setScaleY(0f);
3931                        bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3932                        newShortcutsScreenId = item.screenId;
3933                    }
3934                    break;
3935                case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
3936                    FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
3937                            (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
3938                            (FolderInfo) item, mIconCache);
3939                    workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3940                            item.cellY, 1, 1);
3941                    break;
3942                default:
3943                    throw new RuntimeException("Invalid Item Type");
3944            }
3945        }
3946
3947        if (animateIcons) {
3948            // Animate to the correct page
3949            if (newShortcutsScreenId > -1) {
3950                long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3951                final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3952                final Runnable startBounceAnimRunnable = new Runnable() {
3953                    public void run() {
3954                        anim.playTogether(bounceAnims);
3955                        anim.start();
3956                    }
3957                };
3958                if (newShortcutsScreenId != currentScreenId) {
3959                    // We post the animation slightly delayed to prevent slowdowns
3960                    // when we are loading right after we return to launcher.
3961                    mWorkspace.postDelayed(new Runnable() {
3962                        public void run() {
3963                            if (mWorkspace != null) {
3964                                mWorkspace.snapToPage(newScreenIndex);
3965                                mWorkspace.postDelayed(startBounceAnimRunnable,
3966                                        NEW_APPS_ANIMATION_DELAY);
3967                            }
3968                        }
3969                    }, NEW_APPS_PAGE_MOVE_DELAY);
3970                } else {
3971                    mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
3972                }
3973            }
3974        }
3975        workspace.requestLayout();
3976    }
3977
3978    /**
3979     * Implementation of the method from LauncherModel.Callbacks.
3980     */
3981    public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3982        Runnable r = new Runnable() {
3983            public void run() {
3984                bindFolders(folders);
3985            }
3986        };
3987        if (waitUntilResume(r)) {
3988            return;
3989        }
3990        sFolders.clear();
3991        sFolders.putAll(folders);
3992    }
3993
3994    /**
3995     * Add the views for a widget to the workspace.
3996     *
3997     * Implementation of the method from LauncherModel.Callbacks.
3998     */
3999    public void bindAppWidget(final LauncherAppWidgetInfo item) {
4000        Runnable r = new Runnable() {
4001            public void run() {
4002                bindAppWidget(item);
4003            }
4004        };
4005        if (waitUntilResume(r)) {
4006            return;
4007        }
4008
4009        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4010        if (DEBUG_WIDGETS) {
4011            Log.d(TAG, "bindAppWidget: " + item);
4012        }
4013        final Workspace workspace = mWorkspace;
4014
4015        final int appWidgetId = item.appWidgetId;
4016        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4017        if (DEBUG_WIDGETS) {
4018            Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4019        }
4020
4021        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4022
4023        item.hostView.setTag(item);
4024        item.onBindAppWidget(this);
4025
4026        workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
4027                item.cellY, item.spanX, item.spanY, false);
4028        addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4029
4030        workspace.requestLayout();
4031
4032        if (DEBUG_WIDGETS) {
4033            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4034                    + (SystemClock.uptimeMillis()-start) + "ms");
4035        }
4036    }
4037
4038    public void onPageBoundSynchronously(int page) {
4039        mSynchronouslyBoundPages.add(page);
4040    }
4041
4042    /**
4043     * Callback saying that there aren't any more items to bind.
4044     *
4045     * Implementation of the method from LauncherModel.Callbacks.
4046     */
4047    public void finishBindingItems(final boolean upgradePath) {
4048        Runnable r = new Runnable() {
4049            public void run() {
4050                finishBindingItems(upgradePath);
4051            }
4052        };
4053        if (waitUntilResume(r)) {
4054            return;
4055        }
4056        if (mSavedState != null) {
4057            if (!mWorkspace.hasFocus()) {
4058                mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
4059            }
4060            mSavedState = null;
4061        }
4062
4063        mWorkspace.restoreInstanceStateForRemainingPages();
4064
4065        // If we received the result of any pending adds while the loader was running (e.g. the
4066        // widget configuration forced an orientation change), process them now.
4067        for (int i = 0; i < sPendingAddList.size(); i++) {
4068            completeAdd(sPendingAddList.get(i));
4069        }
4070        sPendingAddList.clear();
4071
4072        // Update the market app icon as necessary (the other icons will be managed in response to
4073        // package changes in bindSearchablesChanged()
4074        if (!DISABLE_MARKET_BUTTON) {
4075            updateAppMarketIcon();
4076        }
4077
4078        mWorkspaceLoading = false;
4079        if (upgradePath) {
4080            mWorkspace.getUniqueComponents(true, null);
4081            mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
4082        }
4083
4084        mWorkspace.post(new Runnable() {
4085            @Override
4086            public void run() {
4087                onFinishBindingItems();
4088            }
4089        });
4090    }
4091
4092    public boolean isAllAppsButtonRank(int rank) {
4093        if (mHotseat != null) {
4094            return mHotseat.isAllAppsButtonRank(rank);
4095        }
4096        return false;
4097    }
4098
4099    private boolean canRunNewAppsAnimation() {
4100        long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4101        return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4102    }
4103
4104    private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4105        ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4106                PropertyValuesHolder.ofFloat("alpha", 1f),
4107                PropertyValuesHolder.ofFloat("scaleX", 1f),
4108                PropertyValuesHolder.ofFloat("scaleY", 1f));
4109        bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4110        bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4111        bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4112        return bounceAnim;
4113    }
4114
4115    public boolean useVerticalBarLayout() {
4116        return LauncherAppState.getInstance().getDynamicGrid().
4117                getDeviceProfile().isVerticalBarLayout();
4118    }
4119
4120    protected Rect getSearchBarBounds() {
4121        return LauncherAppState.getInstance().getDynamicGrid().
4122                getDeviceProfile().getSearchBarBounds();
4123    }
4124
4125    @Override
4126    public void bindSearchablesChanged() {
4127        boolean searchVisible = updateGlobalSearchIcon();
4128        boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
4129        if (mSearchDropTargetBar != null) {
4130            mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4131        }
4132    }
4133
4134    /**
4135     * Add the icons for all apps.
4136     *
4137     * Implementation of the method from LauncherModel.Callbacks.
4138     */
4139    public void bindAllApplications(final ArrayList<AppInfo> apps) {
4140        if (LauncherAppState.isDisableAllApps()) {
4141            if (mIntentsOnWorkspaceFromUpgradePath != null) {
4142                if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4143                    getHotseat().addAllAppsFolder(mIconCache, apps,
4144                            mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4145                }
4146                mIntentsOnWorkspaceFromUpgradePath = null;
4147            }
4148            if (mAppsCustomizeContent != null) {
4149                mAppsCustomizeContent.onPackagesUpdated(
4150                        LauncherModel.getSortedWidgetsAndShortcuts(this));
4151            }
4152        } else {
4153            if (mAppsCustomizeContent != null) {
4154                mAppsCustomizeContent.setApps(apps);
4155                mAppsCustomizeContent.onPackagesUpdated(
4156                        LauncherModel.getSortedWidgetsAndShortcuts(this));
4157            }
4158        }
4159    }
4160
4161    /**
4162     * A package was updated.
4163     *
4164     * Implementation of the method from LauncherModel.Callbacks.
4165     */
4166    public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
4167        Runnable r = new Runnable() {
4168            public void run() {
4169                bindAppsUpdated(apps);
4170            }
4171        };
4172        if (waitUntilResume(r)) {
4173            return;
4174        }
4175
4176        if (mWorkspace != null) {
4177            mWorkspace.updateShortcuts(apps);
4178        }
4179
4180        if (!LauncherAppState.isDisableAllApps() &&
4181                mAppsCustomizeContent != null) {
4182            mAppsCustomizeContent.updateApps(apps);
4183        }
4184    }
4185
4186    /**
4187     * A package was uninstalled.  We take both the super set of packageNames
4188     * in addition to specific applications to remove, the reason being that
4189     * this can be called when a package is updated as well.  In that scenario,
4190     * we only remove specific components from the workspace, where as
4191     * package-removal should clear all items by package name.
4192     *
4193     * Implementation of the method from LauncherModel.Callbacks.
4194     */
4195    public void bindComponentsRemoved(final ArrayList<String> packageNames,
4196                                      final ArrayList<AppInfo> appInfos) {
4197        Runnable r = new Runnable() {
4198            public void run() {
4199                bindComponentsRemoved(packageNames, appInfos);
4200            }
4201        };
4202        if (waitUntilResume(r)) {
4203            return;
4204        }
4205
4206        if (!packageNames.isEmpty()) {
4207            mWorkspace.removeItemsByPackageName(packageNames);
4208        }
4209        if (!appInfos.isEmpty()) {
4210            mWorkspace.removeItemsByApplicationInfo(appInfos);
4211        }
4212
4213        // Notify the drag controller
4214        mDragController.onAppsRemoved(packageNames, appInfos);
4215
4216        // Update AllApps
4217        if (!LauncherAppState.isDisableAllApps() &&
4218                mAppsCustomizeContent != null) {
4219            mAppsCustomizeContent.removeApps(appInfos);
4220        }
4221    }
4222
4223    /**
4224     * A number of packages were updated.
4225     */
4226    private ArrayList<Object> mWidgetsAndShortcuts;
4227    private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
4228            public void run() {
4229                bindPackagesUpdated(mWidgetsAndShortcuts);
4230                mWidgetsAndShortcuts = null;
4231            }
4232        };
4233    public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4234        if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4235            mWidgetsAndShortcuts = widgetsAndShortcuts;
4236            return;
4237        }
4238
4239        // Update the widgets pane
4240        if (mAppsCustomizeContent != null) {
4241            mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
4242        }
4243    }
4244
4245    private int mapConfigurationOriActivityInfoOri(int configOri) {
4246        final Display d = getWindowManager().getDefaultDisplay();
4247        int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4248        switch (d.getRotation()) {
4249        case Surface.ROTATION_0:
4250        case Surface.ROTATION_180:
4251            // We are currently in the same basic orientation as the natural orientation
4252            naturalOri = configOri;
4253            break;
4254        case Surface.ROTATION_90:
4255        case Surface.ROTATION_270:
4256            // We are currently in the other basic orientation to the natural orientation
4257            naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4258                    Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4259            break;
4260        }
4261
4262        int[] oriMap = {
4263                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4264                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4265                ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4266                ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4267        };
4268        // Since the map starts at portrait, we need to offset if this device's natural orientation
4269        // is landscape.
4270        int indexOffset = 0;
4271        if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4272            indexOffset = 1;
4273        }
4274        return oriMap[(d.getRotation() + indexOffset) % 4];
4275    }
4276
4277    public boolean isRotationEnabled() {
4278        boolean enableRotation = sForceEnableRotation ||
4279                getResources().getBoolean(R.bool.allow_rotation);
4280        return enableRotation;
4281    }
4282    public void lockScreenOrientation() {
4283        if (isRotationEnabled()) {
4284            setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4285                    .getConfiguration().orientation));
4286        }
4287    }
4288    public void unlockScreenOrientation(boolean immediate) {
4289        if (isRotationEnabled()) {
4290            if (immediate) {
4291                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4292            } else {
4293                mHandler.postDelayed(new Runnable() {
4294                    public void run() {
4295                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4296                    }
4297                }, mRestoreScreenOrientationDelay);
4298            }
4299        }
4300    }
4301
4302    /**
4303     * Called when the SearchBar hint should be changed.
4304     *
4305     * @param hint the hint to be displayed in the search bar.
4306     */
4307    protected void onSearchBarHintChanged(String hint) {
4308        mLauncherClings.updateSearchBarHint(hint);
4309    }
4310
4311    protected boolean isLauncherPreinstalled() {
4312        PackageManager pm = getPackageManager();
4313        try {
4314            ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4315            if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4316                return true;
4317            } else {
4318                return false;
4319            }
4320        } catch (NameNotFoundException e) {
4321            e.printStackTrace();
4322            return false;
4323        }
4324    }
4325
4326    protected String getFirstRunClingSearchBarHint() {
4327        return "";
4328    }
4329    protected String getFirstRunCustomContentHint() {
4330        return "";
4331    }
4332    protected int getFirstRunFocusedHotseatAppDrawableId() {
4333        return -1;
4334    }
4335    protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4336        return null;
4337    }
4338    protected int getFirstRunFocusedHotseatAppRank() {
4339        return -1;
4340    }
4341    protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4342        return "";
4343    }
4344    protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4345        return "";
4346    }
4347
4348    public void dismissFirstRunCling(View v) {
4349        mLauncherClings.dismissFirstRunCling(v);
4350    }
4351    public void dismissMigrationClingCopyApps(View v) {
4352        mLauncherClings.dismissMigrationClingCopyApps(v);
4353    }
4354    public void dismissMigrationClingUseDefault(View v) {
4355        mLauncherClings.dismissMigrationClingUseDefault(v);
4356    }
4357    public void dismissMigrationWorkspaceCling(View v) {
4358        mLauncherClings.dismissMigrationWorkspaceCling(v);
4359    }
4360    public void dismissWorkspaceCling(View v) {
4361        mLauncherClings.dismissWorkspaceCling(v);
4362    }
4363    public void dismissFolderCling(View v) {
4364        mLauncherClings.dismissFolderCling(v);
4365    }
4366
4367    private boolean shouldRunFirstRunActivity() {
4368        return !ActivityManager.isRunningInTestHarness() &&
4369                !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4370    }
4371
4372    public void showFirstRunActivity() {
4373        if (shouldRunFirstRunActivity() &&
4374                hasFirstRunActivity()) {
4375            Intent firstRunIntent = getFirstRunActivity();
4376            if (firstRunIntent != null) {
4377                startActivity(firstRunIntent);
4378                markFirstRunActivityShown();
4379            }
4380        }
4381    }
4382
4383    private void markFirstRunActivityShown() {
4384        SharedPreferences.Editor editor = mSharedPrefs.edit();
4385        editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4386        editor.apply();
4387    }
4388
4389    void showWorkspaceSearchAndHotseat() {
4390        if (mWorkspace != null) mWorkspace.setAlpha(1f);
4391        if (mHotseat != null) mHotseat.setAlpha(1f);
4392        if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4393        if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
4394    }
4395
4396    void hideWorkspaceSearchAndHotseat() {
4397        if (mWorkspace != null) mWorkspace.setAlpha(0f);
4398        if (mHotseat != null) mHotseat.setAlpha(0f);
4399        if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4400        if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
4401    }
4402
4403
4404    public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4405        ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4406        if (ri == null) {
4407            return null;
4408        }
4409        return new AppInfo(getPackageManager(), ri, mIconCache, null);
4410    }
4411
4412    public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4413            Bitmap icon) {
4414        return new ShortcutInfo(shortcutIntent, caption, icon);
4415    }
4416
4417    public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4418        dragView.setTag(dragInfo);
4419        mWorkspace.onDragStartedWithItem(dragView);
4420        mWorkspace.beginDragShared(dragView, source);
4421    }
4422
4423    /**
4424     * Prints out out state for debugging.
4425     */
4426    public void dumpState() {
4427        Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
4428        Log.d(TAG, "mSavedState=" + mSavedState);
4429        Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4430        Log.d(TAG, "mRestoring=" + mRestoring);
4431        Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4432        Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
4433        Log.d(TAG, "sFolders.size=" + sFolders.size());
4434        mModel.dumpState();
4435
4436        if (mAppsCustomizeContent != null) {
4437            mAppsCustomizeContent.dumpState();
4438        }
4439        Log.d(TAG, "END launcher3 dump state");
4440    }
4441
4442    @Override
4443    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4444        super.dump(prefix, fd, writer, args);
4445        synchronized (sDumpLogs) {
4446            writer.println(" ");
4447            writer.println("Debug logs: ");
4448            for (int i = 0; i < sDumpLogs.size(); i++) {
4449                writer.println("  " + sDumpLogs.get(i));
4450            }
4451        }
4452    }
4453
4454    public static void dumpDebugLogsToConsole() {
4455        if (DEBUG_DUMP_LOG) {
4456            synchronized (sDumpLogs) {
4457                Log.d(TAG, "");
4458                Log.d(TAG, "*********************");
4459                Log.d(TAG, "Launcher debug logs: ");
4460                for (int i = 0; i < sDumpLogs.size(); i++) {
4461                    Log.d(TAG, "  " + sDumpLogs.get(i));
4462                }
4463                Log.d(TAG, "*********************");
4464                Log.d(TAG, "");
4465            }
4466        }
4467    }
4468
4469    public static void addDumpLog(String tag, String log, boolean debugLog) {
4470        addDumpLog(tag, log, null, debugLog);
4471    }
4472
4473    public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
4474        if (debugLog) {
4475            if (e != null) {
4476                Log.d(tag, log, e);
4477            } else {
4478                Log.d(tag, log);
4479            }
4480        }
4481        if (DEBUG_DUMP_LOG) {
4482            sDateStamp.setTime(System.currentTimeMillis());
4483            synchronized (sDumpLogs) {
4484                sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4485                    + (e == null ? "" : (", Exception: " + e)));
4486            }
4487        }
4488    }
4489
4490    public void dumpLogsToLocalData() {
4491        if (DEBUG_DUMP_LOG) {
4492            new AsyncTask<Void, Void, Void>() {
4493                public Void doInBackground(Void ... args) {
4494                    boolean success = false;
4495                    sDateStamp.setTime(sRunStart);
4496                    String FILENAME = sDateStamp.getMonth() + "-"
4497                            + sDateStamp.getDay() + "_"
4498                            + sDateStamp.getHours() + "-"
4499                            + sDateStamp.getMinutes() + "_"
4500                            + sDateStamp.getSeconds() + ".txt";
4501
4502                    FileOutputStream fos = null;
4503                    File outFile = null;
4504                    try {
4505                        outFile = new File(getFilesDir(), FILENAME);
4506                        outFile.createNewFile();
4507                        fos = new FileOutputStream(outFile);
4508                    } catch (Exception e) {
4509                        e.printStackTrace();
4510                    }
4511                    if (fos != null) {
4512                        PrintWriter writer = new PrintWriter(fos);
4513
4514                        writer.println(" ");
4515                        writer.println("Debug logs: ");
4516                        synchronized (sDumpLogs) {
4517                            for (int i = 0; i < sDumpLogs.size(); i++) {
4518                                writer.println("  " + sDumpLogs.get(i));
4519                            }
4520                        }
4521                        writer.close();
4522                    }
4523                    try {
4524                        if (fos != null) {
4525                            fos.close();
4526                            success = true;
4527                        }
4528                    } catch (IOException e) {
4529                        e.printStackTrace();
4530                    }
4531                    return null;
4532                }
4533            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
4534        }
4535    }
4536}
4537
4538interface LauncherTransitionable {
4539    View getContent();
4540    void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
4541    void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
4542    void onLauncherTransitionStep(Launcher l, float t);
4543    void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
4544}
4545
4546interface DebugIntents {
4547    static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4548    static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
4549}
4550