2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.launcher3;
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.AnimatorSet;
22 import android.animation.ObjectAnimator;
23 import android.animation.PropertyValuesHolder;
24 import android.animation.ValueAnimator;
25 import android.annotation.SuppressLint;
26 import android.annotation.TargetApi;
27 import android.app.Activity;
28 import android.app.ActivityManager;
29 import android.app.ActivityOptions;
30 import android.app.AlertDialog;
31 import android.app.SearchManager;
32 import android.appwidget.AppWidgetHostView;
33 import android.appwidget.AppWidgetManager;
34 import android.appwidget.AppWidgetProviderInfo;
35 import android.content.ActivityNotFoundException;
36 import android.content.BroadcastReceiver;
37 import android.content.ComponentCallbacks2;
38 import android.content.ComponentName;
39 import android.content.ContentResolver;
40 import android.content.Context;
41 import android.content.DialogInterface;
42 import android.content.Intent;
43 import android.content.IntentFilter;
44 import android.content.IntentSender;
45 import android.content.SharedPreferences;
46 import android.content.pm.ActivityInfo;
47 import android.content.pm.ApplicationInfo;
48 import android.content.pm.PackageManager;
49 import android.content.pm.PackageManager.NameNotFoundException;
50 import android.content.res.Configuration;
51 import android.database.ContentObserver;
52 import android.database.sqlite.SQLiteDatabase;
53 import android.graphics.Bitmap;
54 import android.graphics.Canvas;
55 import android.graphics.Color;
56 import android.graphics.PorterDuff;
57 import android.graphics.Rect;
58 import android.graphics.drawable.ColorDrawable;
59 import android.graphics.drawable.Drawable;
60 import android.net.Uri;
61 import android.os.AsyncTask;
62 import android.os.Build;
63 import android.os.Bundle;
64 import android.os.Environment;
65 import android.os.Handler;
66 import android.os.Message;
67 import android.os.StrictMode;
68 import android.os.SystemClock;
69 import android.text.Selection;
70 import android.text.SpannableStringBuilder;
71 import android.text.TextUtils;
72 import android.text.method.TextKeyListener;
73 import android.util.Log;
74 import android.view.Display;
75 import android.view.Gravity;
76 import android.view.HapticFeedbackConstants;
77 import android.view.KeyEvent;
78 import android.view.LayoutInflater;
79 import android.view.Menu;
80 import android.view.MotionEvent;
81 import android.view.Surface;
82 import android.view.View;
83 import android.view.View.OnClickListener;
84 import android.view.View.OnLongClickListener;
85 import android.view.ViewGroup;
86 import android.view.ViewStub;
87 import android.view.ViewTreeObserver;
88 import android.view.Window;
89 import android.view.WindowManager;
90 import android.view.accessibility.AccessibilityEvent;
91 import android.view.animation.OvershootInterpolator;
92 import android.view.inputmethod.InputMethodManager;
93 import android.widget.Advanceable;
94 import android.widget.FrameLayout;
95 import android.widget.ImageView;
96 import android.widget.TextView;
97 import android.widget.Toast;
99 import com.android.launcher3.DropTarget.DragObject;
100 import com.android.launcher3.PagedView.PageSwitchListener;
101 import com.android.launcher3.allapps.AllAppsContainerView;
102 import com.android.launcher3.allapps.AppSearchManager;
103 import com.android.launcher3.compat.AppWidgetManagerCompat;
104 import com.android.launcher3.compat.LauncherActivityInfoCompat;
105 import com.android.launcher3.compat.LauncherAppsCompat;
106 import com.android.launcher3.compat.UserHandleCompat;
107 import com.android.launcher3.compat.UserManagerCompat;
108 import com.android.launcher3.model.WidgetsModel;
109 import com.android.launcher3.util.LongArrayMap;
110 import com.android.launcher3.util.Thunk;
111 import com.android.launcher3.widget.PendingAddWidgetInfo;
112 import com.android.launcher3.widget.WidgetHostViewLoader;
113 import com.android.launcher3.widget.WidgetsContainerView;
116 import java.io.FileDescriptor;
117 import java.io.FileOutputStream;
118 import java.io.IOException;
119 import java.io.PrintWriter;
120 import java.lang.reflect.InvocationTargetException;
121 import java.lang.reflect.Method;
122 import java.text.DateFormat;
123 import java.util.ArrayList;
124 import java.util.Collection;
125 import java.util.Date;
126 import java.util.HashMap;
127 import java.util.HashSet;
128 import java.util.List;
129 import java.util.concurrent.atomic.AtomicInteger;
132 * Default launcher application.
134 public class Launcher extends Activity
135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
137 LauncherStateTransitionAnimation.Callbacks {
138 static final String TAG = "Launcher";
139 static final boolean LOGD = false;
142 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
144 static final boolean PROFILE_STARTUP = false;
145 static final boolean DEBUG_WIDGETS = true;
146 static final boolean DEBUG_STRICT_MODE = false;
147 static final boolean DEBUG_RESUME_TIME = false;
148 static final boolean DEBUG_DUMP_LOG = false;
150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152 private static final int REQUEST_CREATE_SHORTCUT = 1;
153 private static final int REQUEST_CREATE_APPWIDGET = 5;
154 private static final int REQUEST_PICK_APPWIDGET = 9;
155 private static final int REQUEST_PICK_WALLPAPER = 10;
157 private static final int REQUEST_BIND_APPWIDGET = 11;
158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
160 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
161 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
162 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
170 protected static final int REQUEST_LAST = 100;
172 static final int SCREEN_COUNT = 5;
174 // To turn on these properties, type
175 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
185 private static final String RUNTIME_STATE = "launcher.state";
187 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
189 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
191 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
193 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
213 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
215 private static final String QSB_WIDGET_ID = "qsb_widget_id";
216 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
218 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
220 /** The different states that Launcher can be in. */
221 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
223 @Thunk State mState = State.WORKSPACE;
224 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
226 private boolean mIsSafeModeEnabled;
228 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
229 LauncherOverlay mLauncherOverlay;
230 InsettableFrameLayout mLauncherOverlayContainer;
232 static final int APPWIDGET_HOST_ID = 1024;
233 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
234 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
235 private static final int ACTIVITY_START_DELAY = 1000;
237 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
238 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240 // How long to wait before the new-shortcut animation automatically pans the workspace
241 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
242 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
243 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
245 private final BroadcastReceiver mCloseSystemDialogsReceiver
246 = new CloseSystemDialogsIntentReceiver();
247 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249 private LayoutInflater mInflater;
251 @Thunk Workspace mWorkspace;
252 private View mLauncherView;
253 private View mPageIndicators;
254 @Thunk DragLayer mDragLayer;
255 private DragController mDragController;
256 private View mWeightWatcher;
258 private AppWidgetManagerCompat mAppWidgetManager;
259 private LauncherAppWidgetHost mAppWidgetHost;
261 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
262 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
263 private int mPendingAddWidgetId = -1;
265 private int[] mTmpAddItemCellCoordinates = new int[2];
267 @Thunk Hotseat mHotseat;
268 private ViewGroup mOverviewPanel;
270 private View mAllAppsButton;
272 private SearchDropTargetBar mSearchDropTargetBar;
274 // Main container view for the all apps screen.
275 @Thunk AllAppsContainerView mAppsView;
277 // Main container view and the model for the widget tray screen.
278 @Thunk WidgetsContainerView mWidgetsView;
279 @Thunk WidgetsModel mWidgetsModel;
281 private boolean mAutoAdvanceRunning = false;
282 private AppWidgetHostView mQsb;
284 private Bundle mSavedState;
285 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
286 // scroll issues (because the workspace may not have been measured yet) and extra work.
287 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
288 private State mOnResumeState = State.NONE;
290 private SpannableStringBuilder mDefaultKeySsb = null;
292 @Thunk boolean mWorkspaceLoading = true;
294 private boolean mPaused = true;
295 private boolean mRestoring;
296 private boolean mWaitingForResult;
297 private boolean mOnResumeNeedsLoad;
299 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
300 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
302 private Bundle mSavedInstanceState;
304 private LauncherModel mModel;
305 private IconCache mIconCache;
306 @Thunk boolean mUserPresent = true;
307 private boolean mVisible = false;
308 private boolean mHasFocus = false;
309 private boolean mAttached = false;
311 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
313 private View.OnTouchListener mHapticFeedbackTouchListener;
315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
328 @Thunk Drawable mWorkspaceBackgroundDrawable;
330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
346 @Thunk ImageView mFolderIconImageView;
347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
351 private DeviceProfile mDeviceProfile;
353 // This is set to the view that launched the activity that navigated the user away from
354 // launcher. Since there is no callback for when the activity has finished launching, enable
355 // the press state and keep this reference to reset the press state when we return to launcher.
356 private BubbleTextView mWaitingForResume;
358 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
359 new HashMap<String, CustomAppWidget>();
361 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
363 if (ENABLE_CUSTOM_WIDGET_TEST) {
364 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
368 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
369 private static Method sClipRevealMethod = null;
371 Class<?> activityOptionsClass = ActivityOptions.class;
373 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
374 View.class, int.class, int.class, int.class, int.class);
375 } catch (Exception e) {
380 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
382 if (mWorkspace != null) {
383 mWorkspace.buildPageHardwareLayers();
388 private static PendingAddArguments sPendingAddItem;
390 @Thunk static class PendingAddArguments {
400 private Stats mStats;
401 FocusIndicatorView mFocusHandler;
402 private boolean mRotationEnabled = false;
404 @Thunk void setOrientation() {
405 if (mRotationEnabled) {
406 unlockScreenOrientation(true);
408 setRequestedOrientation(
409 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
413 private Runnable mUpdateOrientationRunnable = new Runnable() {
420 protected void onCreate(Bundle savedInstanceState) {
421 if (DEBUG_STRICT_MODE) {
422 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
425 .detectNetwork() // or .detectAll() for all detectable problems
428 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
429 .detectLeakedSqlLiteObjects()
430 .detectLeakedClosableObjects()
436 if (mLauncherCallbacks != null) {
437 mLauncherCallbacks.preOnCreate();
440 super.onCreate(savedInstanceState);
442 LauncherAppState.setApplicationContext(getApplicationContext());
443 LauncherAppState app = LauncherAppState.getInstance();
444 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
446 // Load configuration-specific DeviceProfile
447 mDeviceProfile = getResources().getConfiguration().orientation
448 == Configuration.ORIENTATION_LANDSCAPE ?
449 app.getInvariantDeviceProfile().landscapeProfile
450 : app.getInvariantDeviceProfile().portraitProfile;
452 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
453 Context.MODE_PRIVATE);
454 mIsSafeModeEnabled = getPackageManager().isSafeMode();
455 mModel = app.setLauncher(this);
456 mIconCache = app.getIconCache();
458 mDragController = new DragController(this);
459 mInflater = getLayoutInflater();
460 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
462 mStats = new Stats(this);
464 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
466 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
467 mAppWidgetHost.startListening();
469 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
470 // this also ensures that any synchronous binding below doesn't re-trigger another
471 // LauncherModel load.
474 if (PROFILE_STARTUP) {
475 android.os.Debug.startMethodTracing(
476 Environment.getExternalStorageDirectory() + "/launcher");
479 setContentView(R.layout.launcher);
482 mDeviceProfile.layout(this);
484 registerContentObservers();
488 mSavedState = savedInstanceState;
489 restoreState(mSavedState);
491 if (PROFILE_STARTUP) {
492 android.os.Debug.stopMethodTracing();
496 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
497 // If the user leaves launcher, then we should just load items asynchronously when
499 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
501 // We only load the page synchronously if the user rotates (or triggers a
502 // configuration change) while launcher is in the foreground
503 mModel.startLoader(mWorkspace.getRestorePage());
507 // For handling default keys
508 mDefaultKeySsb = new SpannableStringBuilder();
509 Selection.setSelection(mDefaultKeySsb, 0);
511 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
512 registerReceiver(mCloseSystemDialogsReceiver, filter);
514 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
515 // In case we are on a device with locked rotation, we should look at preferences to check
516 // if the user has specifically allowed rotation.
517 if (!mRotationEnabled) {
518 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
521 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
522 // we want the screen to auto-rotate based on the current orientation
525 if (mLauncherCallbacks != null) {
526 mLauncherCallbacks.onCreate(savedInstanceState);
527 if (mLauncherCallbacks.hasLauncherOverlay()) {
528 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
529 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
530 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
531 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
532 mWorkspace.setLauncherOverlay(mLauncherOverlay);
536 if (shouldShowIntroScreen()) {
539 showFirstRunActivity();
540 showFirstRunClings();
545 public void onSettingsChanged(String settings, boolean value) {
546 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
547 mRotationEnabled = value;
548 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
549 mUpdateOrientationRunnable.run();
554 private LauncherCallbacks mLauncherCallbacks;
556 public void onPostCreate(Bundle savedInstanceState) {
557 super.onPostCreate(savedInstanceState);
558 if (mLauncherCallbacks != null) {
559 mLauncherCallbacks.onPostCreate(savedInstanceState);
563 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
564 mLauncherCallbacks = callbacks;
565 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
567 public void onAllAppsBoundsChanged(Rect bounds) {
569 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
571 mAppsView.setFixedBounds(bounds);
572 mWidgetsView.setFixedBounds(bounds);
576 public void dismissAllApps() {
577 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
578 // Dismiss All Apps if we aren't already paused/invisible
580 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
581 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
587 public void setSearchManager(AppSearchManager manager) {
588 mAppsView.setSearchManager(manager);
591 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
592 private boolean mImportanceStored = false;
593 private int mWorkspaceImportanceForAccessibility =
594 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
595 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
598 public void onSearchOverlayOpened() {
599 if (mImportanceStored) {
602 // The underlying workspace and hotseat are temporarily suppressed by the search
603 // overlay. So they sholudn't be accessible.
604 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
605 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
606 mWorkspace.setImportantForAccessibility(
607 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
608 mHotseat.setImportantForAccessibility(
609 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
610 mImportanceStored = true;
614 public void onSearchOverlayClosed() {
615 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
616 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
617 mImportanceStored = false;
624 public void onLauncherProviderChange() {
625 if (mLauncherCallbacks != null) {
626 mLauncherCallbacks.onLauncherProviderChange();
630 /** To be overridden by subclasses to hint to Launcher that we have custom content */
631 protected boolean hasCustomContentToLeft() {
632 if (mLauncherCallbacks != null) {
633 return mLauncherCallbacks.hasCustomContentToLeft();
639 * To be overridden by subclasses to populate the custom content container and call
640 * {@link #addToCustomContentPage}. This will only be invoked if
641 * {@link #hasCustomContentToLeft()} is {@code true}.
643 protected void populateCustomContentContainer() {
644 if (mLauncherCallbacks != null) {
645 mLauncherCallbacks.populateCustomContentContainer();
650 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
651 * ensure the custom content page is added or removed if necessary.
653 protected void invalidateHasCustomContentToLeft() {
654 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
655 // Not bound yet, wait for bindScreens to be called.
659 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
660 // Create the custom content page and call the subclass to populate it.
661 mWorkspace.createCustomContentContainer();
662 populateCustomContentContainer();
663 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
664 mWorkspace.removeCustomContentPage();
668 public Stats getStats() {
672 public LayoutInflater getInflater() {
676 public boolean isDraggingEnabled() {
677 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
678 // that is subsequently removed from the workspace in startBinding().
679 return !mModel.isLoadingWorkspace();
682 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
683 public static int generateViewId() {
684 if (Build.VERSION.SDK_INT >= 17) {
685 return View.generateViewId();
687 // View.generateViewId() is not available. The following fallback logic is a copy
688 // of its implementation.
690 final int result = sNextGeneratedId.get();
691 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
692 int newValue = result + 1;
693 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
694 if (sNextGeneratedId.compareAndSet(result, newValue)) {
701 public int getViewIdForItem(ItemInfo info) {
702 // This cast is safe given the > 2B range for int.
703 int itemId = (int) info.id;
704 if (mItemIdToViewId.containsKey(itemId)) {
705 return mItemIdToViewId.get(itemId);
707 int viewId = generateViewId();
708 mItemIdToViewId.put(itemId, viewId);
713 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
714 * a configuration step, this allows the proper animations to run after other transitions.
716 private long completeAdd(PendingAddArguments args) {
717 long screenId = args.screenId;
718 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
719 // When the screen id represents an actual screen (as opposed to a rank) we make sure
720 // that the drop page actually exists.
721 screenId = ensurePendingDropLayoutExists(args.screenId);
724 switch (args.requestCode) {
725 case REQUEST_CREATE_SHORTCUT:
726 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
729 case REQUEST_CREATE_APPWIDGET:
730 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
732 case REQUEST_RECONFIGURE_APPWIDGET:
733 completeRestoreAppWidget(args.appWidgetId);
736 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
737 // if you turned the screen off and then back while in All Apps, Launcher would not
738 // return to the workspace. Clearing mAddInfo.container here fixes this issue
743 private void handleActivityResult(
744 final int requestCode, final int resultCode, final Intent data) {
745 // Reset the startActivity waiting flag
746 setWaitingForResult(false);
747 final int pendingAddWidgetId = mPendingAddWidgetId;
748 mPendingAddWidgetId = -1;
750 Runnable exitSpringLoaded = new Runnable() {
753 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
754 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
758 if (requestCode == REQUEST_BIND_APPWIDGET) {
759 final int appWidgetId = data != null ?
760 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
761 if (resultCode == RESULT_CANCELED) {
762 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
763 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
764 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
765 } else if (resultCode == RESULT_OK) {
766 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
767 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
770 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
771 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
772 showWorkspace(false);
777 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
778 requestCode == REQUEST_CREATE_APPWIDGET);
780 final boolean workspaceLocked = isWorkspaceLocked();
781 // We have special handling for widgets
783 final int appWidgetId;
784 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
787 appWidgetId = pendingAddWidgetId;
789 appWidgetId = widgetId;
793 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
794 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
795 "returned from the widget configuration activity.");
796 result = RESULT_CANCELED;
797 completeTwoStageWidgetDrop(result, appWidgetId);
798 final Runnable onComplete = new Runnable() {
801 exitSpringLoadedDragModeDelayed(false, 0, null);
804 if (workspaceLocked) {
805 // No need to remove the empty screen if we're mid-binding, as the
806 // the bind will not add the empty screen.
807 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
809 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 if (!workspaceLocked) {
814 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
815 // When the screen id represents an actual screen (as opposed to a rank)
816 // we make sure that the drop page actually exists.
817 mPendingAddInfo.screenId =
818 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
820 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
822 dropLayout.setDropPending(true);
823 final Runnable onComplete = new Runnable() {
826 completeTwoStageWidgetDrop(resultCode, appWidgetId);
827 dropLayout.setDropPending(false);
830 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
835 sPendingAddItem = args;
841 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
842 if (resultCode == RESULT_OK) {
843 // Update the widget view.
844 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
845 pendingAddWidgetId, mPendingAddInfo);
846 if (workspaceLocked) {
847 sPendingAddItem = args;
852 // Leave the widget in the pending state if the user canceled the configure.
856 // The pattern used here is that a user PICKs a specific application,
857 // which, depending on the target, might need to CREATE the actual target.
859 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
860 // launch over to the Music app to actually CREATE_SHORTCUT.
861 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
862 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
864 if (isWorkspaceLocked()) {
865 sPendingAddItem = args;
868 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
871 } else if (resultCode == RESULT_CANCELED) {
872 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
873 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
875 mDragLayer.clearAnimatedView();
880 protected void onActivityResult(
881 final int requestCode, final int resultCode, final Intent data) {
882 handleActivityResult(requestCode, resultCode, data);
883 if (mLauncherCallbacks != null) {
884 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
888 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
889 appWidgetId, ItemInfo info) {
890 PendingAddArguments args = new PendingAddArguments();
891 args.requestCode = requestCode;
893 args.container = info.container;
894 args.screenId = info.screenId;
895 args.cellX = info.cellX;
896 args.cellY = info.cellY;
897 args.appWidgetId = appWidgetId;
902 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
904 * @param screenId the screen id to check
905 * @return the new screen, or screenId if it exists
907 private long ensurePendingDropLayoutExists(long screenId) {
908 CellLayout dropLayout =
909 (CellLayout) mWorkspace.getScreenWithId(screenId);
910 if (dropLayout == null) {
911 // it's possible that the add screen was removed because it was
912 // empty and a re-bind occurred
913 mWorkspace.addExtraEmptyScreen();
914 return mWorkspace.commitExtraEmptyScreen();
920 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
921 CellLayout cellLayout =
922 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
926 AppWidgetHostView boundWidget = null;
927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
930 mPendingAddWidgetInfo);
931 boundWidget = layout;
932 onCompleteRunnable = new Runnable() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
936 mPendingAddInfo.screenId, layout, null);
937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
941 } else if (resultCode == RESULT_CANCELED) {
942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
949 } else if (onCompleteRunnable != null) {
950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
956 protected void onStop() {
958 FirstFrameAnimatorHelper.setIsVisible(false);
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStop();
966 protected void onStart() {
968 FirstFrameAnimatorHelper.setIsVisible(true);
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.onStart();
976 protected void onResume() {
978 if (DEBUG_RESUME_TIME) {
979 startTime = System.currentTimeMillis();
980 Log.v(TAG, "Launcher.onResume()");
983 if (mLauncherCallbacks != null) {
984 mLauncherCallbacks.preOnResume();
989 // Restore the previous launcher state
990 if (mOnResumeState == State.WORKSPACE) {
991 showWorkspace(false);
992 } else if (mOnResumeState == State.APPS) {
993 boolean launchedFromApp = (mWaitingForResume != null);
994 // Don't update the predicted apps if the user is returning to launcher in the apps
995 // view after launching an app, as they may be depending on the UI to be static to
996 // switch to another app, otherwise, if it was
997 showAppsView(false /* animated */, false /* resetListToTop */,
998 !launchedFromApp /* updatePredictedApps */);
999 } else if (mOnResumeState == State.WIDGETS) {
1000 showWidgetsView(false, false);
1002 mOnResumeState = State.NONE;
1004 // Restore the apps state if we are in all apps
1005 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1006 // Otherwise, notify the callbacks if we are in all apps mode
1007 if (mState == State.APPS) {
1008 if (mLauncherCallbacks != null) {
1009 mLauncherCallbacks.onAllAppsShown();
1014 // Background was set to gradient in onPause(), restore to transparent if in all apps.
1015 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1016 : WORKSPACE_BACKGROUND_TRANSPARENT);
1019 if (mRestoring || mOnResumeNeedsLoad) {
1020 setWorkspaceLoading(true);
1021 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
1023 mOnResumeNeedsLoad = false;
1025 if (mBindOnResumeCallbacks.size() > 0) {
1026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
1036 mBindOnResumeCallbacks.clear();
1037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1046 mOnResumeCallbacks.clear();
1049 // Reset the pressed state of icons that were locked in the press state while activities
1051 if (mWaitingForResume != null) {
1052 // Resets the previous workspace icon press state
1053 mWaitingForResume.setStayPressed(false);
1056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1060 getWorkspace().reinflateWidgetsIfNecessary();
1061 reinflateQSBIfNecessary();
1063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1067 if (mWorkspace.getCustomContentCallbacks() != null) {
1068 // If we are resuming and the custom content is the current page, we call onShow().
1069 // It is also poassible that onShow will instead be called slightly after first layout
1070 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1071 if (mWorkspace.isOnOrMovingToCustomContent()) {
1072 mWorkspace.getCustomContentCallbacks().onShow(true);
1075 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
1076 mWorkspace.onResume();
1078 if (!isWorkspaceLoading()) {
1079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onResume();
1089 protected void onPause() {
1090 // Ensure that items added to Launcher are queued until Launcher returns
1091 InstallShortcutReceiver.enableInstallQueue();
1095 mDragController.cancelDrag();
1096 mDragController.resetLastGestureUpTime();
1098 // We call onHide() aggressively. The custom content callbacks should be able to
1099 // debounce excess onHide calls.
1100 if (mWorkspace.getCustomContentCallbacks() != null) {
1101 mWorkspace.getCustomContentCallbacks().onHide();
1104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onPause();
1109 public interface CustomContentCallbacks {
1110 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1111 // by a onResume or by scrolling otherwise.
1112 public void onShow(boolean fromResume);
1114 // Custom content is completely hidden
1115 public void onHide();
1117 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1118 public void onScrollProgressChanged(float progress);
1120 // Indicates whether the user is allowed to scroll away from the custom content.
1121 boolean isScrollingAllowed();
1124 public interface LauncherOverlay {
1127 * Touch interaction leading to overscroll has begun
1129 public void onScrollInteractionBegin();
1132 * Touch interaction related to overscroll has ended
1134 public void onScrollInteractionEnd();
1137 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1138 * screen (or in the case of RTL, the rightmost screen).
1140 public void onScrollChange(int progress, boolean rtl);
1143 * Screen has stopped scrolling
1145 public void onScrollSettled();
1148 * This method can be called by the Launcher in order to force the LauncherOverlay
1149 * to exit fully immersive mode.
1151 public void forceExitFullImmersion();
1154 public interface LauncherAppsCallbacks {
1156 * Updates launcher to the available space that AllApps can take so as not to overlap with
1159 public void onAllAppsBoundsChanged(Rect bounds);
1162 * Called to dismiss all apps if it is showing.
1164 public void dismissAllApps();
1167 * Sets the search manager to be used for app search.
1169 public void setSearchManager(AppSearchManager manager);
1172 public interface LauncherSearchCallbacks {
1174 * Called when the search overlay is shown.
1176 public void onSearchOverlayOpened();
1179 * Called when the search overlay is dismissed.
1181 public void onSearchOverlayClosed();
1184 public interface LauncherOverlayCallbacks {
1186 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1187 * however it doesn't modify any state within the launcher.
1189 public boolean canEnterFullImmersion();
1192 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1193 * eg. by occupying the full screen and handling all touch events.
1195 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1196 * case, Launcher will modify any necessary state and assumes the overlay is
1197 * handling all interaction. If false, the LauncherOverlay should cancel any
1200 public boolean enterFullImmersion();
1203 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1204 * full control over UI and state.
1206 public void exitFullImmersion();
1209 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1212 public boolean canEnterFullImmersion() {
1213 return mState == State.WORKSPACE;
1217 public boolean enterFullImmersion() {
1218 if (mState == State.WORKSPACE) {
1219 // When fully immersed, disregard any touches which fall through.
1220 mDragLayer.setBlockTouch(true);
1227 public void exitFullImmersion() {
1228 mDragLayer.setBlockTouch(false);
1232 protected boolean hasSettings() {
1233 if (mLauncherCallbacks != null) {
1234 return mLauncherCallbacks.hasSettings();
1236 // On devices with a locked orientation, we will at least have the allow rotation
1238 return !Utilities.isRotationAllowedForDevice(this);
1242 public void addToCustomContentPage(View customContent,
1243 CustomContentCallbacks callbacks, String description) {
1244 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
1247 // The custom content needs to offset its content to account for the QSB
1248 public int getTopOffsetForCustomContent() {
1249 return mWorkspace.getPaddingTop();
1253 public Object onRetainNonConfigurationInstance() {
1254 // Flag the loader to stop early before switching
1255 if (mModel.isCurrentCallbacks(this)) {
1256 mModel.stopLoader();
1258 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1260 return Boolean.TRUE;
1263 // We can't hide the IME if it was forced open. So don't bother
1265 public void onWindowFocusChanged(boolean hasFocus) {
1266 super.onWindowFocusChanged(hasFocus);
1267 mHasFocus = hasFocus;
1269 if (mLauncherCallbacks != null) {
1270 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1274 private boolean acceptFilter() {
1275 final InputMethodManager inputManager = (InputMethodManager)
1276 getSystemService(Context.INPUT_METHOD_SERVICE);
1277 return !inputManager.isFullscreenMode();
1281 public boolean onKeyDown(int keyCode, KeyEvent event) {
1282 final int uniChar = event.getUnicodeChar();
1283 final boolean handled = super.onKeyDown(keyCode, event);
1284 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1285 if (!handled && acceptFilter() && isKeyNotWhitespace) {
1286 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1288 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
1289 // something usable has been typed - start a search
1290 // the typed text will be retrieved and cleared by
1291 // showSearchDialog()
1292 // If there are multiple keystrokes before the search dialog takes focus,
1293 // onSearchRequested() will be called for every keystroke,
1294 // but it is idempotent, so it's fine.
1295 return onSearchRequested();
1299 // Eat the long press event so the keyboard doesn't come up.
1300 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1307 private String getTypedText() {
1308 return mDefaultKeySsb.toString();
1311 private void clearTypedText() {
1312 mDefaultKeySsb.clear();
1313 mDefaultKeySsb.clearSpans();
1314 Selection.setSelection(mDefaultKeySsb, 0);
1318 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1321 private static State intToState(int stateOrdinal) {
1322 State state = State.WORKSPACE;
1323 final State[] stateValues = State.values();
1324 for (int i = 0; i < stateValues.length; i++) {
1325 if (stateValues[i].ordinal() == stateOrdinal) {
1326 state = stateValues[i];
1334 * Restores the previous state, if it exists.
1336 * @param savedState The previous state.
1338 @SuppressWarnings("unchecked")
1339 private void restoreState(Bundle savedState) {
1340 if (savedState == null) {
1344 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
1345 if (state == State.APPS || state == State.WIDGETS) {
1346 mOnResumeState = state;
1349 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1350 PagedView.INVALID_RESTORE_PAGE);
1351 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1352 mWorkspace.setRestorePage(currentScreen);
1355 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
1356 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
1358 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1359 mPendingAddInfo.container = pendingAddContainer;
1360 mPendingAddInfo.screenId = pendingAddScreen;
1361 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1362 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
1363 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1364 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1365 AppWidgetProviderInfo info = savedState.getParcelable(
1366 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1367 mPendingAddWidgetInfo = info == null ?
1368 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1370 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
1371 setWaitingForResult(true);
1375 mItemIdToViewId = (HashMap<Integer, Integer>)
1376 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
1380 * Finds all the views we need and configure them properly.
1382 private void setupViews() {
1383 final DragController dragController = mDragController;
1385 mLauncherView = findViewById(R.id.launcher);
1386 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
1387 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1388 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
1389 mWorkspace.setPageSwitchListener(this);
1390 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
1392 mLauncherView.setSystemUiVisibility(
1393 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1394 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
1396 // Setup the drag layer
1397 mDragLayer.setup(this, dragController);
1399 // Setup the hotseat
1400 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1401 if (mHotseat != null) {
1402 mHotseat.setOnLongClickListener(this);
1405 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1406 View widgetButton = findViewById(R.id.widget_button);
1407 widgetButton.setOnClickListener(new OnClickListener() {
1409 public void onClick(View arg0) {
1410 if (!mWorkspace.isSwitchingState()) {
1411 onClickAddWidgetButton(arg0);
1415 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1417 View wallpaperButton = findViewById(R.id.wallpaper_button);
1418 wallpaperButton.setOnClickListener(new OnClickListener() {
1420 public void onClick(View arg0) {
1421 if (!mWorkspace.isSwitchingState()) {
1422 onClickWallpaperPicker(arg0);
1426 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428 View settingsButton = findViewById(R.id.settings_button);
1429 if (hasSettings()) {
1430 settingsButton.setOnClickListener(new OnClickListener() {
1432 public void onClick(View arg0) {
1433 if (!mWorkspace.isSwitchingState()) {
1434 onClickSettingsButton(arg0);
1438 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440 settingsButton.setVisibility(View.GONE);
1443 mOverviewPanel.setAlpha(0f);
1445 // Setup the workspace
1446 mWorkspace.setHapticFeedbackEnabled(false);
1447 mWorkspace.setOnLongClickListener(this);
1448 mWorkspace.setup(dragController);
1449 dragController.addDragListener(mWorkspace);
1451 // Get the search/delete bar
1452 mSearchDropTargetBar = (SearchDropTargetBar)
1453 mDragLayer.findViewById(R.id.search_drop_target_bar);
1456 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
1457 if (isAllAppsSearchOverridden()) {
1458 mAppsView.hideHeaderBar();
1461 // Setup AppsCustomize
1462 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
1464 // Setup the drag controller (drop targets have to be added in reverse order in priority)
1465 dragController.setDragScoller(mWorkspace);
1466 dragController.setScrollView(mDragLayer);
1467 dragController.setMoveTarget(mWorkspace);
1468 dragController.addDropTarget(mWorkspace);
1469 if (mSearchDropTargetBar != null) {
1470 mSearchDropTargetBar.setup(this, dragController);
1471 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
1474 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
1475 Log.v(TAG, "adding WeightWatcher");
1476 mWeightWatcher = new WeightWatcher(this);
1477 mWeightWatcher.setAlpha(0.5f);
1478 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
1479 new FrameLayout.LayoutParams(
1480 FrameLayout.LayoutParams.MATCH_PARENT,
1481 FrameLayout.LayoutParams.WRAP_CONTENT,
1485 boolean show = shouldShowWeightWatcher();
1486 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
1491 * Sets the all apps button. This method is called from {@link Hotseat}.
1493 public void setAllAppsButton(View allAppsButton) {
1494 mAllAppsButton = allAppsButton;
1497 public View getAllAppsButton() {
1498 return mAllAppsButton;
1502 * Creates a view representing a shortcut.
1504 * @param info The data structure describing the shortcut.
1506 View createShortcut(ShortcutInfo info) {
1507 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
1511 * Creates a view representing a shortcut inflated from the specified resource.
1513 * @param parent The group the shortcut belongs to.
1514 * @param info The data structure describing the shortcut.
1516 * @return A View inflated from layoutResId.
1518 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1519 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
1521 favorite.applyFromShortcutInfo(info, mIconCache);
1522 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
1523 favorite.setOnClickListener(this);
1524 favorite.setOnFocusChangeListener(mFocusHandler);
1529 * Add a shortcut to the workspace.
1531 * @param data The intent describing the shortcut.
1533 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
1535 int[] cellXY = mTmpAddItemCellCoordinates;
1536 int[] touchXY = mPendingAddInfo.dropPos;
1537 CellLayout layout = getCellLayout(container, screenId);
1539 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
1543 final View view = createShortcut(info);
1545 boolean foundCellSpan = false;
1546 // First we check if we already know the exact location where we want to add this item.
1547 if (cellX >= 0 && cellY >= 0) {
1550 foundCellSpan = true;
1552 // If appropriate, either create a folder or add to an existing folder
1553 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1557 DragObject dragObject = new DragObject();
1558 dragObject.dragInfo = info;
1559 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1563 } else if (touchXY != null) {
1564 // when dragging and dropping, just find the closest free spot
1565 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
1566 foundCellSpan = (result != null);
1568 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1571 if (!foundCellSpan) {
1572 showOutOfSpaceMessage(isHotseatLayout(layout));
1576 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
1579 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1580 isWorkspaceLocked());
1584 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1585 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
1586 // We want to account for the extra amount of padding that we are adding to the widget
1587 // to ensure that it gets the full amount of space that it has requested
1588 int requiredWidth = minWidth + padding.left + padding.right;
1589 int requiredHeight = minHeight + padding.top + padding.bottom;
1590 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
1593 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1594 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
1597 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1598 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
1602 * Add a widget to the workspace.
1604 * @param appWidgetId The app widget id
1606 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
1607 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1609 ItemInfo info = mPendingAddInfo;
1610 if (appWidgetInfo == null) {
1611 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1612 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
1615 if (appWidgetInfo.isCustomWidget) {
1616 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
1619 LauncherAppWidgetInfo launcherInfo;
1620 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1621 launcherInfo.spanX = info.spanX;
1622 launcherInfo.spanY = info.spanY;
1623 launcherInfo.minSpanX = info.minSpanX;
1624 launcherInfo.minSpanY = info.minSpanY;
1625 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
1627 LauncherModel.addItemToDatabase(this, launcherInfo,
1628 container, screenId, info.cellX, info.cellY);
1631 if (hostView == null) {
1632 // Perform actual inflation because we're live
1633 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1636 // The AppWidgetHostView has already been inflated and instantiated
1637 launcherInfo.hostView = hostView;
1639 launcherInfo.hostView.setTag(launcherInfo);
1640 launcherInfo.hostView.setVisibility(View.VISIBLE);
1641 launcherInfo.notifyWidgetSizeChanged(this);
1643 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1644 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
1646 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
1651 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1653 public void onReceive(Context context, Intent intent) {
1654 final String action = intent.getAction();
1655 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1656 mUserPresent = false;
1657 mDragLayer.clearAllResizeFrames();
1658 updateAutoAdvanceState();
1660 // Reset AllApps to its initial state only if we are not in the middle of
1661 // processing a multi-step drop
1662 if (mAppsView != null && mWidgetsView != null &&
1663 mPendingAddInfo.container == ItemInfo.NO_ID) {
1664 showWorkspace(false);
1666 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1667 mUserPresent = true;
1668 updateAutoAdvanceState();
1669 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1670 mModel.resetLoadedState(false, true);
1671 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
1672 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1673 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1674 mModel.resetLoadedState(false, true);
1675 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
1676 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1677 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
1683 public void onAttachedToWindow() {
1684 super.onAttachedToWindow();
1686 // Listen for broadcasts related to user-presence
1687 final IntentFilter filter = new IntentFilter();
1688 filter.addAction(Intent.ACTION_SCREEN_OFF);
1689 filter.addAction(Intent.ACTION_USER_PRESENT);
1690 // For handling managed profiles
1691 if (ENABLE_DEBUG_INTENTS) {
1692 filter.addAction(DebugIntents.DELETE_DATABASE);
1693 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1695 registerReceiver(mReceiver, filter);
1696 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
1697 setupTransparentSystemBarsForLmp();
1703 * Sets up transparent navigation and status bars in LMP.
1704 * This method is a no-op for other platform versions.
1706 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
1707 private void setupTransparentSystemBarsForLmp() {
1708 if (Utilities.isLmpOrAbove()) {
1709 Window window = getWindow();
1710 window.getAttributes().systemUiVisibility |=
1711 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1712 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1713 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1714 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1715 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1716 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1717 window.setStatusBarColor(Color.TRANSPARENT);
1718 window.setNavigationBarColor(Color.TRANSPARENT);
1723 public void onDetachedFromWindow() {
1724 super.onDetachedFromWindow();
1728 unregisterReceiver(mReceiver);
1731 updateAutoAdvanceState();
1734 public void onWindowVisibilityChanged(int visibility) {
1735 mVisible = visibility == View.VISIBLE;
1736 updateAutoAdvanceState();
1737 // The following code used to be in onResume, but it turns out onResume is called when
1738 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1739 // is a more appropriate event to handle
1741 if (!mWorkspaceLoading) {
1742 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
1743 // We want to let Launcher draw itself at least once before we force it to build
1744 // layers on all the workspace pages, so that transitioning to Launcher from other
1745 // apps is nice and speedy.
1746 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
1747 private boolean mStarted = false;
1748 public void onDraw() {
1749 if (mStarted) return;
1751 // We delay the layer building a bit in order to give
1752 // other message processing a time to run. In particular
1753 // this avoids a delay in hiding the IME if it was
1754 // currently shown, because doing that may involve
1755 // some communication back with the app.
1756 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
1757 final ViewTreeObserver.OnDrawListener listener = this;
1758 mWorkspace.post(new Runnable() {
1760 if (mWorkspace != null &&
1761 mWorkspace.getViewTreeObserver() != null) {
1762 mWorkspace.getViewTreeObserver().
1763 removeOnDrawListener(listener);
1775 @Thunk void sendAdvanceMessage(long delay) {
1776 mHandler.removeMessages(ADVANCE_MSG);
1777 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1778 mHandler.sendMessageDelayed(msg, delay);
1779 mAutoAdvanceSentTime = System.currentTimeMillis();
1782 @Thunk void updateAutoAdvanceState() {
1783 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1784 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1785 mAutoAdvanceRunning = autoAdvanceRunning;
1786 if (autoAdvanceRunning) {
1787 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1788 sendAdvanceMessage(delay);
1790 if (!mWidgetsToAdvance.isEmpty()) {
1791 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1792 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1794 mHandler.removeMessages(ADVANCE_MSG);
1795 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
1800 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1803 public boolean handleMessage(Message msg) {
1804 if (msg.what == ADVANCE_MSG) {
1806 for (View key: mWidgetsToAdvance.keySet()) {
1807 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1808 final int delay = mAdvanceStagger * i;
1809 if (v instanceof Advanceable) {
1810 mHandler.postDelayed(new Runnable() {
1812 ((Advanceable) v).advance();
1818 sendAdvanceMessage(mAdvanceInterval);
1824 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1825 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
1826 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1827 if (v instanceof Advanceable) {
1828 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1829 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
1830 updateAutoAdvanceState();
1834 void removeWidgetToAutoAdvance(View hostView) {
1835 if (mWidgetsToAdvance.containsKey(hostView)) {
1836 mWidgetsToAdvance.remove(hostView);
1837 updateAutoAdvanceState();
1841 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1842 removeWidgetToAutoAdvance(launcherInfo.hostView);
1843 launcherInfo.hostView = null;
1846 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
1847 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1848 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
1851 public DragLayer getDragLayer() {
1855 public AllAppsContainerView getAppsView() {
1859 public WidgetsContainerView getWidgetsView() {
1860 return mWidgetsView;
1863 public Workspace getWorkspace() {
1867 public Hotseat getHotseat() {
1871 public ViewGroup getOverviewPanel() {
1872 return mOverviewPanel;
1875 public SearchDropTargetBar getSearchBar() {
1876 return mSearchDropTargetBar;
1879 public LauncherAppWidgetHost getAppWidgetHost() {
1880 return mAppWidgetHost;
1883 public LauncherModel getModel() {
1887 protected SharedPreferences getSharedPrefs() {
1888 return mSharedPrefs;
1891 public DeviceProfile getDeviceProfile() {
1892 return mDeviceProfile;
1895 public void closeSystemDialogs() {
1896 getWindow().closeAllPanels();
1898 // Whatever we were doing is hereby canceled.
1899 setWaitingForResult(false);
1903 protected void onNewIntent(Intent intent) {
1905 if (DEBUG_RESUME_TIME) {
1906 startTime = System.currentTimeMillis();
1908 super.onNewIntent(intent);
1911 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
1912 // also will cancel mWaitingForResult.
1913 closeSystemDialogs();
1915 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1916 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1917 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1919 if (mWorkspace == null) {
1920 // Can be cases where mWorkspace is null, this prevents a NPE
1923 Folder openFolder = mWorkspace.getOpenFolder();
1924 // In all these cases, only animate if we're already on home
1925 mWorkspace.exitWidgetResizeMode();
1927 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1928 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
1929 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1930 openFolder == null && moveToDefaultScreen) {
1931 mWorkspace.moveToDefaultScreen(true);
1935 exitSpringLoadedDragMode();
1937 // If we are already on home, then just animate back to the workspace,
1938 // otherwise, just wait until onResume to set the state back to Workspace
1939 if (alreadyOnHome) {
1940 showWorkspace(true);
1942 mOnResumeState = State.WORKSPACE;
1945 final View v = getWindow().peekDecorView();
1946 if (v != null && v.getWindowToken() != null) {
1947 InputMethodManager imm = (InputMethodManager)getSystemService(
1948 INPUT_METHOD_SERVICE);
1949 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1952 // Reset the apps view
1953 if (!alreadyOnHome && mAppsView != null) {
1954 mAppsView.scrollToTop();
1957 // Reset the widgets view
1958 if (!alreadyOnHome && mWidgetsView != null) {
1959 mWidgetsView.scrollToTop();
1962 if (mLauncherCallbacks != null) {
1963 mLauncherCallbacks.onHomeIntent();
1967 if (DEBUG_RESUME_TIME) {
1968 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1971 if (mLauncherCallbacks != null) {
1972 mLauncherCallbacks.onNewIntent(intent);
1977 public void onRestoreInstanceState(Bundle state) {
1978 super.onRestoreInstanceState(state);
1979 for (int page: mSynchronouslyBoundPages) {
1980 mWorkspace.restoreInstanceStateForChild(page);
1985 protected void onSaveInstanceState(Bundle outState) {
1986 if (mWorkspace.getChildCount() > 0) {
1987 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1988 mWorkspace.getCurrentPageOffsetFromCustomContent());
1990 super.onSaveInstanceState(outState);
1992 outState.putInt(RUNTIME_STATE, mState.ordinal());
1993 // We close any open folder since it will not be re-opened, and we need to make sure
1994 // this state is reflected.
1997 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
1998 mWaitingForResult) {
1999 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
2000 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
2001 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2005 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
2006 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
2009 // Save the current widgets tray?
2011 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
2013 if (mLauncherCallbacks != null) {
2014 mLauncherCallbacks.onSaveInstanceState(outState);
2019 public void onDestroy() {
2022 // Remove all pending runnables
2023 mHandler.removeMessages(ADVANCE_MSG);
2024 mHandler.removeMessages(0);
2025 mWorkspace.removeCallbacks(mBuildLayersRunnable);
2027 // Stop callbacks from LauncherModel
2028 LauncherAppState app = (LauncherAppState.getInstance());
2030 // It's possible to receive onDestroy after a new Launcher activity has
2031 // been created. In this case, don't interfere with the new Launcher.
2032 if (mModel.isCurrentCallbacks(this)) {
2033 mModel.stopLoader();
2034 app.setLauncher(null);
2038 mAppWidgetHost.stopListening();
2039 } catch (NullPointerException ex) {
2040 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
2042 mAppWidgetHost = null;
2044 mWidgetsToAdvance.clear();
2046 TextKeyListener.getInstance().release();
2048 getContentResolver().unregisterContentObserver(mWidgetObserver);
2049 unregisterReceiver(mCloseSystemDialogsReceiver);
2051 mDragLayer.clearAllResizeFrames();
2052 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
2053 mWorkspace.removeAllWorkspaceScreens();
2055 mDragController = null;
2057 LauncherAnimUtils.onDestroyActivity();
2059 if (mLauncherCallbacks != null) {
2060 mLauncherCallbacks.onDestroy();
2064 public DragController getDragController() {
2065 return mDragController;
2069 public void startActivityForResult(Intent intent, int requestCode) {
2070 onStartForResult(requestCode);
2071 super.startActivityForResult(intent, requestCode);
2075 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2076 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2077 onStartForResult(requestCode);
2079 super.startIntentSenderForResult(intent, requestCode,
2080 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2081 } catch (IntentSender.SendIntentException e) {
2082 throw new ActivityNotFoundException();
2086 private void onStartForResult(int requestCode) {
2087 if (requestCode >= 0) {
2088 setWaitingForResult(true);
2093 * Indicates that we want global search for this activity by setting the globalSearch
2094 * argument for {@link #startSearch} to true.
2097 public void startSearch(String initialQuery, boolean selectInitialQuery,
2098 Bundle appSearchData, boolean globalSearch) {
2100 if (initialQuery == null) {
2101 // Use any text typed in the launcher as the initial query
2102 initialQuery = getTypedText();
2104 if (appSearchData == null) {
2105 appSearchData = new Bundle();
2106 appSearchData.putString("source", "launcher-search");
2108 Rect sourceBounds = new Rect();
2109 if (mSearchDropTargetBar != null) {
2110 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2113 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
2114 appSearchData, sourceBounds);
2115 if (clearTextImmediately) {
2119 // We need to show the workspace after starting the search
2120 showWorkspace(true);
2124 * Start a text search.
2126 * @return {@code true} if the search will start immediately, so any further keypresses
2127 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2128 * to buffer keypresses.
2130 public boolean startSearch(String initialQuery,
2131 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
2132 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2133 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2137 startGlobalSearch(initialQuery, selectInitialQuery,
2138 appSearchData, sourceBounds);
2143 * Starts the global search activity. This code is a copied from SearchManager
2145 private void startGlobalSearch(String initialQuery,
2146 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
2147 final SearchManager searchManager =
2148 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2149 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2150 if (globalSearchActivity == null) {
2151 Log.w(TAG, "No global search activity found.");
2154 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2155 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2156 intent.setComponent(globalSearchActivity);
2157 // Make sure that we have a Bundle to put source in
2158 if (appSearchData == null) {
2159 appSearchData = new Bundle();
2161 appSearchData = new Bundle(appSearchData);
2163 // Set source to package name of app that starts global search if not set already.
2164 if (!appSearchData.containsKey("source")) {
2165 appSearchData.putString("source", getPackageName());
2167 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2168 if (!TextUtils.isEmpty(initialQuery)) {
2169 intent.putExtra(SearchManager.QUERY, initialQuery);
2171 if (selectInitialQuery) {
2172 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2174 intent.setSourceBounds(sourceBounds);
2176 startActivity(intent);
2177 } catch (ActivityNotFoundException ex) {
2178 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2182 public boolean isOnCustomContent() {
2183 return mWorkspace.isOnOrMovingToCustomContent();
2187 public boolean onPrepareOptionsMenu(Menu menu) {
2188 super.onPrepareOptionsMenu(menu);
2189 if (!isOnCustomContent()) {
2190 // Close any open folders
2192 // Stop resizing any widgets
2193 mWorkspace.exitWidgetResizeMode();
2194 if (!mWorkspace.isInOverviewMode()) {
2195 // Show the overview mode
2196 showOverviewMode(true);
2198 showWorkspace(true);
2201 if (mLauncherCallbacks != null) {
2202 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2209 public boolean onSearchRequested() {
2210 startSearch(null, false, null, true);
2211 // Use a custom animation for launching search
2215 public boolean isWorkspaceLocked() {
2216 return mWorkspaceLoading || mWaitingForResult;
2219 public boolean isWorkspaceLoading() {
2220 return mWorkspaceLoading;
2223 private void setWorkspaceLoading(boolean value) {
2224 boolean isLocked = isWorkspaceLocked();
2225 mWorkspaceLoading = value;
2226 if (isLocked != isWorkspaceLocked()) {
2227 onWorkspaceLockedChanged();
2231 private void setWaitingForResult(boolean value) {
2232 boolean isLocked = isWorkspaceLocked();
2233 mWaitingForResult = value;
2234 if (isLocked != isWorkspaceLocked()) {
2235 onWorkspaceLockedChanged();
2239 protected void onWorkspaceLockedChanged() {
2240 if (mLauncherCallbacks != null) {
2241 mLauncherCallbacks.onWorkspaceLockedChanged();
2245 private void resetAddInfo() {
2246 mPendingAddInfo.container = ItemInfo.NO_ID;
2247 mPendingAddInfo.screenId = -1;
2248 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2249 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
2250 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
2251 mPendingAddInfo.dropPos = null;
2254 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
2255 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
2256 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2259 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2260 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2262 if (appWidgetInfo.configure != null) {
2263 mPendingAddWidgetInfo = appWidgetInfo;
2264 mPendingAddWidgetId = appWidgetId;
2266 // Launch over to configure widget, if needed
2267 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2268 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2271 // Otherwise just add it
2272 Runnable onComplete = new Runnable() {
2275 // Exit spring loaded mode if necessary after adding the widget
2276 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2280 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
2282 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
2286 protected void moveToCustomContentScreen(boolean animate) {
2287 // Close any folders that may be open.
2289 mWorkspace.moveToCustomContentScreen(animate);
2292 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2293 int[] cell, int spanX, int spanY) {
2294 switch (info.itemType) {
2295 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2296 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2297 int span[] = new int[2];
2300 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2301 container, screenId, cell, span);
2303 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2304 processShortcutFromDrop(info.componentName, container, screenId, cell);
2307 throw new IllegalStateException("Unknown item type: " + info.itemType);
2312 * Process a shortcut drop.
2314 * @param componentName The name of the component
2315 * @param screenId The ID of the screen where it should be added
2316 * @param cell The cell it should be added to, optional
2318 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2321 mPendingAddInfo.container = container;
2322 mPendingAddInfo.screenId = screenId;
2323 mPendingAddInfo.dropPos = null;
2326 mPendingAddInfo.cellX = cell[0];
2327 mPendingAddInfo.cellY = cell[1];
2330 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2331 createShortcutIntent.setComponent(componentName);
2332 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
2336 * Process a widget drop.
2338 * @param info The PendingAppWidgetInfo of the widget being added.
2339 * @param screenId The ID of the screen where it should be added
2340 * @param cell The cell it should be added to, optional
2342 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2343 int[] cell, int[] span) {
2345 mPendingAddInfo.container = info.container = container;
2346 mPendingAddInfo.screenId = info.screenId = screenId;
2347 mPendingAddInfo.dropPos = null;
2348 mPendingAddInfo.minSpanX = info.minSpanX;
2349 mPendingAddInfo.minSpanY = info.minSpanY;
2352 mPendingAddInfo.cellX = cell[0];
2353 mPendingAddInfo.cellY = cell[1];
2356 mPendingAddInfo.spanX = span[0];
2357 mPendingAddInfo.spanY = span[1];
2360 AppWidgetHostView hostView = info.boundWidget;
2362 if (hostView != null) {
2363 appWidgetId = hostView.getAppWidgetId();
2364 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
2366 // Clear the boundWidget so that it doesn't get destroyed.
2367 info.boundWidget = null;
2369 // In this case, we either need to start an activity to get permission to bind
2370 // the widget, or we need to start an activity to configure the widget, or both.
2371 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
2372 Bundle options = info.bindOptions;
2374 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2375 appWidgetId, info.info, options);
2377 addAppWidgetImpl(appWidgetId, info, null, info.info);
2379 mPendingAddWidgetInfo = info.info;
2380 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2382 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
2383 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2384 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2385 // TODO: we need to make sure that this accounts for the options bundle.
2386 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
2387 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2392 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
2394 final FolderInfo folderInfo = new FolderInfo();
2395 folderInfo.title = getText(R.string.folder_name);
2398 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2400 sFolders.put(folderInfo.id, folderInfo);
2403 FolderIcon newFolder =
2404 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
2405 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
2406 isWorkspaceLocked());
2407 // Force measure the new folder icon
2408 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2409 parent.getShortcutsAndWidgets().measureChild(newFolder);
2413 void removeFolder(FolderInfo folder) {
2414 sFolders.remove(folder.id);
2418 * Registers various content observers. The current implementation registers
2419 * only a favorites observer to keep track of the favorites applications.
2421 private void registerContentObservers() {
2422 ContentResolver resolver = getContentResolver();
2423 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2424 true, mWidgetObserver);
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
2431 case KeyEvent.KEYCODE_HOME:
2433 case KeyEvent.KEYCODE_VOLUME_DOWN:
2434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
2442 case KeyEvent.KEYCODE_HOME:
2447 return super.dispatchKeyEvent(event);
2451 public void onBackPressed() {
2452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2456 if (mDragController.isDragging()) {
2457 mDragController.cancelDrag();
2461 if (isAppsViewVisible()) {
2462 showWorkspace(true);
2463 } else if (isWidgetsViewVisible()) {
2464 showOverviewMode(true);
2465 } else if (mWorkspace.isInOverviewMode()) {
2466 showWorkspace(true);
2467 } else if (mWorkspace.getOpenFolder() != null) {
2468 Folder openFolder = mWorkspace.getOpenFolder();
2469 if (openFolder.isEditingName()) {
2470 openFolder.dismissEditingName();
2475 mWorkspace.exitWidgetResizeMode();
2477 // Back button is a no-op here, but give at least some feedback for the button press
2478 mWorkspace.showOutlinesTemporarily();
2483 * Re-listen when widgets are reset.
2485 @Thunk void onAppWidgetReset() {
2486 if (mAppWidgetHost != null) {
2487 mAppWidgetHost.startListening();
2492 * Launches the intent referred by the clicked shortcut.
2494 * @param v The view representing the clicked shortcut.
2496 public void onClick(View v) {
2497 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2498 // view has detached (it's possible for this to happen if the view is removed mid touch).
2499 if (v.getWindowToken() == null) {
2503 if (!mWorkspace.isFinishedSwitchingState()) {
2507 if (v instanceof Workspace) {
2508 if (mWorkspace.isInOverviewMode()) {
2509 showWorkspace(true);
2514 if (v instanceof CellLayout) {
2515 if (mWorkspace.isInOverviewMode()) {
2516 showWorkspace(mWorkspace.indexOfChild(v), true);
2520 Object tag = v.getTag();
2521 if (tag instanceof ShortcutInfo) {
2522 onClickAppShortcut(v);
2523 } else if (tag instanceof FolderInfo) {
2524 if (v instanceof FolderIcon) {
2525 onClickFolderIcon(v);
2527 } else if (v == mAllAppsButton) {
2528 onClickAllAppsButton(v);
2529 } else if (tag instanceof AppInfo) {
2530 startAppShortcutOrInfoActivity(v);
2531 } else if (tag instanceof LauncherAppWidgetInfo) {
2532 if (v instanceof PendingAppWidgetHostView) {
2533 onClickPendingWidget((PendingAppWidgetHostView) v);
2538 @SuppressLint("ClickableViewAccessibility")
2539 public boolean onTouch(View v, MotionEvent event) {
2544 * Event handler for the app widget view which has not fully restored.
2546 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2547 if (mIsSafeModeEnabled) {
2548 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2552 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
2553 if (v.isReadyForClickSetup()) {
2554 int widgetId = info.appWidgetId;
2555 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2556 if (appWidgetInfo != null) {
2557 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2558 this, appWidgetInfo);
2559 mPendingAddInfo.copyFrom(info);
2560 mPendingAddWidgetId = widgetId;
2562 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2563 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2565 } else if (info.installProgress < 0) {
2566 // The install has not been queued
2567 final String packageName = info.providerName.getPackageName();
2568 showBrokenAppInstallDialog(packageName,
2569 new DialogInterface.OnClickListener() {
2570 public void onClick(DialogInterface dialog, int id) {
2571 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2575 // Download has started.
2576 final String packageName = info.providerName.getPackageName();
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2582 * Event handler for the "grid" button that appears on the home screen, which
2583 * enters all apps mode.
2585 * @param v The view that was clicked.
2587 protected void onClickAllAppsButton(View v) {
2588 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2589 if (isAppsViewVisible()) {
2590 showWorkspace(true);
2592 // Try and refresh the set of predicted apps before we enter launcher
2593 showAppsView(true /* animated */, false /* resetListToTop */,
2594 true /* updatePredictedApps */);
2598 private void showBrokenAppInstallDialog(final String packageName,
2599 DialogInterface.OnClickListener onSearchClickListener) {
2600 new AlertDialog.Builder(this)
2601 .setTitle(R.string.abandoned_promises_title)
2602 .setMessage(R.string.abandoned_promise_explanation)
2603 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2604 .setNeutralButton(R.string.abandoned_clean_this,
2605 new DialogInterface.OnClickListener() {
2606 public void onClick(DialogInterface dialog, int id) {
2607 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2608 mWorkspace.removeAbandonedPromise(packageName, user);
2616 * Event handler for an app shortcut click.
2618 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2620 protected void onClickAppShortcut(final View v) {
2621 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2622 Object tag = v.getTag();
2623 if (!(tag instanceof ShortcutInfo)) {
2624 throw new IllegalArgumentException("Input must be a Shortcut");
2628 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2630 if (shortcut.isDisabled != 0) {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2639 final Intent intent = shortcut.intent;
2641 // Check for special shortcuts
2642 if (intent.getComponent() != null) {
2643 final String shortcutClass = intent.getComponent().getClassName();
2645 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2646 MemoryDumpActivity.startDump(this);
2648 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2649 toggleShowWeightWatcher();
2654 // Check for abandoned promise
2655 if ((v instanceof BubbleTextView)
2656 && shortcut.isPromise()
2657 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
2658 showBrokenAppInstallDialog(
2659 shortcut.getTargetComponent().getPackageName(),
2660 new DialogInterface.OnClickListener() {
2661 public void onClick(DialogInterface dialog, int id) {
2662 startAppShortcutOrInfoActivity(v);
2669 startAppShortcutOrInfoActivity(v);
2671 if (mLauncherCallbacks != null) {
2672 mLauncherCallbacks.onClickAppShortcut(v);
2676 @Thunk void startAppShortcutOrInfoActivity(View v) {
2677 Object tag = v.getTag();
2678 final ShortcutInfo shortcut;
2679 final Intent intent;
2680 if (tag instanceof ShortcutInfo) {
2681 shortcut = (ShortcutInfo) tag;
2682 intent = shortcut.intent;
2683 int[] pos = new int[2];
2684 v.getLocationOnScreen(pos);
2685 intent.setSourceBounds(new Rect(pos[0], pos[1],
2686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2688 } else if (tag instanceof AppInfo) {
2690 intent = ((AppInfo) tag).intent;
2692 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2695 boolean success = startActivitySafely(v, intent, tag);
2696 mStats.recordLaunch(v, intent, shortcut);
2698 if (success && v instanceof BubbleTextView) {
2699 mWaitingForResume = (BubbleTextView) v;
2700 mWaitingForResume.setStayPressed(true);
2705 * Event handler for a folder icon click.
2707 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2709 protected void onClickFolderIcon(View v) {
2710 if (LOGD) Log.d(TAG, "onClickFolder");
2711 if (!(v instanceof FolderIcon)){
2712 throw new IllegalArgumentException("Input must be a FolderIcon");
2715 FolderIcon folderIcon = (FolderIcon) v;
2716 final FolderInfo info = folderIcon.getFolderInfo();
2717 Folder openFolder = mWorkspace.getFolderForTag(info);
2719 // If the folder info reports that the associated folder is open, then verify that
2720 // it is actually opened. There have been a few instances where this gets out of sync.
2721 if (info.opened && openFolder == null) {
2722 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2723 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2724 info.opened = false;
2727 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2728 // Close any open folder
2730 // Open the requested folder
2731 openFolder(folderIcon);
2733 // Find the open folder...
2735 if (openFolder != null) {
2736 folderScreen = mWorkspace.getPageForView(openFolder);
2738 closeFolder(openFolder);
2739 if (folderScreen != mWorkspace.getCurrentPage()) {
2740 // Close any folder open on the current screen
2742 // Pull the folder onto this screen
2743 openFolder(folderIcon);
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickFolderIcon(v);
2754 * Event handler for the (Add) Widgets button that appears after a long press
2755 * on the home screen.
2757 protected void onClickAddWidgetButton(View view) {
2758 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
2759 if (mIsSafeModeEnabled) {
2760 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2762 showWidgetsView(true /* animated */, true /* resetPageToZero */);
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickAddWidgetButton(view);
2770 * Event handler for the wallpaper picker button that appears after a long press
2771 * on the home screen.
2773 protected void onClickWallpaperPicker(View v) {
2774 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2775 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2776 REQUEST_PICK_WALLPAPER);
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onClickWallpaperPicker(v);
2784 * Event handler for a click on the settings button that appears after a long press
2785 * on the home screen.
2787 protected void onClickSettingsButton(View v) {
2788 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onClickSettingsButton(v);
2792 startActivity(new Intent(this, SettingsActivity.class));
2796 public View.OnTouchListener getHapticFeedbackTouchListener() {
2797 if (mHapticFeedbackTouchListener == null) {
2798 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2799 @SuppressLint("ClickableViewAccessibility")
2801 public boolean onTouch(View v, MotionEvent event) {
2802 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2803 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2809 return mHapticFeedbackTouchListener;
2812 public void onDragStarted(View view) {
2813 if (isOnCustomContent()) {
2814 // Custom content screen doesn't participate in drag and drop. If on custom
2815 // content screen, move to default.
2816 moveWorkspaceToDefaultScreen();
2819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onDragStarted(view);
2825 * Called when the user stops interacting with the launcher.
2826 * This implies that the user is now on the homescreen and is not doing housekeeping.
2828 protected void onInteractionEnd() {
2829 if (mLauncherCallbacks != null) {
2830 mLauncherCallbacks.onInteractionEnd();
2835 * Called when the user starts interacting with the launcher.
2836 * The possible interactions are:
2838 * - reorder an app shortcut, or a widget
2839 * - open the overview mode.
2840 * This is a good time to stop doing things that only make sense
2841 * when the user is on the homescreen and not doing housekeeping.
2843 protected void onInteractionBegin() {
2844 if (mLauncherCallbacks != null) {
2845 mLauncherCallbacks.onInteractionBegin();
2849 /** Updates the interaction state. */
2850 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
2851 // Only update the interacting state if we are transitioning to/from a view with an
2853 boolean fromStateWithOverlay;
2854 boolean toStateWithOverlay;
2855 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2856 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2857 toStateWithOverlay = toState != Workspace.State.NORMAL;
2859 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2860 fromState != Workspace.State.NORMAL_HIDDEN;
2861 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2862 toState != Workspace.State.NORMAL_HIDDEN;
2864 if (toStateWithOverlay) {
2865 onInteractionBegin();
2866 } else if (fromStateWithOverlay) {
2871 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
2873 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2874 launcherApps.showAppDetailsForProfile(componentName, user);
2875 } catch (SecurityException e) {
2876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2877 Log.e(TAG, "Launcher does not have permission to launch settings");
2878 } catch (ActivityNotFoundException e) {
2879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2880 Log.e(TAG, "Unable to launch settings");
2884 // returns true if the activity was started
2885 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2886 UserHandleCompat user) {
2887 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
2888 // System applications cannot be installed. For now, show a toast explaining that.
2889 // We may give them the option of disabling apps this way.
2890 int messageId = R.string.uninstall_system_app_text;
2891 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2894 String packageName = componentName.getPackageName();
2895 String className = componentName.getClassName();
2896 Intent intent = new Intent(
2897 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2898 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2899 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
2901 user.addToIntent(intent, Intent.EXTRA_USER);
2903 startActivity(intent);
2908 private boolean startActivity(View v, Intent intent, Object tag) {
2909 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2911 // Only launch using the new animation if the shortcut has not opted out (this is a
2912 // private contract between launcher and may be ignored in the future).
2913 boolean useLaunchAnimation = (v != null) &&
2914 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2915 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2916 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2918 UserHandleCompat user = null;
2919 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2920 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2921 user = userManager.getUserForSerialNumber(serialNumber);
2924 Bundle optsBundle = null;
2925 if (useLaunchAnimation) {
2926 ActivityOptions opts = null;
2927 if (sClipRevealMethod != null) {
2928 // TODO: call method directly when Launcher3 can depend on M APIs
2929 int left = 0, top = 0;
2930 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2931 if (v instanceof TextView) {
2932 // Launch from center of icon, not entire view
2933 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2935 Rect bounds = icon.getBounds();
2936 left = (width - bounds.width()) / 2;
2937 top = v.getPaddingTop();
2938 width = bounds.width();
2939 height = bounds.height();
2943 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2944 left, top, width, height);
2945 } catch (IllegalAccessException e) {
2946 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2947 sClipRevealMethod = null;
2948 } catch (InvocationTargetException e) {
2949 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2950 sClipRevealMethod = null;
2953 if (opts == null && !Utilities.isLmpOrAbove()) {
2954 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2955 v.getMeasuredWidth(), v.getMeasuredHeight());
2957 optsBundle = opts != null ? opts.toBundle() : null;
2960 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2961 // Could be launching some bookkeeping activity
2962 startActivity(intent, optsBundle);
2964 // TODO Component can be null when shortcuts are supported for secondary user
2965 launcherApps.startActivityForProfile(intent.getComponent(), user,
2966 intent.getSourceBounds(), optsBundle);
2969 } catch (SecurityException e) {
2970 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2971 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2972 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2973 "or use the exported attribute for this activity. "
2974 + "tag="+ tag + " intent=" + intent, e);
2979 private boolean startActivitySafely(View v, Intent intent, Object tag) {
2980 boolean success = false;
2981 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2982 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2986 success = startActivity(v, intent, tag);
2987 } catch (ActivityNotFoundException e) {
2988 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2989 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2995 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2996 * in the DragLayer in the exact absolute location of the original FolderIcon.
2998 private void copyFolderIconToImage(FolderIcon fi) {
2999 final int width = fi.getMeasuredWidth();
3000 final int height = fi.getMeasuredHeight();
3002 // Lazy load ImageView, Bitmap and Canvas
3003 if (mFolderIconImageView == null) {
3004 mFolderIconImageView = new ImageView(this);
3006 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3007 mFolderIconBitmap.getHeight() != height) {
3008 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3009 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3012 DragLayer.LayoutParams lp;
3013 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3014 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3016 lp = new DragLayer.LayoutParams(width, height);
3019 // The layout from which the folder is being opened may be scaled, adjust the starting
3020 // view size by this scale factor.
3021 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
3022 lp.customPosition = true;
3023 lp.x = mRectForFolderAnimation.left;
3024 lp.y = mRectForFolderAnimation.top;
3025 lp.width = (int) (scale * width);
3026 lp.height = (int) (scale * height);
3028 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3029 fi.draw(mFolderIconCanvas);
3030 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
3031 if (fi.getFolder() != null) {
3032 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3033 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
3035 // Just in case this image view is still in the drag layer from a previous animation,
3036 // we remove it and re-add it.
3037 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3038 mDragLayer.removeView(mFolderIconImageView);
3040 mDragLayer.addView(mFolderIconImageView, lp);
3041 if (fi.getFolder() != null) {
3042 fi.getFolder().bringToFront();
3046 private void growAndFadeOutFolderIcon(FolderIcon fi) {
3047 if (fi == null) return;
3048 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3049 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3050 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3052 FolderInfo info = (FolderInfo) fi.getTag();
3053 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3054 CellLayout cl = (CellLayout) fi.getParent().getParent();
3055 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3056 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
3059 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3060 copyFolderIconToImage(fi);
3061 fi.setVisibility(View.INVISIBLE);
3063 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
3065 if (Utilities.isLmpOrAbove()) {
3066 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3068 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
3072 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
3073 if (fi == null) return;
3074 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3075 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3076 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3078 final CellLayout cl = (CellLayout) fi.getParent().getParent();
3080 // We remove and re-draw the FolderIcon in-case it has changed
3081 mDragLayer.removeView(mFolderIconImageView);
3082 copyFolderIconToImage(fi);
3083 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
3085 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
3086 oa.addListener(new AnimatorListenerAdapter() {
3088 public void onAnimationEnd(Animator animation) {
3090 cl.clearFolderLeaveBehind();
3091 // Remove the ImageView copy of the FolderIcon and make the original visible.
3092 mDragLayer.removeView(mFolderIconImageView);
3093 fi.setVisibility(View.VISIBLE);
3101 * Opens the user folder described by the specified tag. The opening of the folder
3102 * is animated relative to the specified View. If the View is null, no animation
3105 * @param folderInfo The FolderInfo describing the folder to open.
3107 public void openFolder(FolderIcon folderIcon) {
3108 Folder folder = folderIcon.getFolder();
3109 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3110 if (openFolder != null && openFolder != folder) {
3111 // Close any open folder before opening a folder.
3115 FolderInfo info = folder.mInfo;
3119 // While the folder is open, the position of the icon cannot change.
3120 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3122 // Just verify that the folder hasn't already been added to the DragLayer.
3123 // There was a one-off crash where the folder had a parent already.
3124 if (folder.getParent() == null) {
3125 mDragLayer.addView(folder);
3126 mDragController.addDropTarget((DropTarget) folder);
3128 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3129 folder.getParent() + ").");
3131 folder.animateOpen();
3132 growAndFadeOutFolderIcon(folderIcon);
3134 // Notify the accessibility manager that this folder "window" has appeared and occluded
3135 // the workspace items
3136 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3137 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
3140 public void closeFolder() {
3141 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3142 if (folder != null) {
3143 if (folder.isEditingName()) {
3144 folder.dismissEditingName();
3146 closeFolder(folder);
3150 public void closeFolder(Folder folder) {
3151 folder.getInfo().opened = false;
3153 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3154 if (parent != null) {
3155 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3156 shrinkAndFadeInFolderIcon(fi);
3158 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3161 folder.animateClosed();
3163 // Notify the accessibility manager that this folder "window" has disappeard and no
3164 // longer occludeds the workspace items
3165 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3168 public boolean onLongClick(View v) {
3169 if (!isDraggingEnabled()) return false;
3170 if (isWorkspaceLocked()) return false;
3171 if (mState != State.WORKSPACE) return false;
3173 if (v instanceof Workspace) {
3174 if (!mWorkspace.isInOverviewMode()) {
3175 if (!mWorkspace.isTouchActive()) {
3176 showOverviewMode(true);
3177 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3178 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3188 CellLayout.CellInfo longClickCellInfo = null;
3189 View itemUnderLongClick = null;
3190 if (v.getTag() instanceof ItemInfo) {
3191 ItemInfo info = (ItemInfo) v.getTag();
3192 longClickCellInfo = new CellLayout.CellInfo(v, info);
3193 itemUnderLongClick = longClickCellInfo.cell;
3197 // The hotseat touch handling does not go through Workspace, and we always allow long press
3198 // on hotseat items.
3199 final boolean inHotseat = isHotseatLayout(v);
3200 if (!mDragController.isDragging()) {
3201 if (itemUnderLongClick == null) {
3202 // User long pressed on empty space
3203 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3204 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3205 if (mWorkspace.isInOverviewMode()) {
3206 mWorkspace.startReordering(v);
3208 showOverviewMode(true);
3211 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3212 mHotseat.getOrderInHotseat(
3213 longClickCellInfo.cellX,
3214 longClickCellInfo.cellY));
3215 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
3216 // User long pressed on an item
3217 mWorkspace.startDrag(longClickCellInfo);
3224 boolean isHotseatLayout(View layout) {
3225 return mHotseat != null && layout != null &&
3226 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3230 * Returns the CellLayout of the specified container at the specified screen.
3232 public CellLayout getCellLayout(long container, long screenId) {
3233 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3234 if (mHotseat != null) {
3235 return mHotseat.getLayout();
3240 return mWorkspace.getScreenWithId(screenId);
3245 * For overridden classes.
3247 public boolean isAllAppsVisible() {
3248 return isAppsViewVisible();
3251 public boolean isAppsViewVisible() {
3252 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3255 public boolean isWidgetsViewVisible() {
3256 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
3259 private void setWorkspaceBackground(int background) {
3260 switch (background) {
3261 case WORKSPACE_BACKGROUND_TRANSPARENT:
3262 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3264 case WORKSPACE_BACKGROUND_BLACK:
3265 getWindow().setBackgroundDrawable(null);
3268 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3272 protected void changeWallpaperVisiblity(boolean visible) {
3273 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3274 int curflags = getWindow().getAttributes().flags
3275 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3276 if (wpflags != curflags) {
3277 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3279 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
3283 public void onTrimMemory(int level) {
3284 super.onTrimMemory(level);
3285 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3286 // The widget preview db can result in holding onto over
3287 // 3MB of memory for caching which isn't necessary.
3288 SQLiteDatabase.releaseMemory();
3290 // This clears all widget bitmaps from the widget tray
3293 if (mLauncherCallbacks != null) {
3294 mLauncherCallbacks.onTrimMemory(level);
3299 public void onStateTransitionHideSearchBar() {
3300 // Hide the search bar
3301 if (mSearchDropTargetBar != null) {
3302 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3306 public void showWorkspace(boolean animated) {
3307 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3311 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3312 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3313 onCompleteRunnable, true);
3316 protected void showWorkspace(int snapToPage, boolean animated) {
3317 showWorkspace(snapToPage, animated, null, true);
3320 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3321 boolean notifyLauncherCallbacks) {
3322 boolean changed = mState != State.WORKSPACE ||
3323 mWorkspace.getState() != Workspace.State.NORMAL;
3325 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
3326 mWorkspace.setVisibility(View.VISIBLE);
3327 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3328 snapToPage, animated, onCompleteRunnable);
3330 // Show the search bar (only animate if we were showing the drop target bar in spring
3332 if (mSearchDropTargetBar != null) {
3333 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
3336 // Set focus to the AppsCustomize button
3337 if (mAllAppsButton != null) {
3338 mAllAppsButton.requestFocus();
3342 // Change the state *after* we've called all the transition code
3343 mState = State.WORKSPACE;
3345 // Resume the auto-advance of widgets
3346 mUserPresent = true;
3347 updateAutoAdvanceState();
3350 // Send an accessibility event to announce the context change
3351 getWindow().getDecorView()
3352 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3353 if (notifyLauncherCallbacks) {
3354 // Dismiss all apps when the workspace is shown
3355 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
3356 mLauncherCallbacks.onAllAppsHidden();
3362 void showOverviewMode(boolean animated) {
3363 mWorkspace.setVisibility(View.VISIBLE);
3364 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3365 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3366 null /* onCompleteRunnable */);
3367 mState = State.WORKSPACE;
3371 * Shows the apps view.
3373 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
3374 if (resetListToTop) {
3375 mAppsView.scrollToTop();
3377 if (updatePredictedApps) {
3378 tryAndUpdatePredictedApps();
3380 showAppsOrWidgets(animated, State.APPS);
3384 * Shows the widgets view.
3386 void showWidgetsView(boolean animated, boolean resetPageToZero) {
3387 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
3388 if (resetPageToZero) {
3389 mWidgetsView.scrollToTop();
3391 showAppsOrWidgets(animated, State.WIDGETS);
3393 mWidgetsView.post(new Runnable() {
3396 mWidgetsView.requestFocus();
3402 * Sets up the transition to show the apps/widgets view.
3404 * @return whether the current from and to state allowed this operation
3406 // TODO: calling method should use the return value so that when {@code false} is returned
3407 // the workspace transition doesn't fall into invalid state.
3408 private boolean showAppsOrWidgets(boolean animated, State toState) {
3409 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3410 mState != State.WIDGETS_SPRING_LOADED) {
3413 if (toState != State.APPS && toState != State.WIDGETS) {
3417 if (toState == State.APPS) {
3418 mStateTransitionAnimation.startAnimationToAllApps(animated);
3419 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
3420 mLauncherCallbacks.onAllAppsShown();
3423 mStateTransitionAnimation.startAnimationToWidgets(animated);
3426 // Change the state *after* we've called all the transition code
3429 // Pause the auto-advance of widgets until we are out of AllApps
3430 mUserPresent = false;
3431 updateAutoAdvanceState();
3434 // Send an accessibility event to announce the context change
3435 getWindow().getDecorView()
3436 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3441 * Updates the workspace and interaction state on state change, and return the animation to this
3444 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3445 boolean animated, HashMap<View, Integer> layerViews) {
3446 Workspace.State fromState = mWorkspace.getState();
3447 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3448 updateInteraction(fromState, toState);
3452 public void enterSpringLoadedDragMode() {
3453 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
3454 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3455 mState == State.WIDGETS_SPRING_LOADED) {
3459 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3460 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3461 null /* onCompleteRunnable */);
3462 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
3465 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
3466 final Runnable onCompleteRunnable) {
3467 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
3469 if (successfulDrop) {
3470 // We need to trigger all apps hidden to notify search to update itself before the
3471 // delayed call to showWorkspace below
3472 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
3473 mLauncherCallbacks.onAllAppsHidden();
3477 mHandler.postDelayed(new Runnable() {
3480 if (successfulDrop) {
3481 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3483 // Before we show workspace, hide all apps again because
3484 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3485 // clean up our state transition functions
3486 mWidgetsView.setVisibility(View.GONE);
3487 showWorkspace(true, onCompleteRunnable);
3489 exitSpringLoadedDragMode();
3495 void exitSpringLoadedDragMode() {
3496 if (mState == State.APPS_SPRING_LOADED) {
3497 showAppsView(true /* animated */, false /* resetListToTop */,
3498 false /* updatePredictedApps */);
3499 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3500 showWidgetsView(true, false);
3505 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3508 private void tryAndUpdatePredictedApps() {
3509 if (mLauncherCallbacks != null) {
3510 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3511 if (!apps.isEmpty()) {
3512 mAppsView.setPredictedApps(apps);
3517 void lockAllApps() {
3521 void unlockAllApps() {
3525 protected void disableVoiceButtonProxy(boolean disable) {
3529 public View getOrCreateQsbBar() {
3530 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3531 return mLauncherCallbacks.getQsbBar();
3535 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3536 if (searchProvider == null) {
3540 Bundle opts = new Bundle();
3541 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3542 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3544 SharedPreferences sp = getSharedPreferences(
3545 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3546 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
3547 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
3548 if (!searchProvider.provider.flattenToString().equals(
3549 sp.getString(QSB_WIDGET_PROVIDER, null))
3550 || (widgetInfo == null)
3551 || !widgetInfo.provider.equals(searchProvider.provider)) {
3552 // A valid widget is not already bound.
3553 if (widgetId > -1) {
3554 mAppWidgetHost.deleteAppWidgetId(widgetId);
3558 // Try to bind a new widget
3559 widgetId = mAppWidgetHost.allocateAppWidgetId();
3561 if (!AppWidgetManagerCompat.getInstance(this)
3562 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3563 mAppWidgetHost.deleteAppWidgetId(widgetId);
3568 .putInt(QSB_WIDGET_ID, widgetId)
3569 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3573 if (widgetId != -1) {
3574 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3575 mQsb.updateAppWidgetOptions(opts);
3576 mQsb.setPadding(0, 0, 0, 0);
3577 mSearchDropTargetBar.addView(mQsb);
3578 mSearchDropTargetBar.setQsbSearchBar(mQsb);
3584 private void reinflateQSBIfNecessary() {
3585 if (mQsb instanceof LauncherAppWidgetHostView &&
3586 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3587 mSearchDropTargetBar.removeView(mQsb);
3589 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3594 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3595 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
3596 final List<CharSequence> text = event.getText();
3598 // Populate event with a fake title based on the current state.
3599 if (mState == State.APPS) {
3600 text.add(getString(R.string.all_apps_button_label));
3601 } else if (mState == State.WIDGETS) {
3602 text.add(getString(R.string.widget_button_text));
3604 text.add(getString(R.string.all_apps_home_button_label));
3610 * Receives notifications when system dialogs are to be closed.
3612 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3614 public void onReceive(Context context, Intent intent) {
3615 closeSystemDialogs();
3620 * Receives notifications whenever the appwidgets are reset.
3622 private class AppWidgetResetObserver extends ContentObserver {
3623 public AppWidgetResetObserver() {
3624 super(new Handler());
3628 public void onChange(boolean selfChange) {
3634 * If the activity is currently paused, signal that we need to run the passed Runnable
3637 * This needs to be called from incoming places where resources might have been loaded
3638 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3639 * wrong when we're not running, and if the activity comes back to what the configuration was
3640 * when we were paused, activity is not restarted.
3642 * Implementation of the method from LauncherModel.Callbacks.
3644 * @return {@code true} if we are currently paused. The caller might be able to skip some work
3646 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
3648 if (LOGD) Log.d(TAG, "Deferring update until onResume");
3649 if (deletePreviousRunnables) {
3650 while (mBindOnResumeCallbacks.remove(run)) {
3653 mBindOnResumeCallbacks.add(run);
3660 private boolean waitUntilResume(Runnable run) {
3661 return waitUntilResume(run, false);
3664 public void addOnResumeCallback(Runnable run) {
3665 mOnResumeCallbacks.add(run);
3669 * If the activity is currently paused, signal that we need to re-run the loader
3672 * This needs to be called from incoming places where resources might have been loaded
3673 * while we are paused. That is becaues the Configuration might be wrong
3674 * when we're not running, and if it comes back to what it was when we
3675 * were paused, we are not restarted.
3677 * Implementation of the method from LauncherModel.Callbacks.
3679 * @return true if we are currently paused. The caller might be able to
3680 * skip some work in that case since we will come back again.
3682 public boolean setLoadOnResume() {
3684 if (LOGD) Log.d(TAG, "setLoadOnResume");
3685 mOnResumeNeedsLoad = true;
3693 * Implementation of the method from LauncherModel.Callbacks.
3695 public int getCurrentWorkspaceScreen() {
3696 if (mWorkspace != null) {
3697 return mWorkspace.getCurrentPage();
3699 return SCREEN_COUNT / 2;
3704 * Refreshes the shortcuts shown on the workspace.
3706 * Implementation of the method from LauncherModel.Callbacks.
3708 public void startBinding() {
3709 setWorkspaceLoading(true);
3711 // If we're starting binding all over again, clear any bind calls we'd postponed in
3712 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3713 // from scratch again
3714 mBindOnResumeCallbacks.clear();
3716 // Clear the workspace because it's going to be rebound
3717 mWorkspace.clearDropTargets();
3718 mWorkspace.removeAllWorkspaceScreens();
3720 mWidgetsToAdvance.clear();
3721 if (mHotseat != null) {
3722 mHotseat.resetLayout();
3727 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3728 bindAddScreens(orderedScreenIds);
3730 // If there are no screens, we need to have an empty screen
3731 if (orderedScreenIds.size() == 0) {
3732 mWorkspace.addExtraEmptyScreen();
3735 // Create the custom content page (this call updates mDefaultScreen which calls
3736 // setCurrentPage() so ensure that all pages are added before calling this).
3737 if (hasCustomContentToLeft()) {
3738 mWorkspace.createCustomContentContainer();
3739 populateCustomContentContainer();
3744 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
3745 int count = orderedScreenIds.size();
3746 for (int i = 0; i < count; i++) {
3747 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
3751 private boolean shouldShowWeightWatcher() {
3752 String spKey = LauncherAppState.getSharedPreferencesKey();
3753 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3754 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
3759 private void toggleShowWeightWatcher() {
3760 String spKey = LauncherAppState.getSharedPreferencesKey();
3761 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3762 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3766 SharedPreferences.Editor editor = sp.edit();
3767 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3770 if (mWeightWatcher != null) {
3771 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3775 public void bindAppsAdded(final ArrayList<Long> newScreens,
3776 final ArrayList<ItemInfo> addNotAnimated,
3777 final ArrayList<ItemInfo> addAnimated,
3778 final ArrayList<AppInfo> addedApps) {
3779 Runnable r = new Runnable() {
3781 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
3784 if (waitUntilResume(r)) {
3788 // Add the new screens
3789 if (newScreens != null) {
3790 bindAddScreens(newScreens);
3793 // We add the items without animation on non-visible pages, and with
3794 // animations on the new page (which we will try and snap to).
3795 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
3796 bindItems(addNotAnimated, 0,
3797 addNotAnimated.size(), false);
3799 if (addAnimated != null && !addAnimated.isEmpty()) {
3800 bindItems(addAnimated, 0,
3801 addAnimated.size(), true);
3804 // Remove the extra empty screen
3805 mWorkspace.removeExtraEmptyScreen(false, false);
3807 if (addedApps != null && mAppsView != null) {
3808 mAppsView.addApps(addedApps);
3813 * Bind the items start-end from the list.
3815 * Implementation of the method from LauncherModel.Callbacks.
3817 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3818 final boolean forceAnimateIcons) {
3819 Runnable r = new Runnable() {
3821 bindItems(shortcuts, start, end, forceAnimateIcons);
3824 if (waitUntilResume(r)) {
3828 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3829 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3830 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
3831 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
3832 Workspace workspace = mWorkspace;
3833 long newShortcutsScreenId = -1;
3834 for (int i = start; i < end; i++) {
3835 final ItemInfo item = shortcuts.get(i);
3837 // Short circuit if we are loading dock items for a configuration which has no dock
3838 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3843 switch (item.itemType) {
3844 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3845 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3846 ShortcutInfo info = (ShortcutInfo) item;
3847 View shortcut = createShortcut(info);
3850 * TODO: FIX collision case
3852 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3853 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3854 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3855 View v = cl.getChildAt(item.cellX, item.cellY);
3856 Object tag = v.getTag();
3857 String desc = "Collision while binding workspace item: " + item
3858 + ". Collides with " + tag;
3859 if (LauncherAppState.isDogfoodBuild()) {
3860 throw (new RuntimeException(desc));
3867 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3870 // Animate all the applications up now
3871 shortcut.setAlpha(0f);
3872 shortcut.setScaleX(0f);
3873 shortcut.setScaleY(0f);
3874 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3875 newShortcutsScreenId = item.screenId;
3878 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
3879 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
3880 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
3881 (FolderInfo) item, mIconCache);
3882 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3886 throw new RuntimeException("Invalid Item Type");
3891 // Animate to the correct page
3892 if (newShortcutsScreenId > -1) {
3893 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3894 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3895 final Runnable startBounceAnimRunnable = new Runnable() {
3897 anim.playTogether(bounceAnims);
3901 if (newShortcutsScreenId != currentScreenId) {
3902 // We post the animation slightly delayed to prevent slowdowns
3903 // when we are loading right after we return to launcher.
3904 mWorkspace.postDelayed(new Runnable() {
3906 if (mWorkspace != null) {
3907 mWorkspace.snapToPage(newScreenIndex);
3908 mWorkspace.postDelayed(startBounceAnimRunnable,
3909 NEW_APPS_ANIMATION_DELAY);
3912 }, NEW_APPS_PAGE_MOVE_DELAY);
3914 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
3918 workspace.requestLayout();
3922 * Implementation of the method from LauncherModel.Callbacks.
3924 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
3925 Runnable r = new Runnable() {
3927 bindFolders(folders);
3930 if (waitUntilResume(r)) {
3933 sFolders = folders.clone();
3937 * Add the views for a widget to the workspace.
3939 * Implementation of the method from LauncherModel.Callbacks.
3941 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3942 Runnable r = new Runnable() {
3944 bindAppWidget(item);
3947 if (waitUntilResume(r)) {
3951 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3952 if (DEBUG_WIDGETS) {
3953 Log.d(TAG, "bindAppWidget: " + item);
3955 final Workspace workspace = mWorkspace;
3957 LauncherAppWidgetProviderInfo appWidgetInfo =
3958 LauncherModel.getProviderInfo(this, item.providerName, item.user);
3960 if (!mIsSafeModeEnabled
3961 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3962 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
3963 if (appWidgetInfo == null) {
3964 if (DEBUG_WIDGETS) {
3965 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3966 + " belongs to component " + item.providerName
3967 + ", as the povider is null");
3969 LauncherModel.deleteItemFromDatabase(this, item);
3972 // Note: This assumes that the id remap broadcast is received before this step.
3973 // If that is not the case, the id remap will be ignored and user may see the
3974 // click to setup view.
3975 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3976 pendingInfo.spanX = item.spanX;
3977 pendingInfo.spanY = item.spanY;
3978 pendingInfo.minSpanX = item.minSpanX;
3979 pendingInfo.minSpanY = item.minSpanY;
3980 Bundle options = null;
3981 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3983 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3984 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3985 newWidgetId, appWidgetInfo, options);
3987 // TODO consider showing a permission dialog when the widget is clicked.
3989 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3990 if (DEBUG_WIDGETS) {
3991 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3992 + " belongs to component " + item.providerName
3993 + ", as the launcher is unable to bing a new widget id");
3995 LauncherModel.deleteItemFromDatabase(this, item);
3999 item.appWidgetId = newWidgetId;
4001 // If the widget has a configure activity, it is still needs to set it up, otherwise
4002 // the widget is ready to go.
4003 item.restoreStatus = (appWidgetInfo.configure == null)
4004 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4005 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4007 LauncherModel.updateItemInDatabase(this, item);
4010 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4011 final int appWidgetId = item.appWidgetId;
4012 if (DEBUG_WIDGETS) {
4013 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4014 + appWidgetInfo.provider);
4017 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4019 appWidgetInfo = null;
4020 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4021 mIsSafeModeEnabled);
4022 view.updateIcon(mIconCache);
4023 item.hostView = view;
4024 item.hostView.updateAppWidget(null);
4025 item.hostView.setOnClickListener(this);
4028 item.hostView.setTag(item);
4029 item.onBindAppWidget(this);
4031 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
4032 item.cellY, item.spanX, item.spanY, false);
4033 if (!item.isCustomWidget()) {
4034 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4037 workspace.requestLayout();
4039 if (DEBUG_WIDGETS) {
4040 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4041 + (SystemClock.uptimeMillis()-start) + "ms");
4046 * Restores a pending widget.
4048 * @param appWidgetId The app widget id
4049 * @param cellInfo The position on screen where to create the widget.
4051 private void completeRestoreAppWidget(final int appWidgetId) {
4052 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4053 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4054 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4058 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
4059 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4061 mWorkspace.reinflateWidgetsIfNecessary();
4062 LauncherModel.updateItemInDatabase(this, info);
4065 public void onPageBoundSynchronously(int page) {
4066 mSynchronouslyBoundPages.add(page);
4070 * Callback saying that there aren't any more items to bind.
4072 * Implementation of the method from LauncherModel.Callbacks.
4074 public void finishBindingItems() {
4075 Runnable r = new Runnable() {
4077 finishBindingItems();
4080 if (waitUntilResume(r)) {
4083 if (mSavedState != null) {
4084 if (!mWorkspace.hasFocus()) {
4085 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
4090 mWorkspace.restoreInstanceStateForRemainingPages();
4092 setWorkspaceLoading(false);
4093 sendLoadingCompleteBroadcastIfNecessary();
4095 // If we received the result of any pending adds while the loader was running (e.g. the
4096 // widget configuration forced an orientation change), process them now.
4097 if (sPendingAddItem != null) {
4098 final long screenId = completeAdd(sPendingAddItem);
4100 // TODO: this moves the user to the page where the pending item was added. Ideally,
4101 // the screen would be guaranteed to exist after bind, and the page would be set through
4102 // the workspace restore process.
4103 mWorkspace.post(new Runnable() {
4106 mWorkspace.snapToScreenId(screenId);
4109 sPendingAddItem = null;
4112 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
4114 if (mLauncherCallbacks != null) {
4115 mLauncherCallbacks.finishBindingItems(false);
4119 private void sendLoadingCompleteBroadcastIfNecessary() {
4120 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4122 getResources().getString(R.string.receive_first_load_broadcast_permission);
4123 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4124 sendBroadcast(intent, permission);
4125 SharedPreferences.Editor editor = mSharedPrefs.edit();
4126 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4131 public boolean isAllAppsButtonRank(int rank) {
4132 if (mHotseat != null) {
4133 return mHotseat.isAllAppsButtonRank(rank);
4138 private boolean canRunNewAppsAnimation() {
4139 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4140 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4143 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4144 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4145 PropertyValuesHolder.ofFloat("alpha", 1f),
4146 PropertyValuesHolder.ofFloat("scaleX", 1f),
4147 PropertyValuesHolder.ofFloat("scaleY", 1f));
4148 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4149 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4150 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
4154 public boolean useVerticalBarLayout() {
4155 return mDeviceProfile.isVerticalBarLayout();
4158 protected Rect getSearchBarBounds() {
4159 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
4162 public void bindSearchablesChanged() {
4163 if (mSearchDropTargetBar == null) {
4167 mSearchDropTargetBar.removeView(mQsb);
4170 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
4174 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4175 * multiple calls to bind the same list.)
4177 @Thunk ArrayList<AppInfo> mTmpAppsList;
4178 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4180 bindAllApplications(mTmpAppsList);
4181 mTmpAppsList = null;
4186 * Add the icons for all apps.
4188 * Implementation of the method from LauncherModel.Callbacks.
4190 public void bindAllApplications(final ArrayList<AppInfo> apps) {
4191 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4192 mTmpAppsList = apps;
4196 if (mAppsView != null) {
4197 mAppsView.setApps(apps);
4199 if (mLauncherCallbacks != null) {
4200 mLauncherCallbacks.bindAllApplications(apps);
4205 * A package was updated.
4207 * Implementation of the method from LauncherModel.Callbacks.
4209 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
4210 Runnable r = new Runnable() {
4212 bindAppsUpdated(apps);
4215 if (waitUntilResume(r)) {
4219 if (mAppsView != null) {
4220 mAppsView.updateApps(apps);
4225 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4226 Runnable r = new Runnable() {
4228 bindWidgetsRestored(widgets);
4231 if (waitUntilResume(r)) {
4234 mWorkspace.widgetsRestored(widgets);
4238 * Some shortcuts were updated in the background.
4240 * Implementation of the method from LauncherModel.Callbacks.
4243 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4244 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
4245 Runnable r = new Runnable() {
4247 bindShortcutsChanged(updated, removed, user);
4250 if (waitUntilResume(r)) {
4254 if (!updated.isEmpty()) {
4255 mWorkspace.updateShortcuts(updated);
4258 if (!removed.isEmpty()) {
4259 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4260 for (ShortcutInfo si : removed) {
4261 removedComponents.add(si.getTargetComponent());
4263 mWorkspace.removeItemsByComponentName(removedComponents, user);
4264 // Notify the drag controller
4265 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
4270 * Update the state of a package, typically related to install state.
4272 * Implementation of the method from LauncherModel.Callbacks.
4275 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4276 Runnable r = new Runnable() {
4278 bindRestoreItemsChange(updates);
4281 if (waitUntilResume(r)) {
4285 mWorkspace.updateRestoreItems(updates);
4289 * A package was uninstalled. We take both the super set of packageNames
4290 * in addition to specific applications to remove, the reason being that
4291 * this can be called when a package is updated as well. In that scenario,
4292 * we only remove specific components from the workspace, where as
4293 * package-removal should clear all items by package name.
4295 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
4296 * Implementation of the method from LauncherModel.Callbacks.
4299 public void bindComponentsRemoved(final ArrayList<String> packageNames,
4300 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
4301 Runnable r = new Runnable() {
4303 bindComponentsRemoved(packageNames, appInfos, user, reason);
4306 if (waitUntilResume(r)) {
4311 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4312 for (AppInfo info : appInfos) {
4313 removedComponents.add(info.componentName);
4315 if (!packageNames.isEmpty()) {
4316 mWorkspace.removeItemsByPackageName(packageNames, user);
4318 if (!removedComponents.isEmpty()) {
4319 mWorkspace.removeItemsByComponentName(removedComponents, user);
4321 // Notify the drag controller
4322 mDragController.onAppsRemoved(packageNames, removedComponents);
4325 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
4329 if (mAppsView != null) {
4330 mAppsView.removeApps(appInfos);
4334 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
4336 bindAllPackages(mWidgetsModel);
4341 public void bindAllPackages(final WidgetsModel model) {
4342 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4343 mWidgetsModel = model;
4347 if (mWidgetsView != null && model != null) {
4348 mWidgetsView.addWidgets(model);
4349 mWidgetsModel = null;
4353 private int mapConfigurationOriActivityInfoOri(int configOri) {
4354 final Display d = getWindowManager().getDefaultDisplay();
4355 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4356 switch (d.getRotation()) {
4357 case Surface.ROTATION_0:
4358 case Surface.ROTATION_180:
4359 // We are currently in the same basic orientation as the natural orientation
4360 naturalOri = configOri;
4362 case Surface.ROTATION_90:
4363 case Surface.ROTATION_270:
4364 // We are currently in the other basic orientation to the natural orientation
4365 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4366 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4371 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4372 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4373 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4374 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4376 // Since the map starts at portrait, we need to offset if this device's natural orientation
4378 int indexOffset = 0;
4379 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4382 return oriMap[(d.getRotation() + indexOffset) % 4];
4385 public void lockScreenOrientation() {
4386 if (mRotationEnabled) {
4387 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4388 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4389 .getConfiguration().orientation));
4391 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4396 public void unlockScreenOrientation(boolean immediate) {
4397 if (mRotationEnabled) {
4399 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4401 mHandler.postDelayed(new Runnable() {
4403 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4405 }, mRestoreScreenOrientationDelay);
4410 protected boolean isLauncherPreinstalled() {
4411 if (mLauncherCallbacks != null) {
4412 return mLauncherCallbacks.isLauncherPreinstalled();
4414 PackageManager pm = getPackageManager();
4416 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4417 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4422 } catch (NameNotFoundException e) {
4423 e.printStackTrace();
4429 * This method indicates whether or not we should suggest default wallpaper dimensions
4430 * when our wallpaper cropper was not yet used to set a wallpaper.
4432 protected boolean overrideWallpaperDimensions() {
4433 if (mLauncherCallbacks != null) {
4434 return mLauncherCallbacks.overrideWallpaperDimensions();
4440 * To be overridden by subclasses to indicate that there is an activity to launch
4441 * before showing the standard launcher experience.
4443 protected boolean hasFirstRunActivity() {
4444 if (mLauncherCallbacks != null) {
4445 return mLauncherCallbacks.hasFirstRunActivity();
4451 * To be overridden by subclasses to launch any first run activity
4453 protected Intent getFirstRunActivity() {
4454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.getFirstRunActivity();
4461 * Returns whether the launcher callbacks overrides search in all apps.
4463 @Thunk boolean isAllAppsSearchOverridden() {
4464 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.overrideAllAppsSearch();
4474 private boolean shouldRunFirstRunActivity() {
4475 return !ActivityManager.isRunningInTestHarness() &&
4476 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4479 protected boolean hasRunFirstRunActivity() {
4480 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4483 public boolean showFirstRunActivity() {
4484 if (shouldRunFirstRunActivity() &&
4485 hasFirstRunActivity()) {
4486 Intent firstRunIntent = getFirstRunActivity();
4487 if (firstRunIntent != null) {
4488 startActivity(firstRunIntent);
4489 markFirstRunActivityShown();
4496 private void markFirstRunActivityShown() {
4497 SharedPreferences.Editor editor = mSharedPrefs.edit();
4498 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4503 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4504 * screen that must be displayed and dismissed.
4506 protected boolean hasDismissableIntroScreen() {
4507 if (mLauncherCallbacks != null) {
4508 return mLauncherCallbacks.hasDismissableIntroScreen();
4514 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4516 protected View getIntroScreen() {
4517 if (mLauncherCallbacks != null) {
4518 return mLauncherCallbacks.getIntroScreen();
4524 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4525 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4527 private boolean shouldShowIntroScreen() {
4528 return hasDismissableIntroScreen() &&
4529 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4532 protected void showIntroScreen() {
4533 View introScreen = getIntroScreen();
4534 changeWallpaperVisiblity(false);
4535 if (introScreen != null) {
4536 mDragLayer.showOverlayView(introScreen);
4538 if (mLauncherOverlayContainer != null) {
4539 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4543 public void dismissIntroScreen() {
4544 markIntroScreenDismissed();
4545 if (showFirstRunActivity()) {
4546 // We delay hiding the intro view until the first run activity is showing. This
4548 mWorkspace.postDelayed(new Runnable() {
4551 mDragLayer.dismissOverlayView();
4552 if (mLauncherOverlayContainer != null) {
4553 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4555 showFirstRunClings();
4557 }, ACTIVITY_START_DELAY);
4559 mDragLayer.dismissOverlayView();
4560 if (mLauncherOverlayContainer != null) {
4561 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4563 showFirstRunClings();
4565 changeWallpaperVisiblity(true);
4568 private void markIntroScreenDismissed() {
4569 SharedPreferences.Editor editor = mSharedPrefs.edit();
4570 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4574 @Thunk void showFirstRunClings() {
4575 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4576 // on the device, then we always show the first run cling experience (or if there is no
4577 // launcher2). Otherwise, we prompt the user upon started for migration
4578 LauncherClings launcherClings = new LauncherClings(this);
4579 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4580 if (mModel.canMigrateFromOldLauncherDb(this)) {
4581 launcherClings.showMigrationCling();
4583 launcherClings.showLongPressCling(true);
4588 void showWorkspaceSearchAndHotseat() {
4589 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4590 if (mHotseat != null) mHotseat.setAlpha(1f);
4591 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4592 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
4595 void hideWorkspaceSearchAndHotseat() {
4596 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4597 if (mHotseat != null) mHotseat.setAlpha(0f);
4598 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4599 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
4602 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4603 // Called from search suggestion, not supported in other profiles.
4604 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4605 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4606 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4608 if (activityInfo == null) {
4611 return new AppInfo(this, activityInfo, myUser, mIconCache);
4614 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4616 // Called from search suggestion, not supported in other profiles.
4617 return createShortcutDragInfo(shortcutIntent, caption, icon,
4618 UserHandleCompat.myUserHandle());
4621 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4622 Bitmap icon, UserHandleCompat user) {
4623 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
4624 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
4625 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
4628 protected void moveWorkspaceToDefaultScreen() {
4629 mWorkspace.moveToDefaultScreen(false);
4632 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4633 dragView.setTag(dragInfo);
4634 mWorkspace.onExternalDragStartedWithItem(dragView);
4635 mWorkspace.beginExternalDragShared(dragView, source);
4639 public void onPageSwitch(View newPage, int newPageIndex) {
4640 if (mLauncherCallbacks != null) {
4641 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4646 * Returns a FastBitmapDrawable with the icon, accurately sized.
4648 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4649 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4650 d.setFilterBitmap(true);
4651 resizeIconDrawable(d);
4656 * Resizes an icon drawable to the correct icon size.
4658 public void resizeIconDrawable(Drawable icon) {
4659 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4663 * Prints out out state for debugging.
4665 public void dumpState() {
4666 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
4667 Log.d(TAG, "mSavedState=" + mSavedState);
4668 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4669 Log.d(TAG, "mRestoring=" + mRestoring);
4670 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4671 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
4672 Log.d(TAG, "sFolders.size=" + sFolders.size());
4674 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
4676 Log.d(TAG, "END launcher3 dump state");
4680 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4681 super.dump(prefix, fd, writer, args);
4682 synchronized (sDumpLogs) {
4683 writer.println(" ");
4684 writer.println("Debug logs: ");
4685 for (int i = 0; i < sDumpLogs.size(); i++) {
4686 writer.println(" " + sDumpLogs.get(i));
4689 if (mLauncherCallbacks != null) {
4690 mLauncherCallbacks.dump(prefix, fd, writer, args);
4694 public static void dumpDebugLogsToConsole() {
4695 if (DEBUG_DUMP_LOG) {
4696 synchronized (sDumpLogs) {
4698 Log.d(TAG, "*********************");
4699 Log.d(TAG, "Launcher debug logs: ");
4700 for (int i = 0; i < sDumpLogs.size(); i++) {
4701 Log.d(TAG, " " + sDumpLogs.get(i));
4703 Log.d(TAG, "*********************");
4709 public static void addDumpLog(String tag, String log, boolean debugLog) {
4710 addDumpLog(tag, log, null, debugLog);
4713 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
4721 if (DEBUG_DUMP_LOG) {
4722 sDateStamp.setTime(System.currentTimeMillis());
4723 synchronized (sDumpLogs) {
4724 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4725 + (e == null ? "" : (", Exception: " + e)));
4730 public static CustomAppWidget getCustomAppWidget(String name) {
4731 return sCustomAppWidgets.get(name);
4734 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4735 return sCustomAppWidgets;
4738 public void dumpLogsToLocalData() {
4739 if (DEBUG_DUMP_LOG) {
4740 new AsyncTask<Void, Void, Void>() {
4741 public Void doInBackground(Void ... args) {
4742 boolean success = false;
4743 sDateStamp.setTime(sRunStart);
4744 String FILENAME = sDateStamp.getMonth() + "-"
4745 + sDateStamp.getDay() + "_"
4746 + sDateStamp.getHours() + "-"
4747 + sDateStamp.getMinutes() + "_"
4748 + sDateStamp.getSeconds() + ".txt";
4750 FileOutputStream fos = null;
4751 File outFile = null;
4753 outFile = new File(getFilesDir(), FILENAME);
4754 outFile.createNewFile();
4755 fos = new FileOutputStream(outFile);
4756 } catch (Exception e) {
4757 e.printStackTrace();
4760 PrintWriter writer = new PrintWriter(fos);
4762 writer.println(" ");
4763 writer.println("Debug logs: ");
4764 synchronized (sDumpLogs) {
4765 for (int i = 0; i < sDumpLogs.size(); i++) {
4766 writer.println(" " + sDumpLogs.get(i));
4776 } catch (IOException e) {
4777 e.printStackTrace();
4781 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
4786 interface DebugIntents {
4787 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4788 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";