blob: 2d52341a70f725baf3b9fc04207859990120a61b [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 Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800113import com.android.launcher3.userevent.Logger;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700114import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700115import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700116import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700117import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700118import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700119import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700120import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800122import com.android.wallpaperpicker.WallpaperUtils;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700123
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700130import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700139 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700140 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700141 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700145 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
Dan Sandlerd5024042014-01-09 15:01:33 -0500149 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Sunny Goyal28c6b962015-10-12 11:42:05 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 // To turn on these properties, type
174 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
182 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700183 // Type: int
184 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700185 // Type: long
186 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Adam Cohen3ed316a2014-07-23 18:21:20 -0700197 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
198 static final String ACTION_FIRST_LOAD_COMPLETE =
199 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
200
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 private static final String QSB_WIDGET_ID = "qsb_widget_id";
202 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700207 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
208 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700218 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800230 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700239 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800240 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Winson Chungc51db6a2011-10-05 11:44:49 -0700251 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700252 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
254 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Sunny Goyal594d76d2014-11-06 10:12:54 -0800261 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
265 // scroll issues (because the workspace may not have been measured yet) and extra work.
266 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
267 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
269 private SpannableStringBuilder mDefaultKeySsb = null;
270
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800273 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private boolean mRestoring;
275 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700276 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700280 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700286 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Sunny Goyal639e9062015-08-19 19:17:06 -0700289 private LauncherClings mClings;
290
Sunny Goyale2df0622015-04-24 11:27:00 -0700291 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700313
Sunny Goyala1365452015-10-01 15:46:24 -0700314 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
315 private static final Date sDateStamp = new Date();
316 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700317 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369 private Logger mUserEventLogger;
370
Adam Cohenf9c184a2016-01-15 16:47:43 -0800371 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal7779d622015-06-11 16:18:39 -0700383 private Runnable mUpdateOrientationRunnable = new Runnable() {
384 public void run() {
385 setOrientation();
386 }
387 };
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700415 mDeviceProfile = getResources().getConfiguration().orientation
416 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700417 app.getInvariantDeviceProfile().landscapeProfile
418 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700419
Sunny Goyalf7258242015-10-19 16:59:07 -0700420 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800428 mUserEventLogger = new Logger(this);
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429 mStats = new Stats(this);
430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
Tony Wickhameef44322015-10-20 13:24:36 -0700448 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
449 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700451 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
458 if (PROFILE_STARTUP) {
459 android.os.Debug.stopMethodTracing();
460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700470 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473
474 // For handling default keys
475 mDefaultKeySsb = new SpannableStringBuilder();
476 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800477
478 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
479 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800480
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800481 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 // In case we are on a device with locked rotation, we should look at preferences to check
483 // if the user has specifically allowed rotation.
484 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700485 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
505 public void onSettingsChanged(String settings, boolean value) {
506 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
507 mRotationEnabled = value;
508 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
509 mUpdateOrientationRunnable.run();
510 }
511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
Sunny Goyal32554d12015-12-03 15:31:25 -0800523 /**
524 * Call this after onCreate to set or clear overlay.
525 */
526 public void setLauncherOverlay(LauncherOverlay overlay) {
527 if (overlay != null) {
528 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
529 }
530 mWorkspace.setLauncherOverlay(overlay);
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700548 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Winson Chungef7f8742015-06-04 17:18:17 -0700586 /**
587 * Updates the bounds of all the overlays to match the new fixed bounds.
588 */
589 public void updateOverlayBounds(Rect newBounds) {
590 mAppsView.setSearchBarBounds(newBounds);
591 mWidgetsView.setSearchBarBounds(newBounds);
592 }
593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Anton Hanssona2f665f2013-09-26 12:18:32 +0100632 public Stats getStats() {
633 return mStats;
634 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800635 public Logger getLogger() {return mUserEventLogger; }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100636
Hyunyoung Song3f471442015-04-08 19:01:34 -0700637 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700638 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
639 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700640 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700641 }
642
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000643 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700644 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
645 // This cast is safe as long as the id < 0x00FFFFFF
646 // Since we jail all the dynamically generated views, there should be no clashes
647 // with any other views.
648 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 }
650
651 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700652 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
653 * a configuration step, this allows the proper animations to run after other transitions.
654 */
Adam Cohendb364c32014-05-20 14:23:40 -0700655 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700656 long screenId = args.screenId;
657 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
658 // When the screen id represents an actual screen (as opposed to a rank) we make sure
659 // that the drop page actually exists.
660 screenId = ensurePendingDropLayoutExists(args.screenId);
661 }
Adam Cohendb364c32014-05-20 14:23:40 -0700662
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700665 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700666 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700667 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700669 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700670 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700671 case REQUEST_RECONFIGURE_APPWIDGET:
672 completeRestoreAppWidget(args.appWidgetId);
673 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 }
Michael Jurka27614d22012-04-02 06:40:22 -0700675 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
676 // if you turned the screen off and then back while in All Apps, Launcher would not
677 // return to the workspace. Clearing mAddInfo.container here fixes this issue
678 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700679 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 }
681
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700683 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700684 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700685 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700686 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800687 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700688
Adam Cohenad4e15c2013-10-17 16:21:35 -0700689 Runnable exitSpringLoaded = new Runnable() {
690 @Override
691 public void run() {
692 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
693 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
694 }
695 };
696
Michael Jurka8b805b12012-04-18 14:23:14 -0700697 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700698 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700699 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
701 if (resultCode == RESULT_CANCELED) {
702 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700703 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800706 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700707 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700708
709 // When the user has granted permission to bind widgets, we should check to see if
710 // we can inflate the default search bar widget.
711 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 }
713 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200714 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
715 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700716 // User could have free-scrolled between pages before picking a wallpaper; make sure
717 // we move to the closest one now.
718 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700719 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 }
721 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200723
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700725 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700726
Adam Cohendb364c32014-05-20 14:23:40 -0700727 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 // We have special handling for widgets
729 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800730 final int appWidgetId;
731 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
732 : -1;
733 if (widgetId < 0) {
734 appWidgetId = pendingAddWidgetId;
735 } else {
736 appWidgetId = widgetId;
737 }
738
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800740 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700741 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
742 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 result = RESULT_CANCELED;
744 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 @Override
747 public void run() {
748 exitSpringLoadedDragModeDelayed(false, 0, null);
749 }
750 };
Adam Cohendb364c32014-05-20 14:23:40 -0700751 if (workspaceLocked) {
752 // No need to remove the empty screen if we're mid-binding, as the
753 // the bind will not add the empty screen.
754 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
755 } else {
756 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
757 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
758 }
Winson Chung5aaab772012-04-27 15:24:02 -0700759 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700761 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
762 // When the screen id represents an actual screen (as opposed to a rank)
763 // we make sure that the drop page actually exists.
764 mPendingAddInfo.screenId =
765 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
766 }
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
768
769 dropLayout.setDropPending(true);
770 final Runnable onComplete = new Runnable() {
771 @Override
772 public void run() {
773 completeTwoStageWidgetDrop(resultCode, appWidgetId);
774 dropLayout.setDropPending(false);
775 }
776 };
777 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
779 } else {
780 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
781 mPendingAddInfo);
782 sPendingAddItem = args;
783 }
Winson Chung5aaab772012-04-27 15:24:02 -0700784 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 return;
786 }
787
Sunny Goyalff572272014-07-23 13:58:07 -0700788 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
789 if (resultCode == RESULT_OK) {
790 // Update the widget view.
791 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
792 pendingAddWidgetId, mPendingAddInfo);
793 if (workspaceLocked) {
794 sPendingAddItem = args;
795 } else {
796 completeAdd(args);
797 }
798 }
799 // Leave the widget in the pending state if the user canceled the configure.
800 return;
801 }
802
Sunny Goyalaad90582015-07-09 14:22:50 -0700803 if (requestCode == REQUEST_CREATE_SHORTCUT) {
804 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
805 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
806 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
807 mPendingAddInfo);
808 if (isWorkspaceLocked()) {
809 sPendingAddItem = args;
810 } else {
811 completeAdd(args);
812 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
813 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
814 }
815 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700816 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
817 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800821
822 }
823
824 @Override
825 protected void onActivityResult(
826 final int requestCode, final int resultCode, final Intent data) {
827 handleActivityResult(requestCode, resultCode, data);
828 if (mLauncherCallbacks != null) {
829 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 }
832
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600833 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700834 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600835 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
837 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
838 View v = null;
839 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
840 if (layout != null) {
841 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
842 }
843 Intent intent = sPendingAddItem.intent;
844 sPendingAddItem = null;
845 if (grantResults.length > 0
846 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
847 startActivity(v, intent, null);
848 } else {
849 // TODO: Show a snack bar with link to settings
850 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
851 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
852 }
853 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 if (mLauncherCallbacks != null) {
855 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
856 grantResults);
857 }
858 }
859
Adam Cohendb364c32014-05-20 14:23:40 -0700860 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
861 appWidgetId, ItemInfo info) {
862 PendingAddArguments args = new PendingAddArguments();
863 args.requestCode = requestCode;
864 args.intent = data;
865 args.container = info.container;
866 args.screenId = info.screenId;
867 args.cellX = info.cellX;
868 args.cellY = info.cellY;
869 args.appWidgetId = appWidgetId;
870 return args;
871 }
872
873 /**
874 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
875 *
876 * @param screenId the screen id to check
877 * @return the new screen, or screenId if it exists
878 */
879 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800880 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700881 if (dropLayout == null) {
882 // it's possible that the add screen was removed because it was
883 // empty and a re-bind occurred
884 mWorkspace.addExtraEmptyScreen();
885 return mWorkspace.commitExtraEmptyScreen();
886 } else {
887 return screenId;
888 }
889 }
890
Adam Cohen091440a2015-03-18 14:16:05 -0700891 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800892 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 Runnable onCompleteRunnable = null;
894 int animationType = 0;
895
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 if (resultCode == RESULT_OK) {
898 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
899 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700900 mPendingAddWidgetInfo);
901 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 onCompleteRunnable = new Runnable() {
903 @Override
904 public void run() {
905 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
908 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
910 };
911 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800912 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 if (mDragLayer.getAnimatedView() != null) {
916 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
917 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
918 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700919 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 // The animated view may be null in the case of a rotation during widget configuration
921 onCompleteRunnable.run();
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 protected void onStop() {
927 super.onStop();
928 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700929
930 if (mLauncherCallbacks != null) {
931 mLauncherCallbacks.onStop();
932 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700933 }
934
935 @Override
936 protected void onStart() {
937 super.onStart();
938 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.onStart();
942 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 }
944
945 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200947 long startTime = 0;
948 if (DEBUG_RESUME_TIME) {
949 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400950 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200951 }
Adam Cohen9211d422014-10-07 18:14:53 -0700952
953 if (mLauncherCallbacks != null) {
954 mLauncherCallbacks.preOnResume();
955 }
956
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800958 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700959
Winson Chung4a2afa32012-07-19 14:53:05 -0700960 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700961 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800963 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700964 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700965 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700966 // view after launching an app, as they may be depending on the UI to be static to
967 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700968 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700969 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800970 } else if (mOnResumeState == State.WIDGETS) {
971 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700972 }
973 mOnResumeState = State.NONE;
974
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700975 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700976 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
977 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700978
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800979 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700980 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700981 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700982 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700984 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700986 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200987 // We might have postponed some bind calls until onResume (see waitUntilResume) --
988 // execute them here
989 long startTimeCallbacks = 0;
990 if (DEBUG_RESUME_TIME) {
991 startTimeCallbacks = System.currentTimeMillis();
992 }
993
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
995 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200998 if (DEBUG_RESUME_TIME) {
999 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1000 (System.currentTimeMillis() - startTimeCallbacks));
1001 }
Michael Jurka447bf842013-05-15 14:52:15 +02001002 }
Michael Jurka54554252013-08-01 12:52:23 +02001003 if (mOnResumeCallbacks.size() > 0) {
1004 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1005 mOnResumeCallbacks.get(i).run();
1006 }
1007 mOnResumeCallbacks.clear();
1008 }
Winson Chunge4e50662012-01-23 14:45:13 -08001009
1010 // Reset the pressed state of icons that were locked in the press state while activities
1011 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001012 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001013 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001014 mWaitingForResume.setStayPressed(false);
1015 }
Winson Chung82963d52013-10-09 11:20:57 -07001016
Adam Cohen06dff352012-06-01 17:17:08 -07001017 // It is possible that widgets can receive updates while launcher is not in the foreground.
1018 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1019 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1020 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001021 if (!isWorkspaceLoading()) {
1022 getWorkspace().reinflateWidgetsIfNecessary();
1023 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001024 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001025
Michael Jurka447bf842013-05-15 14:52:15 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1028 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001029
Adam Cohen4b66bf32015-09-18 12:15:19 -07001030 // We want to suppress callbacks about CustomContent being shown if we have just received
1031 // onNewIntent while the user was present within launcher. In that case, we post a call
1032 // to move the user to the main screen (which will occur after onResume). We don't want to
1033 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1034 // suppress here.
1035 if (mWorkspace.getCustomContentCallbacks() != null
1036 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001037 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001038 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001039 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1040 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001041 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001042 }
1043 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001044 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001045 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001046 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001047
Sunny Goyal756adbc2015-04-16 15:20:51 -07001048 if (!isWorkspaceLoading()) {
1049 // Process any items that were added while Launcher was away.
1050 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1051 }
Adam Cohen9211d422014-10-07 18:14:53 -07001052
1053 if (mLauncherCallbacks != null) {
1054 mLauncherCallbacks.onResume();
1055 }
Adam Cohen06dff352012-06-01 17:17:08 -07001056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001060 // Ensure that items added to Launcher are queued until Launcher returns
1061 InstallShortcutReceiver.enableInstallQueue();
1062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001064 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001065 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001066 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001067
1068 // We call onHide() aggressively. The custom content callbacks should be able to
1069 // debounce excess onHide calls.
1070 if (mWorkspace.getCustomContentCallbacks() != null) {
1071 mWorkspace.getCustomContentCallbacks().onHide();
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Adam Cohen9211d422014-10-07 18:14:53 -07001074 if (mLauncherCallbacks != null) {
1075 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078
1079 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001080 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1081 // by a onResume or by scrolling otherwise.
1082 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001083
1084 // Custom content is completely hidden
1085 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001086
1087 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1088 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001089
1090 // Indicates whether the user is allowed to scroll away from the custom content.
1091 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
Adam Cohenc2d6e892014-10-16 09:49:24 -07001094 public interface LauncherOverlay {
1095
1096 /**
1097 * Touch interaction leading to overscroll has begun
1098 */
1099 public void onScrollInteractionBegin();
1100
1101 /**
1102 * Touch interaction related to overscroll has ended
1103 */
1104 public void onScrollInteractionEnd();
1105
1106 /**
1107 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1108 * screen (or in the case of RTL, the rightmost screen).
1109 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001110 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001111
1112 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001113 * Called when the launcher is ready to use the overlay
1114 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001115 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001116 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001117 }
1118
Jun Mukai8af0cd82015-05-12 12:32:12 -07001119 public interface LauncherSearchCallbacks {
1120 /**
1121 * Called when the search overlay is shown.
1122 */
1123 public void onSearchOverlayOpened();
1124
1125 /**
1126 * Called when the search overlay is dismissed.
1127 */
1128 public void onSearchOverlayClosed();
1129 }
1130
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001132 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 }
1134
1135 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1136
Sunny Goyalc86df472016-02-25 09:19:38 -08001137 public void onScrollChanged(float progress) {
1138 if (mWorkspace != null) {
1139 mWorkspace.onOverlayScrollChanged(progress);
1140 }
1141 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 }
1143
Jorim Jaggid017f882014-01-14 17:08:48 -08001144 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001145 if (mLauncherCallbacks != null) {
1146 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001147 } else {
1148 // On devices with a locked orientation, we will at least have the allow rotation
1149 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001150 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001151 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001152 }
1153
Adam Cohen9211d422014-10-07 18:14:53 -07001154 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001155 CustomContentCallbacks callbacks, String description) {
1156 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001157 }
1158
Adam Cohenedb40762013-07-18 16:45:45 -07001159 // The custom content needs to offset its content to account for the QSB
1160 public int getTopOffsetForCustomContent() {
1161 return mWorkspace.getPaddingTop();
1162 }
1163
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001164 @Override
1165 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001167 if (mModel.isCurrentCallbacks(this)) {
1168 mModel.stopLoader();
1169 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001170 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1171
Romain Guy13c2e7b2010-03-10 19:45:00 -08001172 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001173 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001176 @Override
1177 public void onWindowFocusChanged(boolean hasFocus) {
1178 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001179 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001180
1181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1183 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 private boolean acceptFilter() {
1187 final InputMethodManager inputManager = (InputMethodManager)
1188 getSystemService(Context.INPUT_METHOD_SERVICE);
1189 return !inputManager.isFullscreenMode();
1190 }
1191
1192 @Override
1193 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001194 final int uniChar = event.getUnicodeChar();
1195 final boolean handled = super.onKeyDown(keyCode, event);
1196 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1197 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1199 keyCode, event);
1200 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001201 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001202 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 // showSearchDialog()
1204 // If there are multiple keystrokes before the search dialog takes focus,
1205 // onSearchRequested() will be called for every keystroke,
1206 // but it is idempotent, so it's fine.
1207 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
1209 }
1210
Joe Onorato8a9625e2010-01-28 15:55:35 -08001211 // Eat the long press event so the keyboard doesn't come up.
1212 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1213 return true;
1214 }
1215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 return handled;
1217 }
1218
Winson46163472015-09-22 17:31:56 -07001219 @Override
1220 public boolean onKeyUp(int keyCode, KeyEvent event) {
1221 if (keyCode == KeyEvent.KEYCODE_MENU) {
1222 // Ignore the menu key if we are currently dragging or are on the custom content screen
1223 if (!isOnCustomContent() && !mDragController.isDragging()) {
1224 // Close any open folders
1225 closeFolder();
1226
1227 // Stop resizing any widgets
1228 mWorkspace.exitWidgetResizeMode();
1229
1230 // Show the overview mode if we are on the workspace
1231 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1232 !mWorkspace.isSwitchingState()) {
1233 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001234 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001235 }
1236 }
1237 return true;
1238 }
1239 return super.onKeyUp(keyCode, event);
1240 }
1241
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 private String getTypedText() {
1243 return mDefaultKeySsb.toString();
1244 }
1245
1246 private void clearTypedText() {
1247 mDefaultKeySsb.clear();
1248 mDefaultKeySsb.clearSpans();
1249 Selection.setSelection(mDefaultKeySsb, 0);
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001253 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1254 * State
1255 */
1256 private static State intToState(int stateOrdinal) {
1257 State state = State.WORKSPACE;
1258 final State[] stateValues = State.values();
1259 for (int i = 0; i < stateValues.length; i++) {
1260 if (stateValues[i].ordinal() == stateOrdinal) {
1261 state = stateValues[i];
1262 break;
1263 }
1264 }
1265 return state;
1266 }
1267
1268 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 * Restores the previous state, if it exists.
1270 *
1271 * @param savedState The previous state.
1272 */
1273 private void restoreState(Bundle savedState) {
1274 if (savedState == null) {
1275 return;
1276 }
1277
Michael Jurka883f55b2010-10-21 15:47:14 -07001278 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001279 if (state == State.APPS || state == State.WIDGETS) {
1280 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001281 }
1282
Adam Cohen21cd0022013-10-09 18:57:02 -07001283 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1284 PagedView.INVALID_RESTORE_PAGE);
1285 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001286 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288
Sunny Goyal756cd262015-08-20 12:33:21 -07001289 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1290 if (itemValues != null) {
1291 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001292 AppWidgetProviderInfo info = savedState.getParcelable(
1293 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001294 mPendingAddWidgetInfo = info == null ?
1295 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1296
Adam Cohen4637b5a2013-11-04 18:21:24 -08001297 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001298 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 mRestoring = true;
1300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
1303 /**
1304 * Finds all the views we need and configure them properly.
1305 */
1306 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001307 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001308
Craig Mautner360310b2012-10-26 15:13:08 -07001309 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001310 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001311 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1312 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001313 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001314 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001315
John Spurlock77e1f472013-09-11 10:09:51 -04001316 mLauncherView.setSystemUiVisibility(
1317 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001318 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the drag layer
1321 mDragLayer.setup(this, dragController);
1322
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 // Setup the hotseat
1324 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1325 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001326 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 }
1328
Winsone9f27272015-10-13 10:47:51 -07001329 // Setup the overview panel
1330 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331
Winson Chung4c98d922011-05-31 16:50:48 -07001332 // Setup the workspace
1333 mWorkspace.setHapticFeedbackEnabled(false);
1334 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001335 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001336 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001337
Tony Wickham34d2c912015-09-11 09:27:58 -07001338 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001339 mSearchDropTargetBar = (SearchDropTargetBar)
1340 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001341 // Get the app info bar
1342 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1343 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001344
Winson Chungef7f8742015-06-04 17:18:17 -07001345 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001346 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001347 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001348 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1349 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1350 } else {
1351 mAppsView.setSearchBarController(new DefaultAppSearchController());
1352 }
Craig Mautner360310b2012-10-26 15:13:08 -07001353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001355 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001356 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001357 dragController.setMoveTarget(mWorkspace);
1358 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001359 if (mSearchDropTargetBar != null) {
1360 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001361 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001362 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001363 if (mAppInfoDropTargetBar != null) {
1364 mAppInfoDropTargetBar.setup(this, dragController);
1365 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001366
Sunny Goyal322d5562015-06-25 19:35:49 -07001367 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1368 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
Winsone9f27272015-10-13 10:47:51 -07001372 private void setupOverviewPanel() {
1373 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1374
1375 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1376 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1377 @Override
1378 public boolean onLongClick(View v) {
1379 return v.performClick();
1380 }
1381 };
1382
1383 // Bind wallpaper button actions
1384 View wallpaperButton = findViewById(R.id.wallpaper_button);
1385 wallpaperButton.setOnClickListener(new OnClickListener() {
1386 @Override
1387 public void onClick(View view) {
1388 if (!mWorkspace.isSwitchingState()) {
1389 onClickWallpaperPicker(view);
1390 }
1391 }
1392 });
1393 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1394 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395
1396 // Bind widget button actions
1397 mWidgetsButton = findViewById(R.id.widget_button);
1398 mWidgetsButton.setOnClickListener(new OnClickListener() {
1399 @Override
1400 public void onClick(View view) {
1401 if (!mWorkspace.isSwitchingState()) {
1402 onClickAddWidgetButton(view);
1403 }
1404 }
1405 });
1406 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1407 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1408
1409 // Bind settings actions
1410 View settingsButton = findViewById(R.id.settings_button);
1411 boolean hasSettings = hasSettings();
1412 if (hasSettings) {
1413 settingsButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickSettingsButton(view);
1418 }
1419 }
1420 });
1421 settingsButton.setOnLongClickListener(performClickOnLongClick);
1422 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423 } else {
1424 settingsButton.setVisibility(View.GONE);
1425 }
1426
1427 mOverviewPanel.setAlpha(0f);
1428 }
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001431 * Sets the all apps button. This method is called from {@link Hotseat}.
1432 */
1433 public void setAllAppsButton(View allAppsButton) {
1434 mAllAppsButton = allAppsButton;
1435 }
1436
1437 public View getAllAppsButton() {
1438 return mAllAppsButton;
1439 }
1440
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001441 public View getWidgetsButton() {
1442 return mWidgetsButton;
1443 }
1444
Anjali Koppal5ad44842014-03-10 20:34:39 -07001445 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 * Creates a view representing a shortcut.
1447 *
1448 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001450 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001451 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
1454 /**
1455 * Creates a view representing a shortcut inflated from the specified resource.
1456 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 * @param parent The group the shortcut belongs to.
1458 * @param info The data structure describing the shortcut.
1459 *
1460 * @return A View inflated from layoutResId.
1461 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001462 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001463 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001464 parent, false);
1465 favorite.applyFromShortcutInfo(info, mIconCache);
1466 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001468 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 return favorite;
1470 }
1471
1472 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * Add a shortcut to the workspace.
1474 *
1475 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 int cellY) {
1479 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001480 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Sunny Goyal5c97f512015-05-19 16:03:28 -07001482 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001483 if (info == null) {
1484 return;
1485 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001486 final View view = createShortcut(info);
1487
Sunny Goyal5c97f512015-05-19 16:03:28 -07001488 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001489 // First we check if we already know the exact location where we want to add this item.
1490 if (cellX >= 0 && cellY >= 0) {
1491 cellXY[0] = cellX;
1492 cellXY[1] = cellY;
1493 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001494
1495 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001496 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001497 true, null,null)) {
1498 return;
1499 }
1500 DragObject dragObject = new DragObject();
1501 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001502 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1503 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001504 return;
1505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001506 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001508 }
1509
1510 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001511 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 return;
1513 }
1514
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001515 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
1517 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521 }
1522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001526 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 */
Adam Cohen091440a2015-03-18 14:16:05 -07001528 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001529 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1530
1531 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001533 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1534 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Adam Cohen59400422014-03-05 18:07:04 -08001537 if (appWidgetInfo.isCustomWidget) {
1538 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001539 }
1540
Adam Cohen59400422014-03-05 18:07:04 -08001541 LauncherAppWidgetInfo launcherInfo;
1542 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1543 launcherInfo.spanX = info.spanX;
1544 launcherInfo.spanY = info.spanY;
1545 launcherInfo.minSpanX = info.minSpanX;
1546 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001547 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 if (hostView == null) {
1554 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001555 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1556 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001557 } else {
1558 // The AppWidgetHostView has already been inflated and instantiated
1559 launcherInfo.hostView = hostView;
1560 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001562 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001564 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001567 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1568 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1569 item.hostView.setTag(item);
1570 item.onBindAppWidget(this);
1571
1572 item.hostView.setFocusable(true);
1573 item.hostView.setOnFocusChangeListener(mFocusHandler);
1574
1575 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1576 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1577
1578 if (!item.isCustomWidget()) {
1579 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1580 }
1581 }
1582
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1584 @Override
1585 public void onReceive(Context context, Intent intent) {
1586 final String action = intent.getAction();
1587 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1588 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001589 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001590 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001591
Winson Chungc100e8e2011-08-09 16:02:43 -07001592 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001593 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001594 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001595 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001596 if (!showWorkspace(false)) {
1597 // If we are already on the workspace, then manually reset all apps
1598 mAppsView.reset();
1599 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1602 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001603 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001604 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1605 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001606 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001607 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1608 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1609 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001610 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001611 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1612 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 }
1614 }
1615 };
1616
1617 @Override
1618 public void onAttachedToWindow() {
1619 super.onAttachedToWindow();
1620
1621 // Listen for broadcasts related to user-presence
1622 final IntentFilter filter = new IntentFilter();
1623 filter.addAction(Intent.ACTION_SCREEN_OFF);
1624 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001625 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 if (ENABLE_DEBUG_INTENTS) {
1627 filter.addAction(DebugIntents.DELETE_DATABASE);
1628 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1629 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001631 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001632 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001634
1635 if (mLauncherCallbacks != null) {
1636 mLauncherCallbacks.onAttachedToWindow();
1637 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 }
1639
1640 @Override
1641 public void onDetachedFromWindow() {
1642 super.onDetachedFromWindow();
1643 mVisible = false;
1644
Adam Cohend113e0c2010-11-11 10:48:05 -08001645 if (mAttached) {
1646 unregisterReceiver(mReceiver);
1647 mAttached = false;
1648 }
Winson Chungb745afb2015-03-02 11:51:23 -08001649 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001650
1651 if (mLauncherCallbacks != null) {
1652 mLauncherCallbacks.onDetachedFromWindow();
1653 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 }
1655
1656 public void onWindowVisibilityChanged(int visibility) {
1657 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001658 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001659 // The following code used to be in onResume, but it turns out onResume is called when
1660 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1661 // is a more appropriate event to handle
1662 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001663 if (!mWorkspaceLoading) {
1664 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001665 // We want to let Launcher draw itself at least once before we force it to build
1666 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001667 // apps is nice and speedy.
1668 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001669 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001670 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001671 if (mStarted) return;
1672 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001673 // We delay the layer building a bit in order to give
1674 // other message processing a time to run. In particular
1675 // this avoids a delay in hiding the IME if it was
1676 // currently shown, because doing that may involve
1677 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001678 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001679 final ViewTreeObserver.OnDrawListener listener = this;
1680 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001681 public void run() {
1682 if (mWorkspace != null &&
1683 mWorkspace.getViewTreeObserver() != null) {
1684 mWorkspace.getViewTreeObserver().
1685 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001687 }
1688 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 }
1691 });
1692 }
1693 clearTypedText();
1694 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 }
1696
Adam Cohen091440a2015-03-18 14:16:05 -07001697 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 mHandler.removeMessages(ADVANCE_MSG);
1699 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1700 mHandler.sendMessageDelayed(msg, delay);
1701 mAutoAdvanceSentTime = System.currentTimeMillis();
1702 }
1703
Adam Cohen091440a2015-03-18 14:16:05 -07001704 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1706 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1707 mAutoAdvanceRunning = autoAdvanceRunning;
1708 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001709 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 sendAdvanceMessage(delay);
1711 } else {
1712 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001713 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1715 }
1716 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001717 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 }
1719 }
1720 }
1721
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001722 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1723
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001725 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 if (msg.what == ADVANCE_MSG) {
1727 int i = 0;
1728 for (View key: mWidgetsToAdvance.keySet()) {
1729 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001730 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 public void run() {
1734 ((Advanceable) v).advance();
1735 }
1736 }, delay);
1737 }
1738 i++;
1739 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001745
Winsonc0b52fe2015-09-09 16:38:15 -07001746 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001747 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1749 if (v instanceof Advanceable) {
1750 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001751 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001752 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754 }
1755
Winsonc0b52fe2015-09-09 16:38:15 -07001756 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 if (mWidgetsToAdvance.containsKey(hostView)) {
1758 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001759 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 }
1762
Hyunyoung Song3f471442015-04-08 19:01:34 -07001763 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001764 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1765 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 }
1767
Winson Chunga6945242014-01-08 14:04:34 -08001768 public DragLayer getDragLayer() {
1769 return mDragLayer;
1770 }
1771
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001772 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001773 return mAppsView;
1774 }
1775
Hyunyoung Song3f471442015-04-08 19:01:34 -07001776 public WidgetsContainerView getWidgetsView() {
1777 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001778 }
1779
Winson Chunga6945242014-01-08 14:04:34 -08001780 public Workspace getWorkspace() {
1781 return mWorkspace;
1782 }
1783
1784 public Hotseat getHotseat() {
1785 return mHotseat;
1786 }
1787
Jorim Jaggid017f882014-01-14 17:08:48 -08001788 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001789 return mOverviewPanel;
1790 }
1791
Winson Chung006ee262015-08-03 14:40:11 -07001792 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001793 return mSearchDropTargetBar;
1794 }
1795
Tony Wickham34d2c912015-09-11 09:27:58 -07001796 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1797 return mAppInfoDropTargetBar;
1798 }
1799
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001800 public LauncherAppWidgetHost getAppWidgetHost() {
1801 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Winson Chunga9abd0e2010-10-27 17:18:37 -07001804 public LauncherModel getModel() {
1805 return mModel;
1806 }
1807
Winson Chunga6945242014-01-08 14:04:34 -08001808 protected SharedPreferences getSharedPrefs() {
1809 return mSharedPrefs;
1810 }
1811
Adam Cohen2e6da152015-05-06 11:42:25 -07001812 public DeviceProfile getDeviceProfile() {
1813 return mDeviceProfile;
1814 }
1815
Bjorn Bringertc459e522013-06-07 19:36:01 +01001816 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001817 getWindow().closeAllPanels();
1818
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001819 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001820 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001821 }
1822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 @Override
1824 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001825 long startTime = 0;
1826 if (DEBUG_RESUME_TIME) {
1827 startTime = System.currentTimeMillis();
1828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 super.onNewIntent(intent);
1830
1831 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001832 Folder openFolder = mWorkspace.getOpenFolder();
1833 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1834 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1835 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1836 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1837 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001838 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001840
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 if (mWorkspace == null) {
1842 // Can be cases where mWorkspace is null, this prevents a NPE
1843 return;
1844 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001845 // In all these cases, only animate if we're already on home
1846 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001847
Sunny Goyal935fca12015-10-13 10:19:01 -07001848 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001849 exitSpringLoadedDragMode();
1850
1851 // If we are already on home, then just animate back to the workspace,
1852 // otherwise, just wait until onResume to set the state back to Workspace
1853 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001854 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 } else {
1856 mOnResumeState = State.WORKSPACE;
1857 }
1858
1859 final View v = getWindow().peekDecorView();
1860 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001861 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 INPUT_METHOD_SERVICE);
1863 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1864 }
1865
Winson Chungb745afb2015-03-02 11:51:23 -08001866 // Reset the apps view
1867 if (!alreadyOnHome && mAppsView != null) {
1868 mAppsView.scrollToTop();
1869 }
1870
Hyunyoung Song3f471442015-04-08 19:01:34 -07001871 // Reset the widgets view
1872 if (!alreadyOnHome && mWidgetsView != null) {
1873 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001875
Adam Cohen9211d422014-10-07 18:14:53 -07001876 if (mLauncherCallbacks != null) {
1877 mLauncherCallbacks.onHomeIntent();
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
Adam Cohened307df2013-10-02 09:37:31 -07001880
Adam Cohen9211d422014-10-07 18:14:53 -07001881 if (mLauncherCallbacks != null) {
1882 mLauncherCallbacks.onNewIntent(intent);
1883 }
Winson15f8b172015-08-19 11:02:39 -07001884
1885 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1886 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1887 // animation.
1888 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001889 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1890 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001891 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1892 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001893
1894 // We use this flag to suppress noisy callbacks above custom content state
1895 // from onResume.
1896 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001897 mWorkspace.post(new Runnable() {
1898 @Override
1899 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001900 if (mWorkspace != null) {
1901 mWorkspace.moveToDefaultScreen(true);
1902 }
Winson15f8b172015-08-19 11:02:39 -07001903 }
1904 });
1905 }
1906 }
1907
1908 if (DEBUG_RESUME_TIME) {
1909 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1910 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001914 public void onRestoreInstanceState(Bundle state) {
1915 super.onRestoreInstanceState(state);
1916 for (int page: mSynchronouslyBoundPages) {
1917 mWorkspace.restoreInstanceStateForChild(page);
1918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
1921 @Override
1922 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001923 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001924 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1925 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001926 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001927 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928
Michael Jurka883f55b2010-10-21 15:47:14 -07001929 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001930 Folder openFolder = mWorkspace.getOpenFolder();
1931 if (openFolder != null) {
1932 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001937 ContentValues itemValues = new ContentValues();
1938 mPendingAddInfo.writeToValues(itemValues);
1939 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001940 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001941 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
Hyunyoung Song3f471442015-04-08 19:01:34 -07001944 // Save the current widgets tray?
1945 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001946
1947 if (mLauncherCallbacks != null) {
1948 mLauncherCallbacks.onSaveInstanceState(outState);
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
1952 @Override
1953 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001955
Winson Chunge7a03942011-08-05 15:05:12 -07001956 // Remove all pending runnables
1957 mHandler.removeMessages(ADVANCE_MSG);
1958 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001959 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001960
Winson Chungcd2b0142011-06-08 16:02:26 -07001961 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001962 // It's possible to receive onDestroy after a new Launcher activity has
1963 // been created. In this case, don't interfere with the new Launcher.
1964 if (mModel.isCurrentCallbacks(this)) {
1965 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001966 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001967 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001970 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001972 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001974 mAppWidgetHost = null;
1975
1976 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
1978 TextKeyListener.getInstance().release();
1979
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001980 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001981
Michael Jurka2ecf9952012-06-18 12:52:28 -07001982 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001983
1984 if (mLauncherCallbacks != null) {
1985 mLauncherCallbacks.onDestroy();
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988
Adam Cohena9cf38f2011-05-02 15:36:58 -07001989 public DragController getDragController() {
1990 return mDragController;
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
1994 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001995 onStartForResult(requestCode);
1996 super.startActivityForResult(intent, requestCode);
1997 }
1998
1999 @Override
2000 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2001 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2002 onStartForResult(requestCode);
2003 try {
2004 super.startIntentSenderForResult(intent, requestCode,
2005 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2006 } catch (IntentSender.SendIntentException e) {
2007 throw new ActivityNotFoundException();
2008 }
2009 }
2010
2011 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002012 if (requestCode >= 0) {
2013 setWaitingForResult(true);
2014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
Winson Chung70d72102011-08-12 11:24:35 -07002017 /**
2018 * Indicates that we want global search for this activity by setting the globalSearch
2019 * argument for {@link #startSearch} to true.
2020 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002022 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002024
Karl Rosaen138a0412009-04-23 19:00:21 -07002025 if (initialQuery == null) {
2026 // Use any text typed in the launcher as the initial query
2027 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 if (appSearchData == null) {
2030 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002031 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
Winson Chungadf0c182012-08-23 14:59:07 -07002033 Rect sourceBounds = new Rect();
2034 if (mSearchDropTargetBar != null) {
2035 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2036 }
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Ian Parkinson047036e2014-09-01 15:40:53 +01002038 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002039 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002040 if (clearTextImmediately) {
2041 clearTypedText();
2042 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002043
2044 // We need to show the workspace after starting the search
2045 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002046 }
2047
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 /**
2049 * Start a text search.
2050 *
2051 * @return {@code true} if the search will start immediately, so any further keypresses
2052 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2053 * to buffer keypresses.
2054 */
2055 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002057 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2058 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2059 sourceBounds);
2060 }
2061
Michael Jurkaa33411c2012-06-14 16:18:21 -07002062 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002064 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002065 }
2066
2067 /**
2068 * Starts the global search activity. This code is a copied from SearchManager
2069 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002072 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2074 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2075 if (globalSearchActivity == null) {
2076 Log.w(TAG, "No global search activity found.");
2077 return;
2078 }
2079 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2080 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2081 intent.setComponent(globalSearchActivity);
2082 // Make sure that we have a Bundle to put source in
2083 if (appSearchData == null) {
2084 appSearchData = new Bundle();
2085 } else {
2086 appSearchData = new Bundle(appSearchData);
2087 }
Adam Cohen9211d422014-10-07 18:14:53 -07002088 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 if (!appSearchData.containsKey("source")) {
2090 appSearchData.putString("source", getPackageName());
2091 }
2092 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2093 if (!TextUtils.isEmpty(initialQuery)) {
2094 intent.putExtra(SearchManager.QUERY, initialQuery);
2095 }
2096 if (selectInitialQuery) {
2097 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2098 }
2099 intent.setSourceBounds(sourceBounds);
2100 try {
2101 startActivity(intent);
2102 } catch (ActivityNotFoundException ex) {
2103 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106
Yura4f93ec62014-02-04 14:15:21 +00002107 public boolean isOnCustomContent() {
2108 return mWorkspace.isOnOrMovingToCustomContent();
2109 }
2110
Winson Chung70d72102011-08-12 11:24:35 -07002111 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002112 public boolean onPrepareOptionsMenu(Menu menu) {
2113 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002114 if (mLauncherCallbacks != null) {
2115 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2116 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002117 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002120 @Override
2121 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002122 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002123 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002124 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002125 }
2126
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 public boolean isWorkspaceLocked() {
2128 return mWorkspaceLoading || mWaitingForResult;
2129 }
2130
Adam Cohen517a7f52014-03-01 12:12:59 -08002131 public boolean isWorkspaceLoading() {
2132 return mWorkspaceLoading;
2133 }
2134
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002135 private void setWorkspaceLoading(boolean value) {
2136 boolean isLocked = isWorkspaceLocked();
2137 mWorkspaceLoading = value;
2138 if (isLocked != isWorkspaceLocked()) {
2139 onWorkspaceLockedChanged();
2140 }
2141 }
2142
2143 private void setWaitingForResult(boolean value) {
2144 boolean isLocked = isWorkspaceLocked();
2145 mWaitingForResult = value;
2146 if (isLocked != isWorkspaceLocked()) {
2147 onWorkspaceLockedChanged();
2148 }
2149 }
2150
Adam Cohen9211d422014-10-07 18:14:53 -07002151 protected void onWorkspaceLockedChanged() {
2152 if (mLauncherCallbacks != null) {
2153 mLauncherCallbacks.onWorkspaceLockedChanged();
2154 }
2155 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002156
Michael Jurka0280c3b2010-09-17 15:00:07 -07002157 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2161 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002162 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002164
Tony Wickhama0628cc2015-10-14 15:23:04 -07002165 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002166 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002167 if (LOGD) {
2168 Log.d(TAG, "Adding widget from drop");
2169 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002170 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2171 }
2172
Sunny Goyale6b63a32015-01-16 16:14:29 -08002173 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002174 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2175 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002176 if (appWidgetInfo.configure != null) {
2177 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002178 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002179
Bjorn Bringert7984c942009-12-09 15:38:25 +00002180 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002181 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2182 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002185 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002186 Runnable onComplete = new Runnable() {
2187 @Override
2188 public void run() {
2189 // Exit spring loaded mode if necessary after adding the widget
2190 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2191 null);
2192 }
2193 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002194 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002196 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198 }
Romain Guycbb89e42009-06-08 15:52:54 -07002199
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002200 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002201 // Close any folders that may be open.
2202 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002203 mWorkspace.moveToCustomContentScreen(animate);
2204 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002205
2206 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2207 int[] cell, int spanX, int spanY) {
2208 switch (info.itemType) {
2209 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2210 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2211 int span[] = new int[2];
2212 span[0] = spanX;
2213 span[1] = spanY;
2214 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2215 container, screenId, cell, span);
2216 break;
2217 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2218 processShortcutFromDrop(info.componentName, container, screenId, cell);
2219 break;
2220 default:
2221 throw new IllegalStateException("Unknown item type: " + info.itemType);
2222 }
2223 }
2224
Adam Cohenfbba09b2011-07-18 21:43:05 -07002225 /**
2226 * Process a shortcut drop.
2227 *
2228 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002230 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002231 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002232 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2233 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237
2238 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 mPendingAddInfo.cellX = cell[0];
2240 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242
2243 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2244 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002245 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 }
2247
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 /**
2249 * Process a widget drop.
2250 *
2251 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002255 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2256 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 mPendingAddInfo.minSpanX = info.minSpanX;
2261 mPendingAddInfo.minSpanY = info.minSpanY;
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.cellX = cell[0];
2265 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 if (span != null) {
2268 mPendingAddInfo.spanX = span[0];
2269 mPendingAddInfo.spanY = span[1];
2270 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271
Adam Cohened66b2b2012-01-23 17:28:51 -08002272 AppWidgetHostView hostView = info.boundWidget;
2273 int appWidgetId;
2274 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002275 // In the case where we've prebound the widget, we remove it from the DragLayer
2276 if (LOGD) {
2277 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2278 }
2279 getDragLayer().removeView(hostView);
2280
Adam Cohened66b2b2012-01-23 17:28:51 -08002281 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002282 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002283
2284 // Clear the boundWidget so that it doesn't get destroyed.
2285 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002286 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002287 // In this case, we either need to start an activity to get permission to bind
2288 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002290 Bundle options = info.bindOptions;
2291
Sunny Goyalffe83f12014-08-14 17:39:34 -07002292 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2293 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002294 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002295 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002298 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2299 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2300 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002301 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2302 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002303 // TODO: we need to make sure that this accounts for the options bundle.
2304 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002305 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2306 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 }
2309
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002312 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 folderInfo.title = getText(R.string.folder_name);
2314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002316 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2317 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002318 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319
2320 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 FolderIcon newFolder =
2322 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002325 // Force measure the new folder icon
2326 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2327 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002328 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 }
Romain Guycbb89e42009-06-08 15:52:54 -07002330
Winsonc0b52fe2015-09-09 16:38:15 -07002331 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002332 * Unbinds the view for the specified item, and removes the item and all its children.
2333 *
2334 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002335 * @param itemInfo the {@link ItemInfo} for this view.
2336 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002337 */
Winson2949fb52015-09-24 09:56:11 -07002338 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002339 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002340 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002341 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2342 if (folderInfo != null) {
2343 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002344 } else {
2345 mWorkspace.removeWorkspaceItem(v);
2346 }
Winsonc0b52fe2015-09-09 16:38:15 -07002347 if (deleteFromDb) {
2348 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2349 }
2350 } else if (itemInfo instanceof FolderInfo) {
2351 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2352 unbindFolder(folderInfo);
2353 mWorkspace.removeWorkspaceItem(v);
2354 if (deleteFromDb) {
2355 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2356 }
2357 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2358 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002359 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002360 removeWidgetToAutoAdvance(widgetInfo.hostView);
2361 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002362 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002363 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002364 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002365
Winsonc0b52fe2015-09-09 16:38:15 -07002366 } else {
2367 return false;
2368 }
2369 return true;
2370 }
2371
2372 /**
2373 * Unbinds any launcher references to the folder.
2374 */
2375 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002376 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002377 }
2378
Winsonc0b52fe2015-09-09 16:38:15 -07002379 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002381 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002383 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002385 // Deleting an app widget ID is a void call but writes to disk before returning
2386 // to the caller...
2387 new AsyncTask<Void, Void, Void>() {
2388 public Void doInBackground(Void ... args) {
2389 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2390 return null;
2391 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002392 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002393 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002395 }
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 @Override
2398 public boolean dispatchKeyEvent(KeyEvent event) {
2399 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2400 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002402 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002403 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002404 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002405 dumpState();
2406 return true;
2407 }
2408 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002409 }
2410 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2411 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002412 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 return true;
2414 }
2415 }
2416
2417 return super.dispatchKeyEvent(event);
2418 }
2419
Joe Onorato88ec0992009-11-19 13:16:06 -08002420 @Override
2421 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002422 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2423 return;
2424 }
2425
Sunny Goyal45478022015-06-08 16:52:41 -07002426 if (mDragController.isDragging()) {
2427 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002428 return;
2429 }
2430
Winson Chungb745afb2015-03-02 11:51:23 -08002431 if (isAppsViewVisible()) {
2432 showWorkspace(true);
2433 } else if (isWidgetsViewVisible()) {
2434 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002435 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002436 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002437 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002438 Folder openFolder = mWorkspace.getOpenFolder();
2439 if (openFolder.isEditingName()) {
2440 openFolder.dismissEditingName();
2441 } else {
2442 closeFolder();
2443 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002444 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002445 mWorkspace.exitWidgetResizeMode();
2446
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002447 // Back button is a no-op here, but give at least some feedback for the button press
2448 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002449 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002453 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002454 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002455 @Override
2456 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002457 if (mAppWidgetHost != null) {
2458 mAppWidgetHost.startListening();
2459 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002460
2461 // Recreate the QSB, as the widget has been reset.
2462 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002463 }
2464
2465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 * Launches the intent referred by the clicked shortcut.
2467 *
2468 * @param v The view representing the clicked shortcut.
2469 */
2470 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002471 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2472 // view has detached (it's possible for this to happen if the view is removed mid touch).
2473 if (v.getWindowToken() == null) {
2474 return;
2475 }
2476
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002477 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 return;
2479 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002480
Adam Cohen1697b792013-09-17 19:08:21 -07002481 if (v instanceof Workspace) {
2482 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002483 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
2485 return;
2486 }
2487
2488 if (v instanceof CellLayout) {
2489 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002490 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2491 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002493 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
2495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002497 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002503 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002505 } else if (tag instanceof AppInfo) {
2506 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002507 } else if (tag instanceof LauncherAppWidgetInfo) {
2508 if (v instanceof PendingAppWidgetHostView) {
2509 onClickPendingWidget((PendingAppWidgetHostView) v);
2510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 }
2512 }
2513
Sunny Goyal70660032015-05-14 00:07:08 -07002514 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002515 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002516 return false;
2517 }
2518
Michael Jurkaaf442092010-06-10 17:01:57 -07002519 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002520 * Event handler for the app widget view which has not fully restored.
2521 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002522 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002523 if (mIsSafeModeEnabled) {
2524 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2525 return;
2526 }
2527
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002528 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002529 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002530 int widgetId = info.appWidgetId;
2531 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2532 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002533 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2534 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002535 mPendingAddInfo.copyFrom(info);
2536 mPendingAddWidgetId = widgetId;
2537
Sunny Goyalffe83f12014-08-14 17:39:34 -07002538 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2539 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002540 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002541 } else if (info.installProgress < 0) {
2542 // The install has not been queued
2543 final String packageName = info.providerName.getPackageName();
2544 showBrokenAppInstallDialog(packageName,
2545 new DialogInterface.OnClickListener() {
2546 public void onClick(DialogInterface dialog, int id) {
2547 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2548 }
2549 });
2550 } else {
2551 // Download has started.
2552 final String packageName = info.providerName.getPackageName();
2553 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 }
2555 }
2556
2557 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002558 * Event handler for the "grid" button that appears on the home screen, which
2559 * enters all apps mode.
2560 *
2561 * @param v The view that was clicked.
2562 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002563 protected void onClickAllAppsButton(View v) {
2564 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002565 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002566 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002567 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002568
2569 if (mLauncherCallbacks != null) {
2570 mLauncherCallbacks.onClickAllAppsButton(v);
2571 }
Winson Chung76648c52015-07-10 14:33:23 -07002572 }
2573 }
2574
2575 protected void onLongClickAllAppsButton(View v) {
2576 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2577 if (!isAppsViewVisible()) {
2578 showAppsView(true /* animated */, false /* resetListToTop */,
2579 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 }
2581 }
2582
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 private void showBrokenAppInstallDialog(final String packageName,
2584 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002585 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 .setTitle(R.string.abandoned_promises_title)
2587 .setMessage(R.string.abandoned_promise_explanation)
2588 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2589 .setNeutralButton(R.string.abandoned_clean_this,
2590 new DialogInterface.OnClickListener() {
2591 public void onClick(DialogInterface dialog, int id) {
2592 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2593 mWorkspace.removeAbandonedPromise(packageName, user);
2594 }
2595 })
2596 .create().show();
2597 return;
2598 }
2599
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 /**
2601 * Event handler for an app shortcut click.
2602 *
2603 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2604 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002605 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2607 Object tag = v.getTag();
2608 if (!(tag instanceof ShortcutInfo)) {
2609 throw new IllegalArgumentException("Input must be a Shortcut");
2610 }
2611
2612 // Open shortcut
2613 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002614
2615 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002616 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2617 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002618 // Launch activity anyway, framework will tell the user why the app is suspended.
2619 } else {
2620 int error = R.string.activity_not_available;
2621 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2622 error = R.string.safemode_shortcut_error;
2623 }
2624 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2625 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002626 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002627 }
2628
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002630 if ((v instanceof BubbleTextView)
2631 && shortcut.isPromise()
2632 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002634 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002637 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002644 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002645
2646 if (mLauncherCallbacks != null) {
2647 mLauncherCallbacks.onClickAppShortcut(v);
2648 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 }
2650
Adam Cohen091440a2015-03-18 14:16:05 -07002651 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002653 final ShortcutInfo shortcut;
2654 final Intent intent;
2655 if (tag instanceof ShortcutInfo) {
2656 shortcut = (ShortcutInfo) tag;
2657 intent = shortcut.intent;
2658 int[] pos = new int[2];
2659 v.getLocationOnScreen(pos);
2660 intent.setSourceBounds(new Rect(pos[0], pos[1],
2661 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002662
Sunny Goyal508da152014-08-14 10:53:27 -07002663 } else if (tag instanceof AppInfo) {
2664 shortcut = null;
2665 intent = ((AppInfo) tag).intent;
2666 } else {
2667 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2668 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669
2670 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002671 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672
2673 if (success && v instanceof BubbleTextView) {
2674 mWaitingForResume = (BubbleTextView) v;
2675 mWaitingForResume.setStayPressed(true);
2676 }
2677 }
2678
2679 /**
2680 * Event handler for a folder icon click.
2681 *
2682 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2683 */
2684 protected void onClickFolderIcon(View v) {
2685 if (LOGD) Log.d(TAG, "onClickFolder");
2686 if (!(v instanceof FolderIcon)){
2687 throw new IllegalArgumentException("Input must be a FolderIcon");
2688 }
2689
2690 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002691 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002693 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694 }
Adam Cohen9211d422014-10-07 18:14:53 -07002695
2696 if (mLauncherCallbacks != null) {
2697 mLauncherCallbacks.onClickFolderIcon(v);
2698 }
Michael Jurka5130e402011-10-13 04:55:35 -07002699 }
2700
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002701 /**
2702 * Event handler for the (Add) Widgets button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002707 if (mIsSafeModeEnabled) {
2708 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2709 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002710 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002711 if (mLauncherCallbacks != null) {
2712 mLauncherCallbacks.onClickAddWidgetButton(view);
2713 }
Adam Cohen9211d422014-10-07 18:14:53 -07002714 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002715 }
2716
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 /**
2718 * Event handler for the wallpaper picker button that appears after a long press
2719 * on the home screen.
2720 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002721 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002723 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2724 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2725 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002726 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002727 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002728
2729 if (mLauncherCallbacks != null) {
2730 mLauncherCallbacks.onClickWallpaperPicker(v);
2731 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 }
2733
2734 /**
2735 * Event handler for a click on the settings button that appears after a long press
2736 * on the home screen.
2737 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002738 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002740 if (mLauncherCallbacks != null) {
2741 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002742 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002743 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002744 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 }
2746
Adam Cohen61f560d2013-09-30 15:58:20 -07002747 public View.OnTouchListener getHapticFeedbackTouchListener() {
2748 if (mHapticFeedbackTouchListener == null) {
2749 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002750 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002751 @Override
2752 public boolean onTouch(View v, MotionEvent event) {
2753 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2754 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2755 }
2756 return false;
2757 }
2758 };
2759 }
2760 return mHapticFeedbackTouchListener;
2761 }
2762
Adam Cohen9211d422014-10-07 18:14:53 -07002763 public void onDragStarted(View view) {
2764 if (isOnCustomContent()) {
2765 // Custom content screen doesn't participate in drag and drop. If on custom
2766 // content screen, move to default.
2767 moveWorkspaceToDefaultScreen();
2768 }
2769
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onDragStarted(view);
2772 }
2773 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002774
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775 /**
2776 * Called when the user stops interacting with the launcher.
2777 * This implies that the user is now on the homescreen and is not doing housekeeping.
2778 */
Adam Cohen9211d422014-10-07 18:14:53 -07002779 protected void onInteractionEnd() {
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onInteractionEnd();
2782 }
2783 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784
2785 /**
2786 * Called when the user starts interacting with the launcher.
2787 * The possible interactions are:
2788 * - open all apps
2789 * - reorder an app shortcut, or a widget
2790 * - open the overview mode.
2791 * This is a good time to stop doing things that only make sense
2792 * when the user is on the homescreen and not doing housekeeping.
2793 */
Adam Cohen9211d422014-10-07 18:14:53 -07002794 protected void onInteractionBegin() {
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onInteractionBegin();
2797 }
2798 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002799
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 /** Updates the interaction state. */
2801 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002804 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2805 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002808 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 onInteractionEnd();
2810 }
2811 }
2812
Kenny Guyf07af7b2014-07-31 11:39:16 +01002813 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002814 try {
2815 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002816 launcherApps.showAppDetailsForProfile(componentName, user);
2817 } catch (SecurityException e) {
2818 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2819 Log.e(TAG, "Launcher does not have permission to launch settings");
2820 } catch (ActivityNotFoundException e) {
2821 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2822 Log.e(TAG, "Unable to launch settings");
2823 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002824 }
2825
Michael Jurka1e2f4652013-07-08 18:03:46 -07002826 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002827 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2828 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002829 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002830 // System applications cannot be installed. For now, show a toast explaining that.
2831 // We may give them the option of disabling apps this way.
2832 int messageId = R.string.uninstall_system_app_text;
2833 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002834 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002835 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002836 String packageName = componentName.getPackageName();
2837 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002838 Intent intent = new Intent(
2839 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002840 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2841 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002842 if (user != null) {
2843 user.addToIntent(intent, Intent.EXTRA_USER);
2844 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002845 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002846 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002847 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002848 }
2849
Winson Chung8f1eff72015-05-28 17:33:40 -07002850 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002851 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002853 // Only launch using the new animation if the shortcut has not opted out (this is a
2854 // private contract between launcher and may be ignored in the future).
2855 boolean useLaunchAnimation = (v != null) &&
2856 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002857 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2858 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002859
Kenny Guy1317e2d2014-05-08 18:52:50 +01002860 UserHandleCompat user = null;
2861 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2862 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2863 user = userManager.getUserForSerialNumber(serialNumber);
2864 }
2865
2866 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002867 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002868 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002869 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002870 int left = 0, top = 0;
2871 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2872 if (v instanceof TextView) {
2873 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002874 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2875 if (icon != null) {
2876 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002877 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002878 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002879 width = bounds.width();
2880 height = bounds.height();
2881 }
2882 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002883 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2884 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002885 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002886 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002887 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002888 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002889 // On L devices, we use the device default slide-up transition.
2890 // On L MR1 devices, we a custom version of the slide-up transition which
2891 // doesn't have the delay present in the device default.
2892 opts = ActivityOptions.makeCustomAnimation(this,
2893 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002894 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002895 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002896 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002897
2898 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2899 // Could be launching some bookkeeping activity
2900 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002901 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002902 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002903 launcherApps.startActivityForProfile(intent.getComponent(), user,
2904 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002905 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002906 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002908 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2909 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2910 // corresponding permission. Show the appropriate permission prompt if that
2911 // is the case.
2912 if (intent.getComponent() == null
2913 && Intent.ACTION_CALL.equals(intent.getAction())
2914 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2915 PackageManager.PERMISSION_GRANTED) {
2916 // TODO: Rename sPendingAddItem to a generic name.
2917 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2918 0, (ItemInfo) tag);
2919 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2920 REQUEST_PERMISSION_CALL_PHONE);
2921 return false;
2922 }
2923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002925 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002927 "or use the exported attribute for this activity. "
2928 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002930 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002932
Winson Chungbedf9232015-07-10 12:38:30 -07002933 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002934 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002935 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2936 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2937 return false;
2938 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002939 try {
2940 success = startActivity(v, intent, tag);
2941 } catch (ActivityNotFoundException e) {
2942 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2943 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2944 }
2945 return success;
2946 }
2947
Adam Cohen268c4752012-06-06 17:47:33 -07002948 /**
2949 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2950 * in the DragLayer in the exact absolute location of the original FolderIcon.
2951 */
2952 private void copyFolderIconToImage(FolderIcon fi) {
2953 final int width = fi.getMeasuredWidth();
2954 final int height = fi.getMeasuredHeight();
2955
2956 // Lazy load ImageView, Bitmap and Canvas
2957 if (mFolderIconImageView == null) {
2958 mFolderIconImageView = new ImageView(this);
2959 }
2960 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2961 mFolderIconBitmap.getHeight() != height) {
2962 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2963 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2964 }
2965
2966 DragLayer.LayoutParams lp;
2967 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2968 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2969 } else {
2970 lp = new DragLayer.LayoutParams(width, height);
2971 }
2972
Adam Cohen307fe232012-08-16 17:55:58 -07002973 // The layout from which the folder is being opened may be scaled, adjust the starting
2974 // view size by this scale factor.
2975 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002976 lp.customPosition = true;
2977 lp.x = mRectForFolderAnimation.left;
2978 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002979 lp.width = (int) (scale * width);
2980 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002981
2982 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2983 fi.draw(mFolderIconCanvas);
2984 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002985 if (fi.getFolder() != null) {
2986 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2987 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002988 }
Adam Cohen268c4752012-06-06 17:47:33 -07002989 // Just in case this image view is still in the drag layer from a previous animation,
2990 // we remove it and re-add it.
2991 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2992 mDragLayer.removeView(mFolderIconImageView);
2993 }
2994 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002995 if (fi.getFolder() != null) {
2996 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 }
2999
Sunny Goyal08442b82015-10-21 17:15:08 -07003000 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003001 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003002 FolderInfo info = (FolderInfo) fi.getTag();
3003 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3004 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003005 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3006 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003007 }
3008
Adam Cohen268c4752012-06-06 17:47:33 -07003009 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3010 copyFolderIconToImage(fi);
3011 fi.setVisibility(View.INVISIBLE);
3012
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003013 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3014 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003015 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003016 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3017 }
3018 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003019 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003020 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003021 oa.end();
3022 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003023 }
3024
Sunny Goyal935fca12015-10-13 10:19:01 -07003025 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003026 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003027 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003028
Adam Cohen268c4752012-06-06 17:47:33 -07003029 // We remove and re-draw the FolderIcon in-case it has changed
3030 mDragLayer.removeView(mFolderIconImageView);
3031 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003032
3033 if (cl != null) {
3034 cl.clearFolderLeaveBehind();
3035 }
3036
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003037 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003038 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003039 oa.addListener(new AnimatorListenerAdapter() {
3040 @Override
3041 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003042 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003043 // Remove the ImageView copy of the FolderIcon and make the original visible.
3044 mDragLayer.removeView(mFolderIconImageView);
3045 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003046 }
3047 }
3048 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003049 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003050 if (!animate) {
3051 oa.end();
3052 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 }
3054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003056 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 * is animated relative to the specified View. If the View is null, no animation
3058 * is played.
3059 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003060 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003062 public void openFolder(FolderIcon folderIcon, boolean animate) {
3063 animate &= !Utilities.isPowerSaverOn(this);
3064
Adam Cohenfb91f302012-06-11 15:45:18 -07003065 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003066 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3067 if (openFolder != null && openFolder != folder) {
3068 // Close any open folder before opening a folder.
3069 closeFolder();
3070 }
3071
Adam Cohena9cf38f2011-05-02 15:36:58 -07003072 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073
Adam Cohena9cf38f2011-05-02 15:36:58 -07003074 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075
Sunny Goyalf4066152015-04-15 09:42:19 -07003076 // While the folder is open, the position of the icon cannot change.
3077 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3078
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 // Just verify that the folder hasn't already been added to the DragLayer.
3080 // There was a one-off crash where the folder had a parent already.
3081 if (folder.getParent() == null) {
3082 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003083 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003084 } else {
3085 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3086 folder.getParent() + ").");
3087 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003088 if (animate) {
3089 folder.animateOpen();
3090 } else {
3091 folder.open();
3092 }
3093 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003094
3095 // Notify the accessibility manager that this folder "window" has appeared and occluded
3096 // the workspace items
3097 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3098 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003099 }
3100
3101 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003102 closeFolder(true);
3103 }
3104
3105 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003106 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003108 if (folder.isEditingName()) {
3109 folder.dismissEditingName();
3110 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003111 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003112 }
3113 }
3114
Sunny Goyal935fca12015-10-13 10:19:01 -07003115 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003116 animate &= !Utilities.isPowerSaverOn(this);
3117
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 folder.getInfo().opened = false;
3119
3120 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3121 if (parent != null) {
3122 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003123 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003124 if (fi != null) {
3125 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3126 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003128 if (animate) {
3129 folder.animateClosed();
3130 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003131 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 }
Winson Chung83ca4802013-04-12 15:10:52 -07003133
Sunny Goyal935fca12015-10-13 10:19:01 -07003134 // Notify the accessibility manager that this folder "window" has disappeared and no
3135 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003136 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003140 if (!isDraggingEnabled()) return false;
3141 if (isWorkspaceLocked()) return false;
3142 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143
Winson Chung76648c52015-07-10 14:33:23 -07003144 if (v == mAllAppsButton) {
3145 onLongClickAllAppsButton(v);
3146 return true;
3147 }
3148
Adam Cohen1697b792013-09-17 19:08:21 -07003149 if (v instanceof Workspace) {
3150 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003151 if (!mWorkspace.isTouchActive()) {
3152 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003153 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3154 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3155 return true;
3156 } else {
3157 return false;
3158 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003159 } else {
3160 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003161 }
Adam Cohen1697b792013-09-17 19:08:21 -07003162 }
3163
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003164 CellLayout.CellInfo longClickCellInfo = null;
3165 View itemUnderLongClick = null;
3166 if (v.getTag() instanceof ItemInfo) {
3167 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003168 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003169 itemUnderLongClick = longClickCellInfo.cell;
3170 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 }
3172
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 // The hotseat touch handling does not go through Workspace, and we always allow long press
3174 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003175 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003176 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003177 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003178 // User long pressed on empty space
3179 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3180 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003181 if (mWorkspace.isInOverviewMode()) {
3182 mWorkspace.startReordering(v);
3183 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003184 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003187 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3188 mHotseat.getOrderInHotseat(
3189 longClickCellInfo.cellX,
3190 longClickCellInfo.cellY));
3191 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003193 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 }
3195 }
3196 }
3197 return true;
3198 }
3199
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 boolean isHotseatLayout(View layout) {
3201 return mHotseat != null && layout != null &&
3202 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3203 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003204
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 /**
3206 * Returns the CellLayout of the specified container at the specified screen.
3207 */
Sunny Goyal92820592015-03-02 11:31:35 -08003208 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003209 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3210 if (mHotseat != null) {
3211 return mHotseat.getLayout();
3212 } else {
3213 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003214 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003215 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003216 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003217 }
3218 }
3219
Winson Chungb745afb2015-03-02 11:51:23 -08003220 /**
3221 * For overridden classes.
3222 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003223 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003224 return isAppsViewVisible();
3225 }
3226
3227 public boolean isAppsViewVisible() {
3228 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3229 }
3230
3231 public boolean isWidgetsViewVisible() {
3232 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003233 }
3234
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003235 private void setWorkspaceBackground(int background) {
3236 switch (background) {
3237 case WORKSPACE_BACKGROUND_TRANSPARENT:
3238 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3239 break;
3240 case WORKSPACE_BACKGROUND_BLACK:
3241 getWindow().setBackgroundDrawable(null);
3242 break;
3243 default:
3244 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3245 }
Craig Mautner360310b2012-10-26 15:13:08 -07003246 }
3247
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003248 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003249 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3250 int curflags = getWindow().getAttributes().flags
3251 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3252 if (wpflags != curflags) {
3253 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3254 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003255 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003256 }
3257
Michael Jurkae326f182011-11-21 14:05:46 -08003258 @Override
3259 public void onTrimMemory(int level) {
3260 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003261 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3262 // The widget preview db can result in holding onto over
3263 // 3MB of memory for caching which isn't necessary.
3264 SQLiteDatabase.releaseMemory();
3265
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003266 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003267 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003268 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003269 if (mLauncherCallbacks != null) {
3270 mLauncherCallbacks.onTrimMemory(level);
3271 }
Michael Jurkae326f182011-11-21 14:05:46 -08003272 }
3273
Winson5c6bdbb2015-09-03 11:36:19 -07003274 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003275 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003276 }
3277
Winson5c6bdbb2015-09-03 11:36:19 -07003278 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003279 boolean changed = mState != State.WORKSPACE ||
3280 mWorkspace.getState() != Workspace.State.NORMAL;
3281 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003282 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003283 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003284 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003285
Michael Jurkab3e22d92011-10-31 15:58:33 -07003286 // Set focus to the AppsCustomize button
3287 if (mAllAppsButton != null) {
3288 mAllAppsButton.requestFocus();
3289 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003290 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003291
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003292 // Change the state *after* we've called all the transition code
3293 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003294
Winson Chung5fb63472011-02-02 17:03:37 -08003295 // Resume the auto-advance of widgets
3296 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003297 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003298
Winson Chung0f785722015-04-08 10:27:49 -07003299 if (changed) {
3300 // Send an accessibility event to announce the context change
3301 getWindow().getDecorView()
3302 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003303 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -08003304 mUserEventLogger.resetElapsedContainerMillis();
Winson5c6bdbb2015-09-03 11:36:19 -07003305 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003306 }
3307
Winsone9f27272015-10-13 10:47:51 -07003308 /**
3309 * Shows the overview button.
3310 */
Adam Cohened307df2013-10-02 09:37:31 -07003311 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003312 showOverviewMode(animated, false);
3313 }
3314
3315 /**
3316 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3317 * onto one of the overview panel buttons.
3318 */
3319 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3320 Runnable postAnimRunnable = null;
3321 if (requestButtonFocus) {
3322 postAnimRunnable = new Runnable() {
3323 @Override
3324 public void run() {
3325 // Hitting the menu button when in touch mode does not trigger touch mode to
3326 // be disabled, so if requested, force focus on one of the overview panel
3327 // buttons.
3328 mOverviewPanel.requestFocusFromTouch();
3329 }
3330 };
3331 }
Adam Cohened307df2013-10-02 09:37:31 -07003332 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003333 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003334 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003335 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003336 }
3337
Winson Chungb745afb2015-03-02 11:51:23 -08003338 /**
3339 * Shows the apps view.
3340 */
Winson Chung76648c52015-07-10 14:33:23 -07003341 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3342 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003343 if (resetListToTop) {
3344 mAppsView.scrollToTop();
3345 }
Winson Chung4ac30062015-05-08 17:34:17 -07003346 if (updatePredictedApps) {
3347 tryAndUpdatePredictedApps();
3348 }
Winson Chung76648c52015-07-10 14:33:23 -07003349 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003350 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351
Winson Chungb745afb2015-03-02 11:51:23 -08003352 /**
3353 * Shows the widgets view.
3354 */
3355 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003356 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003357 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003358 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003359 }
Winson Chung76648c52015-07-10 14:33:23 -07003360 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003361
3362 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003363 @Override
3364 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003365 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003366 }
3367 });
Winson Chungb745afb2015-03-02 11:51:23 -08003368 }
3369
3370 /**
3371 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003372 *
3373 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003374 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003375 // TODO: calling method should use the return value so that when {@code false} is returned
3376 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003377 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003378 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3379 mState != State.WIDGETS_SPRING_LOADED) {
3380 return false;
3381 }
3382 if (toState != State.APPS && toState != State.WIDGETS) {
3383 return false;
3384 }
Winson Chungb745afb2015-03-02 11:51:23 -08003385
3386 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003387 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3388 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003389 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003390 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003391 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392
Michael Jurkab3e22d92011-10-31 15:58:33 -07003393 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003394 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003395
3396 // Pause the auto-advance of widgets until we are out of AllApps
3397 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003398 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003399 closeFolder();
3400
3401 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003402 getWindow().getDecorView()
3403 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003404 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405 }
3406
Winson Chungcd99cd32015-04-29 11:03:24 -07003407 /**
3408 * Updates the workspace and interaction state on state change, and return the animation to this
3409 * new state.
3410 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003411 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003412 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003413 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003414 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003415 updateInteraction(fromState, toState);
3416 return anim;
3417 }
3418
Jun Mukaif0033da2015-08-04 18:34:30 -07003419 public void onLauncherClingShown() {
3420 // When a launcher cling appears, it should cover the underlying layers, so their focus
3421 // should be blocked.
3422 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3423 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3424 }
3425 }
3426
3427 public void onLauncherClingDismissed() {
3428 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3429 }
3430
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003432 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003433 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003434 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003435 }
Winson Chungb745afb2015-03-02 11:51:23 -08003436
Winson Chung006ee262015-08-03 14:40:11 -07003437 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003438 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003439 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003440
3441 if (isAppsViewVisible()) {
3442 mState = State.APPS_SPRING_LOADED;
3443 } else if (isWidgetsViewVisible()) {
3444 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003445 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003446 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003447 } else {
3448 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003449 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003450 }
Adam Cohen7777d962011-08-18 18:58:38 -07003451
Hyunyoung Song3f471442015-04-08 19:01:34 -07003452 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003453 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003454 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003455
Winson Chunge7a03942011-08-05 15:05:12 -07003456 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003457 @Override
3458 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003459 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003460 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3461 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003462 // Before we show workspace, hide all apps again because
3463 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3464 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003465 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003466 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003467 } else {
3468 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003469 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003470 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003471 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003472 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003473
Tony Wickhame0c33232016-02-08 11:37:04 -08003474 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003475 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3476 || mState == State.WIDGETS_SPRING_LOADED;
3477 }
3478
Michael Jurkad3ef3062010-11-23 16:23:58 -08003479 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003480 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003481 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003482 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003483 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003484 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003485 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3486 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003487 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003488 }
3489
Winson Chung4ac30062015-05-08 17:34:17 -07003490 /**
3491 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3492 * resumed.
3493 */
3494 private void tryAndUpdatePredictedApps() {
3495 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003496 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003497 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003498 mAppsView.setPredictedApps(apps);
3499 }
3500 }
3501 }
3502
Michael Jurkab3e22d92011-10-31 15:58:33 -07003503 void lockAllApps() {
3504 // TODO
3505 }
3506
3507 void unlockAllApps() {
3508 // TODO
3509 }
3510
Sunny Goyal594d76d2014-11-06 10:12:54 -08003511 protected void disableVoiceButtonProxy(boolean disable) {
3512 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003513 }
3514
Winsonf768d932015-09-25 16:12:35 -07003515 public boolean launcherCallbacksProvidesSearch() {
3516 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3517 }
3518
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003519 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003520 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003521 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003522 }
3523
Adam Cohen24ce0b32014-01-14 16:18:14 -08003524 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003525 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3526 if (searchProvider == null) {
3527 return null;
3528 }
3529
3530 Bundle opts = new Bundle();
3531 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003532 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003533
Tony Wickham3a3517f2015-10-06 11:27:04 -07003534 // Determine the min and max dimensions of the widget.
3535 LauncherAppState app = LauncherAppState.getInstance();
3536 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3537 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3538 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003539 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3540 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003541 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003542 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003543 int minWidth = maxWidth;
3544 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003545 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3546 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3547 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3548 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3549 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003550 }
3551 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3552 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3553 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3554 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003555 if (LOGD) {
3556 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3557 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3558 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003559
Tony Wickham775455c2015-10-16 09:49:32 -07003560 if (mLauncherCallbacks != null) {
3561 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3562 }
3563
Sunny Goyalf7258242015-10-19 16:59:07 -07003564 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003565 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003566 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003567 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003568 || (widgetInfo == null)
3569 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003570 // A valid widget is not already bound.
3571 if (widgetId > -1) {
3572 mAppWidgetHost.deleteAppWidgetId(widgetId);
3573 widgetId = -1;
3574 }
3575
3576 // Try to bind a new widget
3577 widgetId = mAppWidgetHost.allocateAppWidgetId();
3578
3579 if (!AppWidgetManagerCompat.getInstance(this)
3580 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3581 mAppWidgetHost.deleteAppWidgetId(widgetId);
3582 widgetId = -1;
3583 }
3584
Sunny Goyalf7258242015-10-19 16:59:07 -07003585 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003586 .putInt(QSB_WIDGET_ID, widgetId)
3587 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003588 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003589 }
3590
Sunny Goyal8d595092015-07-06 12:22:14 -07003591 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003592 if (widgetId != -1) {
3593 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003594 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 mQsb.updateAppWidgetOptions(opts);
3596 mQsb.setPadding(0, 0, 0, 0);
3597 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003598 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003599 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003600 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003601 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003602 }
3603
Sunny Goyal22235bc2015-04-03 09:23:43 -07003604 private void reinflateQSBIfNecessary() {
3605 if (mQsb instanceof LauncherAppWidgetHostView &&
3606 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3607 mSearchDropTargetBar.removeView(mQsb);
3608 mQsb = null;
3609 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3610 }
3611 }
3612
Michael Jurkad7c28052012-04-27 15:43:36 -07003613 @Override
3614 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003615 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003616 final List<CharSequence> text = event.getText();
3617 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003618 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003619 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003620 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003621 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003622 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003623 } else if (mWorkspace != null) {
3624 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003625 } else {
3626 text.add(getString(R.string.all_apps_home_button_label));
3627 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003628 return result;
3629 }
3630
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003631 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003632 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003633 */
Adam Cohen091440a2015-03-18 14:16:05 -07003634 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003635 @Override
3636 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003637 closeSystemDialogs();
3638 }
3639 }
3640
3641 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 * If the activity is currently paused, signal that we need to run the passed Runnable
3643 * in onResume.
3644 *
3645 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003646 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3647 * wrong when we're not running, and if the activity comes back to what the configuration was
3648 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003649 *
3650 * Implementation of the method from LauncherModel.Callbacks.
3651 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003652 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003653 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003654 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003656 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003657 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003658 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003659 }
3660 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003661 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003662 return true;
3663 } else {
3664 return false;
3665 }
3666 }
3667
Michael Jurkac402cd92013-05-20 15:49:32 +02003668 private boolean waitUntilResume(Runnable run) {
3669 return waitUntilResume(run, false);
3670 }
3671
Michael Jurka1e2f4652013-07-08 18:03:46 -07003672 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003673 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003674 }
3675
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003677 * If the activity is currently paused, signal that we need to re-run the loader
3678 * in onResume.
3679 *
3680 * This needs to be called from incoming places where resources might have been loaded
3681 * while we are paused. That is becaues the Configuration might be wrong
3682 * when we're not running, and if it comes back to what it was when we
3683 * were paused, we are not restarted.
3684 *
3685 * Implementation of the method from LauncherModel.Callbacks.
3686 *
3687 * @return true if we are currently paused. The caller might be able to
3688 * skip some work in that case since we will come back again.
3689 */
3690 public boolean setLoadOnResume() {
3691 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003692 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003693 mOnResumeNeedsLoad = true;
3694 return true;
3695 } else {
3696 return false;
3697 }
3698 }
3699
3700 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003702 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003704 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003705 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003706 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003707 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003710
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003712 * Clear any pending bind callbacks. This is called when is loader is planning to
3713 * perform a full rebind from scratch.
3714 */
3715 @Override
3716 public void clearPendingBinds() {
3717 mBindOnResumeCallbacks.clear();
3718 if (mPendingExecutor != null) {
3719 mPendingExecutor.markCompleted();
3720 mPendingExecutor = null;
3721 }
3722 }
3723
3724 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 * Refreshes the shortcuts shown on the workspace.
3726 *
3727 * Implementation of the method from LauncherModel.Callbacks.
3728 */
3729 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003730 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003731
Winson Chungd64d1762013-08-20 14:37:16 -07003732 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003733 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003734 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003735
Michael Jurka05bf644e2011-11-30 20:28:53 -08003736 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003737 if (mHotseat != null) {
3738 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003739 }
3740 }
3741
Adam Cohendcd297f2013-06-18 13:13:40 -07003742 @Override
3743 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003744 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003745
Adam Cohen5084cba2013-09-03 12:01:16 -07003746 // If there are no screens, we need to have an empty screen
3747 if (orderedScreenIds.size() == 0) {
3748 mWorkspace.addExtraEmptyScreen();
3749 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003750
3751 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003752 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003753 if (hasCustomContentToLeft()) {
3754 mWorkspace.createCustomContentContainer();
3755 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003756 }
Winson Chung64359a52013-07-08 17:17:08 -07003757 }
3758
3759 @Override
3760 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 int count = orderedScreenIds.size();
3762 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003763 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003764 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003765 }
3766
Winson Chungd64d1762013-08-20 14:37:16 -07003767 public void bindAppsAdded(final ArrayList<Long> newScreens,
3768 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003769 final ArrayList<ItemInfo> addAnimated,
3770 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 Runnable r = new Runnable() {
3772 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003773 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003774 }
3775 };
3776 if (waitUntilResume(r)) {
3777 return;
3778 }
3779
Winson Chungd64d1762013-08-20 14:37:16 -07003780 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003781 if (newScreens != null) {
3782 bindAddScreens(newScreens);
3783 }
Winson Chungd64d1762013-08-20 14:37:16 -07003784
3785 // We add the items without animation on non-visible pages, and with
3786 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003787 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003788 bindItems(addNotAnimated, 0,
3789 addNotAnimated.size(), false);
3790 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003791 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003792 bindItems(addAnimated, 0,
3793 addAnimated.size(), true);
3794 }
Winson Chungc58497e2013-09-03 17:48:37 -07003795
Winson Chung87412982013-10-03 18:34:14 -07003796 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003797 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003798
Winson Chungb745afb2015-03-02 11:51:23 -08003799 if (addedApps != null && mAppsView != null) {
3800 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003801 }
Winson Chungd64d1762013-08-20 14:37:16 -07003802 }
3803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 * Bind the items start-end from the list.
3806 *
3807 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003808 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003809 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003810 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3811 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003812 Runnable r = new Runnable() {
3813 public void run() {
3814 bindItems(shortcuts, start, end, forceAnimateIcons);
3815 }
3816 };
3817 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003818 return;
3819 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003820
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003822 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3823 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003824 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003825 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003826 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003827 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003828 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003829
3830 // Short circuit if we are loading dock items for a configuration which has no dock
3831 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3832 mHotseat == null) {
3833 continue;
3834 }
3835
Sunny Goyal639e9062015-08-19 19:17:06 -07003836 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 switch (item.itemType) {
3838 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3839 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003840 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003841 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003842
Winson Chung64359a52013-07-08 17:17:08 -07003843 /*
3844 * TODO: FIX collision case
3845 */
Winson Chungce376632013-07-11 16:12:41 -07003846 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3847 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3848 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003849 View v = cl.getChildAt(item.cellX, item.cellY);
3850 Object tag = v.getTag();
3851 String desc = "Collision while binding workspace item: " + item
3852 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003853 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003854 throw (new RuntimeException(desc));
3855 } else {
3856 Log.d(TAG, desc);
3857 }
Winson Chungce376632013-07-11 16:12:41 -07003858 }
Winson Chung64359a52013-07-08 17:17:08 -07003859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003861 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003862 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003863 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003864 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003866 default:
3867 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003869
3870 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3871 item.cellY, 1, 1);
3872 if (animateIcons) {
3873 // Animate all the applications up now
3874 view.setAlpha(0f);
3875 view.setScaleX(0f);
3876 view.setScaleY(0f);
3877 bounceAnims.add(createNewAppBounceAnimation(view, i));
3878 newShortcutsScreenId = item.screenId;
3879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003880 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881
Winson Chung997a9232013-07-24 15:33:46 -07003882 if (animateIcons) {
3883 // Animate to the correct page
3884 if (newShortcutsScreenId > -1) {
3885 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003886 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003887 final Runnable startBounceAnimRunnable = new Runnable() {
3888 public void run() {
3889 anim.playTogether(bounceAnims);
3890 anim.start();
3891 }
3892 };
Winson Chung997a9232013-07-24 15:33:46 -07003893 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003894 // We post the animation slightly delayed to prevent slowdowns
3895 // when we are loading right after we return to launcher.
3896 mWorkspace.postDelayed(new Runnable() {
3897 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003898 if (mWorkspace != null) {
3899 mWorkspace.snapToPage(newScreenIndex);
3900 mWorkspace.postDelayed(startBounceAnimRunnable,
3901 NEW_APPS_ANIMATION_DELAY);
3902 }
Winson Chung94d67682013-09-25 16:29:40 -07003903 }
3904 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003905 } else {
3906 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003907 }
3908 }
Winson Chung64359a52013-07-08 17:17:08 -07003909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003911 }
3912
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 /**
3914 * Implementation of the method from LauncherModel.Callbacks.
3915 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003916 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003917 Runnable r = new Runnable() {
3918 public void run() {
3919 bindFolders(folders);
3920 }
3921 };
3922 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003923 return;
3924 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003925 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927
3928 /**
3929 * Add the views for a widget to the workspace.
3930 *
3931 * Implementation of the method from LauncherModel.Callbacks.
3932 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003933 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003934 Runnable r = new Runnable() {
3935 public void run() {
3936 bindAppWidget(item);
3937 }
3938 };
3939 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003940 return;
3941 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003942
Daniel Sandler843e8602010-06-07 14:59:01 -04003943 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3944 if (DEBUG_WIDGETS) {
3945 Log.d(TAG, "bindAppWidget: " + item);
3946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 final Workspace workspace = mWorkspace;
3948
Adam Cohen59400422014-03-05 18:07:04 -08003949 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003950 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003951
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003952 if (!mIsSafeModeEnabled
3953 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003954 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3955
Sunny Goyalff572272014-07-23 13:58:07 -07003956 if (appWidgetInfo == null) {
3957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3959 + " belongs to component " + item.providerName
3960 + ", as the povider is null");
3961 }
3962 LauncherModel.deleteItemFromDatabase(this, item);
3963 return;
3964 }
Sunny Goyalff572272014-07-23 13:58:07 -07003965
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003966 // If we do not have a valid id, try to bind an id.
3967 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3968 // Note: This assumes that the id remap broadcast is received before this step.
3969 // If that is not the case, the id remap will be ignored and user may see the
3970 // click to setup view.
3971 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3972 pendingInfo.spanX = item.spanX;
3973 pendingInfo.spanY = item.spanY;
3974 pendingInfo.minSpanX = item.minSpanX;
3975 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003976 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003977
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003978 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3979 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3980 newWidgetId, appWidgetInfo, options);
3981
3982 // TODO consider showing a permission dialog when the widget is clicked.
3983 if (!success) {
3984 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3985 if (DEBUG_WIDGETS) {
3986 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3987 + " belongs to component " + item.providerName
3988 + ", as the launcher is unable to bing a new widget id");
3989 }
3990 LauncherModel.deleteItemFromDatabase(this, item);
3991 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003992 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003993
3994 item.appWidgetId = newWidgetId;
3995
3996 // If the widget has a configure activity, it is still needs to set it up, otherwise
3997 // the widget is ready to go.
3998 item.restoreStatus = (appWidgetInfo.configure == null)
3999 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4000 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4001
4002 LauncherModel.updateItemInDatabase(this, item);
4003 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4004 && (appWidgetInfo.configure == null)) {
4005 // If the ID is already valid, verify if we need to configure or not.
4006 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4007 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004008 }
Sunny Goyalff572272014-07-23 13:58:07 -07004009 }
4010
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004011 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004012 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004013 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004014 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4015 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004016 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004017
Sunny Goyal56c73602015-09-25 12:55:01 -07004018 // Verify that we own the widget
4019 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4020 if (info == null || appWidgetInfo == null ||
4021 !info.provider.equals(appWidgetInfo.provider)) {
Hyunyoung Song70264a92016-02-03 15:28:47 -08004022 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
4023 + " appWidgetInfo=" + appWidgetInfo);
Sunny Goyal56c73602015-09-25 12:55:01 -07004024 deleteWidgetInfo(item);
4025 return;
4026 }
4027
Sunny Goyal651077b2014-06-30 14:15:31 -07004028 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004029 item.minSpanX = appWidgetInfo.minSpanX;
4030 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004031 } else {
4032 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004033 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4034 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004035 view.updateIcon(mIconCache);
4036 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004037 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004038 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004041 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004042 workspace.requestLayout();
4043
Daniel Sandler843e8602010-06-07 14:59:01 -04004044 if (DEBUG_WIDGETS) {
4045 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4046 + (SystemClock.uptimeMillis()-start) + "ms");
4047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 }
4049
Sunny Goyalff572272014-07-23 13:58:07 -07004050 /**
4051 * Restores a pending widget.
4052 *
4053 * @param appWidgetId The app widget id
4054 * @param cellInfo The position on screen where to create the widget.
4055 */
4056 private void completeRestoreAppWidget(final int appWidgetId) {
4057 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4058 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4059 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4060 return;
4061 }
4062
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004063 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004064 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4065
4066 mWorkspace.reinflateWidgetsIfNecessary();
4067 LauncherModel.updateItemInDatabase(this, info);
4068 }
4069
Adam Cohen1462de32012-07-24 22:34:36 -07004070 public void onPageBoundSynchronously(int page) {
4071 mSynchronouslyBoundPages.add(page);
4072 }
4073
Sunny Goyal527c7d32015-08-28 15:19:36 -07004074 @Override
4075 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4076 if (mPendingExecutor != null) {
4077 mPendingExecutor.markCompleted();
4078 }
4079 mPendingExecutor = executor;
4080 executor.attachTo(this);
4081 }
4082
4083 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4084 if (mPendingExecutor == executor) {
4085 mPendingExecutor = null;
4086 }
4087 }
4088
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 /**
4090 * Callback saying that there aren't any more items to bind.
4091 *
4092 * Implementation of the method from LauncherModel.Callbacks.
4093 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004094 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004095 Runnable r = new Runnable() {
4096 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004097 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004098 }
4099 };
4100 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004101 return;
4102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 if (mSavedState != null) {
4104 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004105 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004107
4108 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4109 if (folderId != 0) {
4110 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4111 if (view instanceof FolderIcon) {
4112 FolderIcon icon = (FolderIcon) view;
4113 FolderInfo info = icon.getFolderInfo();
4114 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4115 mWorkspace.getNextPage());
4116 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4117 || info.screenId == currentScreenId) {
4118 // We can show the folder
4119 openFolder(icon, false);
4120 } else {
4121 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4122 " but current screen is " + currentScreenId);
4123 }
4124 }
4125 }
4126
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127 mSavedState = null;
4128 }
4129
Adam Cohen1462de32012-07-24 22:34:36 -07004130 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004132 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004133 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004134
4135 // If we received the result of any pending adds while the loader was running (e.g. the
4136 // widget configuration forced an orientation change), process them now.
4137 if (sPendingAddItem != null) {
4138 final long screenId = completeAdd(sPendingAddItem);
4139
4140 // TODO: this moves the user to the page where the pending item was added. Ideally,
4141 // the screen would be guaranteed to exist after bind, and the page would be set through
4142 // the workspace restore process.
4143 mWorkspace.post(new Runnable() {
4144 @Override
4145 public void run() {
4146 mWorkspace.snapToScreenId(screenId);
4147 }
4148 });
4149 sPendingAddItem = null;
4150 }
4151
Sunny Goyal756adbc2015-04-16 15:20:51 -07004152 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004153
4154 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004155 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004156 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004157 }
4158
Adam Cohen3ed316a2014-07-23 18:21:20 -07004159 private void sendLoadingCompleteBroadcastIfNecessary() {
4160 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4161 String permission =
4162 getResources().getString(R.string.receive_first_load_broadcast_permission);
4163 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4164 sendBroadcast(intent, permission);
4165 SharedPreferences.Editor editor = mSharedPrefs.edit();
4166 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4167 editor.apply();
4168 }
4169 }
4170
Winson Chunga0b7e862013-09-05 16:03:15 -07004171 public boolean isAllAppsButtonRank(int rank) {
4172 if (mHotseat != null) {
4173 return mHotseat.isAllAppsButtonRank(rank);
4174 }
4175 return false;
4176 }
4177
Winson Chunga2413752012-04-03 14:22:34 -07004178 private boolean canRunNewAppsAnimation() {
4179 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004180 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4181 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004182 }
4183
Winson Chungc9168342013-06-26 14:54:55 -07004184 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004185 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004186 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4187 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004188 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004189 return bounceAnim;
4190 }
4191
Adam Cohen27772732013-11-11 14:00:56 +00004192 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004193 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004194 }
4195
Tony Wickham3a3517f2015-10-06 11:27:04 -07004196 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004197 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004198 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004199 }
4200
Tony Wickham55616cd2015-09-23 14:55:17 -07004201 public int getSearchBarHeight() {
4202 if (mLauncherCallbacks != null) {
4203 return mLauncherCallbacks.getSearchBarHeight();
4204 }
4205 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4206 }
4207
Winson Chung88fa7412015-08-03 14:25:28 -07004208 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004209 if (mSearchDropTargetBar == null) {
4210 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004211 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004212 if (mQsb != null) {
4213 mSearchDropTargetBar.removeView(mQsb);
4214 mQsb = null;
4215 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004216 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004217 }
4218
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004219 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004220 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4221 * multiple calls to bind the same list.)
4222 */
4223 @Thunk ArrayList<AppInfo> mTmpAppsList;
4224 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4225 public void run() {
4226 bindAllApplications(mTmpAppsList);
4227 mTmpAppsList = null;
4228 }
4229 };
4230
4231 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004232 * Add the icons for all apps.
4233 *
4234 * Implementation of the method from LauncherModel.Callbacks.
4235 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004236 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004237 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4238 mTmpAppsList = apps;
4239 return;
4240 }
4241
Winson Chungb745afb2015-03-02 11:51:23 -08004242 if (mAppsView != null) {
4243 mAppsView.setApps(apps);
4244 }
Adam Cohen9211d422014-10-07 18:14:53 -07004245 if (mLauncherCallbacks != null) {
4246 mLauncherCallbacks.bindAllApplications(apps);
4247 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004248 }
4249
4250 /**
4251 * A package was updated.
4252 *
4253 * Implementation of the method from LauncherModel.Callbacks.
4254 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004255 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004256 Runnable r = new Runnable() {
4257 public void run() {
4258 bindAppsUpdated(apps);
4259 }
4260 };
4261 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004262 return;
4263 }
4264
Winson Chungb745afb2015-03-02 11:51:23 -08004265 if (mAppsView != null) {
4266 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004268 }
4269
Sunny Goyal4390ace2014-10-13 11:33:11 -07004270 @Override
4271 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4272 Runnable r = new Runnable() {
4273 public void run() {
4274 bindWidgetsRestored(widgets);
4275 }
4276 };
4277 if (waitUntilResume(r)) {
4278 return;
4279 }
4280 mWorkspace.widgetsRestored(widgets);
4281 }
4282
Joe Onorato9c1289c2009-08-17 11:03:03 -04004283 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004284 * Some shortcuts were updated in the background.
4285 *
4286 * Implementation of the method from LauncherModel.Callbacks.
4287 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004288 @Override
4289 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4290 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004291 Runnable r = new Runnable() {
4292 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004293 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004294 }
4295 };
4296 if (waitUntilResume(r)) {
4297 return;
4298 }
4299
Sunny Goyal4390ace2014-10-13 11:33:11 -07004300 if (!updated.isEmpty()) {
4301 mWorkspace.updateShortcuts(updated);
4302 }
4303
4304 if (!removed.isEmpty()) {
4305 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4306 for (ShortcutInfo si : removed) {
4307 removedComponents.add(si.getTargetComponent());
4308 }
4309 mWorkspace.removeItemsByComponentName(removedComponents, user);
4310 // Notify the drag controller
4311 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004312 }
4313 }
4314
4315 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004316 * Update the state of a package, typically related to install state.
4317 *
4318 * Implementation of the method from LauncherModel.Callbacks.
4319 */
Sunny Goyale755d462014-07-22 13:48:29 -07004320 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004321 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4322 Runnable r = new Runnable() {
4323 public void run() {
4324 bindRestoreItemsChange(updates);
4325 }
4326 };
4327 if (waitUntilResume(r)) {
4328 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004329 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004330
Sunny Goyal756adbc2015-04-16 15:20:51 -07004331 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004332 }
4333
4334 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004335 * A package was uninstalled. We take both the super set of packageNames
4336 * in addition to specific applications to remove, the reason being that
4337 * this can be called when a package is updated as well. In that scenario,
4338 * we only remove specific components from the workspace, where as
4339 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004340 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004341 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004342 * Implementation of the method from LauncherModel.Callbacks.
4343 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004344 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004345 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004346 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004347 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004348 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004349 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004350 }
Winson Chungd64d1762013-08-20 14:37:16 -07004351 };
4352 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004353 return;
4354 }
4355
Sunny Goyal1a745e82014-10-02 15:58:31 -07004356 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004357 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4358 for (AppInfo info : appInfos) {
4359 removedComponents.add(info.componentName);
4360 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004361 if (!packageNames.isEmpty()) {
4362 mWorkspace.removeItemsByPackageName(packageNames, user);
4363 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004364 if (!removedComponents.isEmpty()) {
4365 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004366 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004367 // Notify the drag controller
4368 mDragController.onAppsRemoved(packageNames, removedComponents);
4369
Sunny Goyal1a745e82014-10-02 15:58:31 -07004370 } else {
4371 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004372 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004373
Winson Chungdf95eb12013-10-16 14:57:07 -07004374 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004375 if (mAppsView != null) {
4376 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004377 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004378 }
Joe Onoratobe386092009-11-17 17:32:16 -08004379
Michael Jurkac402cd92013-05-20 15:49:32 +02004380 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004381 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004382 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004383 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004384 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004385
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004386 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004387 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004388 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004389 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004390 return;
4391 }
4392
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004393 if (mWidgetsView != null && model != null) {
4394 mWidgetsView.addWidgets(model);
4395 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004396 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004397 }
4398
Winson Chung400438b2011-01-16 17:53:48 -08004399 private int mapConfigurationOriActivityInfoOri(int configOri) {
4400 final Display d = getWindowManager().getDefaultDisplay();
4401 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4402 switch (d.getRotation()) {
4403 case Surface.ROTATION_0:
4404 case Surface.ROTATION_180:
4405 // We are currently in the same basic orientation as the natural orientation
4406 naturalOri = configOri;
4407 break;
4408 case Surface.ROTATION_90:
4409 case Surface.ROTATION_270:
4410 // We are currently in the other basic orientation to the natural orientation
4411 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4412 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4413 break;
4414 }
4415
4416 int[] oriMap = {
4417 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4418 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4419 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4420 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4421 };
4422 // Since the map starts at portrait, we need to offset if this device's natural orientation
4423 // is landscape.
4424 int indexOffset = 0;
4425 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4426 indexOffset = 1;
4427 }
4428 return oriMap[(d.getRotation() + indexOffset) % 4];
4429 }
Adam Cohen446e9402011-09-15 18:21:21 -07004430
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004431 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004432 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004433 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004434 if (Utilities.ATLEAST_JB_MR2) {
4435 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4436 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004437 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4438 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004439 }
Winson Chung4b919f82012-05-01 10:44:08 -07004440 }
4441 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004442
Winson Chung4b919f82012-05-01 10:44:08 -07004443 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004444 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004445 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004446 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004447 } else {
4448 mHandler.postDelayed(new Runnable() {
4449 public void run() {
4450 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4451 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004452 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004453 }
Winson Chung4b919f82012-05-01 10:44:08 -07004454 }
Winson Chung400438b2011-01-16 17:53:48 -08004455 }
4456
Winson Chunge43a1e72014-01-15 10:33:02 -08004457 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004458 if (mLauncherCallbacks != null) {
4459 return mLauncherCallbacks.isLauncherPreinstalled();
4460 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004461 PackageManager pm = getPackageManager();
4462 try {
4463 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4464 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4465 return true;
4466 } else {
4467 return false;
4468 }
4469 } catch (NameNotFoundException e) {
4470 e.printStackTrace();
4471 return false;
4472 }
4473 }
4474
Adam Cohenea90f832014-05-21 15:03:34 -07004475 /**
4476 * This method indicates whether or not we should suggest default wallpaper dimensions
4477 * when our wallpaper cropper was not yet used to set a wallpaper.
4478 */
4479 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004480 if (mLauncherCallbacks != null) {
4481 return mLauncherCallbacks.overrideWallpaperDimensions();
4482 }
Adam Cohenea90f832014-05-21 15:03:34 -07004483 return true;
4484 }
4485
Adam Cohen432609a2014-03-13 17:03:22 -07004486 /**
4487 * To be overridden by subclasses to indicate that there is an activity to launch
4488 * before showing the standard launcher experience.
4489 */
4490 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004491 if (mLauncherCallbacks != null) {
4492 return mLauncherCallbacks.hasFirstRunActivity();
4493 }
Adam Cohen432609a2014-03-13 17:03:22 -07004494 return false;
4495 }
4496
4497 /**
4498 * To be overridden by subclasses to launch any first run activity
4499 */
4500 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004501 if (mLauncherCallbacks != null) {
4502 return mLauncherCallbacks.getFirstRunActivity();
4503 }
Adam Cohen432609a2014-03-13 17:03:22 -07004504 return null;
4505 }
4506
Winson Chunga6945242014-01-08 14:04:34 -08004507 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004508 return !ActivityManager.isRunningInTestHarness() &&
4509 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004510 }
4511
Adam Cohen4a9423d2014-03-28 14:22:31 -07004512 protected boolean hasRunFirstRunActivity() {
4513 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4514 }
4515
Adam Cohen432609a2014-03-13 17:03:22 -07004516 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004517 if (shouldRunFirstRunActivity() &&
4518 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004519 Intent firstRunIntent = getFirstRunActivity();
4520 if (firstRunIntent != null) {
4521 startActivity(firstRunIntent);
4522 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004523 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004524 }
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004527 }
4528
4529 private void markFirstRunActivityShown() {
4530 SharedPreferences.Editor editor = mSharedPrefs.edit();
4531 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4532 editor.apply();
4533 }
4534
Adam Cohen432609a2014-03-13 17:03:22 -07004535 /**
4536 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4537 * screen that must be displayed and dismissed.
4538 */
4539 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004540 if (mLauncherCallbacks != null) {
4541 return mLauncherCallbacks.hasDismissableIntroScreen();
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543 return false;
4544 }
4545
4546 /**
4547 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4548 */
4549 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004550 if (mLauncherCallbacks != null) {
4551 return mLauncherCallbacks.getIntroScreen();
4552 }
Adam Cohen432609a2014-03-13 17:03:22 -07004553 return null;
4554 }
4555
4556 /**
4557 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4558 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4559 */
4560 private boolean shouldShowIntroScreen() {
4561 return hasDismissableIntroScreen() &&
4562 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4563 }
4564
4565 protected void showIntroScreen() {
4566 View introScreen = getIntroScreen();
4567 changeWallpaperVisiblity(false);
4568 if (introScreen != null) {
4569 mDragLayer.showOverlayView(introScreen);
4570 }
4571 }
4572
4573 public void dismissIntroScreen() {
4574 markIntroScreenDismissed();
4575 if (showFirstRunActivity()) {
4576 // We delay hiding the intro view until the first run activity is showing. This
4577 // avoids a blip.
4578 mWorkspace.postDelayed(new Runnable() {
4579 @Override
4580 public void run() {
4581 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004582 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004583 }
4584 }, ACTIVITY_START_DELAY);
4585 } else {
4586 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004587 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004588 }
4589 changeWallpaperVisiblity(true);
4590 }
4591
4592 private void markIntroScreenDismissed() {
4593 SharedPreferences.Editor editor = mSharedPrefs.edit();
4594 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4595 editor.apply();
4596 }
4597
Adam Cohen091440a2015-03-18 14:16:05 -07004598 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004599 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4600 // on the device, then we always show the first run cling experience (or if there is no
4601 // launcher2). Otherwise, we prompt the user upon started for migration
4602 LauncherClings launcherClings = new LauncherClings(this);
4603 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004604 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004605 if (mModel.canMigrateFromOldLauncherDb(this)) {
4606 launcherClings.showMigrationCling();
4607 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004608 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 }
4610 }
4611 }
4612
Winson Chunga6945242014-01-08 14:04:34 -08004613 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004614 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4615 if (mHotseat != null) mHotseat.setAlpha(1f);
4616 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004617 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4618 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004619 }
4620
4621 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004622 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4623 if (mHotseat != null) mHotseat.setAlpha(0f);
4624 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004625 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4626 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004627 }
4628
Winson Chung5ffd51d2015-06-25 11:36:50 -07004629 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004630 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004631 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004632 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004633 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004634 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004635 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4636 if (userHandle != null) {
4637 user = UserHandleCompat.fromUser(userHandle);
4638 }
4639 }
4640 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004641 }
4642
4643 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004644 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004645 if (user == null) {
4646 user = UserHandleCompat.myUserHandle();
4647 }
4648
4649 // Called from search suggestion, add the profile extra to the intent to ensure that we
4650 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004651 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004652 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004653 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 return null;
4655 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004656 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004657 }
4658
Winson Chung5ffd51d2015-06-25 11:36:50 -07004659 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4661 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004662 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004663 UserHandleCompat.myUserHandle());
4664 }
4665
Winson Chung5ffd51d2015-06-25 11:36:50 -07004666 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004667 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4668 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004669 mWorkspace.onExternalDragStartedWithItem(dragView);
4670 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004671 }
4672
Winson Chung5ffd51d2015-06-25 11:36:50 -07004673 protected void moveWorkspaceToDefaultScreen() {
4674 mWorkspace.moveToDefaultScreen(false);
4675 }
4676
Anjali Koppalf5d29132014-02-28 13:33:27 -08004677 @Override
4678 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004679 if (mLauncherCallbacks != null) {
4680 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4681 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004682 }
4683
Winson Chung80baf5a2010-08-09 16:03:15 -07004684 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004685 * Returns a FastBitmapDrawable with the icon, accurately sized.
4686 */
4687 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4688 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4689 d.setFilterBitmap(true);
4690 resizeIconDrawable(d);
4691 return d;
4692 }
4693
4694 /**
4695 * Resizes an icon drawable to the correct icon size.
4696 */
Winson5fbe0742015-09-30 15:33:00 -07004697 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004698 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004699 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004700 }
4701
4702 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004703 * Prints out out state for debugging.
4704 */
4705 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004706 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004707 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004708 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4709 Log.d(TAG, "mRestoring=" + mRestoring);
4710 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004711 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004712 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004713 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004714
Daniel Sandler325dc232013-06-05 22:57:57 -04004715 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004716 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004717
4718 @Override
4719 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4720 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004721 // Dump workspace
4722 writer.println(prefix + "Workspace Items");
4723 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4724 writer.println(prefix + " Homescreen " + i);
4725
4726 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4727 for (int j = 0; j < layout.getChildCount(); j++) {
4728 Object tag = layout.getChildAt(j).getTag();
4729 if (tag != null) {
4730 writer.println(prefix + " " + tag.toString());
4731 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004732 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004733 }
Sunny Goyala1365452015-10-01 15:46:24 -07004734
4735 writer.println(prefix + " Hotseat");
4736 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4737 for (int j = 0; j < layout.getChildCount(); j++) {
4738 Object tag = layout.getChildAt(j).getTag();
4739 if (tag != null) {
4740 writer.println(prefix + " " + tag.toString());
4741 }
4742 }
4743
4744 synchronized (sDumpLogs) {
4745 writer.println();
4746 writer.println(prefix + "Debug logs");
4747 for (String log : sDumpLogs) {
4748 writer.println(prefix + " " + log);
4749 }
4750 }
4751
Adam Cohen9211d422014-10-07 18:14:53 -07004752 if (mLauncherCallbacks != null) {
4753 mLauncherCallbacks.dump(prefix, fd, writer, args);
4754 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004755 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004756
Sunny Goyala1365452015-10-01 15:46:24 -07004757 public static void addDumpLog(String tag, String log) {
4758 Log.d(tag, log);
4759 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004760 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004761 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004762 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004763 }
4764
Adam Cohen59400422014-03-05 18:07:04 -08004765 public static CustomAppWidget getCustomAppWidget(String name) {
4766 return sCustomAppWidgets.get(name);
4767 }
4768
4769 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4770 return sCustomAppWidgets;
4771 }
4772
Sunny Goyal29538332016-02-18 09:10:19 -08004773 public static List<View> getFolderContents(View icon) {
4774 if (icon instanceof FolderIcon) {
4775 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4776 } else {
4777 return Collections.EMPTY_LIST;
4778 }
4779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004780}
Michael Jurka2763be32011-02-24 11:19:57 -08004781
Dan Sandlerd5024042014-01-09 15:01:33 -05004782interface DebugIntents {
4783 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4784 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004785}