blob: a2f26a33a12e8cca6e877e0d5995018cb5c2fd4b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070038import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700109import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700111import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700112import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700114import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700115import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700116
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700136 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700155 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
156 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
157 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
158
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700159 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
160
Mathew Inwood876a8462013-06-14 14:12:41 +0100161 /**
162 * IntentStarter uses request codes starting with this. This must be greater than all activity
163 * request codes used internally.
164 */
165 protected static final int REQUEST_LAST = 100;
166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700179 // Type: Content Values / parcelable
180 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: parcelable
182 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000183 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Adam Cohen432609a2014-03-13 17:03:22 -0700186 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800187 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
188
Adam Cohen3ed316a2014-07-23 18:21:20 -0700189 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
190 static final String ACTION_FIRST_LOAD_COMPLETE =
191 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
192
Adam Cohen39a06042013-07-19 14:30:12 -0700193 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700194 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700195
Sunny Goyal594d76d2014-11-06 10:12:54 -0800196 private static final String QSB_WIDGET_ID = "qsb_widget_id";
197 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
198
Winson Chunga6945242014-01-08 14:04:34 -0800199 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenc2d6e892014-10-16 09:49:24 -0700210 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
211 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700212 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800229 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700232 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Sunny Goyalffe83f12014-08-14 17:39:34 -0700234 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700235 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700237 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800238 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800239 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700240
Michael Jurka0280c3b2010-09-17 15:00:07 -0700241 private int[] mTmpAddItemCellCoordinates = new int[2];
242
Sunny Goyal316490e2015-06-02 09:38:28 -0700243 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800244 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700245
Michael Jurka838a4ca2011-02-07 13:33:06 -0800246 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700247 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700248
Winson Chungc51db6a2011-10-05 11:44:49 -0700249 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700280 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800281 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700282 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
Sunny Goyal639e9062015-08-19 19:17:06 -0700285 private LauncherClings mClings;
286
Sunny Goyale2df0622015-04-24 11:27:00 -0700287 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Adam Cohen61f560d2013-09-30 15:58:20 -0700289 private View.OnTouchListener mHapticFeedbackTouchListener;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700293 private static final int ADVANCE_INTERVAL = 20000;
294 private static final int ADVANCE_STAGGER = 250;
295
296 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700310 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700311 static Date sDateStamp = new Date();
312 static DateFormat sDateFormat =
313 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
314 static long sRunStart = System.currentTimeMillis();
315 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700316
Winson Chung46353de2012-02-16 14:05:10 -0800317 // We only want to get the SharedPreferences once since it does an FS stat each time we get
318 // it from the context.
319 private SharedPreferences mSharedPrefs;
320
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 // Holds the page that we need to animate to, and the icon views that we need to animate up
322 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private Bitmap mFolderIconBitmap;
325 private Canvas mFolderIconCanvas;
326 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700328 private DeviceProfile mDeviceProfile;
329
Winson Chung13eb5272015-05-11 16:30:13 -0700330 // This is set to the view that launched the activity that navigated the user away from
331 // launcher. Since there is no callback for when the activity has finished launching, enable
332 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
336 new HashMap<String, CustomAppWidget>();
337
338 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
339 static {
340 if (ENABLE_CUSTOM_WIDGET_TEST) {
341 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
342 }
343 }
344
Adam Cohen091440a2015-03-18 14:16:05 -0700345 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700347 if (mWorkspace != null) {
348 mWorkspace.buildPageHardwareLayers();
349 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 }
351 };
352
Adam Cohendb364c32014-05-20 14:23:40 -0700353 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354
Adam Cohen091440a2015-03-18 14:16:05 -0700355 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int requestCode;
357 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700358 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700359 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int cellX;
361 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700362 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 }
364
Daniel Sandlerff02d492013-08-05 02:12:05 -0400365 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700366 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400368
369 @Thunk void setOrientation() {
370 if (mRotationEnabled) {
371 unlockScreenOrientation(true);
372 } else {
373 setRequestedOrientation(
374 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700375 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400376 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377
Sunny Goyal7779d622015-06-11 16:18:39 -0700378 private Runnable mUpdateOrientationRunnable = new Runnable() {
379 public void run() {
380 setOrientation();
381 }
382 };
383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
400
Adam Cohen9211d422014-10-07 18:14:53 -0700401 if (mLauncherCallbacks != null) {
402 mLauncherCallbacks.preOnCreate();
403 }
404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400408 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Adam Cohen2e6da152015-05-06 11:42:25 -0700410 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700411 mDeviceProfile = getResources().getConfiguration().orientation
412 == Configuration.ORIENTATION_LANDSCAPE ?
413 app.getInvariantDeviceProfile().landscapeProfile
414 : app.getInvariantDeviceProfile().portraitProfile;
415
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700417 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700418 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800419 mModel = app.setLauncher(this);
420 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700421
Joe Onorato41a12d22009-10-31 18:30:00 -0400422 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700423 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Daniel Sandlerff02d492013-08-05 02:12:05 -0400425 mStats = new Stats(this);
426
Sunny Goyalffe83f12014-08-14 17:39:34 -0700427 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200438 android.os.Debug.startMethodTracing(
439 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700445 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
452 if (PROFILE_STARTUP) {
453 android.os.Debug.stopMethodTracing();
454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467
468 // For handling default keys
469 mDefaultKeySsb = new SpannableStringBuilder();
470 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800471
472 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
473 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800474
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
476 // In case we are on a device with locked rotation, we should look at preferences to check
477 // if the user has specifically allowed rotation.
478 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700479 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 }
481
482 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
483 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400484 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700485
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 if (mLauncherCallbacks.hasLauncherOverlay()) {
489 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700490 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
491 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
492 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 mWorkspace.setLauncherOverlay(mLauncherOverlay);
494 }
Adam Cohen9211d422014-10-07 18:14:53 -0700495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 showFirstRunClings();
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
504
Sunny Goyal7779d622015-06-11 16:18:39 -0700505 @Override
506 public void onSettingsChanged(String settings, boolean value) {
507 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
508 mRotationEnabled = value;
509 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
510 mUpdateOrientationRunnable.run();
511 }
512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
524 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
525 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700526 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700527 private boolean mWorkspaceImportanceStored = false;
528 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 private int mWorkspaceImportanceForAccessibility =
530 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
531 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
532
533 @Override
534 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 return;
537 }
538 // The underlying workspace and hotseat are temporarily suppressed by the search
539 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspace != null) {
541 mWorkspaceImportanceForAccessibility =
542 mWorkspace.getImportantForAccessibility();
543 mWorkspace.setImportantForAccessibility(
544 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
545 mWorkspaceImportanceStored = true;
546 }
547 if (mHotseat != null) {
548 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
549 mHotseat.setImportantForAccessibility(
550 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
551 mHotseatImportanceStored = true;
552 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 }
554
555 @Override
556 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspaceImportanceStored && mWorkspace != null) {
558 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
559 }
560 if (mHotseatImportanceStored && mHotseat != null) {
561 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
562 }
563 mWorkspaceImportanceStored = false;
564 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566 });
Adam Cohen9211d422014-10-07 18:14:53 -0700567 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700568 }
569
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700570 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700571 public void onLauncherProviderChange() {
572 if (mLauncherCallbacks != null) {
573 mLauncherCallbacks.onLauncherProviderChange();
574 }
575 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700576
Winson Chungef7f8742015-06-04 17:18:17 -0700577 /**
578 * Updates the bounds of all the overlays to match the new fixed bounds.
579 */
580 public void updateOverlayBounds(Rect newBounds) {
581 mAppsView.setSearchBarBounds(newBounds);
582 mWidgetsView.setSearchBarBounds(newBounds);
583 }
584
Adam Cohen9211d422014-10-07 18:14:53 -0700585 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700586 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700587 if (mLauncherCallbacks != null) {
588 return mLauncherCallbacks.hasCustomContentToLeft();
589 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 return false;
591 }
592
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500594 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 * {@link #addToCustomContentPage}. This will only be invoked if
596 * {@link #hasCustomContentToLeft()} is {@code true}.
597 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700599 if (mLauncherCallbacks != null) {
600 mLauncherCallbacks.populateCustomContentContainer();
601 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 }
603
604 /**
605 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
606 * ensure the custom content page is added or removed if necessary.
607 */
608 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600609 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 // Not bound yet, wait for bindScreens to be called.
611 return;
612 }
613
614 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
615 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500616 mWorkspace.createCustomContentContainer();
617 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
619 mWorkspace.removeCustomContentPage();
620 }
621 }
622
Anton Hanssona2f665f2013-09-26 12:18:32 +0100623 public Stats getStats() {
624 return mStats;
625 }
626
Hyunyoung Song3f471442015-04-08 19:01:34 -0700627 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700628 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
629 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700630 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700631 }
632
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000633 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700634 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
635 // This cast is safe as long as the id < 0x00FFFFFF
636 // Since we jail all the dynamically generated views, there should be no clashes
637 // with any other views.
638 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000639 }
640
641 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700642 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
643 * a configuration step, this allows the proper animations to run after other transitions.
644 */
Adam Cohendb364c32014-05-20 14:23:40 -0700645 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700646 long screenId = args.screenId;
647 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
648 // When the screen id represents an actual screen (as opposed to a rank) we make sure
649 // that the drop page actually exists.
650 screenId = ensurePendingDropLayoutExists(args.screenId);
651 }
Adam Cohendb364c32014-05-20 14:23:40 -0700652
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700655 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700657 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700659 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700660 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700661 case REQUEST_RECONFIGURE_APPWIDGET:
662 completeRestoreAppWidget(args.appWidgetId);
663 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 }
Michael Jurka27614d22012-04-02 06:40:22 -0700665 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
666 // if you turned the screen off and then back while in All Apps, Launcher would not
667 // return to the workspace. Clearing mAddInfo.container here fixes this issue
668 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700669 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 }
671
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800672 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700674 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700675 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700676 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800677 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700678
Adam Cohenad4e15c2013-10-17 16:21:35 -0700679 Runnable exitSpringLoaded = new Runnable() {
680 @Override
681 public void run() {
682 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
683 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
684 }
685 };
686
Michael Jurka8b805b12012-04-18 14:23:14 -0700687 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700688 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700689 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700690 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
691 if (resultCode == RESULT_CANCELED) {
692 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700693 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700694 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700695 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800696 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700697 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700698
699 // When the user has granted permission to bind widgets, we should check to see if
700 // we can inflate the default search bar widget.
701 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 }
703 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200704 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
705 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700706 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200707 }
708 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700709 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200710
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700712 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700713
Adam Cohendb364c32014-05-20 14:23:40 -0700714 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 // We have special handling for widgets
716 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800717 final int appWidgetId;
718 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
719 : -1;
720 if (widgetId < 0) {
721 appWidgetId = pendingAddWidgetId;
722 } else {
723 appWidgetId = widgetId;
724 }
725
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800727 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700728 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
729 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 result = RESULT_CANCELED;
731 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700732 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 @Override
734 public void run() {
735 exitSpringLoadedDragModeDelayed(false, 0, null);
736 }
737 };
Adam Cohendb364c32014-05-20 14:23:40 -0700738 if (workspaceLocked) {
739 // No need to remove the empty screen if we're mid-binding, as the
740 // the bind will not add the empty screen.
741 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
742 } else {
743 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
744 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
745 }
Winson Chung5aaab772012-04-27 15:24:02 -0700746 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700747 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700748 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
749 // When the screen id represents an actual screen (as opposed to a rank)
750 // we make sure that the drop page actually exists.
751 mPendingAddInfo.screenId =
752 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
753 }
Adam Cohendb364c32014-05-20 14:23:40 -0700754 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
755
756 dropLayout.setDropPending(true);
757 final Runnable onComplete = new Runnable() {
758 @Override
759 public void run() {
760 completeTwoStageWidgetDrop(resultCode, appWidgetId);
761 dropLayout.setDropPending(false);
762 }
763 };
764 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
765 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
766 } else {
767 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
768 mPendingAddInfo);
769 sPendingAddItem = args;
770 }
Winson Chung5aaab772012-04-27 15:24:02 -0700771 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800772 return;
773 }
774
Sunny Goyalff572272014-07-23 13:58:07 -0700775 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
776 if (resultCode == RESULT_OK) {
777 // Update the widget view.
778 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
779 pendingAddWidgetId, mPendingAddInfo);
780 if (workspaceLocked) {
781 sPendingAddItem = args;
782 } else {
783 completeAdd(args);
784 }
785 }
786 // Leave the widget in the pending state if the user canceled the configure.
787 return;
788 }
789
Sunny Goyalaad90582015-07-09 14:22:50 -0700790 if (requestCode == REQUEST_CREATE_SHORTCUT) {
791 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
792 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
793 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
794 mPendingAddInfo);
795 if (isWorkspaceLocked()) {
796 sPendingAddItem = args;
797 } else {
798 completeAdd(args);
799 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
800 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
801 }
802 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700803 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800808
809 }
810
811 @Override
812 protected void onActivityResult(
813 final int requestCode, final int resultCode, final Intent data) {
814 handleActivityResult(requestCode, resultCode, data);
815 if (mLauncherCallbacks != null) {
816 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
817 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800818 }
819
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600820 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700821 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600822 int[] grantResults) {
823 if (mLauncherCallbacks != null) {
824 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
825 grantResults);
826 }
827 }
828
Adam Cohendb364c32014-05-20 14:23:40 -0700829 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
830 appWidgetId, ItemInfo info) {
831 PendingAddArguments args = new PendingAddArguments();
832 args.requestCode = requestCode;
833 args.intent = data;
834 args.container = info.container;
835 args.screenId = info.screenId;
836 args.cellX = info.cellX;
837 args.cellY = info.cellY;
838 args.appWidgetId = appWidgetId;
839 return args;
840 }
841
842 /**
843 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
844 *
845 * @param screenId the screen id to check
846 * @return the new screen, or screenId if it exists
847 */
848 private long ensurePendingDropLayoutExists(long screenId) {
849 CellLayout dropLayout =
850 (CellLayout) mWorkspace.getScreenWithId(screenId);
851 if (dropLayout == null) {
852 // it's possible that the add screen was removed because it was
853 // empty and a re-bind occurred
854 mWorkspace.addExtraEmptyScreen();
855 return mWorkspace.commitExtraEmptyScreen();
856 } else {
857 return screenId;
858 }
859 }
860
Adam Cohen091440a2015-03-18 14:16:05 -0700861 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700862 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 Runnable onCompleteRunnable = null;
865 int animationType = 0;
866
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700867 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 if (resultCode == RESULT_OK) {
869 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
870 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700871 mPendingAddWidgetInfo);
872 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 onCompleteRunnable = new Runnable() {
874 @Override
875 public void run() {
876 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
879 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 }
881 };
882 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800883 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700886 if (mDragLayer.getAnimatedView() != null) {
887 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
888 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
889 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 // The animated view may be null in the case of a rotation during widget configuration
892 onCompleteRunnable.run();
893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700897 protected void onStop() {
898 super.onStop();
899 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700900
901 if (mLauncherCallbacks != null) {
902 mLauncherCallbacks.onStop();
903 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700904 }
905
906 @Override
907 protected void onStart() {
908 super.onStart();
909 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700910
911 if (mLauncherCallbacks != null) {
912 mLauncherCallbacks.onStart();
913 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700914 }
915
916 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200918 long startTime = 0;
919 if (DEBUG_RESUME_TIME) {
920 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400921 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200922 }
Adam Cohen9211d422014-10-07 18:14:53 -0700923
924 if (mLauncherCallbacks != null) {
925 mLauncherCallbacks.preOnResume();
926 }
927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700929
Winson Chung4a2afa32012-07-19 14:53:05 -0700930 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700931 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700932 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800933 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700934 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700935 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700936 // view after launching an app, as they may be depending on the UI to be static to
937 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700938 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700939 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800940 } else if (mOnResumeState == State.WIDGETS) {
941 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700942 }
943 mOnResumeState = State.NONE;
944
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700945 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700946 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
947 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700948
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800949 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700950 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700951 setWorkspaceLoading(true);
Sunny Goyal22aa3812015-08-19 16:24:27 -0700952
953 // If we're starting binding all over again, clear any bind calls we'd postponed in
954 // the past (see waitUntilResume) -- we don't need them since we're starting binding
955 // from scratch again
956 mBindOnResumeCallbacks.clear();
957
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700958 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700960 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700962 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200963 // We might have postponed some bind calls until onResume (see waitUntilResume) --
964 // execute them here
965 long startTimeCallbacks = 0;
966 if (DEBUG_RESUME_TIME) {
967 startTimeCallbacks = System.currentTimeMillis();
968 }
969
Michael Jurka1e2f4652013-07-08 18:03:46 -0700970 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
971 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200972 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700973 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200974 if (DEBUG_RESUME_TIME) {
975 Log.d(TAG, "Time spent processing callbacks in onResume: " +
976 (System.currentTimeMillis() - startTimeCallbacks));
977 }
Michael Jurka447bf842013-05-15 14:52:15 +0200978 }
Michael Jurka54554252013-08-01 12:52:23 +0200979 if (mOnResumeCallbacks.size() > 0) {
980 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
981 mOnResumeCallbacks.get(i).run();
982 }
983 mOnResumeCallbacks.clear();
984 }
Winson Chunge4e50662012-01-23 14:45:13 -0800985
986 // Reset the pressed state of icons that were locked in the press state while activities
987 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800988 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800989 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800990 mWaitingForResume.setStayPressed(false);
991 }
Winson Chung82963d52013-10-09 11:20:57 -0700992
Adam Cohen06dff352012-06-01 17:17:08 -0700993 // It is possible that widgets can receive updates while launcher is not in the foreground.
994 // Consequently, the widgets will be inflated in the orientation of the foreground activity
995 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
996 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700997 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -0700998 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700999
Michael Jurka447bf842013-05-15 14:52:15 +02001000 if (DEBUG_RESUME_TIME) {
1001 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1002 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001003
1004 if (mWorkspace.getCustomContentCallbacks() != null) {
1005 // If we are resuming and the custom content is the current page, we call onShow().
1006 // It is also poassible that onShow will instead be called slightly after first layout
1007 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1008 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001009 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001010 }
1011 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001012 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001013 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001014
Sunny Goyal756adbc2015-04-16 15:20:51 -07001015 if (!isWorkspaceLoading()) {
1016 // Process any items that were added while Launcher was away.
1017 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1018 }
Adam Cohen9211d422014-10-07 18:14:53 -07001019
1020 if (mLauncherCallbacks != null) {
1021 mLauncherCallbacks.onResume();
1022 }
Adam Cohen06dff352012-06-01 17:17:08 -07001023 }
1024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001027 // Ensure that items added to Launcher are queued until Launcher returns
1028 InstallShortcutReceiver.enableInstallQueue();
1029
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001031 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001032 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001033 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001034
1035 // We call onHide() aggressively. The custom content callbacks should be able to
1036 // debounce excess onHide calls.
1037 if (mWorkspace.getCustomContentCallbacks() != null) {
1038 mWorkspace.getCustomContentCallbacks().onHide();
1039 }
Romain Guycbb89e42009-06-08 15:52:54 -07001040
Adam Cohen9211d422014-10-07 18:14:53 -07001041 if (mLauncherCallbacks != null) {
1042 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001043 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001044 }
1045
1046 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001047 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1048 // by a onResume or by scrolling otherwise.
1049 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001050
1051 // Custom content is completely hidden
1052 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001053
1054 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1055 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001056
1057 // Indicates whether the user is allowed to scroll away from the custom content.
1058 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001059 }
1060
Adam Cohenc2d6e892014-10-16 09:49:24 -07001061 public interface LauncherOverlay {
1062
1063 /**
1064 * Touch interaction leading to overscroll has begun
1065 */
1066 public void onScrollInteractionBegin();
1067
1068 /**
1069 * Touch interaction related to overscroll has ended
1070 */
1071 public void onScrollInteractionEnd();
1072
1073 /**
1074 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1075 * screen (or in the case of RTL, the rightmost screen).
1076 */
1077 public void onScrollChange(int progress, boolean rtl);
1078
1079 /**
1080 * Screen has stopped scrolling
1081 */
1082 public void onScrollSettled();
1083
1084 /**
1085 * This method can be called by the Launcher in order to force the LauncherOverlay
1086 * to exit fully immersive mode.
1087 */
1088 public void forceExitFullImmersion();
1089 }
1090
Jun Mukai8af0cd82015-05-12 12:32:12 -07001091 public interface LauncherSearchCallbacks {
1092 /**
1093 * Called when the search overlay is shown.
1094 */
1095 public void onSearchOverlayOpened();
1096
1097 /**
1098 * Called when the search overlay is dismissed.
1099 */
1100 public void onSearchOverlayClosed();
1101 }
1102
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 public interface LauncherOverlayCallbacks {
1104 /**
1105 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1106 * however it doesn't modify any state within the launcher.
1107 */
1108 public boolean canEnterFullImmersion();
1109
1110 /**
1111 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1112 * eg. by occupying the full screen and handling all touch events.
1113 *
1114 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1115 * case, Launcher will modify any necessary state and assumes the overlay is
1116 * handling all interaction. If false, the LauncherOverlay should cancel any
1117 *
1118 */
1119 public boolean enterFullImmersion();
1120
1121 /**
1122 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1123 * full control over UI and state.
1124 */
1125 public void exitFullImmersion();
1126 }
1127
1128 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1129
1130 @Override
1131 public boolean canEnterFullImmersion() {
1132 return mState == State.WORKSPACE;
1133 }
1134
1135 @Override
1136 public boolean enterFullImmersion() {
1137 if (mState == State.WORKSPACE) {
1138 // When fully immersed, disregard any touches which fall through.
1139 mDragLayer.setBlockTouch(true);
1140 return true;
1141 }
1142 return false;
1143 }
1144
1145 @Override
1146 public void exitFullImmersion() {
1147 mDragLayer.setBlockTouch(false);
1148 }
1149 }
1150
Jorim Jaggid017f882014-01-14 17:08:48 -08001151 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001152 if (mLauncherCallbacks != null) {
1153 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001154 } else {
1155 // On devices with a locked orientation, we will at least have the allow rotation
1156 // setting.
1157 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001158 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001159 }
1160
Adam Cohen9211d422014-10-07 18:14:53 -07001161 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001162 CustomContentCallbacks callbacks, String description) {
1163 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001164 }
1165
Adam Cohenedb40762013-07-18 16:45:45 -07001166 // The custom content needs to offset its content to account for the QSB
1167 public int getTopOffsetForCustomContent() {
1168 return mWorkspace.getPaddingTop();
1169 }
1170
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001171 @Override
1172 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001174 if (mModel.isCurrentCallbacks(this)) {
1175 mModel.stopLoader();
1176 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001177 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1178
Romain Guy13c2e7b2010-03-10 19:45:00 -08001179 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001180 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001182 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001183 @Override
1184 public void onWindowFocusChanged(boolean hasFocus) {
1185 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001186 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001187
1188 if (mLauncherCallbacks != null) {
1189 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1190 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 private boolean acceptFilter() {
1194 final InputMethodManager inputManager = (InputMethodManager)
1195 getSystemService(Context.INPUT_METHOD_SERVICE);
1196 return !inputManager.isFullscreenMode();
1197 }
1198
1199 @Override
1200 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001201 final int uniChar = event.getUnicodeChar();
1202 final boolean handled = super.onKeyDown(keyCode, event);
1203 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1204 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1206 keyCode, event);
1207 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001209 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 // showSearchDialog()
1211 // If there are multiple keystrokes before the search dialog takes focus,
1212 // onSearchRequested() will be called for every keystroke,
1213 // but it is idempotent, so it's fine.
1214 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
1217
Joe Onorato8a9625e2010-01-28 15:55:35 -08001218 // Eat the long press event so the keyboard doesn't come up.
1219 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1220 return true;
1221 }
1222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 return handled;
1224 }
1225
Karl Rosaen138a0412009-04-23 19:00:21 -07001226 private String getTypedText() {
1227 return mDefaultKeySsb.toString();
1228 }
1229
1230 private void clearTypedText() {
1231 mDefaultKeySsb.clear();
1232 mDefaultKeySsb.clearSpans();
1233 Selection.setSelection(mDefaultKeySsb, 0);
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001237 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1238 * State
1239 */
1240 private static State intToState(int stateOrdinal) {
1241 State state = State.WORKSPACE;
1242 final State[] stateValues = State.values();
1243 for (int i = 0; i < stateValues.length; i++) {
1244 if (stateValues[i].ordinal() == stateOrdinal) {
1245 state = stateValues[i];
1246 break;
1247 }
1248 }
1249 return state;
1250 }
1251
1252 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 * Restores the previous state, if it exists.
1254 *
1255 * @param savedState The previous state.
1256 */
1257 private void restoreState(Bundle savedState) {
1258 if (savedState == null) {
1259 return;
1260 }
1261
Michael Jurka883f55b2010-10-21 15:47:14 -07001262 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001263 if (state == State.APPS || state == State.WIDGETS) {
1264 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001265 }
1266
Adam Cohen21cd0022013-10-09 18:57:02 -07001267 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1268 PagedView.INVALID_RESTORE_PAGE);
1269 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001270 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
Sunny Goyal756cd262015-08-20 12:33:21 -07001273 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1274 if (itemValues != null) {
1275 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001276 AppWidgetProviderInfo info = savedState.getParcelable(
1277 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001278 mPendingAddWidgetInfo = info == null ?
1279 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1280
Adam Cohen4637b5a2013-11-04 18:21:24 -08001281 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001282 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 mRestoring = true;
1284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
1287 /**
1288 * Finds all the views we need and configure them properly.
1289 */
1290 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001291 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001292
Craig Mautner360310b2012-10-26 15:13:08 -07001293 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001294 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001295 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1296 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001297 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001298 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001299
John Spurlock77e1f472013-09-11 10:09:51 -04001300 mLauncherView.setSystemUiVisibility(
1301 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001302 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303
Winson Chung4c98d922011-05-31 16:50:48 -07001304 // Setup the drag layer
1305 mDragLayer.setup(this, dragController);
1306
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 // Setup the hotseat
1308 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1309 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001310 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 }
1312
Jorim Jaggid017f882014-01-14 17:08:48 -08001313 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Tony Wickhamc75917f2015-09-02 16:11:09 -07001314 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1315 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1316 @Override
1317 public boolean onLongClick(View v) {
1318 return v.performClick();
1319 }
1320 };
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001321 mWidgetsButton = findViewById(R.id.widget_button);
1322 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001323 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001324 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001325 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001326 onClickAddWidgetButton(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001327 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001328 }
1329 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001330 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001331 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001332
1333 View wallpaperButton = findViewById(R.id.wallpaper_button);
1334 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001335 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001336 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001337 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001338 onClickWallpaperPicker(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001339 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001340 }
1341 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001342 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
Adam Cohen61f560d2013-09-30 15:58:20 -07001343 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1344
1345 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001346 if (hasSettings()) {
1347 settingsButton.setOnClickListener(new OnClickListener() {
1348 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001349 public void onClick(View view) {
Jorim Jaggid017f882014-01-14 17:08:48 -08001350 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001351 onClickSettingsButton(view);
Jorim Jaggid017f882014-01-14 17:08:48 -08001352 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001353 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001354 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001355 settingsButton.setOnLongClickListener(performClickOnLongClick);
Jorim Jaggid017f882014-01-14 17:08:48 -08001356 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1357 } else {
1358 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001359 }
1360
Adam Cohendbdff6b2013-09-18 19:09:15 -07001361 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001362
Winson Chung4c98d922011-05-31 16:50:48 -07001363 // Setup the workspace
1364 mWorkspace.setHapticFeedbackEnabled(false);
1365 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001366 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001367 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001368
Winson Chungf0ea4d32011-06-06 14:27:16 -07001369 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001370 mSearchDropTargetBar = (SearchDropTargetBar)
1371 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001372
Winson Chungef7f8742015-06-04 17:18:17 -07001373 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001374 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001375 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001376 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1377 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1378 } else {
1379 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1380 }
Craig Mautner360310b2012-10-26 15:13:08 -07001381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001383 dragController.setDragScoller(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001384 dragController.setMoveTarget(mWorkspace);
1385 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001386 if (mSearchDropTargetBar != null) {
1387 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001388 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001389 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001390
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001391 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001392 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001393 mWeightWatcher = new WeightWatcher(this);
1394 mWeightWatcher.setAlpha(0.5f);
1395 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001396 new FrameLayout.LayoutParams(
1397 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001398 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001399 Gravity.BOTTOM)
1400 );
Adam Cohen39a06042013-07-19 14:30:12 -07001401
1402 boolean show = shouldShowWeightWatcher();
1403 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
1407 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001408 * Sets the all apps button. This method is called from {@link Hotseat}.
1409 */
1410 public void setAllAppsButton(View allAppsButton) {
1411 mAllAppsButton = allAppsButton;
1412 }
1413
1414 public View getAllAppsButton() {
1415 return mAllAppsButton;
1416 }
1417
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001418 public View getWidgetsButton() {
1419 return mWidgetsButton;
1420 }
1421
Anjali Koppal5ad44842014-03-10 20:34:39 -07001422 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 * Creates a view representing a shortcut.
1424 *
1425 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001427 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001428 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430
1431 /**
1432 * Creates a view representing a shortcut inflated from the specified resource.
1433 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 * @param parent The group the shortcut belongs to.
1435 * @param info The data structure describing the shortcut.
1436 *
1437 * @return A View inflated from layoutResId.
1438 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001439 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001440 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001441 parent, false);
1442 favorite.applyFromShortcutInfo(info, mIconCache);
1443 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001445 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 return favorite;
1447 }
1448
1449 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 * Add a shortcut to the workspace.
1451 *
1452 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001454 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001455 int cellY) {
1456 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001457 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001458
Sunny Goyal5c97f512015-05-19 16:03:28 -07001459 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001460 if (info == null) {
1461 return;
1462 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001463 final View view = createShortcut(info);
1464
Sunny Goyal5c97f512015-05-19 16:03:28 -07001465 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001466 // First we check if we already know the exact location where we want to add this item.
1467 if (cellX >= 0 && cellY >= 0) {
1468 cellXY[0] = cellX;
1469 cellXY[1] = cellY;
1470 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001471
1472 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001473 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001474 true, null,null)) {
1475 return;
1476 }
1477 DragObject dragObject = new DragObject();
1478 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001479 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1480 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001481 return;
1482 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001483 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001485 }
1486
1487 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001488 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 return;
1490 }
1491
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001492 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
1494 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001495 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001496 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498 }
1499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001501 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001503 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohen091440a2015-03-18 14:16:05 -07001505 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001506 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1507
1508 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001509 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001510 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1511 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 }
Romain Guycbb89e42009-06-08 15:52:54 -07001513
Adam Cohen59400422014-03-05 18:07:04 -08001514 if (appWidgetInfo.isCustomWidget) {
1515 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001516 }
1517
Adam Cohen59400422014-03-05 18:07:04 -08001518 LauncherAppWidgetInfo launcherInfo;
1519 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1520 launcherInfo.spanX = info.spanX;
1521 launcherInfo.spanY = info.spanY;
1522 launcherInfo.minSpanX = info.minSpanX;
1523 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001524 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001527 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
1529 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 if (hostView == null) {
1531 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001532 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1533 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001534 } else {
1535 // The AppWidgetHostView has already been inflated and instantiated
1536 launcherInfo.hostView = hostView;
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001539 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001540 launcherInfo.notifyWidgetSizeChanged(this);
1541
Adam Cohen59400422014-03-05 18:07:04 -08001542 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1543 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001544
1545 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001547 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Adam Cohended9f8d2010-11-03 13:25:16 -07001550 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1551 @Override
1552 public void onReceive(Context context, Intent intent) {
1553 final String action = intent.getAction();
1554 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1555 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001556 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001557 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001558
Winson Chungc100e8e2011-08-09 16:02:43 -07001559 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001560 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001561 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001562 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001563 if (!showWorkspace(false)) {
1564 // If we are already on the workspace, then manually reset all apps
1565 mAppsView.reset();
1566 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1569 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001570 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001571 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1572 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001573 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001574 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1575 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1576 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001577 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001578 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1579 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 }
1581 }
1582 };
1583
1584 @Override
1585 public void onAttachedToWindow() {
1586 super.onAttachedToWindow();
1587
1588 // Listen for broadcasts related to user-presence
1589 final IntentFilter filter = new IntentFilter();
1590 filter.addAction(Intent.ACTION_SCREEN_OFF);
1591 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001592 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001593 if (ENABLE_DEBUG_INTENTS) {
1594 filter.addAction(DebugIntents.DELETE_DATABASE);
1595 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1596 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001598 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001599 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001600 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 mVisible = true;
1602 }
1603
Adam Cohen0b395352014-06-09 22:54:36 +00001604 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001605 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001606 * This method is a no-op for other platform versions.
1607 */
Sunny Goyald0091012015-01-20 15:55:34 -08001608 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001609 private void setupTransparentSystemBarsForLollipop() {
1610 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001611 Window window = getWindow();
1612 window.getAttributes().systemUiVisibility |=
1613 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1614 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1615 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1616 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1617 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1618 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1619 window.setStatusBarColor(Color.TRANSPARENT);
1620 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001621 }
1622 }
1623
Adam Cohended9f8d2010-11-03 13:25:16 -07001624 @Override
1625 public void onDetachedFromWindow() {
1626 super.onDetachedFromWindow();
1627 mVisible = false;
1628
Adam Cohend113e0c2010-11-11 10:48:05 -08001629 if (mAttached) {
1630 unregisterReceiver(mReceiver);
1631 mAttached = false;
1632 }
Winson Chungb745afb2015-03-02 11:51:23 -08001633 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 }
1635
1636 public void onWindowVisibilityChanged(int visibility) {
1637 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001638 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001639 // The following code used to be in onResume, but it turns out onResume is called when
1640 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1641 // is a more appropriate event to handle
1642 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001643 if (!mWorkspaceLoading) {
1644 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001645 // We want to let Launcher draw itself at least once before we force it to build
1646 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001647 // apps is nice and speedy.
1648 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001649 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001651 if (mStarted) return;
1652 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001653 // We delay the layer building a bit in order to give
1654 // other message processing a time to run. In particular
1655 // this avoids a delay in hiding the IME if it was
1656 // currently shown, because doing that may involve
1657 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001658 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 final ViewTreeObserver.OnDrawListener listener = this;
1660 mWorkspace.post(new Runnable() {
1661 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001662 if (mWorkspace != null &&
1663 mWorkspace.getViewTreeObserver() != null) {
1664 mWorkspace.getViewTreeObserver().
1665 removeOnDrawListener(listener);
1666 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 }
1668 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001669 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 }
1671 });
1672 }
1673 clearTypedText();
1674 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 }
1676
Adam Cohen091440a2015-03-18 14:16:05 -07001677 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 mHandler.removeMessages(ADVANCE_MSG);
1679 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1680 mHandler.sendMessageDelayed(msg, delay);
1681 mAutoAdvanceSentTime = System.currentTimeMillis();
1682 }
1683
Adam Cohen091440a2015-03-18 14:16:05 -07001684 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1686 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1687 mAutoAdvanceRunning = autoAdvanceRunning;
1688 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001689 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 sendAdvanceMessage(delay);
1691 } else {
1692 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001693 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1695 }
1696 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001697 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
1699 }
1700 }
1701
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001702 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1703
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001705 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 if (msg.what == ADVANCE_MSG) {
1707 int i = 0;
1708 for (View key: mWidgetsToAdvance.keySet()) {
1709 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001710 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001712 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 public void run() {
1714 ((Advanceable) v).advance();
1715 }
1716 }, delay);
1717 }
1718 i++;
1719 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001720 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001722 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001725
1726 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001727 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1729 if (v instanceof Advanceable) {
1730 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001731 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001732 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734 }
1735
1736 void removeWidgetToAutoAdvance(View hostView) {
1737 if (mWidgetsToAdvance.containsKey(hostView)) {
1738 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 }
1742
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 launcherInfo.hostView = null;
1746 }
1747
Hyunyoung Song3f471442015-04-08 19:01:34 -07001748 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001749 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1750 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752
Winson Chunga6945242014-01-08 14:04:34 -08001753 public DragLayer getDragLayer() {
1754 return mDragLayer;
1755 }
1756
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001757 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001758 return mAppsView;
1759 }
1760
Hyunyoung Song3f471442015-04-08 19:01:34 -07001761 public WidgetsContainerView getWidgetsView() {
1762 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001763 }
1764
Winson Chunga6945242014-01-08 14:04:34 -08001765 public Workspace getWorkspace() {
1766 return mWorkspace;
1767 }
1768
1769 public Hotseat getHotseat() {
1770 return mHotseat;
1771 }
1772
Jorim Jaggid017f882014-01-14 17:08:48 -08001773 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001774 return mOverviewPanel;
1775 }
1776
Winson Chung006ee262015-08-03 14:40:11 -07001777 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001778 return mSearchDropTargetBar;
1779 }
1780
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001781 public LauncherAppWidgetHost getAppWidgetHost() {
1782 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Winson Chunga9abd0e2010-10-27 17:18:37 -07001785 public LauncherModel getModel() {
1786 return mModel;
1787 }
1788
Winson Chunga6945242014-01-08 14:04:34 -08001789 protected SharedPreferences getSharedPrefs() {
1790 return mSharedPrefs;
1791 }
1792
Adam Cohen2e6da152015-05-06 11:42:25 -07001793 public DeviceProfile getDeviceProfile() {
1794 return mDeviceProfile;
1795 }
1796
Bjorn Bringertc459e522013-06-07 19:36:01 +01001797 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001798 getWindow().closeAllPanels();
1799
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001800 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001801 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001802 }
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 @Override
1805 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001806 long startTime = 0;
1807 if (DEBUG_RESUME_TIME) {
1808 startTime = System.currentTimeMillis();
1809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 super.onNewIntent(intent);
1811
1812 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001813 Folder openFolder = mWorkspace.getOpenFolder();
1814 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1815 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1816 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1817 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1818 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001819 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001820 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001821
Adam Cohen6fecd412013-10-02 17:41:50 -07001822 if (mWorkspace == null) {
1823 // Can be cases where mWorkspace is null, this prevents a NPE
1824 return;
1825 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001826 // In all these cases, only animate if we're already on home
1827 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001828
Adam Cohen6fecd412013-10-02 17:41:50 -07001829 closeFolder();
1830 exitSpringLoadedDragMode();
1831
1832 // If we are already on home, then just animate back to the workspace,
1833 // otherwise, just wait until onResume to set the state back to Workspace
1834 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001835 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001836 } else {
1837 mOnResumeState = State.WORKSPACE;
1838 }
1839
1840 final View v = getWindow().peekDecorView();
1841 if (v != null && v.getWindowToken() != null) {
1842 InputMethodManager imm = (InputMethodManager)getSystemService(
1843 INPUT_METHOD_SERVICE);
1844 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1845 }
1846
Winson Chungb745afb2015-03-02 11:51:23 -08001847 // Reset the apps view
1848 if (!alreadyOnHome && mAppsView != null) {
1849 mAppsView.scrollToTop();
1850 }
1851
Hyunyoung Song3f471442015-04-08 19:01:34 -07001852 // Reset the widgets view
1853 if (!alreadyOnHome && mWidgetsView != null) {
1854 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001856
Adam Cohen9211d422014-10-07 18:14:53 -07001857 if (mLauncherCallbacks != null) {
1858 mLauncherCallbacks.onHomeIntent();
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
Adam Cohened307df2013-10-02 09:37:31 -07001861
Adam Cohen9211d422014-10-07 18:14:53 -07001862 if (mLauncherCallbacks != null) {
1863 mLauncherCallbacks.onNewIntent(intent);
1864 }
Winson15f8b172015-08-19 11:02:39 -07001865
1866 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1867 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1868 // animation.
1869 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001870 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1871 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001872 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1873 openFolder == null && moveToDefaultScreen) {
1874 mWorkspace.post(new Runnable() {
1875 @Override
1876 public void run() {
1877 mWorkspace.moveToDefaultScreen(true);
1878 }
1879 });
1880 }
1881 }
1882
1883 if (DEBUG_RESUME_TIME) {
1884 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1885 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001889 public void onRestoreInstanceState(Bundle state) {
1890 super.onRestoreInstanceState(state);
1891 for (int page: mSynchronouslyBoundPages) {
1892 mWorkspace.restoreInstanceStateForChild(page);
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
1895
1896 @Override
1897 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001898 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001899 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1900 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001901 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001902 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
Michael Jurka883f55b2010-10-21 15:47:14 -07001904 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001905 // We close any open folder since it will not be re-opened, and we need to make sure
1906 // this state is reflected.
1907 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908
Adam Cohendcd297f2013-06-18 13:13:40 -07001909 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001911 ContentValues itemValues = new ContentValues();
1912 mPendingAddInfo.writeToValues(itemValues);
1913 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001914 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001915 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
Hyunyoung Song3f471442015-04-08 19:01:34 -07001918 // Save the current widgets tray?
1919 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001920
1921 if (mLauncherCallbacks != null) {
1922 mLauncherCallbacks.onSaveInstanceState(outState);
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
1926 @Override
1927 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001929
Winson Chunge7a03942011-08-05 15:05:12 -07001930 // Remove all pending runnables
1931 mHandler.removeMessages(ADVANCE_MSG);
1932 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001933 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001934
Winson Chungcd2b0142011-06-08 16:02:26 -07001935 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001936 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001937
1938 // It's possible to receive onDestroy after a new Launcher activity has
1939 // been created. In this case, don't interfere with the new Launcher.
1940 if (mModel.isCurrentCallbacks(this)) {
1941 mModel.stopLoader();
1942 app.setLauncher(null);
1943 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001946 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001948 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001950 mAppWidgetHost = null;
1951
1952 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
1954 TextKeyListener.getInstance().release();
1955
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001956 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001957
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001958 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001959 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001960 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001961 mWorkspace = null;
1962 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001963
Michael Jurka2ecf9952012-06-18 12:52:28 -07001964 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001965
1966 if (mLauncherCallbacks != null) {
1967 mLauncherCallbacks.onDestroy();
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
Adam Cohena9cf38f2011-05-02 15:36:58 -07001971 public DragController getDragController() {
1972 return mDragController;
1973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 @Override
1976 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001977 onStartForResult(requestCode);
1978 super.startActivityForResult(intent, requestCode);
1979 }
1980
1981 @Override
1982 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1983 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1984 onStartForResult(requestCode);
1985 try {
1986 super.startIntentSenderForResult(intent, requestCode,
1987 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1988 } catch (IntentSender.SendIntentException e) {
1989 throw new ActivityNotFoundException();
1990 }
1991 }
1992
1993 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001994 if (requestCode >= 0) {
1995 setWaitingForResult(true);
1996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
Winson Chung70d72102011-08-12 11:24:35 -07001999 /**
2000 * Indicates that we want global search for this activity by setting the globalSearch
2001 * argument for {@link #startSearch} to true.
2002 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002004 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002006
Karl Rosaen138a0412009-04-23 19:00:21 -07002007 if (initialQuery == null) {
2008 // Use any text typed in the launcher as the initial query
2009 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 if (appSearchData == null) {
2012 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002013 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
Winson Chungadf0c182012-08-23 14:59:07 -07002015 Rect sourceBounds = new Rect();
2016 if (mSearchDropTargetBar != null) {
2017 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2018 }
Romain Guycbb89e42009-06-08 15:52:54 -07002019
Ian Parkinson047036e2014-09-01 15:40:53 +01002020 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002021 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002022 if (clearTextImmediately) {
2023 clearTypedText();
2024 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002025
2026 // We need to show the workspace after starting the search
2027 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002028 }
2029
Ian Parkinson047036e2014-09-01 15:40:53 +01002030 /**
2031 * Start a text search.
2032 *
2033 * @return {@code true} if the search will start immediately, so any further keypresses
2034 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2035 * to buffer keypresses.
2036 */
2037 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002038 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002039 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2040 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2041 sourceBounds);
2042 }
2043
Michael Jurkaa33411c2012-06-14 16:18:21 -07002044 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002045 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002047 }
2048
2049 /**
2050 * Starts the global search activity. This code is a copied from SearchManager
2051 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002052 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002053 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002054 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002055 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2056 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2057 if (globalSearchActivity == null) {
2058 Log.w(TAG, "No global search activity found.");
2059 return;
2060 }
2061 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2062 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2063 intent.setComponent(globalSearchActivity);
2064 // Make sure that we have a Bundle to put source in
2065 if (appSearchData == null) {
2066 appSearchData = new Bundle();
2067 } else {
2068 appSearchData = new Bundle(appSearchData);
2069 }
Adam Cohen9211d422014-10-07 18:14:53 -07002070 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 if (!appSearchData.containsKey("source")) {
2072 appSearchData.putString("source", getPackageName());
2073 }
2074 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2075 if (!TextUtils.isEmpty(initialQuery)) {
2076 intent.putExtra(SearchManager.QUERY, initialQuery);
2077 }
2078 if (selectInitialQuery) {
2079 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2080 }
2081 intent.setSourceBounds(sourceBounds);
2082 try {
2083 startActivity(intent);
2084 } catch (ActivityNotFoundException ex) {
2085 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Winson Chungbedf9232015-07-10 12:38:30 -07002089 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2090 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2091 return;
2092 }
2093
2094 // If not handled, then just start the provided search intent
2095 startActivitySafely(v, searchIntent, null);
2096 }
2097
Yura4f93ec62014-02-04 14:15:21 +00002098 public boolean isOnCustomContent() {
2099 return mWorkspace.isOnOrMovingToCustomContent();
2100 }
2101
Winson Chung70d72102011-08-12 11:24:35 -07002102 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002103 public boolean onPrepareOptionsMenu(Menu menu) {
2104 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002105 if (!isOnCustomContent()) {
2106 // Close any open folders
2107 closeFolder();
2108 // Stop resizing any widgets
2109 mWorkspace.exitWidgetResizeMode();
2110 if (!mWorkspace.isInOverviewMode()) {
2111 // Show the overview mode
2112 showOverviewMode(true);
2113 } else {
2114 showWorkspace(true);
2115 }
Winson Chunge0298742014-01-17 12:03:00 -08002116 }
Adam Cohen9211d422014-10-07 18:14:53 -07002117 if (mLauncherCallbacks != null) {
2118 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2119 }
2120
Michael Jurkab94f3f82013-09-11 18:08:54 +02002121 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002124 @Override
2125 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002126 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002127 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002128 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002129 }
2130
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 public boolean isWorkspaceLocked() {
2132 return mWorkspaceLoading || mWaitingForResult;
2133 }
2134
Adam Cohen517a7f52014-03-01 12:12:59 -08002135 public boolean isWorkspaceLoading() {
2136 return mWorkspaceLoading;
2137 }
2138
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002139 private void setWorkspaceLoading(boolean value) {
2140 boolean isLocked = isWorkspaceLocked();
2141 mWorkspaceLoading = value;
2142 if (isLocked != isWorkspaceLocked()) {
2143 onWorkspaceLockedChanged();
2144 }
2145 }
2146
2147 private void setWaitingForResult(boolean value) {
2148 boolean isLocked = isWorkspaceLocked();
2149 mWaitingForResult = value;
2150 if (isLocked != isWorkspaceLocked()) {
2151 onWorkspaceLockedChanged();
2152 }
2153 }
2154
Adam Cohen9211d422014-10-07 18:14:53 -07002155 protected void onWorkspaceLockedChanged() {
2156 if (mLauncherCallbacks != null) {
2157 mLauncherCallbacks.onWorkspaceLockedChanged();
2158 }
2159 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002160
Michael Jurka0280c3b2010-09-17 15:00:07 -07002161 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002162 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002163 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002164 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2165 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002166 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002168
Sunny Goyale6b63a32015-01-16 16:14:29 -08002169 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002170 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002171 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2172 }
2173
Sunny Goyale6b63a32015-01-16 16:14:29 -08002174 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002175 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2176 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002177 if (appWidgetInfo.configure != null) {
2178 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002179 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002180
Bjorn Bringert7984c942009-12-09 15:38:25 +00002181 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002182 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2183 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002186 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002187 Runnable onComplete = new Runnable() {
2188 @Override
2189 public void run() {
2190 // Exit spring loaded mode if necessary after adding the widget
2191 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2192 null);
2193 }
2194 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002195 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002196 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002197 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199 }
Romain Guycbb89e42009-06-08 15:52:54 -07002200
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002201 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002202 // Close any folders that may be open.
2203 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002204 mWorkspace.moveToCustomContentScreen(animate);
2205 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002206
2207 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2208 int[] cell, int spanX, int spanY) {
2209 switch (info.itemType) {
2210 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2211 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2212 int span[] = new int[2];
2213 span[0] = spanX;
2214 span[1] = spanY;
2215 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2216 container, screenId, cell, span);
2217 break;
2218 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2219 processShortcutFromDrop(info.componentName, container, screenId, cell);
2220 break;
2221 default:
2222 throw new IllegalStateException("Unknown item type: " + info.itemType);
2223 }
2224 }
2225
Adam Cohenfbba09b2011-07-18 21:43:05 -07002226 /**
2227 * Process a shortcut drop.
2228 *
2229 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002231 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002232 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002233 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2234 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238
2239 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.cellX = cell[0];
2241 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002243
2244 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2245 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002246 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 }
2248
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 /**
2250 * Process a widget drop.
2251 *
2252 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002256 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2257 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002260 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002261 mPendingAddInfo.minSpanX = info.minSpanX;
2262 mPendingAddInfo.minSpanY = info.minSpanY;
2263
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.cellX = cell[0];
2266 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002268 if (span != null) {
2269 mPendingAddInfo.spanX = span[0];
2270 mPendingAddInfo.spanY = span[1];
2271 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272
Adam Cohened66b2b2012-01-23 17:28:51 -08002273 AppWidgetHostView hostView = info.boundWidget;
2274 int appWidgetId;
2275 if (hostView != null) {
2276 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002277 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002278
2279 // Clear the boundWidget so that it doesn't get destroyed.
2280 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002281 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002282 // In this case, we either need to start an activity to get permission to bind
2283 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002284 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002285 Bundle options = info.bindOptions;
2286
Sunny Goyalffe83f12014-08-14 17:39:34 -07002287 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2288 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002289 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002291 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002293 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2294 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2295 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002296 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2297 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002298 // TODO: we need to make sure that this accounts for the options bundle.
2299 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002300 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2301 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002303 }
2304
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002306 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002307 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 folderInfo.title = getText(R.string.folder_name);
2309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002311 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2312 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002313 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314
2315 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 FolderIcon newFolder =
2317 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002320 // Force measure the new folder icon
2321 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2322 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002323 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 }
Romain Guycbb89e42009-06-08 15:52:54 -07002325
Joe Onorato9c1289c2009-08-17 11:03:03 -04002326 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002327 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 }
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 @Override
2331 public boolean dispatchKeyEvent(KeyEvent event) {
2332 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2333 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002335 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002336 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002337 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002338 dumpState();
2339 return true;
2340 }
2341 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002342 }
2343 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2344 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002345 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 return true;
2347 }
2348 }
2349
2350 return super.dispatchKeyEvent(event);
2351 }
2352
Joe Onorato88ec0992009-11-19 13:16:06 -08002353 @Override
2354 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002355 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2356 return;
2357 }
2358
Sunny Goyal45478022015-06-08 16:52:41 -07002359 if (mDragController.isDragging()) {
2360 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002361 return;
2362 }
2363
Winson Chungb745afb2015-03-02 11:51:23 -08002364 if (isAppsViewVisible()) {
2365 showWorkspace(true);
2366 } else if (isWidgetsViewVisible()) {
2367 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002368 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002369 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002370 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002371 Folder openFolder = mWorkspace.getOpenFolder();
2372 if (openFolder.isEditingName()) {
2373 openFolder.dismissEditingName();
2374 } else {
2375 closeFolder();
2376 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002377 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002378 mWorkspace.exitWidgetResizeMode();
2379
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002380 // Back button is a no-op here, but give at least some feedback for the button press
2381 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002382 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002386 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002387 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002388 @Override
2389 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002390 if (mAppWidgetHost != null) {
2391 mAppWidgetHost.startListening();
2392 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002393 }
2394
2395 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 * Launches the intent referred by the clicked shortcut.
2397 *
2398 * @param v The view representing the clicked shortcut.
2399 */
2400 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002401 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2402 // view has detached (it's possible for this to happen if the view is removed mid touch).
2403 if (v.getWindowToken() == null) {
2404 return;
2405 }
2406
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002407 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002408 return;
2409 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002410
Adam Cohen1697b792013-09-17 19:08:21 -07002411 if (v instanceof Workspace) {
2412 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002413 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002414 }
2415 return;
2416 }
2417
2418 if (v instanceof CellLayout) {
2419 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002420 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2421 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002422 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002423 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002424 }
2425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002427 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002428 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002430 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002431 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002432 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002433 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002434 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002435 } else if (tag instanceof AppInfo) {
2436 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002437 } else if (tag instanceof LauncherAppWidgetInfo) {
2438 if (v instanceof PendingAppWidgetHostView) {
2439 onClickPendingWidget((PendingAppWidgetHostView) v);
2440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
2442 }
2443
Sunny Goyal70660032015-05-14 00:07:08 -07002444 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002445 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002446 return false;
2447 }
2448
Michael Jurkaaf442092010-06-10 17:01:57 -07002449 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002450 * Event handler for the app widget view which has not fully restored.
2451 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002452 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002453 if (mIsSafeModeEnabled) {
2454 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2455 return;
2456 }
2457
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002458 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002459 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002460 int widgetId = info.appWidgetId;
2461 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2462 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002463 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2464 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002465 mPendingAddInfo.copyFrom(info);
2466 mPendingAddWidgetId = widgetId;
2467
Sunny Goyalffe83f12014-08-14 17:39:34 -07002468 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2469 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002470 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002471 } else if (info.installProgress < 0) {
2472 // The install has not been queued
2473 final String packageName = info.providerName.getPackageName();
2474 showBrokenAppInstallDialog(packageName,
2475 new DialogInterface.OnClickListener() {
2476 public void onClick(DialogInterface dialog, int id) {
2477 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2478 }
2479 });
2480 } else {
2481 // Download has started.
2482 final String packageName = info.providerName.getPackageName();
2483 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002484 }
2485 }
2486
2487 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002488 * Event handler for the "grid" button that appears on the home screen, which
2489 * enters all apps mode.
2490 *
2491 * @param v The view that was clicked.
2492 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 protected void onClickAllAppsButton(View v) {
2494 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002495 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002496 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002497 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002498
2499 if (mLauncherCallbacks != null) {
2500 mLauncherCallbacks.onClickAllAppsButton(v);
2501 }
Winson Chung76648c52015-07-10 14:33:23 -07002502 }
2503 }
2504
2505 protected void onLongClickAllAppsButton(View v) {
2506 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2507 if (!isAppsViewVisible()) {
2508 showAppsView(true /* animated */, false /* resetListToTop */,
2509 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 }
2511 }
2512
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002513 private void showBrokenAppInstallDialog(final String packageName,
2514 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002515 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002516 .setTitle(R.string.abandoned_promises_title)
2517 .setMessage(R.string.abandoned_promise_explanation)
2518 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2519 .setNeutralButton(R.string.abandoned_clean_this,
2520 new DialogInterface.OnClickListener() {
2521 public void onClick(DialogInterface dialog, int id) {
2522 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2523 mWorkspace.removeAbandonedPromise(packageName, user);
2524 }
2525 })
2526 .create().show();
2527 return;
2528 }
2529
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 /**
2531 * Event handler for an app shortcut click.
2532 *
2533 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2534 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002535 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2537 Object tag = v.getTag();
2538 if (!(tag instanceof ShortcutInfo)) {
2539 throw new IllegalArgumentException("Input must be a Shortcut");
2540 }
2541
2542 // Open shortcut
2543 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002544
2545 if (shortcut.isDisabled != 0) {
2546 int error = R.string.activity_not_available;
2547 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2548 error = R.string.safemode_shortcut_error;
2549 }
2550 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2551 return;
2552 }
2553
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002554 final Intent intent = shortcut.intent;
2555
2556 // Check for special shortcuts
2557 if (intent.getComponent() != null) {
2558 final String shortcutClass = intent.getComponent().getClassName();
2559
2560 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2561 MemoryDumpActivity.startDump(this);
2562 return;
2563 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2564 toggleShowWeightWatcher();
2565 return;
2566 }
2567 }
2568
Chris Wren40c5ed32014-06-24 18:24:23 -04002569 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002570 if ((v instanceof BubbleTextView)
2571 && shortcut.isPromise()
2572 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002573 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002574 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002575 new DialogInterface.OnClickListener() {
2576 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002577 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002578 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002579 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002580 return;
2581 }
2582
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002583 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002584 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002585
2586 if (mLauncherCallbacks != null) {
2587 mLauncherCallbacks.onClickAppShortcut(v);
2588 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002589 }
2590
Adam Cohen091440a2015-03-18 14:16:05 -07002591 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002592 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002593 final ShortcutInfo shortcut;
2594 final Intent intent;
2595 if (tag instanceof ShortcutInfo) {
2596 shortcut = (ShortcutInfo) tag;
2597 intent = shortcut.intent;
2598 int[] pos = new int[2];
2599 v.getLocationOnScreen(pos);
2600 intent.setSourceBounds(new Rect(pos[0], pos[1],
2601 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002602
Sunny Goyal508da152014-08-14 10:53:27 -07002603 } else if (tag instanceof AppInfo) {
2604 shortcut = null;
2605 intent = ((AppInfo) tag).intent;
2606 } else {
2607 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2608 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609
2610 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002611 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612
2613 if (success && v instanceof BubbleTextView) {
2614 mWaitingForResume = (BubbleTextView) v;
2615 mWaitingForResume.setStayPressed(true);
2616 }
2617 }
2618
2619 /**
2620 * Event handler for a folder icon click.
2621 *
2622 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2623 */
2624 protected void onClickFolderIcon(View v) {
2625 if (LOGD) Log.d(TAG, "onClickFolder");
2626 if (!(v instanceof FolderIcon)){
2627 throw new IllegalArgumentException("Input must be a FolderIcon");
2628 }
2629
Sunny Goyal317698b2015-07-29 11:45:41 -07002630 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 FolderIcon folderIcon = (FolderIcon) v;
2632 final FolderInfo info = folderIcon.getFolderInfo();
2633 Folder openFolder = mWorkspace.getFolderForTag(info);
2634
2635 // If the folder info reports that the associated folder is open, then verify that
2636 // it is actually opened. There have been a few instances where this gets out of sync.
2637 if (info.opened && openFolder == null) {
2638 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2639 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2640 info.opened = false;
2641 }
2642
2643 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2644 // Close any open folder
2645 closeFolder();
2646 // Open the requested folder
2647 openFolder(folderIcon);
2648 } else {
2649 // Find the open folder...
2650 int folderScreen;
2651 if (openFolder != null) {
2652 folderScreen = mWorkspace.getPageForView(openFolder);
2653 // .. and close it
2654 closeFolder(openFolder);
2655 if (folderScreen != mWorkspace.getCurrentPage()) {
2656 // Close any folder open on the current screen
2657 closeFolder();
2658 // Pull the folder onto this screen
2659 openFolder(folderIcon);
2660 }
2661 }
2662 }
Adam Cohen9211d422014-10-07 18:14:53 -07002663
2664 if (mLauncherCallbacks != null) {
2665 mLauncherCallbacks.onClickFolderIcon(v);
2666 }
Michael Jurka5130e402011-10-13 04:55:35 -07002667 }
2668
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002669 /**
2670 * Event handler for the (Add) Widgets button that appears after a long press
2671 * on the home screen.
2672 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002673 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002675 if (mIsSafeModeEnabled) {
2676 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2677 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002678 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002679 if (mLauncherCallbacks != null) {
2680 mLauncherCallbacks.onClickAddWidgetButton(view);
2681 }
Adam Cohen9211d422014-10-07 18:14:53 -07002682 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002683 }
2684
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 /**
2686 * Event handler for the wallpaper picker button that appears after a long press
2687 * on the home screen.
2688 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002689 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002691 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2692 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2693 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2694 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002695 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002696
2697 if (mLauncherCallbacks != null) {
2698 mLauncherCallbacks.onClickWallpaperPicker(v);
2699 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 }
2701
2702 /**
2703 * Event handler for a click on the settings button that appears after a long press
2704 * on the home screen.
2705 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002706 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002708 if (mLauncherCallbacks != null) {
2709 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002710 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002711 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002712 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 }
2714
Adam Cohen61f560d2013-09-30 15:58:20 -07002715 public View.OnTouchListener getHapticFeedbackTouchListener() {
2716 if (mHapticFeedbackTouchListener == null) {
2717 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002718 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002719 @Override
2720 public boolean onTouch(View v, MotionEvent event) {
2721 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2722 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2723 }
2724 return false;
2725 }
2726 };
2727 }
2728 return mHapticFeedbackTouchListener;
2729 }
2730
Adam Cohen9211d422014-10-07 18:14:53 -07002731 public void onDragStarted(View view) {
2732 if (isOnCustomContent()) {
2733 // Custom content screen doesn't participate in drag and drop. If on custom
2734 // content screen, move to default.
2735 moveWorkspaceToDefaultScreen();
2736 }
2737
2738 if (mLauncherCallbacks != null) {
2739 mLauncherCallbacks.onDragStarted(view);
2740 }
2741 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002742
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002743 /**
2744 * Called when the user stops interacting with the launcher.
2745 * This implies that the user is now on the homescreen and is not doing housekeeping.
2746 */
Adam Cohen9211d422014-10-07 18:14:53 -07002747 protected void onInteractionEnd() {
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onInteractionEnd();
2750 }
2751 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002752
2753 /**
2754 * Called when the user starts interacting with the launcher.
2755 * The possible interactions are:
2756 * - open all apps
2757 * - reorder an app shortcut, or a widget
2758 * - open the overview mode.
2759 * This is a good time to stop doing things that only make sense
2760 * when the user is on the homescreen and not doing housekeeping.
2761 */
Adam Cohen9211d422014-10-07 18:14:53 -07002762 protected void onInteractionBegin() {
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onInteractionBegin();
2765 }
2766 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002767
Winson Chungcd99cd32015-04-29 11:03:24 -07002768 /** Updates the interaction state. */
2769 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002770 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002771 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002772 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2773 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002774 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002775 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002776 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002777 onInteractionEnd();
2778 }
2779 }
2780
Kenny Guyf07af7b2014-07-31 11:39:16 +01002781 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002782 try {
2783 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002784 launcherApps.showAppDetailsForProfile(componentName, user);
2785 } catch (SecurityException e) {
2786 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2787 Log.e(TAG, "Launcher does not have permission to launch settings");
2788 } catch (ActivityNotFoundException e) {
2789 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2790 Log.e(TAG, "Unable to launch settings");
2791 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002792 }
2793
Michael Jurka1e2f4652013-07-08 18:03:46 -07002794 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002795 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2796 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002797 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002798 // System applications cannot be installed. For now, show a toast explaining that.
2799 // We may give them the option of disabling apps this way.
2800 int messageId = R.string.uninstall_system_app_text;
2801 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002802 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002803 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002804 String packageName = componentName.getPackageName();
2805 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002806 Intent intent = new Intent(
2807 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002808 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2809 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002810 if (user != null) {
2811 user.addToIntent(intent, Intent.EXTRA_USER);
2812 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002813 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002814 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002815 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002816 }
2817
Winson Chung8f1eff72015-05-28 17:33:40 -07002818 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002819 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002821 // Only launch using the new animation if the shortcut has not opted out (this is a
2822 // private contract between launcher and may be ignored in the future).
2823 boolean useLaunchAnimation = (v != null) &&
2824 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002825 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2826 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002827
Kenny Guy1317e2d2014-05-08 18:52:50 +01002828 UserHandleCompat user = null;
2829 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2830 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2831 user = userManager.getUserForSerialNumber(serialNumber);
2832 }
2833
2834 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002835 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002836 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002837 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002838 int left = 0, top = 0;
2839 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2840 if (v instanceof TextView) {
2841 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002842 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2843 if (icon != null) {
2844 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002846 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002847 width = bounds.width();
2848 height = bounds.height();
2849 }
2850 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002851 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2852 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002853 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002854 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002855 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002856 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002857 // On L devices, we use the device default slide-up transition.
2858 // On L MR1 devices, we a custom version of the slide-up transition which
2859 // doesn't have the delay present in the device default.
2860 opts = ActivityOptions.makeCustomAnimation(this,
2861 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002862 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002863 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002864 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002865
2866 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2867 // Could be launching some bookkeeping activity
2868 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002869 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002870 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002871 launcherApps.startActivityForProfile(intent.getComponent(), user,
2872 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002873 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002874 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 } catch (SecurityException e) {
2876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002877 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002879 "or use the exported attribute for this activity. "
2880 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002882 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002884
Winson Chungbedf9232015-07-10 12:38:30 -07002885 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002886 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002887 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2888 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2889 return false;
2890 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002891 try {
2892 success = startActivity(v, intent, tag);
2893 } catch (ActivityNotFoundException e) {
2894 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2895 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2896 }
2897 return success;
2898 }
2899
Adam Cohen268c4752012-06-06 17:47:33 -07002900 /**
2901 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2902 * in the DragLayer in the exact absolute location of the original FolderIcon.
2903 */
2904 private void copyFolderIconToImage(FolderIcon fi) {
2905 final int width = fi.getMeasuredWidth();
2906 final int height = fi.getMeasuredHeight();
2907
2908 // Lazy load ImageView, Bitmap and Canvas
2909 if (mFolderIconImageView == null) {
2910 mFolderIconImageView = new ImageView(this);
2911 }
2912 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2913 mFolderIconBitmap.getHeight() != height) {
2914 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2915 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2916 }
2917
2918 DragLayer.LayoutParams lp;
2919 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2920 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2921 } else {
2922 lp = new DragLayer.LayoutParams(width, height);
2923 }
2924
Adam Cohen307fe232012-08-16 17:55:58 -07002925 // The layout from which the folder is being opened may be scaled, adjust the starting
2926 // view size by this scale factor.
2927 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002928 lp.customPosition = true;
2929 lp.x = mRectForFolderAnimation.left;
2930 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002931 lp.width = (int) (scale * width);
2932 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002933
2934 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2935 fi.draw(mFolderIconCanvas);
2936 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002937 if (fi.getFolder() != null) {
2938 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2939 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002940 }
Adam Cohen268c4752012-06-06 17:47:33 -07002941 // Just in case this image view is still in the drag layer from a previous animation,
2942 // we remove it and re-add it.
2943 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2944 mDragLayer.removeView(mFolderIconImageView);
2945 }
2946 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002947 if (fi.getFolder() != null) {
2948 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002949 }
Adam Cohen268c4752012-06-06 17:47:33 -07002950 }
2951
Adam Cohen2801caf2011-05-13 20:57:39 -07002952 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002953 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002954 FolderInfo info = (FolderInfo) fi.getTag();
2955 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2956 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002957 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2958 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002959 }
2960
Adam Cohen268c4752012-06-06 17:47:33 -07002961 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2962 copyFolderIconToImage(fi);
2963 fi.setVisibility(View.INVISIBLE);
2964
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002965 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2966 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002967 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002968 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2969 }
2970 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002971 oa.start();
2972 }
2973
Adam Cohen268c4752012-06-06 17:47:33 -07002974 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002975 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002976 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002977
Adam Cohen268c4752012-06-06 17:47:33 -07002978 // We remove and re-draw the FolderIcon in-case it has changed
2979 mDragLayer.removeView(mFolderIconImageView);
2980 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002981 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002982 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002983 oa.addListener(new AnimatorListenerAdapter() {
2984 @Override
2985 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002986 if (cl != null) {
2987 cl.clearFolderLeaveBehind();
2988 // Remove the ImageView copy of the FolderIcon and make the original visible.
2989 mDragLayer.removeView(mFolderIconImageView);
2990 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002991 }
2992 }
2993 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002994 oa.start();
2995 }
2996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002998 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 * is animated relative to the specified View. If the View is null, no animation
3000 * is played.
3001 *
3002 * @param folderInfo The FolderInfo describing the folder to open.
3003 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003004 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003005 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003006 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3007 if (openFolder != null && openFolder != folder) {
3008 // Close any open folder before opening a folder.
3009 closeFolder();
3010 }
3011
Adam Cohena9cf38f2011-05-02 15:36:58 -07003012 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013
Adam Cohena9cf38f2011-05-02 15:36:58 -07003014 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015
Sunny Goyalf4066152015-04-15 09:42:19 -07003016 // While the folder is open, the position of the icon cannot change.
3017 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3018
Adam Cohen4554ee12011-08-03 16:13:21 -07003019 // Just verify that the folder hasn't already been added to the DragLayer.
3020 // There was a one-off crash where the folder had a parent already.
3021 if (folder.getParent() == null) {
3022 mDragLayer.addView(folder);
3023 mDragController.addDropTarget((DropTarget) folder);
3024 } else {
3025 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3026 folder.getParent() + ").");
3027 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003028 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003029 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003030
3031 // Notify the accessibility manager that this folder "window" has appeared and occluded
3032 // the workspace items
3033 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3034 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003035 }
3036
3037 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003038 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003039 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003040 if (folder.isEditingName()) {
3041 folder.dismissEditingName();
3042 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003043 closeFolder(folder);
3044 }
3045 }
3046
Sunny Goyal83a8f042015-05-19 12:52:12 -07003047 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003048 folder.getInfo().opened = false;
3049
3050 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3051 if (parent != null) {
3052 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3053 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003054 if (fi != null) {
3055 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3056 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 }
3058 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003059
3060 // Notify the accessibility manager that this folder "window" has disappeard and no
3061 // longer occludeds the workspace items
3062 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 }
3064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003066 if (!isDraggingEnabled()) return false;
3067 if (isWorkspaceLocked()) return false;
3068 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Winson Chung76648c52015-07-10 14:33:23 -07003070 if (v == mAllAppsButton) {
3071 onLongClickAllAppsButton(v);
3072 return true;
3073 }
3074
Adam Cohen1697b792013-09-17 19:08:21 -07003075 if (v instanceof Workspace) {
3076 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003077 if (!mWorkspace.isTouchActive()) {
3078 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003079 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3080 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3081 return true;
3082 } else {
3083 return false;
3084 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003085 } else {
3086 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003087 }
Adam Cohen1697b792013-09-17 19:08:21 -07003088 }
3089
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003090 CellLayout.CellInfo longClickCellInfo = null;
3091 View itemUnderLongClick = null;
3092 if (v.getTag() instanceof ItemInfo) {
3093 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003094 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003095 itemUnderLongClick = longClickCellInfo.cell;
3096 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 }
3098
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 // The hotseat touch handling does not go through Workspace, and we always allow long press
3100 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003101 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003102 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003103 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003104 // User long pressed on empty space
3105 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3106 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003107 if (mWorkspace.isInOverviewMode()) {
3108 mWorkspace.startReordering(v);
3109 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003110 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003113 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3114 mHotseat.getOrderInHotseat(
3115 longClickCellInfo.cellX,
3116 longClickCellInfo.cellY));
3117 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003119 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
3121 }
3122 }
3123 return true;
3124 }
3125
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 boolean isHotseatLayout(View layout) {
3127 return mHotseat != null && layout != null &&
3128 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3129 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003130
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 /**
3132 * Returns the CellLayout of the specified container at the specified screen.
3133 */
Sunny Goyal92820592015-03-02 11:31:35 -08003134 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003135 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3136 if (mHotseat != null) {
3137 return mHotseat.getLayout();
3138 } else {
3139 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003140 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003142 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003143 }
3144 }
3145
Winson Chungb745afb2015-03-02 11:51:23 -08003146 /**
3147 * For overridden classes.
3148 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003149 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003150 return isAppsViewVisible();
3151 }
3152
3153 public boolean isAppsViewVisible() {
3154 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3155 }
3156
3157 public boolean isWidgetsViewVisible() {
3158 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003159 }
3160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003161 private void setWorkspaceBackground(int background) {
3162 switch (background) {
3163 case WORKSPACE_BACKGROUND_TRANSPARENT:
3164 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3165 break;
3166 case WORKSPACE_BACKGROUND_BLACK:
3167 getWindow().setBackgroundDrawable(null);
3168 break;
3169 default:
3170 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3171 }
Craig Mautner360310b2012-10-26 15:13:08 -07003172 }
3173
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003174 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003175 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3176 int curflags = getWindow().getAttributes().flags
3177 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3178 if (wpflags != curflags) {
3179 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3180 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003181 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003182 }
3183
Michael Jurkae326f182011-11-21 14:05:46 -08003184 @Override
3185 public void onTrimMemory(int level) {
3186 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003187 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3188 // The widget preview db can result in holding onto over
3189 // 3MB of memory for caching which isn't necessary.
3190 SQLiteDatabase.releaseMemory();
3191
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003192 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003193 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003194 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003195 if (mLauncherCallbacks != null) {
3196 mLauncherCallbacks.onTrimMemory(level);
3197 }
Michael Jurkae326f182011-11-21 14:05:46 -08003198 }
3199
Winson5c6bdbb2015-09-03 11:36:19 -07003200 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003201 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003202 }
3203
Winson5c6bdbb2015-09-03 11:36:19 -07003204 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003205 boolean changed = mState != State.WORKSPACE ||
3206 mWorkspace.getState() != Workspace.State.NORMAL;
3207 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003208 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003209 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003210 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003211
Michael Jurkab3e22d92011-10-31 15:58:33 -07003212 // Set focus to the AppsCustomize button
3213 if (mAllAppsButton != null) {
3214 mAllAppsButton.requestFocus();
3215 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003216 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003217
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003218 // Change the state *after* we've called all the transition code
3219 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003220
Winson Chung5fb63472011-02-02 17:03:37 -08003221 // Resume the auto-advance of widgets
3222 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003223 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003224
Winson Chung0f785722015-04-08 10:27:49 -07003225 if (changed) {
3226 // Send an accessibility event to announce the context change
3227 getWindow().getDecorView()
3228 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003229 }
Winson5c6bdbb2015-09-03 11:36:19 -07003230 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003231 }
3232
Adam Cohened307df2013-10-02 09:37:31 -07003233 void showOverviewMode(boolean animated) {
3234 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003235 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003236 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003237 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003238 }
3239
Winson Chungb745afb2015-03-02 11:51:23 -08003240 /**
3241 * Shows the apps view.
3242 */
Winson Chung76648c52015-07-10 14:33:23 -07003243 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3244 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003245 if (resetListToTop) {
3246 mAppsView.scrollToTop();
3247 }
Winson Chung4ac30062015-05-08 17:34:17 -07003248 if (updatePredictedApps) {
3249 tryAndUpdatePredictedApps();
3250 }
Winson Chung76648c52015-07-10 14:33:23 -07003251 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003252 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003253
Winson Chungb745afb2015-03-02 11:51:23 -08003254 /**
3255 * Shows the widgets view.
3256 */
3257 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003258 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003259 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003260 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003261 }
Winson Chung76648c52015-07-10 14:33:23 -07003262 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003263
3264 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003265 @Override
3266 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003267 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003268 }
3269 });
Winson Chungb745afb2015-03-02 11:51:23 -08003270 }
3271
3272 /**
3273 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003274 *
3275 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003276 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003277 // TODO: calling method should use the return value so that when {@code false} is returned
3278 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003279 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003280 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3281 mState != State.WIDGETS_SPRING_LOADED) {
3282 return false;
3283 }
3284 if (toState != State.APPS && toState != State.WIDGETS) {
3285 return false;
3286 }
Winson Chungb745afb2015-03-02 11:51:23 -08003287
3288 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003289 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3290 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003291 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003292 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003293 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003294
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003296 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297
3298 // Pause the auto-advance of widgets until we are out of AllApps
3299 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003300 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 closeFolder();
3302
3303 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003304 getWindow().getDecorView()
3305 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003306 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003307 }
3308
Winson Chungcd99cd32015-04-29 11:03:24 -07003309 /**
3310 * Updates the workspace and interaction state on state change, and return the animation to this
3311 * new state.
3312 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003313 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003314 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003315 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003316 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003317 updateInteraction(fromState, toState);
3318 return anim;
3319 }
3320
Jun Mukaif0033da2015-08-04 18:34:30 -07003321 public void onLauncherClingShown() {
3322 // When a launcher cling appears, it should cover the underlying layers, so their focus
3323 // should be blocked.
3324 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3325 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3326 }
3327 }
3328
3329 public void onLauncherClingDismissed() {
3330 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3331 }
3332
Hyunyoung Song3f471442015-04-08 19:01:34 -07003333 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003334 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003335 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003336 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003337 }
Winson Chungb745afb2015-03-02 11:51:23 -08003338
Winson Chung006ee262015-08-03 14:40:11 -07003339 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003340 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003341 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003342
3343 if (isAppsViewVisible()) {
3344 mState = State.APPS_SPRING_LOADED;
3345 } else if (isWidgetsViewVisible()) {
3346 mState = State.WIDGETS_SPRING_LOADED;
3347 } else {
3348 mState = State.WORKSPACE_SPRING_LOADED;
3349 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003350 }
Adam Cohen7777d962011-08-18 18:58:38 -07003351
Hyunyoung Song3f471442015-04-08 19:01:34 -07003352 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003353 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003354 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003355
Winson Chunge7a03942011-08-05 15:05:12 -07003356 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003357 @Override
3358 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003359 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3361 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003362 // Before we show workspace, hide all apps again because
3363 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3364 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003365 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003366 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003367 } else {
3368 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003369 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003370 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003371 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003372 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373
Tony Wickham94e0d372015-09-11 12:17:48 -07003374 private boolean isStateSpringLoaded() {
3375 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3376 || mState == State.WIDGETS_SPRING_LOADED;
3377 }
3378
Michael Jurkad3ef3062010-11-23 16:23:58 -08003379 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003380 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003381 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003382 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003383 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003384 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003385 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3386 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003388 }
3389
Winson Chung4ac30062015-05-08 17:34:17 -07003390 /**
3391 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3392 * resumed.
3393 */
3394 private void tryAndUpdatePredictedApps() {
3395 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003396 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003397 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003398 mAppsView.setPredictedApps(apps);
3399 }
3400 }
3401 }
3402
Michael Jurkab3e22d92011-10-31 15:58:33 -07003403 void lockAllApps() {
3404 // TODO
3405 }
3406
3407 void unlockAllApps() {
3408 // TODO
3409 }
3410
Sunny Goyal594d76d2014-11-06 10:12:54 -08003411 protected void disableVoiceButtonProxy(boolean disable) {
3412 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003413 }
3414
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003415 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003416 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3417 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003418 }
3419
Adam Cohen24ce0b32014-01-14 16:18:14 -08003420 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003421 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3422 if (searchProvider == null) {
3423 return null;
3424 }
3425
3426 Bundle opts = new Bundle();
3427 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003428 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003429
3430 SharedPreferences sp = getSharedPreferences(
3431 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3432 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003433 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003434 if (!searchProvider.provider.flattenToString().equals(
3435 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003436 || (widgetInfo == null)
3437 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003438 // A valid widget is not already bound.
3439 if (widgetId > -1) {
3440 mAppWidgetHost.deleteAppWidgetId(widgetId);
3441 widgetId = -1;
3442 }
3443
3444 // Try to bind a new widget
3445 widgetId = mAppWidgetHost.allocateAppWidgetId();
3446
3447 if (!AppWidgetManagerCompat.getInstance(this)
3448 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3449 mAppWidgetHost.deleteAppWidgetId(widgetId);
3450 widgetId = -1;
3451 }
3452
3453 sp.edit()
3454 .putInt(QSB_WIDGET_ID, widgetId)
3455 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3456 .commit();
3457 }
3458
Sunny Goyal8d595092015-07-06 12:22:14 -07003459 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003460 if (widgetId != -1) {
3461 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003462 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003463 mQsb.updateAppWidgetOptions(opts);
3464 mQsb.setPadding(0, 0, 0, 0);
3465 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003466 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003467 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003468 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003469 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003470 }
3471
Sunny Goyal22235bc2015-04-03 09:23:43 -07003472 private void reinflateQSBIfNecessary() {
3473 if (mQsb instanceof LauncherAppWidgetHostView &&
3474 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3475 mSearchDropTargetBar.removeView(mQsb);
3476 mQsb = null;
3477 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3478 }
3479 }
3480
Michael Jurkad7c28052012-04-27 15:43:36 -07003481 @Override
3482 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003483 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003484 final List<CharSequence> text = event.getText();
3485 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003486 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003487 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003488 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003489 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003490 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003491 } else if (mWorkspace != null) {
3492 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003493 } else {
3494 text.add(getString(R.string.all_apps_home_button_label));
3495 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003496 return result;
3497 }
3498
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003499 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003500 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003501 */
Adam Cohen091440a2015-03-18 14:16:05 -07003502 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003503 @Override
3504 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003505 closeSystemDialogs();
3506 }
3507 }
3508
3509 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003510 * If the activity is currently paused, signal that we need to run the passed Runnable
3511 * in onResume.
3512 *
3513 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003514 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3515 * wrong when we're not running, and if the activity comes back to what the configuration was
3516 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 *
3518 * Implementation of the method from LauncherModel.Callbacks.
3519 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003520 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003521 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003522 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003523 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003524 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003525 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003526 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003527 }
3528 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003529 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003530 return true;
3531 } else {
3532 return false;
3533 }
3534 }
3535
Michael Jurkac402cd92013-05-20 15:49:32 +02003536 private boolean waitUntilResume(Runnable run) {
3537 return waitUntilResume(run, false);
3538 }
3539
Michael Jurka1e2f4652013-07-08 18:03:46 -07003540 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003541 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003542 }
3543
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003545 * If the activity is currently paused, signal that we need to re-run the loader
3546 * in onResume.
3547 *
3548 * This needs to be called from incoming places where resources might have been loaded
3549 * while we are paused. That is becaues the Configuration might be wrong
3550 * when we're not running, and if it comes back to what it was when we
3551 * were paused, we are not restarted.
3552 *
3553 * Implementation of the method from LauncherModel.Callbacks.
3554 *
3555 * @return true if we are currently paused. The caller might be able to
3556 * skip some work in that case since we will come back again.
3557 */
3558 public boolean setLoadOnResume() {
3559 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003560 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003561 mOnResumeNeedsLoad = true;
3562 return true;
3563 } else {
3564 return false;
3565 }
3566 }
3567
3568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003570 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003572 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003573 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003574 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003575 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003578
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 /**
3580 * Refreshes the shortcuts shown on the workspace.
3581 *
3582 * Implementation of the method from LauncherModel.Callbacks.
3583 */
3584 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003585 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003586
Michael Jurka7607c2f2013-04-03 14:33:19 -07003587 // If we're starting binding all over again, clear any bind calls we'd postponed in
3588 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3589 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003590 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003591
Winson Chungd64d1762013-08-20 14:37:16 -07003592 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003593 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003594 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003595
Michael Jurka05bf644e2011-11-30 20:28:53 -08003596 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003597 if (mHotseat != null) {
3598 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 }
3600 }
3601
Adam Cohendcd297f2013-06-18 13:13:40 -07003602 @Override
3603 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003604 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003605
Adam Cohen5084cba2013-09-03 12:01:16 -07003606 // If there are no screens, we need to have an empty screen
3607 if (orderedScreenIds.size() == 0) {
3608 mWorkspace.addExtraEmptyScreen();
3609 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003610
3611 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003612 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003613 if (hasCustomContentToLeft()) {
3614 mWorkspace.createCustomContentContainer();
3615 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003616 }
Winson Chung64359a52013-07-08 17:17:08 -07003617 }
3618
3619 @Override
3620 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003621 int count = orderedScreenIds.size();
3622 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003623 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003624 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003625 }
3626
Adam Cohen39a06042013-07-19 14:30:12 -07003627 private boolean shouldShowWeightWatcher() {
3628 String spKey = LauncherAppState.getSharedPreferencesKey();
3629 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003630 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003631
3632 return show;
3633 }
3634
3635 private void toggleShowWeightWatcher() {
3636 String spKey = LauncherAppState.getSharedPreferencesKey();
3637 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3638 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3639
3640 show = !show;
3641
3642 SharedPreferences.Editor editor = sp.edit();
3643 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3644 editor.commit();
3645
3646 if (mWeightWatcher != null) {
3647 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3648 }
3649 }
3650
Winson Chungd64d1762013-08-20 14:37:16 -07003651 public void bindAppsAdded(final ArrayList<Long> newScreens,
3652 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003653 final ArrayList<ItemInfo> addAnimated,
3654 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003655 Runnable r = new Runnable() {
3656 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003657 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003658 }
3659 };
3660 if (waitUntilResume(r)) {
3661 return;
3662 }
3663
Winson Chungd64d1762013-08-20 14:37:16 -07003664 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003665 if (newScreens != null) {
3666 bindAddScreens(newScreens);
3667 }
Winson Chungd64d1762013-08-20 14:37:16 -07003668
3669 // We add the items without animation on non-visible pages, and with
3670 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003671 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003672 bindItems(addNotAnimated, 0,
3673 addNotAnimated.size(), false);
3674 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003675 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003676 bindItems(addAnimated, 0,
3677 addAnimated.size(), true);
3678 }
Winson Chungc58497e2013-09-03 17:48:37 -07003679
Winson Chung87412982013-10-03 18:34:14 -07003680 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003681 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003682
Winson Chungb745afb2015-03-02 11:51:23 -08003683 if (addedApps != null && mAppsView != null) {
3684 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003685 }
Winson Chungd64d1762013-08-20 14:37:16 -07003686 }
3687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 * Bind the items start-end from the list.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692 */
Winson Chung64359a52013-07-08 17:17:08 -07003693 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3694 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
3697 bindItems(shortcuts, start, end, forceAnimateIcons);
3698 }
3699 };
3700 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 return;
3702 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003703
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003705 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3706 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003707 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003709 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003712
3713 // Short circuit if we are loading dock items for a configuration which has no dock
3714 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3715 mHotseat == null) {
3716 continue;
3717 }
3718
Sunny Goyal639e9062015-08-19 19:17:06 -07003719 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 switch (item.itemType) {
3721 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3722 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003723 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003724 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003725
Winson Chung64359a52013-07-08 17:17:08 -07003726 /*
3727 * TODO: FIX collision case
3728 */
Winson Chungce376632013-07-11 16:12:41 -07003729 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3730 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3731 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003732 View v = cl.getChildAt(item.cellX, item.cellY);
3733 Object tag = v.getTag();
3734 String desc = "Collision while binding workspace item: " + item
3735 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003736 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003737 throw (new RuntimeException(desc));
3738 } else {
3739 Log.d(TAG, desc);
3740 }
Winson Chungce376632013-07-11 16:12:41 -07003741 }
Winson Chung64359a52013-07-08 17:17:08 -07003742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003744 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003745 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003746 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003747 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003749 default:
3750 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003752
3753 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3754 item.cellY, 1, 1);
3755 if (animateIcons) {
3756 // Animate all the applications up now
3757 view.setAlpha(0f);
3758 view.setScaleX(0f);
3759 view.setScaleY(0f);
3760 bounceAnims.add(createNewAppBounceAnimation(view, i));
3761 newShortcutsScreenId = item.screenId;
3762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764
Winson Chung997a9232013-07-24 15:33:46 -07003765 if (animateIcons) {
3766 // Animate to the correct page
3767 if (newShortcutsScreenId > -1) {
3768 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003769 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003770 final Runnable startBounceAnimRunnable = new Runnable() {
3771 public void run() {
3772 anim.playTogether(bounceAnims);
3773 anim.start();
3774 }
3775 };
Winson Chung997a9232013-07-24 15:33:46 -07003776 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003777 // We post the animation slightly delayed to prevent slowdowns
3778 // when we are loading right after we return to launcher.
3779 mWorkspace.postDelayed(new Runnable() {
3780 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003781 if (mWorkspace != null) {
3782 mWorkspace.snapToPage(newScreenIndex);
3783 mWorkspace.postDelayed(startBounceAnimRunnable,
3784 NEW_APPS_ANIMATION_DELAY);
3785 }
Winson Chung94d67682013-09-25 16:29:40 -07003786 }
3787 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003788 } else {
3789 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003790 }
3791 }
Winson Chung64359a52013-07-08 17:17:08 -07003792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003794 }
3795
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 /**
3797 * Implementation of the method from LauncherModel.Callbacks.
3798 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003799 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 Runnable r = new Runnable() {
3801 public void run() {
3802 bindFolders(folders);
3803 }
3804 };
3805 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 return;
3807 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003808 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810
3811 /**
3812 * Add the views for a widget to the workspace.
3813 *
3814 * Implementation of the method from LauncherModel.Callbacks.
3815 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003816 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003817 Runnable r = new Runnable() {
3818 public void run() {
3819 bindAppWidget(item);
3820 }
3821 };
3822 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003823 return;
3824 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003825
Daniel Sandler843e8602010-06-07 14:59:01 -04003826 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3827 if (DEBUG_WIDGETS) {
3828 Log.d(TAG, "bindAppWidget: " + item);
3829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 final Workspace workspace = mWorkspace;
3831
Adam Cohen59400422014-03-05 18:07:04 -08003832 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003833 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003834
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003835 if (!mIsSafeModeEnabled
3836 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003837 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3838
Sunny Goyalff572272014-07-23 13:58:07 -07003839 if (appWidgetInfo == null) {
3840 if (DEBUG_WIDGETS) {
3841 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3842 + " belongs to component " + item.providerName
3843 + ", as the povider is null");
3844 }
3845 LauncherModel.deleteItemFromDatabase(this, item);
3846 return;
3847 }
Sunny Goyalff572272014-07-23 13:58:07 -07003848
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003849 // If we do not have a valid id, try to bind an id.
3850 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3851 // Note: This assumes that the id remap broadcast is received before this step.
3852 // If that is not the case, the id remap will be ignored and user may see the
3853 // click to setup view.
3854 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3855 pendingInfo.spanX = item.spanX;
3856 pendingInfo.spanY = item.spanY;
3857 pendingInfo.minSpanX = item.minSpanX;
3858 pendingInfo.minSpanY = item.minSpanY;
3859 Bundle options = null;
3860 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003861
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003862 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3863 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3864 newWidgetId, appWidgetInfo, options);
3865
3866 // TODO consider showing a permission dialog when the widget is clicked.
3867 if (!success) {
3868 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3869 if (DEBUG_WIDGETS) {
3870 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3871 + " belongs to component " + item.providerName
3872 + ", as the launcher is unable to bing a new widget id");
3873 }
3874 LauncherModel.deleteItemFromDatabase(this, item);
3875 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003876 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003877
3878 item.appWidgetId = newWidgetId;
3879
3880 // If the widget has a configure activity, it is still needs to set it up, otherwise
3881 // the widget is ready to go.
3882 item.restoreStatus = (appWidgetInfo.configure == null)
3883 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3884 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3885
3886 LauncherModel.updateItemInDatabase(this, item);
3887 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3888 && (appWidgetInfo.configure == null)) {
3889 // If the ID is already valid, verify if we need to configure or not.
3890 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3891 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003892 }
Sunny Goyalff572272014-07-23 13:58:07 -07003893 }
3894
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003895 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003896 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003897 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003898 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3899 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003900 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003901
Sunny Goyal651077b2014-06-30 14:15:31 -07003902 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003903 item.minSpanX = appWidgetInfo.minSpanX;
3904 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003905 } else {
3906 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003907 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3908 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003909 view.updateIcon(mIconCache);
3910 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003911 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003912 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003916 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917
Adam Cohendcd297f2013-06-18 13:13:40 -07003918 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003920 if (!item.isCustomWidget()) {
3921 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3922 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003923
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 workspace.requestLayout();
3925
Daniel Sandler843e8602010-06-07 14:59:01 -04003926 if (DEBUG_WIDGETS) {
3927 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3928 + (SystemClock.uptimeMillis()-start) + "ms");
3929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 }
3931
Sunny Goyalff572272014-07-23 13:58:07 -07003932 /**
3933 * Restores a pending widget.
3934 *
3935 * @param appWidgetId The app widget id
3936 * @param cellInfo The position on screen where to create the widget.
3937 */
3938 private void completeRestoreAppWidget(final int appWidgetId) {
3939 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3940 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3941 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3942 return;
3943 }
3944
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003945 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003946 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3947
3948 mWorkspace.reinflateWidgetsIfNecessary();
3949 LauncherModel.updateItemInDatabase(this, info);
3950 }
3951
Adam Cohen1462de32012-07-24 22:34:36 -07003952 public void onPageBoundSynchronously(int page) {
3953 mSynchronouslyBoundPages.add(page);
3954 }
3955
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 /**
3957 * Callback saying that there aren't any more items to bind.
3958 *
3959 * Implementation of the method from LauncherModel.Callbacks.
3960 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003961 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003962 Runnable r = new Runnable() {
3963 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003964 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003965 }
3966 };
3967 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003968 return;
3969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 if (mSavedState != null) {
3971 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003972 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974 mSavedState = null;
3975 }
3976
Adam Cohen1462de32012-07-24 22:34:36 -07003977 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003979 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003980 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003981
3982 // If we received the result of any pending adds while the loader was running (e.g. the
3983 // widget configuration forced an orientation change), process them now.
3984 if (sPendingAddItem != null) {
3985 final long screenId = completeAdd(sPendingAddItem);
3986
3987 // TODO: this moves the user to the page where the pending item was added. Ideally,
3988 // the screen would be guaranteed to exist after bind, and the page would be set through
3989 // the workspace restore process.
3990 mWorkspace.post(new Runnable() {
3991 @Override
3992 public void run() {
3993 mWorkspace.snapToScreenId(screenId);
3994 }
3995 });
3996 sPendingAddItem = null;
3997 }
3998
Sunny Goyal756adbc2015-04-16 15:20:51 -07003999 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004000
4001 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004002 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004003 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004004 }
4005
Adam Cohen3ed316a2014-07-23 18:21:20 -07004006 private void sendLoadingCompleteBroadcastIfNecessary() {
4007 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4008 String permission =
4009 getResources().getString(R.string.receive_first_load_broadcast_permission);
4010 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4011 sendBroadcast(intent, permission);
4012 SharedPreferences.Editor editor = mSharedPrefs.edit();
4013 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4014 editor.apply();
4015 }
4016 }
4017
Winson Chunga0b7e862013-09-05 16:03:15 -07004018 public boolean isAllAppsButtonRank(int rank) {
4019 if (mHotseat != null) {
4020 return mHotseat.isAllAppsButtonRank(rank);
4021 }
4022 return false;
4023 }
4024
Winson Chunga2413752012-04-03 14:22:34 -07004025 private boolean canRunNewAppsAnimation() {
4026 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004027 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4028 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004029 }
4030
Winson Chungc9168342013-06-26 14:54:55 -07004031 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004032 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004033 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4034 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004035 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004036 return bounceAnim;
4037 }
4038
Adam Cohen27772732013-11-11 14:00:56 +00004039 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004040 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004041 }
4042
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004043 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004044 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004045 }
4046
Winson Chung88fa7412015-08-03 14:25:28 -07004047 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004048 if (mSearchDropTargetBar == null) {
4049 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004050 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004051 if (mQsb != null) {
4052 mSearchDropTargetBar.removeView(mQsb);
4053 mQsb = null;
4054 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004055 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004056 }
4057
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004058 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004059 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4060 * multiple calls to bind the same list.)
4061 */
4062 @Thunk ArrayList<AppInfo> mTmpAppsList;
4063 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4064 public void run() {
4065 bindAllApplications(mTmpAppsList);
4066 mTmpAppsList = null;
4067 }
4068 };
4069
4070 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 * Add the icons for all apps.
4072 *
4073 * Implementation of the method from LauncherModel.Callbacks.
4074 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004075 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004076 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4077 mTmpAppsList = apps;
4078 return;
4079 }
4080
Winson Chungb745afb2015-03-02 11:51:23 -08004081 if (mAppsView != null) {
4082 mAppsView.setApps(apps);
4083 }
Adam Cohen9211d422014-10-07 18:14:53 -07004084 if (mLauncherCallbacks != null) {
4085 mLauncherCallbacks.bindAllApplications(apps);
4086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 }
4088
4089 /**
4090 * A package was updated.
4091 *
4092 * Implementation of the method from LauncherModel.Callbacks.
4093 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004094 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004095 Runnable r = new Runnable() {
4096 public void run() {
4097 bindAppsUpdated(apps);
4098 }
4099 };
4100 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004101 return;
4102 }
4103
Winson Chungb745afb2015-03-02 11:51:23 -08004104 if (mAppsView != null) {
4105 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 }
4108
Sunny Goyal4390ace2014-10-13 11:33:11 -07004109 @Override
4110 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4111 Runnable r = new Runnable() {
4112 public void run() {
4113 bindWidgetsRestored(widgets);
4114 }
4115 };
4116 if (waitUntilResume(r)) {
4117 return;
4118 }
4119 mWorkspace.widgetsRestored(widgets);
4120 }
4121
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004123 * Some shortcuts were updated in the background.
4124 *
4125 * Implementation of the method from LauncherModel.Callbacks.
4126 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004127 @Override
4128 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4129 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004130 Runnable r = new Runnable() {
4131 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004132 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004133 }
4134 };
4135 if (waitUntilResume(r)) {
4136 return;
4137 }
4138
Sunny Goyal4390ace2014-10-13 11:33:11 -07004139 if (!updated.isEmpty()) {
4140 mWorkspace.updateShortcuts(updated);
4141 }
4142
4143 if (!removed.isEmpty()) {
4144 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4145 for (ShortcutInfo si : removed) {
4146 removedComponents.add(si.getTargetComponent());
4147 }
4148 mWorkspace.removeItemsByComponentName(removedComponents, user);
4149 // Notify the drag controller
4150 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004151 }
4152 }
4153
4154 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004155 * Update the state of a package, typically related to install state.
4156 *
4157 * Implementation of the method from LauncherModel.Callbacks.
4158 */
Sunny Goyale755d462014-07-22 13:48:29 -07004159 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004160 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4161 Runnable r = new Runnable() {
4162 public void run() {
4163 bindRestoreItemsChange(updates);
4164 }
4165 };
4166 if (waitUntilResume(r)) {
4167 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004168 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004169
Sunny Goyal756adbc2015-04-16 15:20:51 -07004170 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004171 }
4172
4173 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004174 * A package was uninstalled. We take both the super set of packageNames
4175 * in addition to specific applications to remove, the reason being that
4176 * this can be called when a package is updated as well. In that scenario,
4177 * we only remove specific components from the workspace, where as
4178 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004179 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004180 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004181 * Implementation of the method from LauncherModel.Callbacks.
4182 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004183 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004184 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004185 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004186 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004187 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004188 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004189 }
Winson Chungd64d1762013-08-20 14:37:16 -07004190 };
4191 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004192 return;
4193 }
4194
Sunny Goyal1a745e82014-10-02 15:58:31 -07004195 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004196 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4197 for (AppInfo info : appInfos) {
4198 removedComponents.add(info.componentName);
4199 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004200 if (!packageNames.isEmpty()) {
4201 mWorkspace.removeItemsByPackageName(packageNames, user);
4202 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203 if (!removedComponents.isEmpty()) {
4204 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004205 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004206 // Notify the drag controller
4207 mDragController.onAppsRemoved(packageNames, removedComponents);
4208
Sunny Goyal1a745e82014-10-02 15:58:31 -07004209 } else {
4210 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004211 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004212
Winson Chungdf95eb12013-10-16 14:57:07 -07004213 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004214 if (mAppsView != null) {
4215 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004217 }
Joe Onoratobe386092009-11-17 17:32:16 -08004218
Michael Jurkac402cd92013-05-20 15:49:32 +02004219 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004220 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004221 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004222 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004223 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004224
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004225 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004226 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004227 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004228 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004229 return;
4230 }
4231
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004232 if (mWidgetsView != null && model != null) {
4233 mWidgetsView.addWidgets(model);
4234 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004235 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004236 }
4237
Winson Chung400438b2011-01-16 17:53:48 -08004238 private int mapConfigurationOriActivityInfoOri(int configOri) {
4239 final Display d = getWindowManager().getDefaultDisplay();
4240 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4241 switch (d.getRotation()) {
4242 case Surface.ROTATION_0:
4243 case Surface.ROTATION_180:
4244 // We are currently in the same basic orientation as the natural orientation
4245 naturalOri = configOri;
4246 break;
4247 case Surface.ROTATION_90:
4248 case Surface.ROTATION_270:
4249 // We are currently in the other basic orientation to the natural orientation
4250 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4251 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4252 break;
4253 }
4254
4255 int[] oriMap = {
4256 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4257 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4258 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4259 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4260 };
4261 // Since the map starts at portrait, we need to offset if this device's natural orientation
4262 // is landscape.
4263 int indexOffset = 0;
4264 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4265 indexOffset = 1;
4266 }
4267 return oriMap[(d.getRotation() + indexOffset) % 4];
4268 }
Adam Cohen446e9402011-09-15 18:21:21 -07004269
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004270 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004271 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004272 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004273 if (Utilities.ATLEAST_JB_MR2) {
4274 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4275 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004276 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4277 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004278 }
Winson Chung4b919f82012-05-01 10:44:08 -07004279 }
4280 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004281
Winson Chung4b919f82012-05-01 10:44:08 -07004282 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004283 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004284 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004285 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004286 } else {
4287 mHandler.postDelayed(new Runnable() {
4288 public void run() {
4289 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4290 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004291 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004292 }
Winson Chung4b919f82012-05-01 10:44:08 -07004293 }
Winson Chung400438b2011-01-16 17:53:48 -08004294 }
4295
Winson Chunge43a1e72014-01-15 10:33:02 -08004296 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004297 if (mLauncherCallbacks != null) {
4298 return mLauncherCallbacks.isLauncherPreinstalled();
4299 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004300 PackageManager pm = getPackageManager();
4301 try {
4302 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4303 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4304 return true;
4305 } else {
4306 return false;
4307 }
4308 } catch (NameNotFoundException e) {
4309 e.printStackTrace();
4310 return false;
4311 }
4312 }
4313
Adam Cohenea90f832014-05-21 15:03:34 -07004314 /**
4315 * This method indicates whether or not we should suggest default wallpaper dimensions
4316 * when our wallpaper cropper was not yet used to set a wallpaper.
4317 */
4318 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004319 if (mLauncherCallbacks != null) {
4320 return mLauncherCallbacks.overrideWallpaperDimensions();
4321 }
Adam Cohenea90f832014-05-21 15:03:34 -07004322 return true;
4323 }
4324
Adam Cohen432609a2014-03-13 17:03:22 -07004325 /**
4326 * To be overridden by subclasses to indicate that there is an activity to launch
4327 * before showing the standard launcher experience.
4328 */
4329 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004330 if (mLauncherCallbacks != null) {
4331 return mLauncherCallbacks.hasFirstRunActivity();
4332 }
Adam Cohen432609a2014-03-13 17:03:22 -07004333 return false;
4334 }
4335
4336 /**
4337 * To be overridden by subclasses to launch any first run activity
4338 */
4339 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004340 if (mLauncherCallbacks != null) {
4341 return mLauncherCallbacks.getFirstRunActivity();
4342 }
Adam Cohen432609a2014-03-13 17:03:22 -07004343 return null;
4344 }
4345
Winson Chunga6945242014-01-08 14:04:34 -08004346 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004347 return !ActivityManager.isRunningInTestHarness() &&
4348 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004349 }
4350
Adam Cohen4a9423d2014-03-28 14:22:31 -07004351 protected boolean hasRunFirstRunActivity() {
4352 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4353 }
4354
Adam Cohen432609a2014-03-13 17:03:22 -07004355 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004356 if (shouldRunFirstRunActivity() &&
4357 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004358 Intent firstRunIntent = getFirstRunActivity();
4359 if (firstRunIntent != null) {
4360 startActivity(firstRunIntent);
4361 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004363 }
4364 }
Adam Cohen432609a2014-03-13 17:03:22 -07004365 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004366 }
4367
4368 private void markFirstRunActivityShown() {
4369 SharedPreferences.Editor editor = mSharedPrefs.edit();
4370 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4371 editor.apply();
4372 }
4373
Adam Cohen432609a2014-03-13 17:03:22 -07004374 /**
4375 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4376 * screen that must be displayed and dismissed.
4377 */
4378 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004379 if (mLauncherCallbacks != null) {
4380 return mLauncherCallbacks.hasDismissableIntroScreen();
4381 }
Adam Cohen432609a2014-03-13 17:03:22 -07004382 return false;
4383 }
4384
4385 /**
4386 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4387 */
4388 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004389 if (mLauncherCallbacks != null) {
4390 return mLauncherCallbacks.getIntroScreen();
4391 }
Adam Cohen432609a2014-03-13 17:03:22 -07004392 return null;
4393 }
4394
4395 /**
4396 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4397 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4398 */
4399 private boolean shouldShowIntroScreen() {
4400 return hasDismissableIntroScreen() &&
4401 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4402 }
4403
4404 protected void showIntroScreen() {
4405 View introScreen = getIntroScreen();
4406 changeWallpaperVisiblity(false);
4407 if (introScreen != null) {
4408 mDragLayer.showOverlayView(introScreen);
4409 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004410 if (mLauncherOverlayContainer != null) {
4411 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4412 }
Adam Cohen432609a2014-03-13 17:03:22 -07004413 }
4414
4415 public void dismissIntroScreen() {
4416 markIntroScreenDismissed();
4417 if (showFirstRunActivity()) {
4418 // We delay hiding the intro view until the first run activity is showing. This
4419 // avoids a blip.
4420 mWorkspace.postDelayed(new Runnable() {
4421 @Override
4422 public void run() {
4423 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004424 if (mLauncherOverlayContainer != null) {
4425 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4426 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004427 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004428 }
4429 }, ACTIVITY_START_DELAY);
4430 } else {
4431 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004432 if (mLauncherOverlayContainer != null) {
4433 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4434 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004435 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004436 }
4437 changeWallpaperVisiblity(true);
4438 }
4439
4440 private void markIntroScreenDismissed() {
4441 SharedPreferences.Editor editor = mSharedPrefs.edit();
4442 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4443 editor.apply();
4444 }
4445
Adam Cohen091440a2015-03-18 14:16:05 -07004446 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004447 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4448 // on the device, then we always show the first run cling experience (or if there is no
4449 // launcher2). Otherwise, we prompt the user upon started for migration
4450 LauncherClings launcherClings = new LauncherClings(this);
4451 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004452 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004453 if (mModel.canMigrateFromOldLauncherDb(this)) {
4454 launcherClings.showMigrationCling();
4455 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004456 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004457 }
4458 }
4459 }
4460
Winson Chunga6945242014-01-08 14:04:34 -08004461 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004462 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4463 if (mHotseat != null) mHotseat.setAlpha(1f);
4464 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004465 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4466 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004467 }
4468
4469 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004470 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4471 if (mHotseat != null) mHotseat.setAlpha(0f);
4472 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004473 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4474 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004475 }
4476
Winson Chung5ffd51d2015-06-25 11:36:50 -07004477 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004478 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004479 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004480 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004481 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004482 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004483 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4484 if (userHandle != null) {
4485 user = UserHandleCompat.fromUser(userHandle);
4486 }
4487 }
4488 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004489 }
4490
4491 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004492 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004493 if (user == null) {
4494 user = UserHandleCompat.myUserHandle();
4495 }
4496
4497 // Called from search suggestion, add the profile extra to the intent to ensure that we
4498 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004499 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004500 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004501 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004502 return null;
4503 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004504 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004505 }
4506
Winson Chung5ffd51d2015-06-25 11:36:50 -07004507 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004508 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4509 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004510 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004511 UserHandleCompat.myUserHandle());
4512 }
4513
Winson Chung5ffd51d2015-06-25 11:36:50 -07004514 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004515 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4516 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004517 mWorkspace.onExternalDragStartedWithItem(dragView);
4518 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004519 }
4520
Winson Chung5ffd51d2015-06-25 11:36:50 -07004521 protected void moveWorkspaceToDefaultScreen() {
4522 mWorkspace.moveToDefaultScreen(false);
4523 }
4524
Anjali Koppalf5d29132014-02-28 13:33:27 -08004525 @Override
4526 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004527 if (mLauncherCallbacks != null) {
4528 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4529 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004530 }
4531
Winson Chung80baf5a2010-08-09 16:03:15 -07004532 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004533 * Returns a FastBitmapDrawable with the icon, accurately sized.
4534 */
4535 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4536 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4537 d.setFilterBitmap(true);
4538 resizeIconDrawable(d);
4539 return d;
4540 }
4541
4542 /**
4543 * Resizes an icon drawable to the correct icon size.
4544 */
4545 public void resizeIconDrawable(Drawable icon) {
4546 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4547 }
4548
4549 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004550 * Prints out out state for debugging.
4551 */
4552 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004553 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004554 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004555 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4556 Log.d(TAG, "mRestoring=" + mRestoring);
4557 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004558 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004559 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004560 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004561
Daniel Sandler325dc232013-06-05 22:57:57 -04004562 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004563 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004564
4565 @Override
4566 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4567 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004568 synchronized (sDumpLogs) {
4569 writer.println(" ");
4570 writer.println("Debug logs: ");
4571 for (int i = 0; i < sDumpLogs.size(); i++) {
4572 writer.println(" " + sDumpLogs.get(i));
4573 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004574 }
Adam Cohen9211d422014-10-07 18:14:53 -07004575 if (mLauncherCallbacks != null) {
4576 mLauncherCallbacks.dump(prefix, fd, writer, args);
4577 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004578 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004579
4580 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004581 if (DEBUG_DUMP_LOG) {
4582 synchronized (sDumpLogs) {
4583 Log.d(TAG, "");
4584 Log.d(TAG, "*********************");
4585 Log.d(TAG, "Launcher debug logs: ");
4586 for (int i = 0; i < sDumpLogs.size(); i++) {
4587 Log.d(TAG, " " + sDumpLogs.get(i));
4588 }
4589 Log.d(TAG, "*********************");
4590 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004591 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004592 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004593 }
4594
4595 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004596 addDumpLog(tag, log, null, debugLog);
4597 }
4598
4599 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004600 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004601 if (e != null) {
4602 Log.d(tag, log, e);
4603 } else {
4604 Log.d(tag, log);
4605 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004606 }
Winson Chungede41292013-09-19 16:27:36 -07004607 if (DEBUG_DUMP_LOG) {
4608 sDateStamp.setTime(System.currentTimeMillis());
4609 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004610 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4611 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004612 }
Winson Chungede41292013-09-19 16:27:36 -07004613 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004614 }
4615
Adam Cohen59400422014-03-05 18:07:04 -08004616 public static CustomAppWidget getCustomAppWidget(String name) {
4617 return sCustomAppWidgets.get(name);
4618 }
4619
4620 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4621 return sCustomAppWidgets;
4622 }
4623
Winson Chungede41292013-09-19 16:27:36 -07004624 public void dumpLogsToLocalData() {
4625 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004626 new AsyncTask<Void, Void, Void>() {
4627 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004628 boolean success = false;
4629 sDateStamp.setTime(sRunStart);
4630 String FILENAME = sDateStamp.getMonth() + "-"
4631 + sDateStamp.getDay() + "_"
4632 + sDateStamp.getHours() + "-"
4633 + sDateStamp.getMinutes() + "_"
4634 + sDateStamp.getSeconds() + ".txt";
4635
4636 FileOutputStream fos = null;
4637 File outFile = null;
4638 try {
4639 outFile = new File(getFilesDir(), FILENAME);
4640 outFile.createNewFile();
4641 fos = new FileOutputStream(outFile);
4642 } catch (Exception e) {
4643 e.printStackTrace();
4644 }
4645 if (fos != null) {
4646 PrintWriter writer = new PrintWriter(fos);
4647
4648 writer.println(" ");
4649 writer.println("Debug logs: ");
4650 synchronized (sDumpLogs) {
4651 for (int i = 0; i < sDumpLogs.size(); i++) {
4652 writer.println(" " + sDumpLogs.get(i));
4653 }
4654 }
4655 writer.close();
4656 }
4657 try {
4658 if (fos != null) {
4659 fos.close();
4660 success = true;
4661 }
4662 } catch (IOException e) {
4663 e.printStackTrace();
4664 }
Michael Jurka43467462013-11-14 12:03:58 +01004665 return null;
Winson Chungede41292013-09-19 16:27:36 -07004666 }
Michael Jurka43467462013-11-14 12:03:58 +01004667 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004668 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004670}
Michael Jurka2763be32011-02-24 11:19:57 -08004671
Dan Sandlerd5024042014-01-09 15:01:33 -05004672interface DebugIntents {
4673 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4674 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004675}