blob: 02579a2e257e6350306ae74917179502673d5ac9 [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
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070055import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
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;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700109import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700110import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700111import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700112import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700113import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700114import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700115import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700116import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700117
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700134 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700135 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Daniel Sandlerf061f822013-06-27 13:59:36 -0400137 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700138 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700139 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700150 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Sunny Goyal28c6b962015-10-12 11:42:05 -0700152 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
153
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700154 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
155 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
156 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
157
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700158 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 // To turn on these properties, type
167 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Winson Chung2672ff92012-05-04 16:22:30 -0700170 // The Intent extra that defines whether to ignore the launch animation
171 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400172 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
175 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700176 // Type: int
177 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700178 // Type: long
179 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700180 // Type: Content Values / parcelable
181 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000184 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Adam Cohen432609a2014-03-13 17:03:22 -0700187 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800188 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
189
Adam Cohen3ed316a2014-07-23 18:21:20 -0700190 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
191 static final String ACTION_FIRST_LOAD_COMPLETE =
192 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
193
Sunny Goyal594d76d2014-11-06 10:12:54 -0800194 private static final String QSB_WIDGET_ID = "qsb_widget_id";
195 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
196
Winson Chunga6945242014-01-08 14:04:34 -0800197 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700200 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
201 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700202
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700206 private boolean mIsSafeModeEnabled;
207
Adam Cohenc2d6e892014-10-16 09:49:24 -0700208 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
209 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700210 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700226 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800227 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700230
231 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Sunny Goyalffe83f12014-08-14 17:39:34 -0700233 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700236 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800237 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700249 private AppInfoDropTargetBar mAppInfoDropTargetBar;
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>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700283 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Sunny Goyal639e9062015-08-19 19:17:06 -0700286 private LauncherClings mClings;
287
Sunny Goyale2df0622015-04-24 11:27:00 -0700288 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700289
Adam Cohen61f560d2013-09-30 15:58:20 -0700290 private View.OnTouchListener mHapticFeedbackTouchListener;
291
Adam Cohended9f8d2010-11-03 13:25:16 -0700292 // Related to the auto-advancing of widgets
293 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700294 private static final int ADVANCE_INTERVAL = 20000;
295 private static final int ADVANCE_STAGGER = 250;
296
297 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 private long mAutoAdvanceSentTime;
299 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700301
Winson Chung400438b2011-01-16 17:53:48 -0800302 // Determines how long to wait after a rotation before restoring the screen orientation to
303 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700307
Adam Cohen1462de32012-07-24 22:34:36 -0700308 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700309 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700310
Sunny Goyala1365452015-10-01 15:46:24 -0700311 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
312 private static final Date sDateStamp = new Date();
313 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700314 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Winson Chungf0c6ae02012-03-21 16:10:31 -0700320 // Holds the page that we need to animate to, and the icon views that we need to animate up
321 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700323 private Bitmap mFolderIconBitmap;
324 private Canvas mFolderIconCanvas;
325 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700326
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700327 private DeviceProfile mDeviceProfile;
328
Adam Cohen4b66bf32015-09-18 12:15:19 -0700329 private boolean mMoveToDefaultScreenFromNewIntent;
330
Winson Chung13eb5272015-05-11 16:30:13 -0700331 // This is set to the view that launched the activity that navigated the user away from
332 // launcher. Since there is no callback for when the activity has finished launching, enable
333 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 private BubbleTextView mWaitingForResume;
335
Adam Cohen59400422014-03-05 18:07:04 -0800336 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
337 new HashMap<String, CustomAppWidget>();
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700340 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
341 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800342 }
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 Sandlercc8befa2013-06-11 14:45:48 -0400407 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700408
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700410 mDeviceProfile = getResources().getConfiguration().orientation
411 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700412 app.getInvariantDeviceProfile().landscapeProfile
413 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700414
Sunny Goyalf7258242015-10-19 16:59:07 -0700415 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700416 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800417 mModel = app.setLauncher(this);
418 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700419
Joe Onorato41a12d22009-10-31 18:30:00 -0400420 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700421 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Daniel Sandlerff02d492013-08-05 02:12:05 -0400423 mStats = new Stats(this);
424
Sunny Goyalffe83f12014-08-14 17:39:34 -0700425 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700441
Tony Wickhameef44322015-10-20 13:24:36 -0700442 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
443 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
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
Sunny Goyal08442b82015-10-21 17:15:08 -0700539 // overlay. So they shouldn'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()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700706 // User could have free-scrolled between pages before picking a wallpaper; make sure
707 // we move to the closest one now.
708 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700709 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200710 }
711 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200713
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700715 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700716
Adam Cohendb364c32014-05-20 14:23:40 -0700717 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 // We have special handling for widgets
719 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800720 final int appWidgetId;
721 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
722 : -1;
723 if (widgetId < 0) {
724 appWidgetId = pendingAddWidgetId;
725 } else {
726 appWidgetId = widgetId;
727 }
728
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800730 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700731 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
732 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 result = RESULT_CANCELED;
734 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700735 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 @Override
737 public void run() {
738 exitSpringLoadedDragModeDelayed(false, 0, null);
739 }
740 };
Adam Cohendb364c32014-05-20 14:23:40 -0700741 if (workspaceLocked) {
742 // No need to remove the empty screen if we're mid-binding, as the
743 // the bind will not add the empty screen.
744 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
745 } else {
746 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
747 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
748 }
Winson Chung5aaab772012-04-27 15:24:02 -0700749 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700750 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700751 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
752 // When the screen id represents an actual screen (as opposed to a rank)
753 // we make sure that the drop page actually exists.
754 mPendingAddInfo.screenId =
755 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
756 }
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
758
759 dropLayout.setDropPending(true);
760 final Runnable onComplete = new Runnable() {
761 @Override
762 public void run() {
763 completeTwoStageWidgetDrop(resultCode, appWidgetId);
764 dropLayout.setDropPending(false);
765 }
766 };
767 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
768 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
769 } else {
770 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
771 mPendingAddInfo);
772 sPendingAddItem = args;
773 }
Winson Chung5aaab772012-04-27 15:24:02 -0700774 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 return;
776 }
777
Sunny Goyalff572272014-07-23 13:58:07 -0700778 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
779 if (resultCode == RESULT_OK) {
780 // Update the widget view.
781 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
782 pendingAddWidgetId, mPendingAddInfo);
783 if (workspaceLocked) {
784 sPendingAddItem = args;
785 } else {
786 completeAdd(args);
787 }
788 }
789 // Leave the widget in the pending state if the user canceled the configure.
790 return;
791 }
792
Sunny Goyalaad90582015-07-09 14:22:50 -0700793 if (requestCode == REQUEST_CREATE_SHORTCUT) {
794 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
795 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
796 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
797 mPendingAddInfo);
798 if (isWorkspaceLocked()) {
799 sPendingAddItem = args;
800 } else {
801 completeAdd(args);
802 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
804 }
805 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800811
812 }
813
814 @Override
815 protected void onActivityResult(
816 final int requestCode, final int resultCode, final Intent data) {
817 handleActivityResult(requestCode, resultCode, data);
818 if (mLauncherCallbacks != null) {
819 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
820 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800821 }
822
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600823 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700824 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600825 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700826 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
827 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
828 View v = null;
829 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
830 if (layout != null) {
831 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
832 }
833 Intent intent = sPendingAddItem.intent;
834 sPendingAddItem = null;
835 if (grantResults.length > 0
836 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
837 startActivity(v, intent, null);
838 } else {
839 // TODO: Show a snack bar with link to settings
840 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
841 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
842 }
843 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600844 if (mLauncherCallbacks != null) {
845 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
846 grantResults);
847 }
848 }
849
Adam Cohendb364c32014-05-20 14:23:40 -0700850 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
851 appWidgetId, ItemInfo info) {
852 PendingAddArguments args = new PendingAddArguments();
853 args.requestCode = requestCode;
854 args.intent = data;
855 args.container = info.container;
856 args.screenId = info.screenId;
857 args.cellX = info.cellX;
858 args.cellY = info.cellY;
859 args.appWidgetId = appWidgetId;
860 return args;
861 }
862
863 /**
864 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
865 *
866 * @param screenId the screen id to check
867 * @return the new screen, or screenId if it exists
868 */
869 private long ensurePendingDropLayoutExists(long screenId) {
870 CellLayout dropLayout =
871 (CellLayout) mWorkspace.getScreenWithId(screenId);
872 if (dropLayout == null) {
873 // it's possible that the add screen was removed because it was
874 // empty and a re-bind occurred
875 mWorkspace.addExtraEmptyScreen();
876 return mWorkspace.commitExtraEmptyScreen();
877 } else {
878 return screenId;
879 }
880 }
881
Adam Cohen091440a2015-03-18 14:16:05 -0700882 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800883 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 Runnable onCompleteRunnable = null;
885 int animationType = 0;
886
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700887 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 if (resultCode == RESULT_OK) {
889 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
890 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 mPendingAddWidgetInfo);
892 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 onCompleteRunnable = new Runnable() {
894 @Override
895 public void run() {
896 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700897 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
899 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 }
901 };
902 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800903 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 if (mDragLayer.getAnimatedView() != null) {
907 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
908 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
909 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700911 // The animated view may be null in the case of a rotation during widget configuration
912 onCompleteRunnable.run();
913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700917 protected void onStop() {
918 super.onStop();
919 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700920
921 if (mLauncherCallbacks != null) {
922 mLauncherCallbacks.onStop();
923 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700924 }
925
926 @Override
927 protected void onStart() {
928 super.onStart();
929 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700930
931 if (mLauncherCallbacks != null) {
932 mLauncherCallbacks.onStart();
933 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700934 }
935
936 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200938 long startTime = 0;
939 if (DEBUG_RESUME_TIME) {
940 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400941 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200942 }
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.preOnResume();
946 }
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700949
Winson Chung4a2afa32012-07-19 14:53:05 -0700950 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700951 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700952 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800953 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700954 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700955 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700956 // view after launching an app, as they may be depending on the UI to be static to
957 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700958 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700959 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800960 } else if (mOnResumeState == State.WIDGETS) {
961 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 }
963 mOnResumeState = State.NONE;
964
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700965 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700966 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
967 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700968
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800969 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700970 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700971 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700972 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700974 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700976 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200977 // We might have postponed some bind calls until onResume (see waitUntilResume) --
978 // execute them here
979 long startTimeCallbacks = 0;
980 if (DEBUG_RESUME_TIME) {
981 startTimeCallbacks = System.currentTimeMillis();
982 }
983
Michael Jurka1e2f4652013-07-08 18:03:46 -0700984 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
985 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200986 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700987 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200988 if (DEBUG_RESUME_TIME) {
989 Log.d(TAG, "Time spent processing callbacks in onResume: " +
990 (System.currentTimeMillis() - startTimeCallbacks));
991 }
Michael Jurka447bf842013-05-15 14:52:15 +0200992 }
Michael Jurka54554252013-08-01 12:52:23 +0200993 if (mOnResumeCallbacks.size() > 0) {
994 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
995 mOnResumeCallbacks.get(i).run();
996 }
997 mOnResumeCallbacks.clear();
998 }
Winson Chunge4e50662012-01-23 14:45:13 -0800999
1000 // Reset the pressed state of icons that were locked in the press state while activities
1001 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001002 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001003 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001004 mWaitingForResume.setStayPressed(false);
1005 }
Winson Chung82963d52013-10-09 11:20:57 -07001006
Adam Cohen06dff352012-06-01 17:17:08 -07001007 // It is possible that widgets can receive updates while launcher is not in the foreground.
1008 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1009 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1010 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001011 if (!isWorkspaceLoading()) {
1012 getWorkspace().reinflateWidgetsIfNecessary();
1013 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001014 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001015
Michael Jurka447bf842013-05-15 14:52:15 +02001016 if (DEBUG_RESUME_TIME) {
1017 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1018 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001019
Adam Cohen4b66bf32015-09-18 12:15:19 -07001020 // We want to suppress callbacks about CustomContent being shown if we have just received
1021 // onNewIntent while the user was present within launcher. In that case, we post a call
1022 // to move the user to the main screen (which will occur after onResume). We don't want to
1023 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1024 // suppress here.
1025 if (mWorkspace.getCustomContentCallbacks() != null
1026 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001027 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001028 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001029 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1030 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001031 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001032 }
1033 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001034 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001035 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001036 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001037
Sunny Goyal756adbc2015-04-16 15:20:51 -07001038 if (!isWorkspaceLoading()) {
1039 // Process any items that were added while Launcher was away.
1040 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1041 }
Adam Cohen9211d422014-10-07 18:14:53 -07001042
1043 if (mLauncherCallbacks != null) {
1044 mLauncherCallbacks.onResume();
1045 }
Adam Cohen06dff352012-06-01 17:17:08 -07001046 }
1047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001050 // Ensure that items added to Launcher are queued until Launcher returns
1051 InstallShortcutReceiver.enableInstallQueue();
1052
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001054 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001055 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001056 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001057
1058 // We call onHide() aggressively. The custom content callbacks should be able to
1059 // debounce excess onHide calls.
1060 if (mWorkspace.getCustomContentCallbacks() != null) {
1061 mWorkspace.getCustomContentCallbacks().onHide();
1062 }
Romain Guycbb89e42009-06-08 15:52:54 -07001063
Adam Cohen9211d422014-10-07 18:14:53 -07001064 if (mLauncherCallbacks != null) {
1065 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001066 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 }
1068
1069 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001070 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1071 // by a onResume or by scrolling otherwise.
1072 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001073
1074 // Custom content is completely hidden
1075 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001076
1077 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1078 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001079
1080 // Indicates whether the user is allowed to scroll away from the custom content.
1081 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001082 }
1083
Adam Cohenc2d6e892014-10-16 09:49:24 -07001084 public interface LauncherOverlay {
1085
1086 /**
1087 * Touch interaction leading to overscroll has begun
1088 */
1089 public void onScrollInteractionBegin();
1090
1091 /**
1092 * Touch interaction related to overscroll has ended
1093 */
1094 public void onScrollInteractionEnd();
1095
1096 /**
1097 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1098 * screen (or in the case of RTL, the rightmost screen).
1099 */
1100 public void onScrollChange(int progress, boolean rtl);
1101
1102 /**
1103 * Screen has stopped scrolling
1104 */
1105 public void onScrollSettled();
1106
1107 /**
1108 * This method can be called by the Launcher in order to force the LauncherOverlay
1109 * to exit fully immersive mode.
1110 */
1111 public void forceExitFullImmersion();
1112 }
1113
Jun Mukai8af0cd82015-05-12 12:32:12 -07001114 public interface LauncherSearchCallbacks {
1115 /**
1116 * Called when the search overlay is shown.
1117 */
1118 public void onSearchOverlayOpened();
1119
1120 /**
1121 * Called when the search overlay is dismissed.
1122 */
1123 public void onSearchOverlayClosed();
1124 }
1125
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 public interface LauncherOverlayCallbacks {
1127 /**
1128 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1129 * however it doesn't modify any state within the launcher.
1130 */
1131 public boolean canEnterFullImmersion();
1132
1133 /**
1134 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1135 * eg. by occupying the full screen and handling all touch events.
1136 *
1137 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1138 * case, Launcher will modify any necessary state and assumes the overlay is
1139 * handling all interaction. If false, the LauncherOverlay should cancel any
1140 *
1141 */
1142 public boolean enterFullImmersion();
1143
1144 /**
1145 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1146 * full control over UI and state.
1147 */
1148 public void exitFullImmersion();
1149 }
1150
1151 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1152
1153 @Override
1154 public boolean canEnterFullImmersion() {
1155 return mState == State.WORKSPACE;
1156 }
1157
1158 @Override
1159 public boolean enterFullImmersion() {
1160 if (mState == State.WORKSPACE) {
1161 // When fully immersed, disregard any touches which fall through.
1162 mDragLayer.setBlockTouch(true);
1163 return true;
1164 }
1165 return false;
1166 }
1167
1168 @Override
1169 public void exitFullImmersion() {
1170 mDragLayer.setBlockTouch(false);
1171 }
1172 }
1173
Jorim Jaggid017f882014-01-14 17:08:48 -08001174 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001175 if (mLauncherCallbacks != null) {
1176 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001177 } else {
1178 // On devices with a locked orientation, we will at least have the allow rotation
1179 // setting.
1180 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001181 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001182 }
1183
Adam Cohen9211d422014-10-07 18:14:53 -07001184 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001185 CustomContentCallbacks callbacks, String description) {
1186 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001187 }
1188
Adam Cohenedb40762013-07-18 16:45:45 -07001189 // The custom content needs to offset its content to account for the QSB
1190 public int getTopOffsetForCustomContent() {
1191 return mWorkspace.getPaddingTop();
1192 }
1193
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001194 @Override
1195 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001197 if (mModel.isCurrentCallbacks(this)) {
1198 mModel.stopLoader();
1199 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001200 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1201
Romain Guy13c2e7b2010-03-10 19:45:00 -08001202 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001203 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001206 @Override
1207 public void onWindowFocusChanged(boolean hasFocus) {
1208 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001209 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001210
1211 if (mLauncherCallbacks != null) {
1212 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1213 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 private boolean acceptFilter() {
1217 final InputMethodManager inputManager = (InputMethodManager)
1218 getSystemService(Context.INPUT_METHOD_SERVICE);
1219 return !inputManager.isFullscreenMode();
1220 }
1221
1222 @Override
1223 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001224 final int uniChar = event.getUnicodeChar();
1225 final boolean handled = super.onKeyDown(keyCode, event);
1226 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1227 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1229 keyCode, event);
1230 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001232 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 // showSearchDialog()
1234 // If there are multiple keystrokes before the search dialog takes focus,
1235 // onSearchRequested() will be called for every keystroke,
1236 // but it is idempotent, so it's fine.
1237 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 }
1239 }
1240
Joe Onorato8a9625e2010-01-28 15:55:35 -08001241 // Eat the long press event so the keyboard doesn't come up.
1242 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1243 return true;
1244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 return handled;
1247 }
1248
Winson46163472015-09-22 17:31:56 -07001249 @Override
1250 public boolean onKeyUp(int keyCode, KeyEvent event) {
1251 if (keyCode == KeyEvent.KEYCODE_MENU) {
1252 // Ignore the menu key if we are currently dragging or are on the custom content screen
1253 if (!isOnCustomContent() && !mDragController.isDragging()) {
1254 // Close any open folders
1255 closeFolder();
1256
1257 // Stop resizing any widgets
1258 mWorkspace.exitWidgetResizeMode();
1259
1260 // Show the overview mode if we are on the workspace
1261 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1262 !mWorkspace.isSwitchingState()) {
1263 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001264 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001265 }
1266 }
1267 return true;
1268 }
1269 return super.onKeyUp(keyCode, event);
1270 }
1271
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 private String getTypedText() {
1273 return mDefaultKeySsb.toString();
1274 }
1275
1276 private void clearTypedText() {
1277 mDefaultKeySsb.clear();
1278 mDefaultKeySsb.clearSpans();
1279 Selection.setSelection(mDefaultKeySsb, 0);
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001283 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1284 * State
1285 */
1286 private static State intToState(int stateOrdinal) {
1287 State state = State.WORKSPACE;
1288 final State[] stateValues = State.values();
1289 for (int i = 0; i < stateValues.length; i++) {
1290 if (stateValues[i].ordinal() == stateOrdinal) {
1291 state = stateValues[i];
1292 break;
1293 }
1294 }
1295 return state;
1296 }
1297
1298 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 * Restores the previous state, if it exists.
1300 *
1301 * @param savedState The previous state.
1302 */
1303 private void restoreState(Bundle savedState) {
1304 if (savedState == null) {
1305 return;
1306 }
1307
Michael Jurka883f55b2010-10-21 15:47:14 -07001308 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001309 if (state == State.APPS || state == State.WIDGETS) {
1310 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001311 }
1312
Adam Cohen21cd0022013-10-09 18:57:02 -07001313 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1314 PagedView.INVALID_RESTORE_PAGE);
1315 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001316 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
Sunny Goyal756cd262015-08-20 12:33:21 -07001319 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1320 if (itemValues != null) {
1321 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001322 AppWidgetProviderInfo info = savedState.getParcelable(
1323 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001324 mPendingAddWidgetInfo = info == null ?
1325 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1326
Adam Cohen4637b5a2013-11-04 18:21:24 -08001327 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001328 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 /**
1334 * Finds all the views we need and configure them properly.
1335 */
1336 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001337 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001338
Craig Mautner360310b2012-10-26 15:13:08 -07001339 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001340 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001341 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1342 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001343 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001344 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001345
John Spurlock77e1f472013-09-11 10:09:51 -04001346 mLauncherView.setSystemUiVisibility(
1347 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
Winson Chung4c98d922011-05-31 16:50:48 -07001350 // Setup the drag layer
1351 mDragLayer.setup(this, dragController);
1352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 // Setup the hotseat
1354 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1355 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001356 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 }
1358
Winsone9f27272015-10-13 10:47:51 -07001359 // Setup the overview panel
1360 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001361
Winson Chung4c98d922011-05-31 16:50:48 -07001362 // Setup the workspace
1363 mWorkspace.setHapticFeedbackEnabled(false);
1364 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001365 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001366 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001367
Tony Wickham34d2c912015-09-11 09:27:58 -07001368 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001369 mSearchDropTargetBar = (SearchDropTargetBar)
1370 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001371 // Get the app info bar
1372 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1373 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001374
Winson Chungef7f8742015-06-04 17:18:17 -07001375 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001376 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001377 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001378 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1379 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1380 } else {
1381 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1382 }
Craig Mautner360310b2012-10-26 15:13:08 -07001383
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001385 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001386 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001387 dragController.setMoveTarget(mWorkspace);
1388 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001389 if (mSearchDropTargetBar != null) {
1390 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001391 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001392 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001393 if (mAppInfoDropTargetBar != null) {
1394 mAppInfoDropTargetBar.setup(this, dragController);
1395 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001396
Sunny Goyal322d5562015-06-25 19:35:49 -07001397 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1398 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
Winsone9f27272015-10-13 10:47:51 -07001402 private void setupOverviewPanel() {
1403 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1404
1405 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1406 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1407 @Override
1408 public boolean onLongClick(View v) {
1409 return v.performClick();
1410 }
1411 };
1412
1413 // Bind wallpaper button actions
1414 View wallpaperButton = findViewById(R.id.wallpaper_button);
1415 wallpaperButton.setOnClickListener(new OnClickListener() {
1416 @Override
1417 public void onClick(View view) {
1418 if (!mWorkspace.isSwitchingState()) {
1419 onClickWallpaperPicker(view);
1420 }
1421 }
1422 });
1423 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1424 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425
1426 // Bind widget button actions
1427 mWidgetsButton = findViewById(R.id.widget_button);
1428 mWidgetsButton.setOnClickListener(new OnClickListener() {
1429 @Override
1430 public void onClick(View view) {
1431 if (!mWorkspace.isSwitchingState()) {
1432 onClickAddWidgetButton(view);
1433 }
1434 }
1435 });
1436 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1437 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 // Bind settings actions
1440 View settingsButton = findViewById(R.id.settings_button);
1441 boolean hasSettings = hasSettings();
1442 if (hasSettings) {
1443 settingsButton.setOnClickListener(new OnClickListener() {
1444 @Override
1445 public void onClick(View view) {
1446 if (!mWorkspace.isSwitchingState()) {
1447 onClickSettingsButton(view);
1448 }
1449 }
1450 });
1451 settingsButton.setOnLongClickListener(performClickOnLongClick);
1452 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1453 } else {
1454 settingsButton.setVisibility(View.GONE);
1455 }
1456
1457 mOverviewPanel.setAlpha(0f);
1458 }
1459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001461 * Sets the all apps button. This method is called from {@link Hotseat}.
1462 */
1463 public void setAllAppsButton(View allAppsButton) {
1464 mAllAppsButton = allAppsButton;
1465 }
1466
1467 public View getAllAppsButton() {
1468 return mAllAppsButton;
1469 }
1470
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001471 public View getWidgetsButton() {
1472 return mWidgetsButton;
1473 }
1474
Anjali Koppal5ad44842014-03-10 20:34:39 -07001475 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * Creates a view representing a shortcut.
1477 *
1478 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001480 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
1484 /**
1485 * Creates a view representing a shortcut inflated from the specified resource.
1486 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 * @param parent The group the shortcut belongs to.
1488 * @param info The data structure describing the shortcut.
1489 *
1490 * @return A View inflated from layoutResId.
1491 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001493 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001494 parent, false);
1495 favorite.applyFromShortcutInfo(info, mIconCache);
1496 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001498 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 return favorite;
1500 }
1501
1502 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 * Add a shortcut to the workspace.
1504 *
1505 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001508 int cellY) {
1509 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Sunny Goyal5c97f512015-05-19 16:03:28 -07001512 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001513 if (info == null) {
1514 return;
1515 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 final View view = createShortcut(info);
1517
Sunny Goyal5c97f512015-05-19 16:03:28 -07001518 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 // First we check if we already know the exact location where we want to add this item.
1520 if (cellX >= 0 && cellY >= 0) {
1521 cellXY[0] = cellX;
1522 cellXY[1] = cellY;
1523 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001524
1525 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001526 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001527 true, null,null)) {
1528 return;
1529 }
1530 DragObject dragObject = new DragObject();
1531 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001532 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1533 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 return;
1535 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001537 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 }
1539
1540 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001541 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 return;
1543 }
1544
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001545 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
1547 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
1552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001554 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001556 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 */
Adam Cohen091440a2015-03-18 14:16:05 -07001558 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001559 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1560
1561 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001563 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1564 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Adam Cohen59400422014-03-05 18:07:04 -08001567 if (appWidgetInfo.isCustomWidget) {
1568 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001569 }
1570
Adam Cohen59400422014-03-05 18:07:04 -08001571 LauncherAppWidgetInfo launcherInfo;
1572 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1573 launcherInfo.spanX = info.spanX;
1574 launcherInfo.spanY = info.spanY;
1575 launcherInfo.minSpanX = info.minSpanX;
1576 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001577 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001580 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581
1582 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 if (hostView == null) {
1584 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001585 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1586 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 } else {
1588 // The AppWidgetHostView has already been inflated and instantiated
1589 launcherInfo.hostView = hostView;
1590 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001594 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1598 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1599 item.hostView.setTag(item);
1600 item.onBindAppWidget(this);
1601
1602 item.hostView.setFocusable(true);
1603 item.hostView.setOnFocusChangeListener(mFocusHandler);
1604
1605 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1606 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1607
1608 if (!item.isCustomWidget()) {
1609 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1610 }
1611 }
1612
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1614 @Override
1615 public void onReceive(Context context, Intent intent) {
1616 final String action = intent.getAction();
1617 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1618 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001619 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001620 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001621
Winson Chungc100e8e2011-08-09 16:02:43 -07001622 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001623 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001624 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001625 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001626 if (!showWorkspace(false)) {
1627 // If we are already on the workspace, then manually reset all apps
1628 mAppsView.reset();
1629 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1632 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001633 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001634 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1635 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001636 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001637 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1638 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1639 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001640 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1642 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 }
1644 }
1645 };
1646
1647 @Override
1648 public void onAttachedToWindow() {
1649 super.onAttachedToWindow();
1650
1651 // Listen for broadcasts related to user-presence
1652 final IntentFilter filter = new IntentFilter();
1653 filter.addAction(Intent.ACTION_SCREEN_OFF);
1654 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001655 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001656 if (ENABLE_DEBUG_INTENTS) {
1657 filter.addAction(DebugIntents.DELETE_DATABASE);
1658 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1659 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001662 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 mVisible = true;
1664 }
1665
1666 @Override
1667 public void onDetachedFromWindow() {
1668 super.onDetachedFromWindow();
1669 mVisible = false;
1670
Adam Cohend113e0c2010-11-11 10:48:05 -08001671 if (mAttached) {
1672 unregisterReceiver(mReceiver);
1673 mAttached = false;
1674 }
Winson Chungb745afb2015-03-02 11:51:23 -08001675 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001676 }
1677
1678 public void onWindowVisibilityChanged(int visibility) {
1679 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001680 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 // The following code used to be in onResume, but it turns out onResume is called when
1682 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1683 // is a more appropriate event to handle
1684 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001685 if (!mWorkspaceLoading) {
1686 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001687 // We want to let Launcher draw itself at least once before we force it to build
1688 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 // apps is nice and speedy.
1690 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001691 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001692 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001693 if (mStarted) return;
1694 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001695 // We delay the layer building a bit in order to give
1696 // other message processing a time to run. In particular
1697 // this avoids a delay in hiding the IME if it was
1698 // currently shown, because doing that may involve
1699 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001700 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 final ViewTreeObserver.OnDrawListener listener = this;
1702 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001703 public void run() {
1704 if (mWorkspace != null &&
1705 mWorkspace.getViewTreeObserver() != null) {
1706 mWorkspace.getViewTreeObserver().
1707 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001708 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001709 }
1710 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001711 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001712 }
1713 });
1714 }
1715 clearTypedText();
1716 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 }
1718
Adam Cohen091440a2015-03-18 14:16:05 -07001719 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 mHandler.removeMessages(ADVANCE_MSG);
1721 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1722 mHandler.sendMessageDelayed(msg, delay);
1723 mAutoAdvanceSentTime = System.currentTimeMillis();
1724 }
1725
Adam Cohen091440a2015-03-18 14:16:05 -07001726 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1728 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1729 mAutoAdvanceRunning = autoAdvanceRunning;
1730 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 sendAdvanceMessage(delay);
1733 } else {
1734 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001735 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1737 }
1738 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001739 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741 }
1742 }
1743
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1745
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 if (msg.what == ADVANCE_MSG) {
1749 int i = 0;
1750 for (View key: mWidgetsToAdvance.keySet()) {
1751 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001752 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 public void run() {
1756 ((Advanceable) v).advance();
1757 }
1758 }, delay);
1759 }
1760 i++;
1761 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001762 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001764 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001766 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001767
Winsonc0b52fe2015-09-09 16:38:15 -07001768 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001769 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1771 if (v instanceof Advanceable) {
1772 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001773 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001774 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
1776 }
1777
Winsonc0b52fe2015-09-09 16:38:15 -07001778 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 if (mWidgetsToAdvance.containsKey(hostView)) {
1780 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001781 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001783 }
1784
Hyunyoung Song3f471442015-04-08 19:01:34 -07001785 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001786 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1787 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
1789
Winson Chunga6945242014-01-08 14:04:34 -08001790 public DragLayer getDragLayer() {
1791 return mDragLayer;
1792 }
1793
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001794 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001795 return mAppsView;
1796 }
1797
Hyunyoung Song3f471442015-04-08 19:01:34 -07001798 public WidgetsContainerView getWidgetsView() {
1799 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001800 }
1801
Winson Chunga6945242014-01-08 14:04:34 -08001802 public Workspace getWorkspace() {
1803 return mWorkspace;
1804 }
1805
1806 public Hotseat getHotseat() {
1807 return mHotseat;
1808 }
1809
Jorim Jaggid017f882014-01-14 17:08:48 -08001810 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001811 return mOverviewPanel;
1812 }
1813
Winson Chung006ee262015-08-03 14:40:11 -07001814 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001815 return mSearchDropTargetBar;
1816 }
1817
Tony Wickham34d2c912015-09-11 09:27:58 -07001818 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1819 return mAppInfoDropTargetBar;
1820 }
1821
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001822 public LauncherAppWidgetHost getAppWidgetHost() {
1823 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Winson Chunga9abd0e2010-10-27 17:18:37 -07001826 public LauncherModel getModel() {
1827 return mModel;
1828 }
1829
Winson Chunga6945242014-01-08 14:04:34 -08001830 protected SharedPreferences getSharedPrefs() {
1831 return mSharedPrefs;
1832 }
1833
Adam Cohen2e6da152015-05-06 11:42:25 -07001834 public DeviceProfile getDeviceProfile() {
1835 return mDeviceProfile;
1836 }
1837
Bjorn Bringertc459e522013-06-07 19:36:01 +01001838 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 getWindow().closeAllPanels();
1840
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001841 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001842 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 @Override
1846 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001847 long startTime = 0;
1848 if (DEBUG_RESUME_TIME) {
1849 startTime = System.currentTimeMillis();
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 super.onNewIntent(intent);
1852
1853 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001854 Folder openFolder = mWorkspace.getOpenFolder();
1855 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1856 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1857 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1858 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1859 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001860 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001861 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001862
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 if (mWorkspace == null) {
1864 // Can be cases where mWorkspace is null, this prevents a NPE
1865 return;
1866 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 // In all these cases, only animate if we're already on home
1868 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001869
Sunny Goyal935fca12015-10-13 10:19:01 -07001870 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001871 exitSpringLoadedDragMode();
1872
1873 // If we are already on home, then just animate back to the workspace,
1874 // otherwise, just wait until onResume to set the state back to Workspace
1875 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001876 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 } else {
1878 mOnResumeState = State.WORKSPACE;
1879 }
1880
1881 final View v = getWindow().peekDecorView();
1882 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001883 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 INPUT_METHOD_SERVICE);
1885 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1886 }
1887
Winson Chungb745afb2015-03-02 11:51:23 -08001888 // Reset the apps view
1889 if (!alreadyOnHome && mAppsView != null) {
1890 mAppsView.scrollToTop();
1891 }
1892
Hyunyoung Song3f471442015-04-08 19:01:34 -07001893 // Reset the widgets view
1894 if (!alreadyOnHome && mWidgetsView != null) {
1895 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001896 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001897
Adam Cohen9211d422014-10-07 18:14:53 -07001898 if (mLauncherCallbacks != null) {
1899 mLauncherCallbacks.onHomeIntent();
1900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
Adam Cohened307df2013-10-02 09:37:31 -07001902
Adam Cohen9211d422014-10-07 18:14:53 -07001903 if (mLauncherCallbacks != null) {
1904 mLauncherCallbacks.onNewIntent(intent);
1905 }
Winson15f8b172015-08-19 11:02:39 -07001906
1907 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1908 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1909 // animation.
1910 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001911 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1912 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001913 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1914 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001915
1916 // We use this flag to suppress noisy callbacks above custom content state
1917 // from onResume.
1918 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001919 mWorkspace.post(new Runnable() {
1920 @Override
1921 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001922 if (mWorkspace != null) {
1923 mWorkspace.moveToDefaultScreen(true);
1924 }
Winson15f8b172015-08-19 11:02:39 -07001925 }
1926 });
1927 }
1928 }
1929
1930 if (DEBUG_RESUME_TIME) {
1931 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1932 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001933 }
1934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001936 public void onRestoreInstanceState(Bundle state) {
1937 super.onRestoreInstanceState(state);
1938 for (int page: mSynchronouslyBoundPages) {
1939 mWorkspace.restoreInstanceStateForChild(page);
1940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942
1943 @Override
1944 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001945 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001946 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1947 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001948 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001949 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950
Michael Jurka883f55b2010-10-21 15:47:14 -07001951 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001952 Folder openFolder = mWorkspace.getOpenFolder();
1953 if (openFolder != null) {
1954 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001959 ContentValues itemValues = new ContentValues();
1960 mPendingAddInfo.writeToValues(itemValues);
1961 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001962 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001963 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
Hyunyoung Song3f471442015-04-08 19:01:34 -07001966 // Save the current widgets tray?
1967 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001968
1969 if (mLauncherCallbacks != null) {
1970 mLauncherCallbacks.onSaveInstanceState(outState);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001977
Winson Chunge7a03942011-08-05 15:05:12 -07001978 // Remove all pending runnables
1979 mHandler.removeMessages(ADVANCE_MSG);
1980 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001981 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001982
Winson Chungcd2b0142011-06-08 16:02:26 -07001983 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001984 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001985
1986 // It's possible to receive onDestroy after a new Launcher activity has
1987 // been created. In this case, don't interfere with the new Launcher.
1988 if (mModel.isCurrentCallbacks(this)) {
1989 mModel.stopLoader();
1990 app.setLauncher(null);
1991 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001994 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001996 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001998 mAppWidgetHost = null;
1999
2000 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
2002 TextKeyListener.getInstance().release();
2003
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002004 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002005
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002006 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002007 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002008 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009 mWorkspace = null;
2010 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002011
Michael Jurka2ecf9952012-06-18 12:52:28 -07002012 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onDestroy();
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 public DragController getDragController() {
2020 return mDragController;
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 @Override
2024 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002025 onStartForResult(requestCode);
2026 super.startActivityForResult(intent, requestCode);
2027 }
2028
2029 @Override
2030 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2031 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2032 onStartForResult(requestCode);
2033 try {
2034 super.startIntentSenderForResult(intent, requestCode,
2035 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2036 } catch (IntentSender.SendIntentException e) {
2037 throw new ActivityNotFoundException();
2038 }
2039 }
2040
2041 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002042 if (requestCode >= 0) {
2043 setWaitingForResult(true);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Winson Chung70d72102011-08-12 11:24:35 -07002047 /**
2048 * Indicates that we want global search for this activity by setting the globalSearch
2049 * argument for {@link #startSearch} to true.
2050 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002052 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002054
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 if (initialQuery == null) {
2056 // Use any text typed in the launcher as the initial query
2057 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 if (appSearchData == null) {
2060 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002061 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
Winson Chungadf0c182012-08-23 14:59:07 -07002063 Rect sourceBounds = new Rect();
2064 if (mSearchDropTargetBar != null) {
2065 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2066 }
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 if (clearTextImmediately) {
2071 clearTypedText();
2072 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002073
2074 // We need to show the workspace after starting the search
2075 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 }
2077
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 /**
2079 * Start a text search.
2080 *
2081 * @return {@code true} if the search will start immediately, so any further keypresses
2082 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2083 * to buffer keypresses.
2084 */
2085 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002087 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2088 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2089 sourceBounds);
2090 }
2091
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002094 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 }
2096
2097 /**
2098 * Starts the global search activity. This code is a copied from SearchManager
2099 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2105 if (globalSearchActivity == null) {
2106 Log.w(TAG, "No global search activity found.");
2107 return;
2108 }
2109 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2110 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2111 intent.setComponent(globalSearchActivity);
2112 // Make sure that we have a Bundle to put source in
2113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
2115 } else {
2116 appSearchData = new Bundle(appSearchData);
2117 }
Adam Cohen9211d422014-10-07 18:14:53 -07002118 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002119 if (!appSearchData.containsKey("source")) {
2120 appSearchData.putString("source", getPackageName());
2121 }
2122 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2123 if (!TextUtils.isEmpty(initialQuery)) {
2124 intent.putExtra(SearchManager.QUERY, initialQuery);
2125 }
2126 if (selectInitialQuery) {
2127 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2128 }
2129 intent.setSourceBounds(sourceBounds);
2130 try {
2131 startActivity(intent);
2132 } catch (ActivityNotFoundException ex) {
2133 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136
Winson Chungbedf9232015-07-10 12:38:30 -07002137 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2138 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2139 return;
2140 }
2141
2142 // If not handled, then just start the provided search intent
2143 startActivitySafely(v, searchIntent, null);
2144 }
2145
Yura4f93ec62014-02-04 14:15:21 +00002146 public boolean isOnCustomContent() {
2147 return mWorkspace.isOnOrMovingToCustomContent();
2148 }
2149
Winson Chung70d72102011-08-12 11:24:35 -07002150 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002151 public boolean onPrepareOptionsMenu(Menu menu) {
2152 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002153 if (mLauncherCallbacks != null) {
2154 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2155 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002156 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002159 @Override
2160 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002161 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002162 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002163 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002164 }
2165
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 public boolean isWorkspaceLocked() {
2167 return mWorkspaceLoading || mWaitingForResult;
2168 }
2169
Adam Cohen517a7f52014-03-01 12:12:59 -08002170 public boolean isWorkspaceLoading() {
2171 return mWorkspaceLoading;
2172 }
2173
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002174 private void setWorkspaceLoading(boolean value) {
2175 boolean isLocked = isWorkspaceLocked();
2176 mWorkspaceLoading = value;
2177 if (isLocked != isWorkspaceLocked()) {
2178 onWorkspaceLockedChanged();
2179 }
2180 }
2181
2182 private void setWaitingForResult(boolean value) {
2183 boolean isLocked = isWorkspaceLocked();
2184 mWaitingForResult = value;
2185 if (isLocked != isWorkspaceLocked()) {
2186 onWorkspaceLockedChanged();
2187 }
2188 }
2189
Adam Cohen9211d422014-10-07 18:14:53 -07002190 protected void onWorkspaceLockedChanged() {
2191 if (mLauncherCallbacks != null) {
2192 mLauncherCallbacks.onWorkspaceLockedChanged();
2193 }
2194 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002195
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002197 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002198 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002199 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2200 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002201 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002202 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002203
Tony Wickhama0628cc2015-10-14 15:23:04 -07002204 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002205 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002206 if (LOGD) {
2207 Log.d(TAG, "Adding widget from drop");
2208 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002209 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2210 }
2211
Sunny Goyale6b63a32015-01-16 16:14:29 -08002212 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002213 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2214 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002215 if (appWidgetInfo.configure != null) {
2216 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002217 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002218
Bjorn Bringert7984c942009-12-09 15:38:25 +00002219 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002220 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2221 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002224 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002225 Runnable onComplete = new Runnable() {
2226 @Override
2227 public void run() {
2228 // Exit spring loaded mode if necessary after adding the widget
2229 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2230 null);
2231 }
2232 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002233 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002234 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002235 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 }
2237 }
Romain Guycbb89e42009-06-08 15:52:54 -07002238
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002239 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002240 // Close any folders that may be open.
2241 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002242 mWorkspace.moveToCustomContentScreen(animate);
2243 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002244
2245 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2246 int[] cell, int spanX, int spanY) {
2247 switch (info.itemType) {
2248 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2249 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2250 int span[] = new int[2];
2251 span[0] = spanX;
2252 span[1] = spanY;
2253 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2254 container, screenId, cell, span);
2255 break;
2256 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2257 processShortcutFromDrop(info.componentName, container, screenId, cell);
2258 break;
2259 default:
2260 throw new IllegalStateException("Unknown item type: " + info.itemType);
2261 }
2262 }
2263
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 /**
2265 * Process a shortcut drop.
2266 *
2267 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002271 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2272 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002273 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002274 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276
2277 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.cellX = cell[0];
2279 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281
2282 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2283 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002284 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285 }
2286
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 /**
2288 * Process a widget drop.
2289 *
2290 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002294 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2295 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002299 mPendingAddInfo.minSpanX = info.minSpanX;
2300 mPendingAddInfo.minSpanY = info.minSpanY;
2301
Adam Cohenfbba09b2011-07-18 21:43:05 -07002302 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 mPendingAddInfo.cellX = cell[0];
2304 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002306 if (span != null) {
2307 mPendingAddInfo.spanX = span[0];
2308 mPendingAddInfo.spanY = span[1];
2309 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002310
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 AppWidgetHostView hostView = info.boundWidget;
2312 int appWidgetId;
2313 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002314 // In the case where we've prebound the widget, we remove it from the DragLayer
2315 if (LOGD) {
2316 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2317 }
2318 getDragLayer().removeView(hostView);
2319
Adam Cohened66b2b2012-01-23 17:28:51 -08002320 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002321 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002322
2323 // Clear the boundWidget so that it doesn't get destroyed.
2324 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002325 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002326 // In this case, we either need to start an activity to get permission to bind
2327 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002329 Bundle options = info.bindOptions;
2330
Sunny Goyalffe83f12014-08-14 17:39:34 -07002331 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2332 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002333 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002334 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002335 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002336 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002337 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2338 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2339 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002340 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2341 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002342 // TODO: we need to make sure that this accounts for the options bundle.
2343 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002344 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2345 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002346 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 }
2348
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002350 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002351 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 folderInfo.title = getText(R.string.folder_name);
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002355 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2356 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002357 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358
2359 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 FolderIcon newFolder =
2361 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002364 // Force measure the new folder icon
2365 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2366 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002367 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 }
Romain Guycbb89e42009-06-08 15:52:54 -07002369
Winsonc0b52fe2015-09-09 16:38:15 -07002370 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002371 * Unbinds the view for the specified item, and removes the item and all its children.
2372 *
2373 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002374 * @param itemInfo the {@link ItemInfo} for this view.
2375 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002376 */
Winson2949fb52015-09-24 09:56:11 -07002377 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002378 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002379 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002380 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2381 if (folderInfo != null) {
2382 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002383 } else {
2384 mWorkspace.removeWorkspaceItem(v);
2385 }
Winsonc0b52fe2015-09-09 16:38:15 -07002386 if (deleteFromDb) {
2387 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2388 }
2389 } else if (itemInfo instanceof FolderInfo) {
2390 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2391 unbindFolder(folderInfo);
2392 mWorkspace.removeWorkspaceItem(v);
2393 if (deleteFromDb) {
2394 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2395 }
2396 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2397 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002398 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 removeWidgetToAutoAdvance(widgetInfo.hostView);
2400 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002401 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002402 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002403 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002404
Winsonc0b52fe2015-09-09 16:38:15 -07002405 } else {
2406 return false;
2407 }
2408 return true;
2409 }
2410
2411 /**
2412 * Unbinds any launcher references to the folder.
2413 */
2414 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002415 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002416 }
2417
Winsonc0b52fe2015-09-09 16:38:15 -07002418 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002419 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002420 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002421 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002422 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002423 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002424 // Deleting an app widget ID is a void call but writes to disk before returning
2425 // to the caller...
2426 new AsyncTask<Void, Void, Void>() {
2427 public Void doInBackground(Void ... args) {
2428 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2429 return null;
2430 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002431 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002432 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002433 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002434 }
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 @Override
2437 public boolean dispatchKeyEvent(KeyEvent event) {
2438 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2439 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002441 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002442 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002443 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002444 dumpState();
2445 return true;
2446 }
2447 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 }
2449 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2450 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002451 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 return true;
2453 }
2454 }
2455
2456 return super.dispatchKeyEvent(event);
2457 }
2458
Joe Onorato88ec0992009-11-19 13:16:06 -08002459 @Override
2460 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002461 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2462 return;
2463 }
2464
Sunny Goyal45478022015-06-08 16:52:41 -07002465 if (mDragController.isDragging()) {
2466 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002467 return;
2468 }
2469
Winson Chungb745afb2015-03-02 11:51:23 -08002470 if (isAppsViewVisible()) {
2471 showWorkspace(true);
2472 } else if (isWidgetsViewVisible()) {
2473 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002474 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002475 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002477 Folder openFolder = mWorkspace.getOpenFolder();
2478 if (openFolder.isEditingName()) {
2479 openFolder.dismissEditingName();
2480 } else {
2481 closeFolder();
2482 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002484 mWorkspace.exitWidgetResizeMode();
2485
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002486 // Back button is a no-op here, but give at least some feedback for the button press
2487 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002488 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002492 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002493 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002494 @Override
2495 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002496 if (mAppWidgetHost != null) {
2497 mAppWidgetHost.startListening();
2498 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002499
2500 // Recreate the QSB, as the widget has been reset.
2501 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002502 }
2503
2504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 * Launches the intent referred by the clicked shortcut.
2506 *
2507 * @param v The view representing the clicked shortcut.
2508 */
2509 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002510 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2511 // view has detached (it's possible for this to happen if the view is removed mid touch).
2512 if (v.getWindowToken() == null) {
2513 return;
2514 }
2515
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002516 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002517 return;
2518 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002519
Adam Cohen1697b792013-09-17 19:08:21 -07002520 if (v instanceof Workspace) {
2521 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002522 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002523 }
2524 return;
2525 }
2526
2527 if (v instanceof CellLayout) {
2528 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002529 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2530 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002531 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002532 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002536 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002539 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002540 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002542 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002544 } else if (tag instanceof AppInfo) {
2545 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002546 } else if (tag instanceof LauncherAppWidgetInfo) {
2547 if (v instanceof PendingAppWidgetHostView) {
2548 onClickPendingWidget((PendingAppWidgetHostView) v);
2549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 }
2551 }
2552
Sunny Goyal70660032015-05-14 00:07:08 -07002553 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002554 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002555 return false;
2556 }
2557
Michael Jurkaaf442092010-06-10 17:01:57 -07002558 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002559 * Event handler for the app widget view which has not fully restored.
2560 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002561 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002562 if (mIsSafeModeEnabled) {
2563 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2564 return;
2565 }
2566
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002568 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002569 int widgetId = info.appWidgetId;
2570 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2571 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002572 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2573 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 mPendingAddInfo.copyFrom(info);
2575 mPendingAddWidgetId = widgetId;
2576
Sunny Goyalffe83f12014-08-14 17:39:34 -07002577 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2578 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002579 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002580 } else if (info.installProgress < 0) {
2581 // The install has not been queued
2582 final String packageName = info.providerName.getPackageName();
2583 showBrokenAppInstallDialog(packageName,
2584 new DialogInterface.OnClickListener() {
2585 public void onClick(DialogInterface dialog, int id) {
2586 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2587 }
2588 });
2589 } else {
2590 // Download has started.
2591 final String packageName = info.providerName.getPackageName();
2592 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002593 }
2594 }
2595
2596 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002597 * Event handler for the "grid" button that appears on the home screen, which
2598 * enters all apps mode.
2599 *
2600 * @param v The view that was clicked.
2601 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 protected void onClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002604 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002605 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002606 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002607
2608 if (mLauncherCallbacks != null) {
2609 mLauncherCallbacks.onClickAllAppsButton(v);
2610 }
Winson Chung76648c52015-07-10 14:33:23 -07002611 }
2612 }
2613
2614 protected void onLongClickAllAppsButton(View v) {
2615 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2616 if (!isAppsViewVisible()) {
2617 showAppsView(true /* animated */, false /* resetListToTop */,
2618 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 }
2620 }
2621
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 private void showBrokenAppInstallDialog(final String packageName,
2623 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002624 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 .setTitle(R.string.abandoned_promises_title)
2626 .setMessage(R.string.abandoned_promise_explanation)
2627 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2628 .setNeutralButton(R.string.abandoned_clean_this,
2629 new DialogInterface.OnClickListener() {
2630 public void onClick(DialogInterface dialog, int id) {
2631 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2632 mWorkspace.removeAbandonedPromise(packageName, user);
2633 }
2634 })
2635 .create().show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 /**
2640 * Event handler for an app shortcut click.
2641 *
2642 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2643 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2646 Object tag = v.getTag();
2647 if (!(tag instanceof ShortcutInfo)) {
2648 throw new IllegalArgumentException("Input must be a Shortcut");
2649 }
2650
2651 // Open shortcut
2652 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002653
2654 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002655 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2656 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002657 // Launch activity anyway, framework will tell the user why the app is suspended.
2658 } else {
2659 int error = R.string.activity_not_available;
2660 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2661 error = R.string.safemode_shortcut_error;
2662 }
2663 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2664 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002665 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002666 }
2667
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002669 if ((v instanceof BubbleTextView)
2670 && shortcut.isPromise()
2671 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002672 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002673 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 new DialogInterface.OnClickListener() {
2675 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002676 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 return;
2680 }
2681
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002682 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002683 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002684
2685 if (mLauncherCallbacks != null) {
2686 mLauncherCallbacks.onClickAppShortcut(v);
2687 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 }
2689
Adam Cohen091440a2015-03-18 14:16:05 -07002690 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002692 final ShortcutInfo shortcut;
2693 final Intent intent;
2694 if (tag instanceof ShortcutInfo) {
2695 shortcut = (ShortcutInfo) tag;
2696 intent = shortcut.intent;
2697 int[] pos = new int[2];
2698 v.getLocationOnScreen(pos);
2699 intent.setSourceBounds(new Rect(pos[0], pos[1],
2700 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002701
Sunny Goyal508da152014-08-14 10:53:27 -07002702 } else if (tag instanceof AppInfo) {
2703 shortcut = null;
2704 intent = ((AppInfo) tag).intent;
2705 } else {
2706 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2707 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708
2709 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002710 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711
2712 if (success && v instanceof BubbleTextView) {
2713 mWaitingForResume = (BubbleTextView) v;
2714 mWaitingForResume.setStayPressed(true);
2715 }
2716 }
2717
2718 /**
2719 * Event handler for a folder icon click.
2720 *
2721 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2722 */
2723 protected void onClickFolderIcon(View v) {
2724 if (LOGD) Log.d(TAG, "onClickFolder");
2725 if (!(v instanceof FolderIcon)){
2726 throw new IllegalArgumentException("Input must be a FolderIcon");
2727 }
2728
2729 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002730 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002732 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 }
Adam Cohen9211d422014-10-07 18:14:53 -07002734
2735 if (mLauncherCallbacks != null) {
2736 mLauncherCallbacks.onClickFolderIcon(v);
2737 }
Michael Jurka5130e402011-10-13 04:55:35 -07002738 }
2739
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002740 /**
2741 * Event handler for the (Add) Widgets button that appears after a long press
2742 * on the home screen.
2743 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002744 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002746 if (mIsSafeModeEnabled) {
2747 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2748 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002749 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002750 if (mLauncherCallbacks != null) {
2751 mLauncherCallbacks.onClickAddWidgetButton(view);
2752 }
Adam Cohen9211d422014-10-07 18:14:53 -07002753 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002754 }
2755
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002756 /**
2757 * Event handler for the wallpaper picker button that appears after a long press
2758 * on the home screen.
2759 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002760 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002761 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002762 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2763 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2764 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2765 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002766 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002767
2768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onClickWallpaperPicker(v);
2770 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002771 }
2772
2773 /**
2774 * Event handler for a click on the settings button that appears after a long press
2775 * on the home screen.
2776 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002777 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002781 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002782 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002783 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002784 }
2785
Adam Cohen61f560d2013-09-30 15:58:20 -07002786 public View.OnTouchListener getHapticFeedbackTouchListener() {
2787 if (mHapticFeedbackTouchListener == null) {
2788 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002789 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002790 @Override
2791 public boolean onTouch(View v, MotionEvent event) {
2792 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2793 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2794 }
2795 return false;
2796 }
2797 };
2798 }
2799 return mHapticFeedbackTouchListener;
2800 }
2801
Adam Cohen9211d422014-10-07 18:14:53 -07002802 public void onDragStarted(View view) {
2803 if (isOnCustomContent()) {
2804 // Custom content screen doesn't participate in drag and drop. If on custom
2805 // content screen, move to default.
2806 moveWorkspaceToDefaultScreen();
2807 }
2808
2809 if (mLauncherCallbacks != null) {
2810 mLauncherCallbacks.onDragStarted(view);
2811 }
2812 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002813
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002814 /**
2815 * Called when the user stops interacting with the launcher.
2816 * This implies that the user is now on the homescreen and is not doing housekeeping.
2817 */
Adam Cohen9211d422014-10-07 18:14:53 -07002818 protected void onInteractionEnd() {
2819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onInteractionEnd();
2821 }
2822 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002823
2824 /**
2825 * Called when the user starts interacting with the launcher.
2826 * The possible interactions are:
2827 * - open all apps
2828 * - reorder an app shortcut, or a widget
2829 * - open the overview mode.
2830 * This is a good time to stop doing things that only make sense
2831 * when the user is on the homescreen and not doing housekeeping.
2832 */
Adam Cohen9211d422014-10-07 18:14:53 -07002833 protected void onInteractionBegin() {
2834 if (mLauncherCallbacks != null) {
2835 mLauncherCallbacks.onInteractionBegin();
2836 }
2837 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002838
Winson Chungcd99cd32015-04-29 11:03:24 -07002839 /** Updates the interaction state. */
2840 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002841 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002842 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002843 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2844 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002845 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002846 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002847 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002848 onInteractionEnd();
2849 }
2850 }
2851
Kenny Guyf07af7b2014-07-31 11:39:16 +01002852 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002853 try {
2854 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002855 launcherApps.showAppDetailsForProfile(componentName, user);
2856 } catch (SecurityException e) {
2857 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2858 Log.e(TAG, "Launcher does not have permission to launch settings");
2859 } catch (ActivityNotFoundException e) {
2860 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2861 Log.e(TAG, "Unable to launch settings");
2862 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002863 }
2864
Michael Jurka1e2f4652013-07-08 18:03:46 -07002865 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002866 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2867 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002868 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002869 // System applications cannot be installed. For now, show a toast explaining that.
2870 // We may give them the option of disabling apps this way.
2871 int messageId = R.string.uninstall_system_app_text;
2872 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002875 String packageName = componentName.getPackageName();
2876 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002877 Intent intent = new Intent(
2878 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002879 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2880 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002881 if (user != null) {
2882 user.addToIntent(intent, Intent.EXTRA_USER);
2883 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002885 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002886 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002887 }
2888
Winson Chung8f1eff72015-05-28 17:33:40 -07002889 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002890 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002892 // Only launch using the new animation if the shortcut has not opted out (this is a
2893 // private contract between launcher and may be ignored in the future).
2894 boolean useLaunchAnimation = (v != null) &&
2895 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002896 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2897 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002898
Kenny Guy1317e2d2014-05-08 18:52:50 +01002899 UserHandleCompat user = null;
2900 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2901 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2902 user = userManager.getUserForSerialNumber(serialNumber);
2903 }
2904
2905 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002906 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002907 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002908 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002909 int left = 0, top = 0;
2910 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2911 if (v instanceof TextView) {
2912 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002913 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2914 if (icon != null) {
2915 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002916 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002917 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002918 width = bounds.width();
2919 height = bounds.height();
2920 }
2921 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002922 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2923 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002924 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002925 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002926 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002927 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002928 // On L devices, we use the device default slide-up transition.
2929 // On L MR1 devices, we a custom version of the slide-up transition which
2930 // doesn't have the delay present in the device default.
2931 opts = ActivityOptions.makeCustomAnimation(this,
2932 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002933 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002934 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002935 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002936
2937 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2938 // Could be launching some bookkeeping activity
2939 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002940 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002941 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002942 launcherApps.startActivityForProfile(intent.getComponent(), user,
2943 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002944 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002945 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002947 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2948 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2949 // corresponding permission. Show the appropriate permission prompt if that
2950 // is the case.
2951 if (intent.getComponent() == null
2952 && Intent.ACTION_CALL.equals(intent.getAction())
2953 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2954 PackageManager.PERMISSION_GRANTED) {
2955 // TODO: Rename sPendingAddItem to a generic name.
2956 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2957 0, (ItemInfo) tag);
2958 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2959 REQUEST_PERMISSION_CALL_PHONE);
2960 return false;
2961 }
2962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002964 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002966 "or use the exported attribute for this activity. "
2967 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002969 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002971
Winson Chungbedf9232015-07-10 12:38:30 -07002972 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002973 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002974 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2975 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2976 return false;
2977 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002978 try {
2979 success = startActivity(v, intent, tag);
2980 } catch (ActivityNotFoundException e) {
2981 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2982 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2983 }
2984 return success;
2985 }
2986
Adam Cohen268c4752012-06-06 17:47:33 -07002987 /**
2988 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2989 * in the DragLayer in the exact absolute location of the original FolderIcon.
2990 */
2991 private void copyFolderIconToImage(FolderIcon fi) {
2992 final int width = fi.getMeasuredWidth();
2993 final int height = fi.getMeasuredHeight();
2994
2995 // Lazy load ImageView, Bitmap and Canvas
2996 if (mFolderIconImageView == null) {
2997 mFolderIconImageView = new ImageView(this);
2998 }
2999 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3000 mFolderIconBitmap.getHeight() != height) {
3001 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3002 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3003 }
3004
3005 DragLayer.LayoutParams lp;
3006 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3007 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3008 } else {
3009 lp = new DragLayer.LayoutParams(width, height);
3010 }
3011
Adam Cohen307fe232012-08-16 17:55:58 -07003012 // The layout from which the folder is being opened may be scaled, adjust the starting
3013 // view size by this scale factor.
3014 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003015 lp.customPosition = true;
3016 lp.x = mRectForFolderAnimation.left;
3017 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003018 lp.width = (int) (scale * width);
3019 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003020
3021 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3022 fi.draw(mFolderIconCanvas);
3023 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003024 if (fi.getFolder() != null) {
3025 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3026 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003027 }
Adam Cohen268c4752012-06-06 17:47:33 -07003028 // Just in case this image view is still in the drag layer from a previous animation,
3029 // we remove it and re-add it.
3030 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3031 mDragLayer.removeView(mFolderIconImageView);
3032 }
3033 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003034 if (fi.getFolder() != null) {
3035 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003036 }
Adam Cohen268c4752012-06-06 17:47:33 -07003037 }
3038
Sunny Goyal08442b82015-10-21 17:15:08 -07003039 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003040 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003041 FolderInfo info = (FolderInfo) fi.getTag();
3042 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3043 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003044 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3045 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003046 }
3047
Adam Cohen268c4752012-06-06 17:47:33 -07003048 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3049 copyFolderIconToImage(fi);
3050 fi.setVisibility(View.INVISIBLE);
3051
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003052 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3053 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003054 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003055 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3056 }
3057 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003058 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003059 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003060 oa.end();
3061 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003062 }
3063
Sunny Goyal935fca12015-10-13 10:19:01 -07003064 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003065 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003066 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 // We remove and re-draw the FolderIcon in-case it has changed
3069 mDragLayer.removeView(mFolderIconImageView);
3070 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003071 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003072 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003073 oa.addListener(new AnimatorListenerAdapter() {
3074 @Override
3075 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003076 if (cl != null) {
3077 cl.clearFolderLeaveBehind();
3078 // Remove the ImageView copy of the FolderIcon and make the original visible.
3079 mDragLayer.removeView(mFolderIconImageView);
3080 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003081 }
3082 }
3083 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003084 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 if (!animate) {
3086 oa.end();
3087 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003088 }
3089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003091 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 * is animated relative to the specified View. If the View is null, no animation
3093 * is played.
3094 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003095 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003097 public void openFolder(FolderIcon folderIcon, boolean animate) {
3098 animate &= !Utilities.isPowerSaverOn(this);
3099
Adam Cohenfb91f302012-06-11 15:45:18 -07003100 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003101 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3102 if (openFolder != null && openFolder != folder) {
3103 // Close any open folder before opening a folder.
3104 closeFolder();
3105 }
3106
Adam Cohena9cf38f2011-05-02 15:36:58 -07003107 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108
Adam Cohena9cf38f2011-05-02 15:36:58 -07003109 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110
Sunny Goyalf4066152015-04-15 09:42:19 -07003111 // While the folder is open, the position of the icon cannot change.
3112 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3113
Adam Cohen4554ee12011-08-03 16:13:21 -07003114 // Just verify that the folder hasn't already been added to the DragLayer.
3115 // There was a one-off crash where the folder had a parent already.
3116 if (folder.getParent() == null) {
3117 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003118 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 } else {
3120 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3121 folder.getParent() + ").");
3122 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003123 if (animate) {
3124 folder.animateOpen();
3125 } else {
3126 folder.open();
3127 }
3128 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003129
3130 // Notify the accessibility manager that this folder "window" has appeared and occluded
3131 // the workspace items
3132 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3133 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003134 }
3135
3136 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003137 closeFolder(true);
3138 }
3139
3140 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003141 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003143 if (folder.isEditingName()) {
3144 folder.dismissEditingName();
3145 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003146 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003147 }
3148 }
3149
Sunny Goyal935fca12015-10-13 10:19:01 -07003150 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003151 animate &= !Utilities.isPowerSaverOn(this);
3152
Adam Cohen4554ee12011-08-03 16:13:21 -07003153 folder.getInfo().opened = false;
3154
3155 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3156 if (parent != null) {
3157 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003158 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003159 if (fi != null) {
3160 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3161 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003163 if (animate) {
3164 folder.animateClosed();
3165 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003166 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003167 }
Winson Chung83ca4802013-04-12 15:10:52 -07003168
Sunny Goyal935fca12015-10-13 10:19:01 -07003169 // Notify the accessibility manager that this folder "window" has disappeared and no
3170 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003171 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003175 if (!isDraggingEnabled()) return false;
3176 if (isWorkspaceLocked()) return false;
3177 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178
Winson Chung76648c52015-07-10 14:33:23 -07003179 if (v == mAllAppsButton) {
3180 onLongClickAllAppsButton(v);
3181 return true;
3182 }
3183
Adam Cohen1697b792013-09-17 19:08:21 -07003184 if (v instanceof Workspace) {
3185 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003186 if (!mWorkspace.isTouchActive()) {
3187 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003188 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3189 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3190 return true;
3191 } else {
3192 return false;
3193 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003194 } else {
3195 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003196 }
Adam Cohen1697b792013-09-17 19:08:21 -07003197 }
3198
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003199 CellLayout.CellInfo longClickCellInfo = null;
3200 View itemUnderLongClick = null;
3201 if (v.getTag() instanceof ItemInfo) {
3202 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003203 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003204 itemUnderLongClick = longClickCellInfo.cell;
3205 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 }
3207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 // The hotseat touch handling does not go through Workspace, and we always allow long press
3209 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003210 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003211 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003212 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003213 // User long pressed on empty space
3214 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3215 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003216 if (mWorkspace.isInOverviewMode()) {
3217 mWorkspace.startReordering(v);
3218 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003219 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003222 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3223 mHotseat.getOrderInHotseat(
3224 longClickCellInfo.cellX,
3225 longClickCellInfo.cellY));
3226 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003228 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 }
3230 }
3231 }
3232 return true;
3233 }
3234
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 boolean isHotseatLayout(View layout) {
3236 return mHotseat != null && layout != null &&
3237 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3238 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003239
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 /**
3241 * Returns the CellLayout of the specified container at the specified screen.
3242 */
Sunny Goyal92820592015-03-02 11:31:35 -08003243 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3245 if (mHotseat != null) {
3246 return mHotseat.getLayout();
3247 } else {
3248 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003249 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003250 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003251 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003252 }
3253 }
3254
Winson Chungb745afb2015-03-02 11:51:23 -08003255 /**
3256 * For overridden classes.
3257 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003258 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003259 return isAppsViewVisible();
3260 }
3261
3262 public boolean isAppsViewVisible() {
3263 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3264 }
3265
3266 public boolean isWidgetsViewVisible() {
3267 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003268 }
3269
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003270 private void setWorkspaceBackground(int background) {
3271 switch (background) {
3272 case WORKSPACE_BACKGROUND_TRANSPARENT:
3273 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3274 break;
3275 case WORKSPACE_BACKGROUND_BLACK:
3276 getWindow().setBackgroundDrawable(null);
3277 break;
3278 default:
3279 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3280 }
Craig Mautner360310b2012-10-26 15:13:08 -07003281 }
3282
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003283 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003284 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3285 int curflags = getWindow().getAttributes().flags
3286 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3287 if (wpflags != curflags) {
3288 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3289 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003290 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003291 }
3292
Michael Jurkae326f182011-11-21 14:05:46 -08003293 @Override
3294 public void onTrimMemory(int level) {
3295 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003296 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3297 // The widget preview db can result in holding onto over
3298 // 3MB of memory for caching which isn't necessary.
3299 SQLiteDatabase.releaseMemory();
3300
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003301 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003302 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003303 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003304 if (mLauncherCallbacks != null) {
3305 mLauncherCallbacks.onTrimMemory(level);
3306 }
Michael Jurkae326f182011-11-21 14:05:46 -08003307 }
3308
Winson5c6bdbb2015-09-03 11:36:19 -07003309 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003310 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003311 }
3312
Winson5c6bdbb2015-09-03 11:36:19 -07003313 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003314 boolean changed = mState != State.WORKSPACE ||
3315 mWorkspace.getState() != Workspace.State.NORMAL;
3316 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003318 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003319 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003320
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321 // Set focus to the AppsCustomize button
3322 if (mAllAppsButton != null) {
3323 mAllAppsButton.requestFocus();
3324 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003325 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003326
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003327 // Change the state *after* we've called all the transition code
3328 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003329
Winson Chung5fb63472011-02-02 17:03:37 -08003330 // Resume the auto-advance of widgets
3331 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003332 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003333
Winson Chung0f785722015-04-08 10:27:49 -07003334 if (changed) {
3335 // Send an accessibility event to announce the context change
3336 getWindow().getDecorView()
3337 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003338 }
Winson5c6bdbb2015-09-03 11:36:19 -07003339 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003340 }
3341
Winsone9f27272015-10-13 10:47:51 -07003342 /**
3343 * Shows the overview button.
3344 */
Adam Cohened307df2013-10-02 09:37:31 -07003345 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003346 showOverviewMode(animated, false);
3347 }
3348
3349 /**
3350 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3351 * onto one of the overview panel buttons.
3352 */
3353 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3354 Runnable postAnimRunnable = null;
3355 if (requestButtonFocus) {
3356 postAnimRunnable = new Runnable() {
3357 @Override
3358 public void run() {
3359 // Hitting the menu button when in touch mode does not trigger touch mode to
3360 // be disabled, so if requested, force focus on one of the overview panel
3361 // buttons.
3362 mOverviewPanel.requestFocusFromTouch();
3363 }
3364 };
3365 }
Adam Cohened307df2013-10-02 09:37:31 -07003366 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003367 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003368 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003369 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003370 }
3371
Winson Chungb745afb2015-03-02 11:51:23 -08003372 /**
3373 * Shows the apps view.
3374 */
Winson Chung76648c52015-07-10 14:33:23 -07003375 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3376 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003377 if (resetListToTop) {
3378 mAppsView.scrollToTop();
3379 }
Winson Chung4ac30062015-05-08 17:34:17 -07003380 if (updatePredictedApps) {
3381 tryAndUpdatePredictedApps();
3382 }
Winson Chung76648c52015-07-10 14:33:23 -07003383 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003384 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003385
Winson Chungb745afb2015-03-02 11:51:23 -08003386 /**
3387 * Shows the widgets view.
3388 */
3389 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003390 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003391 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003393 }
Winson Chung76648c52015-07-10 14:33:23 -07003394 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003395
3396 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003397 @Override
3398 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003399 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003400 }
3401 });
Winson Chungb745afb2015-03-02 11:51:23 -08003402 }
3403
3404 /**
3405 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003406 *
3407 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003408 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003409 // TODO: calling method should use the return value so that when {@code false} is returned
3410 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003411 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003412 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3413 mState != State.WIDGETS_SPRING_LOADED) {
3414 return false;
3415 }
3416 if (toState != State.APPS && toState != State.WIDGETS) {
3417 return false;
3418 }
Winson Chungb745afb2015-03-02 11:51:23 -08003419
3420 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003421 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3422 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003423 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003424 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003425 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003426
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003428 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429
3430 // Pause the auto-advance of widgets until we are out of AllApps
3431 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003432 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003433 closeFolder();
3434
3435 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003436 getWindow().getDecorView()
3437 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003438 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439 }
3440
Winson Chungcd99cd32015-04-29 11:03:24 -07003441 /**
3442 * Updates the workspace and interaction state on state change, and return the animation to this
3443 * new state.
3444 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003445 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003446 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003447 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003448 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003449 updateInteraction(fromState, toState);
3450 return anim;
3451 }
3452
Jun Mukaif0033da2015-08-04 18:34:30 -07003453 public void onLauncherClingShown() {
3454 // When a launcher cling appears, it should cover the underlying layers, so their focus
3455 // should be blocked.
3456 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3457 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3458 }
3459 }
3460
3461 public void onLauncherClingDismissed() {
3462 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3463 }
3464
Hyunyoung Song3f471442015-04-08 19:01:34 -07003465 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003466 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003467 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003468 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003469 }
Winson Chungb745afb2015-03-02 11:51:23 -08003470
Winson Chung006ee262015-08-03 14:40:11 -07003471 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003472 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003473 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003474
3475 if (isAppsViewVisible()) {
3476 mState = State.APPS_SPRING_LOADED;
3477 } else if (isWidgetsViewVisible()) {
3478 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003479 } else if (Workspace.IS_SPRING_LOADED) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003480 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003481 } else {
3482 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003483 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003484 }
Adam Cohen7777d962011-08-18 18:58:38 -07003485
Hyunyoung Song3f471442015-04-08 19:01:34 -07003486 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003487 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003488 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003489
Winson Chunge7a03942011-08-05 15:05:12 -07003490 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003491 @Override
3492 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003493 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003494 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3495 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003496 // Before we show workspace, hide all apps again because
3497 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3498 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003499 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003500 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003501 } else {
3502 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003503 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003504 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003505 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003506 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003507
Tony Wickham94e0d372015-09-11 12:17:48 -07003508 private boolean isStateSpringLoaded() {
3509 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3510 || mState == State.WIDGETS_SPRING_LOADED;
3511 }
3512
Michael Jurkad3ef3062010-11-23 16:23:58 -08003513 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003514 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003515 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003516 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003517 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003518 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003519 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3520 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522 }
3523
Winson Chung4ac30062015-05-08 17:34:17 -07003524 /**
3525 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3526 * resumed.
3527 */
3528 private void tryAndUpdatePredictedApps() {
3529 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003530 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003531 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003532 mAppsView.setPredictedApps(apps);
3533 }
3534 }
3535 }
3536
Michael Jurkab3e22d92011-10-31 15:58:33 -07003537 void lockAllApps() {
3538 // TODO
3539 }
3540
3541 void unlockAllApps() {
3542 // TODO
3543 }
3544
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545 protected void disableVoiceButtonProxy(boolean disable) {
3546 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003547 }
3548
Winsonf768d932015-09-25 16:12:35 -07003549 public boolean launcherCallbacksProvidesSearch() {
3550 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3551 }
3552
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003553 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003554 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003555 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003556 }
3557
Adam Cohen24ce0b32014-01-14 16:18:14 -08003558 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003559 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3560 if (searchProvider == null) {
3561 return null;
3562 }
3563
3564 Bundle opts = new Bundle();
3565 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003566 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003567
Tony Wickham3a3517f2015-10-06 11:27:04 -07003568 // Determine the min and max dimensions of the widget.
3569 LauncherAppState app = LauncherAppState.getInstance();
3570 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3571 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3572 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003573 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3574 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003575 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003576 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003577 int minWidth = maxWidth;
3578 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003579 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3580 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3581 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3582 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3583 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003584 }
3585 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3586 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3587 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3588 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003589 if (LOGD) {
3590 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3591 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3592 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003593
Tony Wickham775455c2015-10-16 09:49:32 -07003594 if (mLauncherCallbacks != null) {
3595 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3596 }
3597
Sunny Goyalf7258242015-10-19 16:59:07 -07003598 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003599 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003600 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003601 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003602 || (widgetInfo == null)
3603 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003604 // A valid widget is not already bound.
3605 if (widgetId > -1) {
3606 mAppWidgetHost.deleteAppWidgetId(widgetId);
3607 widgetId = -1;
3608 }
3609
3610 // Try to bind a new widget
3611 widgetId = mAppWidgetHost.allocateAppWidgetId();
3612
3613 if (!AppWidgetManagerCompat.getInstance(this)
3614 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3615 mAppWidgetHost.deleteAppWidgetId(widgetId);
3616 widgetId = -1;
3617 }
3618
Sunny Goyalf7258242015-10-19 16:59:07 -07003619 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003620 .putInt(QSB_WIDGET_ID, widgetId)
3621 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003622 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003623 }
3624
Sunny Goyal8d595092015-07-06 12:22:14 -07003625 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003626 if (widgetId != -1) {
3627 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003628 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003629 mQsb.updateAppWidgetOptions(opts);
3630 mQsb.setPadding(0, 0, 0, 0);
3631 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003632 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003633 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003634 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003635 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003636 }
3637
Sunny Goyal22235bc2015-04-03 09:23:43 -07003638 private void reinflateQSBIfNecessary() {
3639 if (mQsb instanceof LauncherAppWidgetHostView &&
3640 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3641 mSearchDropTargetBar.removeView(mQsb);
3642 mQsb = null;
3643 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3644 }
3645 }
3646
Michael Jurkad7c28052012-04-27 15:43:36 -07003647 @Override
3648 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003649 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003650 final List<CharSequence> text = event.getText();
3651 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003652 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003653 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003654 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003655 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003656 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003657 } else if (mWorkspace != null) {
3658 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003659 } else {
3660 text.add(getString(R.string.all_apps_home_button_label));
3661 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003662 return result;
3663 }
3664
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003665 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003666 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003667 */
Adam Cohen091440a2015-03-18 14:16:05 -07003668 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003669 @Override
3670 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003671 closeSystemDialogs();
3672 }
3673 }
3674
3675 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 * If the activity is currently paused, signal that we need to run the passed Runnable
3677 * in onResume.
3678 *
3679 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003680 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3681 * wrong when we're not running, and if the activity comes back to what the configuration was
3682 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 *
3684 * Implementation of the method from LauncherModel.Callbacks.
3685 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003686 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003687 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003688 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003689 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003690 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003691 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003692 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003693 }
3694 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003695 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003696 return true;
3697 } else {
3698 return false;
3699 }
3700 }
3701
Michael Jurkac402cd92013-05-20 15:49:32 +02003702 private boolean waitUntilResume(Runnable run) {
3703 return waitUntilResume(run, false);
3704 }
3705
Michael Jurka1e2f4652013-07-08 18:03:46 -07003706 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003707 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003708 }
3709
Michael Jurka7607c2f2013-04-03 14:33:19 -07003710 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003711 * If the activity is currently paused, signal that we need to re-run the loader
3712 * in onResume.
3713 *
3714 * This needs to be called from incoming places where resources might have been loaded
3715 * while we are paused. That is becaues the Configuration might be wrong
3716 * when we're not running, and if it comes back to what it was when we
3717 * were paused, we are not restarted.
3718 *
3719 * Implementation of the method from LauncherModel.Callbacks.
3720 *
3721 * @return true if we are currently paused. The caller might be able to
3722 * skip some work in that case since we will come back again.
3723 */
3724 public boolean setLoadOnResume() {
3725 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003726 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003727 mOnResumeNeedsLoad = true;
3728 return true;
3729 } else {
3730 return false;
3731 }
3732 }
3733
3734 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003738 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003739 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003740 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003741 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003744
Joe Onorato9c1289c2009-08-17 11:03:03 -04003745 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003746 * Clear any pending bind callbacks. This is called when is loader is planning to
3747 * perform a full rebind from scratch.
3748 */
3749 @Override
3750 public void clearPendingBinds() {
3751 mBindOnResumeCallbacks.clear();
3752 if (mPendingExecutor != null) {
3753 mPendingExecutor.markCompleted();
3754 mPendingExecutor = null;
3755 }
3756 }
3757
3758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 * Refreshes the shortcuts shown on the workspace.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
3762 */
3763 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003764 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003765
Winson Chungd64d1762013-08-20 14:37:16 -07003766 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003767 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003768 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003769
Michael Jurka05bf644e2011-11-30 20:28:53 -08003770 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003771 if (mHotseat != null) {
3772 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003773 }
3774 }
3775
Adam Cohendcd297f2013-06-18 13:13:40 -07003776 @Override
3777 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003778 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003779
Adam Cohen5084cba2013-09-03 12:01:16 -07003780 // If there are no screens, we need to have an empty screen
3781 if (orderedScreenIds.size() == 0) {
3782 mWorkspace.addExtraEmptyScreen();
3783 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003784
3785 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003786 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003787 if (hasCustomContentToLeft()) {
3788 mWorkspace.createCustomContentContainer();
3789 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003790 }
Winson Chung64359a52013-07-08 17:17:08 -07003791 }
3792
3793 @Override
3794 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003795 int count = orderedScreenIds.size();
3796 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003797 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003798 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003799 }
3800
Winson Chungd64d1762013-08-20 14:37:16 -07003801 public void bindAppsAdded(final ArrayList<Long> newScreens,
3802 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003803 final ArrayList<ItemInfo> addAnimated,
3804 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003805 Runnable r = new Runnable() {
3806 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003807 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003808 }
3809 };
3810 if (waitUntilResume(r)) {
3811 return;
3812 }
3813
Winson Chungd64d1762013-08-20 14:37:16 -07003814 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003815 if (newScreens != null) {
3816 bindAddScreens(newScreens);
3817 }
Winson Chungd64d1762013-08-20 14:37:16 -07003818
3819 // We add the items without animation on non-visible pages, and with
3820 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003821 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003822 bindItems(addNotAnimated, 0,
3823 addNotAnimated.size(), false);
3824 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003825 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 bindItems(addAnimated, 0,
3827 addAnimated.size(), true);
3828 }
Winson Chungc58497e2013-09-03 17:48:37 -07003829
Winson Chung87412982013-10-03 18:34:14 -07003830 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003831 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003832
Winson Chungb745afb2015-03-02 11:51:23 -08003833 if (addedApps != null && mAppsView != null) {
3834 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003835 }
Winson Chungd64d1762013-08-20 14:37:16 -07003836 }
3837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003838 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 * Bind the items start-end from the list.
3840 *
3841 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003842 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003843 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003844 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3845 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003846 Runnable r = new Runnable() {
3847 public void run() {
3848 bindItems(shortcuts, start, end, forceAnimateIcons);
3849 }
3850 };
3851 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003852 return;
3853 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003854
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003856 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3857 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003858 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003860 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003861 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003863
3864 // Short circuit if we are loading dock items for a configuration which has no dock
3865 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3866 mHotseat == null) {
3867 continue;
3868 }
3869
Sunny Goyal639e9062015-08-19 19:17:06 -07003870 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 switch (item.itemType) {
3872 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3873 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003874 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003875 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003876
Winson Chung64359a52013-07-08 17:17:08 -07003877 /*
3878 * TODO: FIX collision case
3879 */
Winson Chungce376632013-07-11 16:12:41 -07003880 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3881 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3882 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003883 View v = cl.getChildAt(item.cellX, item.cellY);
3884 Object tag = v.getTag();
3885 String desc = "Collision while binding workspace item: " + item
3886 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003887 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003888 throw (new RuntimeException(desc));
3889 } else {
3890 Log.d(TAG, desc);
3891 }
Winson Chungce376632013-07-11 16:12:41 -07003892 }
Winson Chung64359a52013-07-08 17:17:08 -07003893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003895 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003896 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003897 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003898 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003899 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003900 default:
3901 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003903
3904 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3905 item.cellY, 1, 1);
3906 if (animateIcons) {
3907 // Animate all the applications up now
3908 view.setAlpha(0f);
3909 view.setScaleX(0f);
3910 view.setScaleY(0f);
3911 bounceAnims.add(createNewAppBounceAnimation(view, i));
3912 newShortcutsScreenId = item.screenId;
3913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003914 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003915
Winson Chung997a9232013-07-24 15:33:46 -07003916 if (animateIcons) {
3917 // Animate to the correct page
3918 if (newShortcutsScreenId > -1) {
3919 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003920 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003921 final Runnable startBounceAnimRunnable = new Runnable() {
3922 public void run() {
3923 anim.playTogether(bounceAnims);
3924 anim.start();
3925 }
3926 };
Winson Chung997a9232013-07-24 15:33:46 -07003927 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003928 // We post the animation slightly delayed to prevent slowdowns
3929 // when we are loading right after we return to launcher.
3930 mWorkspace.postDelayed(new Runnable() {
3931 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003932 if (mWorkspace != null) {
3933 mWorkspace.snapToPage(newScreenIndex);
3934 mWorkspace.postDelayed(startBounceAnimRunnable,
3935 NEW_APPS_ANIMATION_DELAY);
3936 }
Winson Chung94d67682013-09-25 16:29:40 -07003937 }
3938 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003939 } else {
3940 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003941 }
3942 }
Winson Chung64359a52013-07-08 17:17:08 -07003943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003944 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003945 }
3946
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 /**
3948 * Implementation of the method from LauncherModel.Callbacks.
3949 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003950 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003951 Runnable r = new Runnable() {
3952 public void run() {
3953 bindFolders(folders);
3954 }
3955 };
3956 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003957 return;
3958 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003959 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961
3962 /**
3963 * Add the views for a widget to the workspace.
3964 *
3965 * Implementation of the method from LauncherModel.Callbacks.
3966 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003967 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003968 Runnable r = new Runnable() {
3969 public void run() {
3970 bindAppWidget(item);
3971 }
3972 };
3973 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003974 return;
3975 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003976
Daniel Sandler843e8602010-06-07 14:59:01 -04003977 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3978 if (DEBUG_WIDGETS) {
3979 Log.d(TAG, "bindAppWidget: " + item);
3980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 final Workspace workspace = mWorkspace;
3982
Adam Cohen59400422014-03-05 18:07:04 -08003983 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003984 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003985
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003986 if (!mIsSafeModeEnabled
3987 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003988 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3989
Sunny Goyalff572272014-07-23 13:58:07 -07003990 if (appWidgetInfo == null) {
3991 if (DEBUG_WIDGETS) {
3992 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3993 + " belongs to component " + item.providerName
3994 + ", as the povider is null");
3995 }
3996 LauncherModel.deleteItemFromDatabase(this, item);
3997 return;
3998 }
Sunny Goyalff572272014-07-23 13:58:07 -07003999
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004000 // If we do not have a valid id, try to bind an id.
4001 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
4002 // Note: This assumes that the id remap broadcast is received before this step.
4003 // If that is not the case, the id remap will be ignored and user may see the
4004 // click to setup view.
4005 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
4006 pendingInfo.spanX = item.spanX;
4007 pendingInfo.spanY = item.spanY;
4008 pendingInfo.minSpanX = item.minSpanX;
4009 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004010 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004011
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004012 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4013 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4014 newWidgetId, appWidgetInfo, options);
4015
4016 // TODO consider showing a permission dialog when the widget is clicked.
4017 if (!success) {
4018 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4019 if (DEBUG_WIDGETS) {
4020 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4021 + " belongs to component " + item.providerName
4022 + ", as the launcher is unable to bing a new widget id");
4023 }
4024 LauncherModel.deleteItemFromDatabase(this, item);
4025 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004026 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004027
4028 item.appWidgetId = newWidgetId;
4029
4030 // If the widget has a configure activity, it is still needs to set it up, otherwise
4031 // the widget is ready to go.
4032 item.restoreStatus = (appWidgetInfo.configure == null)
4033 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4034 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4035
4036 LauncherModel.updateItemInDatabase(this, item);
4037 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4038 && (appWidgetInfo.configure == null)) {
4039 // If the ID is already valid, verify if we need to configure or not.
4040 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4041 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004042 }
Sunny Goyalff572272014-07-23 13:58:07 -07004043 }
4044
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004045 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004047 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004048 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4049 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004051
Sunny Goyal56c73602015-09-25 12:55:01 -07004052 // Verify that we own the widget
4053 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4054 if (info == null || appWidgetInfo == null ||
4055 !info.provider.equals(appWidgetInfo.provider)) {
4056 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
4057 deleteWidgetInfo(item);
4058 return;
4059 }
4060
Sunny Goyal651077b2014-06-30 14:15:31 -07004061 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004062 item.minSpanX = appWidgetInfo.minSpanX;
4063 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 } else {
4065 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004066 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4067 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004068 view.updateIcon(mIconCache);
4069 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004070 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004071 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004074 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 workspace.requestLayout();
4076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4079 + (SystemClock.uptimeMillis()-start) + "ms");
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
Sunny Goyalff572272014-07-23 13:58:07 -07004083 /**
4084 * Restores a pending widget.
4085 *
4086 * @param appWidgetId The app widget id
4087 * @param cellInfo The position on screen where to create the widget.
4088 */
4089 private void completeRestoreAppWidget(final int appWidgetId) {
4090 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4091 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4092 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4093 return;
4094 }
4095
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004096 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004097 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4098
4099 mWorkspace.reinflateWidgetsIfNecessary();
4100 LauncherModel.updateItemInDatabase(this, info);
4101 }
4102
Adam Cohen1462de32012-07-24 22:34:36 -07004103 public void onPageBoundSynchronously(int page) {
4104 mSynchronouslyBoundPages.add(page);
4105 }
4106
Sunny Goyal527c7d32015-08-28 15:19:36 -07004107 @Override
4108 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4109 if (mPendingExecutor != null) {
4110 mPendingExecutor.markCompleted();
4111 }
4112 mPendingExecutor = executor;
4113 executor.attachTo(this);
4114 }
4115
4116 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4117 if (mPendingExecutor == executor) {
4118 mPendingExecutor = null;
4119 }
4120 }
4121
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 /**
4123 * Callback saying that there aren't any more items to bind.
4124 *
4125 * Implementation of the method from LauncherModel.Callbacks.
4126 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004127 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004128 Runnable r = new Runnable() {
4129 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004130 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004131 }
4132 };
4133 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004134 return;
4135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004136 if (mSavedState != null) {
4137 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004138 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004140
4141 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4142 if (folderId != 0) {
4143 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4144 if (view instanceof FolderIcon) {
4145 FolderIcon icon = (FolderIcon) view;
4146 FolderInfo info = icon.getFolderInfo();
4147 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4148 mWorkspace.getNextPage());
4149 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4150 || info.screenId == currentScreenId) {
4151 // We can show the folder
4152 openFolder(icon, false);
4153 } else {
4154 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4155 " but current screen is " + currentScreenId);
4156 }
4157 }
4158 }
4159
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 mSavedState = null;
4161 }
4162
Adam Cohen1462de32012-07-24 22:34:36 -07004163 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004165 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004166 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004167
4168 // If we received the result of any pending adds while the loader was running (e.g. the
4169 // widget configuration forced an orientation change), process them now.
4170 if (sPendingAddItem != null) {
4171 final long screenId = completeAdd(sPendingAddItem);
4172
4173 // TODO: this moves the user to the page where the pending item was added. Ideally,
4174 // the screen would be guaranteed to exist after bind, and the page would be set through
4175 // the workspace restore process.
4176 mWorkspace.post(new Runnable() {
4177 @Override
4178 public void run() {
4179 mWorkspace.snapToScreenId(screenId);
4180 }
4181 });
4182 sPendingAddItem = null;
4183 }
4184
Sunny Goyal756adbc2015-04-16 15:20:51 -07004185 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004186
4187 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004188 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004189 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004190 }
4191
Adam Cohen3ed316a2014-07-23 18:21:20 -07004192 private void sendLoadingCompleteBroadcastIfNecessary() {
4193 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4194 String permission =
4195 getResources().getString(R.string.receive_first_load_broadcast_permission);
4196 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4197 sendBroadcast(intent, permission);
4198 SharedPreferences.Editor editor = mSharedPrefs.edit();
4199 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4200 editor.apply();
4201 }
4202 }
4203
Winson Chunga0b7e862013-09-05 16:03:15 -07004204 public boolean isAllAppsButtonRank(int rank) {
4205 if (mHotseat != null) {
4206 return mHotseat.isAllAppsButtonRank(rank);
4207 }
4208 return false;
4209 }
4210
Winson Chunga2413752012-04-03 14:22:34 -07004211 private boolean canRunNewAppsAnimation() {
4212 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004213 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4214 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004215 }
4216
Winson Chungc9168342013-06-26 14:54:55 -07004217 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004218 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004219 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4220 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004221 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004222 return bounceAnim;
4223 }
4224
Adam Cohen27772732013-11-11 14:00:56 +00004225 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004226 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004227 }
4228
Tony Wickham3a3517f2015-10-06 11:27:04 -07004229 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004230 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004231 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004232 }
4233
Tony Wickham55616cd2015-09-23 14:55:17 -07004234 public int getSearchBarHeight() {
4235 if (mLauncherCallbacks != null) {
4236 return mLauncherCallbacks.getSearchBarHeight();
4237 }
4238 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4239 }
4240
Winson Chung88fa7412015-08-03 14:25:28 -07004241 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004242 if (mSearchDropTargetBar == null) {
4243 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004244 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004245 if (mQsb != null) {
4246 mSearchDropTargetBar.removeView(mQsb);
4247 mQsb = null;
4248 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004249 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004250 }
4251
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004252 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004253 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4254 * multiple calls to bind the same list.)
4255 */
4256 @Thunk ArrayList<AppInfo> mTmpAppsList;
4257 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4258 public void run() {
4259 bindAllApplications(mTmpAppsList);
4260 mTmpAppsList = null;
4261 }
4262 };
4263
4264 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004265 * Add the icons for all apps.
4266 *
4267 * Implementation of the method from LauncherModel.Callbacks.
4268 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004269 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004270 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4271 mTmpAppsList = apps;
4272 return;
4273 }
4274
Winson Chungb745afb2015-03-02 11:51:23 -08004275 if (mAppsView != null) {
4276 mAppsView.setApps(apps);
4277 }
Adam Cohen9211d422014-10-07 18:14:53 -07004278 if (mLauncherCallbacks != null) {
4279 mLauncherCallbacks.bindAllApplications(apps);
4280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004281 }
4282
4283 /**
4284 * A package was updated.
4285 *
4286 * Implementation of the method from LauncherModel.Callbacks.
4287 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004288 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004289 Runnable r = new Runnable() {
4290 public void run() {
4291 bindAppsUpdated(apps);
4292 }
4293 };
4294 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004295 return;
4296 }
4297
Winson Chungb745afb2015-03-02 11:51:23 -08004298 if (mAppsView != null) {
4299 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 }
4302
Sunny Goyal4390ace2014-10-13 11:33:11 -07004303 @Override
4304 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4305 Runnable r = new Runnable() {
4306 public void run() {
4307 bindWidgetsRestored(widgets);
4308 }
4309 };
4310 if (waitUntilResume(r)) {
4311 return;
4312 }
4313 mWorkspace.widgetsRestored(widgets);
4314 }
4315
Joe Onorato9c1289c2009-08-17 11:03:03 -04004316 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004317 * Some shortcuts were updated in the background.
4318 *
4319 * Implementation of the method from LauncherModel.Callbacks.
4320 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004321 @Override
4322 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4323 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004324 Runnable r = new Runnable() {
4325 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004326 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004327 }
4328 };
4329 if (waitUntilResume(r)) {
4330 return;
4331 }
4332
Sunny Goyal4390ace2014-10-13 11:33:11 -07004333 if (!updated.isEmpty()) {
4334 mWorkspace.updateShortcuts(updated);
4335 }
4336
4337 if (!removed.isEmpty()) {
4338 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4339 for (ShortcutInfo si : removed) {
4340 removedComponents.add(si.getTargetComponent());
4341 }
4342 mWorkspace.removeItemsByComponentName(removedComponents, user);
4343 // Notify the drag controller
4344 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004345 }
4346 }
4347
4348 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004349 * Update the state of a package, typically related to install state.
4350 *
4351 * Implementation of the method from LauncherModel.Callbacks.
4352 */
Sunny Goyale755d462014-07-22 13:48:29 -07004353 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004354 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4355 Runnable r = new Runnable() {
4356 public void run() {
4357 bindRestoreItemsChange(updates);
4358 }
4359 };
4360 if (waitUntilResume(r)) {
4361 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004362 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004363
Sunny Goyal756adbc2015-04-16 15:20:51 -07004364 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004365 }
4366
4367 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004368 * A package was uninstalled. We take both the super set of packageNames
4369 * in addition to specific applications to remove, the reason being that
4370 * this can be called when a package is updated as well. In that scenario,
4371 * we only remove specific components from the workspace, where as
4372 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004373 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004374 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004375 * Implementation of the method from LauncherModel.Callbacks.
4376 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004377 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004378 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004379 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004380 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004381 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004382 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004383 }
Winson Chungd64d1762013-08-20 14:37:16 -07004384 };
4385 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004386 return;
4387 }
4388
Sunny Goyal1a745e82014-10-02 15:58:31 -07004389 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004390 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4391 for (AppInfo info : appInfos) {
4392 removedComponents.add(info.componentName);
4393 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004394 if (!packageNames.isEmpty()) {
4395 mWorkspace.removeItemsByPackageName(packageNames, user);
4396 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004397 if (!removedComponents.isEmpty()) {
4398 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004399 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004400 // Notify the drag controller
4401 mDragController.onAppsRemoved(packageNames, removedComponents);
4402
Sunny Goyal1a745e82014-10-02 15:58:31 -07004403 } else {
4404 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004405 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004406
Winson Chungdf95eb12013-10-16 14:57:07 -07004407 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004408 if (mAppsView != null) {
4409 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004411 }
Joe Onoratobe386092009-11-17 17:32:16 -08004412
Michael Jurkac402cd92013-05-20 15:49:32 +02004413 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004414 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004415 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004416 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004417 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004418
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004419 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004420 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004421 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004422 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004423 return;
4424 }
4425
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004426 if (mWidgetsView != null && model != null) {
4427 mWidgetsView.addWidgets(model);
4428 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004429 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004430 }
4431
Winson Chung400438b2011-01-16 17:53:48 -08004432 private int mapConfigurationOriActivityInfoOri(int configOri) {
4433 final Display d = getWindowManager().getDefaultDisplay();
4434 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4435 switch (d.getRotation()) {
4436 case Surface.ROTATION_0:
4437 case Surface.ROTATION_180:
4438 // We are currently in the same basic orientation as the natural orientation
4439 naturalOri = configOri;
4440 break;
4441 case Surface.ROTATION_90:
4442 case Surface.ROTATION_270:
4443 // We are currently in the other basic orientation to the natural orientation
4444 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4445 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4446 break;
4447 }
4448
4449 int[] oriMap = {
4450 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4451 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4452 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4453 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4454 };
4455 // Since the map starts at portrait, we need to offset if this device's natural orientation
4456 // is landscape.
4457 int indexOffset = 0;
4458 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4459 indexOffset = 1;
4460 }
4461 return oriMap[(d.getRotation() + indexOffset) % 4];
4462 }
Adam Cohen446e9402011-09-15 18:21:21 -07004463
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004464 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004465 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004466 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004467 if (Utilities.ATLEAST_JB_MR2) {
4468 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4469 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004470 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4471 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004472 }
Winson Chung4b919f82012-05-01 10:44:08 -07004473 }
4474 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004475
Winson Chung4b919f82012-05-01 10:44:08 -07004476 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004477 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004478 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004479 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004480 } else {
4481 mHandler.postDelayed(new Runnable() {
4482 public void run() {
4483 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4484 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004485 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004486 }
Winson Chung4b919f82012-05-01 10:44:08 -07004487 }
Winson Chung400438b2011-01-16 17:53:48 -08004488 }
4489
Winson Chunge43a1e72014-01-15 10:33:02 -08004490 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004491 if (mLauncherCallbacks != null) {
4492 return mLauncherCallbacks.isLauncherPreinstalled();
4493 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004494 PackageManager pm = getPackageManager();
4495 try {
4496 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4497 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4498 return true;
4499 } else {
4500 return false;
4501 }
4502 } catch (NameNotFoundException e) {
4503 e.printStackTrace();
4504 return false;
4505 }
4506 }
4507
Adam Cohenea90f832014-05-21 15:03:34 -07004508 /**
4509 * This method indicates whether or not we should suggest default wallpaper dimensions
4510 * when our wallpaper cropper was not yet used to set a wallpaper.
4511 */
4512 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004513 if (mLauncherCallbacks != null) {
4514 return mLauncherCallbacks.overrideWallpaperDimensions();
4515 }
Adam Cohenea90f832014-05-21 15:03:34 -07004516 return true;
4517 }
4518
Adam Cohen432609a2014-03-13 17:03:22 -07004519 /**
4520 * To be overridden by subclasses to indicate that there is an activity to launch
4521 * before showing the standard launcher experience.
4522 */
4523 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004524 if (mLauncherCallbacks != null) {
4525 return mLauncherCallbacks.hasFirstRunActivity();
4526 }
Adam Cohen432609a2014-03-13 17:03:22 -07004527 return false;
4528 }
4529
4530 /**
4531 * To be overridden by subclasses to launch any first run activity
4532 */
4533 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004534 if (mLauncherCallbacks != null) {
4535 return mLauncherCallbacks.getFirstRunActivity();
4536 }
Adam Cohen432609a2014-03-13 17:03:22 -07004537 return null;
4538 }
4539
Winson Chunga6945242014-01-08 14:04:34 -08004540 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004541 return !ActivityManager.isRunningInTestHarness() &&
4542 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004543 }
4544
Adam Cohen4a9423d2014-03-28 14:22:31 -07004545 protected boolean hasRunFirstRunActivity() {
4546 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4547 }
4548
Adam Cohen432609a2014-03-13 17:03:22 -07004549 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004550 if (shouldRunFirstRunActivity() &&
4551 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004552 Intent firstRunIntent = getFirstRunActivity();
4553 if (firstRunIntent != null) {
4554 startActivity(firstRunIntent);
4555 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004556 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004557 }
4558 }
Adam Cohen432609a2014-03-13 17:03:22 -07004559 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004560 }
4561
4562 private void markFirstRunActivityShown() {
4563 SharedPreferences.Editor editor = mSharedPrefs.edit();
4564 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4565 editor.apply();
4566 }
4567
Adam Cohen432609a2014-03-13 17:03:22 -07004568 /**
4569 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4570 * screen that must be displayed and dismissed.
4571 */
4572 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004573 if (mLauncherCallbacks != null) {
4574 return mLauncherCallbacks.hasDismissableIntroScreen();
4575 }
Adam Cohen432609a2014-03-13 17:03:22 -07004576 return false;
4577 }
4578
4579 /**
4580 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4581 */
4582 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004583 if (mLauncherCallbacks != null) {
4584 return mLauncherCallbacks.getIntroScreen();
4585 }
Adam Cohen432609a2014-03-13 17:03:22 -07004586 return null;
4587 }
4588
4589 /**
4590 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4591 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4592 */
4593 private boolean shouldShowIntroScreen() {
4594 return hasDismissableIntroScreen() &&
4595 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4596 }
4597
4598 protected void showIntroScreen() {
4599 View introScreen = getIntroScreen();
4600 changeWallpaperVisiblity(false);
4601 if (introScreen != null) {
4602 mDragLayer.showOverlayView(introScreen);
4603 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004604 if (mLauncherOverlayContainer != null) {
4605 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4606 }
Adam Cohen432609a2014-03-13 17:03:22 -07004607 }
4608
4609 public void dismissIntroScreen() {
4610 markIntroScreenDismissed();
4611 if (showFirstRunActivity()) {
4612 // We delay hiding the intro view until the first run activity is showing. This
4613 // avoids a blip.
4614 mWorkspace.postDelayed(new Runnable() {
4615 @Override
4616 public void run() {
4617 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004618 if (mLauncherOverlayContainer != null) {
4619 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4620 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004621 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004622 }
4623 }, ACTIVITY_START_DELAY);
4624 } else {
4625 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004626 if (mLauncherOverlayContainer != null) {
4627 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4628 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004629 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004630 }
4631 changeWallpaperVisiblity(true);
4632 }
4633
4634 private void markIntroScreenDismissed() {
4635 SharedPreferences.Editor editor = mSharedPrefs.edit();
4636 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4637 editor.apply();
4638 }
4639
Adam Cohen091440a2015-03-18 14:16:05 -07004640 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004641 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4642 // on the device, then we always show the first run cling experience (or if there is no
4643 // launcher2). Otherwise, we prompt the user upon started for migration
4644 LauncherClings launcherClings = new LauncherClings(this);
4645 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004646 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004647 if (mModel.canMigrateFromOldLauncherDb(this)) {
4648 launcherClings.showMigrationCling();
4649 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004650 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004651 }
4652 }
4653 }
4654
Winson Chunga6945242014-01-08 14:04:34 -08004655 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004656 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4657 if (mHotseat != null) mHotseat.setAlpha(1f);
4658 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004659 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4660 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004661 }
4662
4663 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004664 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4665 if (mHotseat != null) mHotseat.setAlpha(0f);
4666 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004667 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4668 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004669 }
4670
Winson Chung5ffd51d2015-06-25 11:36:50 -07004671 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004672 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004673 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004674 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004675 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004676 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004677 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4678 if (userHandle != null) {
4679 user = UserHandleCompat.fromUser(userHandle);
4680 }
4681 }
4682 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004683 }
4684
4685 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004686 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004687 if (user == null) {
4688 user = UserHandleCompat.myUserHandle();
4689 }
4690
4691 // Called from search suggestion, add the profile extra to the intent to ensure that we
4692 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004693 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004694 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004695 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004696 return null;
4697 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004698 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004699 }
4700
Winson Chung5ffd51d2015-06-25 11:36:50 -07004701 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004702 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4703 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004704 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004705 UserHandleCompat.myUserHandle());
4706 }
4707
Winson Chung5ffd51d2015-06-25 11:36:50 -07004708 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004709 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4710 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004711 mWorkspace.onExternalDragStartedWithItem(dragView);
4712 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004713 }
4714
Winson Chung5ffd51d2015-06-25 11:36:50 -07004715 protected void moveWorkspaceToDefaultScreen() {
4716 mWorkspace.moveToDefaultScreen(false);
4717 }
4718
Anjali Koppalf5d29132014-02-28 13:33:27 -08004719 @Override
4720 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004721 if (mLauncherCallbacks != null) {
4722 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4723 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004724 }
4725
Winson Chung80baf5a2010-08-09 16:03:15 -07004726 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004727 * Returns a FastBitmapDrawable with the icon, accurately sized.
4728 */
4729 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4730 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4731 d.setFilterBitmap(true);
4732 resizeIconDrawable(d);
4733 return d;
4734 }
4735
4736 /**
4737 * Resizes an icon drawable to the correct icon size.
4738 */
Winson5fbe0742015-09-30 15:33:00 -07004739 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004740 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004741 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004742 }
4743
4744 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004745 * Prints out out state for debugging.
4746 */
4747 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004748 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004749 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004750 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4751 Log.d(TAG, "mRestoring=" + mRestoring);
4752 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004753 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004754 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004755 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004756
Daniel Sandler325dc232013-06-05 22:57:57 -04004757 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004758 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004759
4760 @Override
4761 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4762 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004763 // Dump workspace
4764 writer.println(prefix + "Workspace Items");
4765 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4766 writer.println(prefix + " Homescreen " + i);
4767
4768 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4769 for (int j = 0; j < layout.getChildCount(); j++) {
4770 Object tag = layout.getChildAt(j).getTag();
4771 if (tag != null) {
4772 writer.println(prefix + " " + tag.toString());
4773 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004774 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004775 }
Sunny Goyala1365452015-10-01 15:46:24 -07004776
4777 writer.println(prefix + " Hotseat");
4778 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4779 for (int j = 0; j < layout.getChildCount(); j++) {
4780 Object tag = layout.getChildAt(j).getTag();
4781 if (tag != null) {
4782 writer.println(prefix + " " + tag.toString());
4783 }
4784 }
4785
4786 synchronized (sDumpLogs) {
4787 writer.println();
4788 writer.println(prefix + "Debug logs");
4789 for (String log : sDumpLogs) {
4790 writer.println(prefix + " " + log);
4791 }
4792 }
4793
Adam Cohen9211d422014-10-07 18:14:53 -07004794 if (mLauncherCallbacks != null) {
4795 mLauncherCallbacks.dump(prefix, fd, writer, args);
4796 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004797 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004798
Sunny Goyala1365452015-10-01 15:46:24 -07004799 public static void addDumpLog(String tag, String log) {
4800 Log.d(tag, log);
4801 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004802 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004803 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004804 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004805 }
4806
Adam Cohen59400422014-03-05 18:07:04 -08004807 public static CustomAppWidget getCustomAppWidget(String name) {
4808 return sCustomAppWidgets.get(name);
4809 }
4810
4811 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4812 return sCustomAppWidgets;
4813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004814}
Michael Jurka2763be32011-02-24 11:19:57 -08004815
Dan Sandlerd5024042014-01-09 15:01:33 -05004816interface DebugIntents {
4817 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4818 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004819}