blob: 13e451a27690cc227a5ebb3c080e9a95759c64f1 [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;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080048import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020063import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020065import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080066import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070067import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070068import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040069import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070070import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.Selection;
72import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070073import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.HapticFeedbackConstants;
78import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import 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;
Tony Wickhame2217252016-03-22 16:34:23 -070089import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070090import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
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;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800113import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700114import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700115import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700116import com.android.launcher3.pageindicators.PageIndicator;
117import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700120import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800137import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700153 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700161 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700166 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
167 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
168 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
169
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700170 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
171
Mathew Inwood876a8462013-06-14 14:12:41 +0100172 /**
173 * IntentStarter uses request codes starting with this. This must be greater than all activity
174 * request codes used internally.
175 */
176 protected static final int REQUEST_LAST = 100;
177
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700179 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800180 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Winson Chung2672ff92012-05-04 16:22:30 -0700182 // The Intent extra that defines whether to ignore the launch animation
183 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400184 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700190 // Type: Content Values / parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 private static final String QSB_WIDGET_ID = "qsb_widget_id";
201 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700226 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700227 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700230
231 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Sunny Goyalffe83f12014-08-14 17:39:34 -0700233 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700236 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800237 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700249 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700281 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700282 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700284 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Sunny Goyal639e9062015-08-19 19:17:06 -0700287 private LauncherClings mClings;
288
Adam Cohen61f560d2013-09-30 15:58:20 -0700289 private View.OnTouchListener mHapticFeedbackTouchListener;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700293 private static final int ADVANCE_INTERVAL = 20000;
294 private static final int ADVANCE_STAGGER = 250;
295
296 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Winson Chung46353de2012-02-16 14:05:10 -0800310 // We only want to get the SharedPreferences once since it does an FS stat each time we get
311 // it from the context.
312 private SharedPreferences mSharedPrefs;
313
Winson Chungf0c6ae02012-03-21 16:10:31 -0700314 // Holds the page that we need to animate to, and the icon views that we need to animate up
315 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700317 private Bitmap mFolderIconBitmap;
318 private Canvas mFolderIconCanvas;
319 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700320
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700321 private DeviceProfile mDeviceProfile;
322
Adam Cohen4b66bf32015-09-18 12:15:19 -0700323 private boolean mMoveToDefaultScreenFromNewIntent;
324
Winson Chung13eb5272015-05-11 16:30:13 -0700325 // This is set to the view that launched the activity that navigated the user away from
326 // launcher. Since there is no callback for when the activity has finished launching, enable
327 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 private BubbleTextView mWaitingForResume;
329
Adam Cohen59400422014-03-05 18:07:04 -0800330 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
331 new HashMap<String, CustomAppWidget>();
332
Adam Cohen59400422014-03-05 18:07:04 -0800333 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700334 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
335 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800336 }
337 }
338
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700341 if (mWorkspace != null) {
342 mWorkspace.buildPageHardwareLayers();
343 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 }
345 };
346
Adam Cohendb364c32014-05-20 14:23:40 -0700347 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800348
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350 int requestCode;
351 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700352 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700353 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 int cellX;
355 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700356 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 }
358
Hyunyoung Songaa953652016-04-19 18:30:24 -0700359 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800360
Adam Cohenf9c184a2016-01-15 16:47:43 -0800361 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700362 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400363
364 @Thunk void setOrientation() {
365 if (mRotationEnabled) {
366 unlockScreenOrientation(true);
367 } else {
368 setRequestedOrientation(
369 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700370 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700372
Sunny Goyal745bad92016-05-02 10:54:12 -0700373 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
376 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700377 if (DEBUG_STRICT_MODE) {
378 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
379 .detectDiskReads()
380 .detectDiskWrites()
381 .detectNetwork() // or .detectAll() for all detectable problems
382 .penaltyLog()
383 .build());
384 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
385 .detectLeakedSqlLiteObjects()
386 .detectLeakedClosableObjects()
387 .penaltyLog()
388 .penaltyDeath()
389 .build());
390 }
391
Adam Cohen9211d422014-10-07 18:14:53 -0700392 if (mLauncherCallbacks != null) {
393 mLauncherCallbacks.preOnCreate();
394 }
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400397
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400398 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700399
Adam Cohen2e6da152015-05-06 11:42:25 -0700400 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700401 mDeviceProfile = getResources().getConfiguration().orientation
402 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700403 app.getInvariantDeviceProfile().landscapeProfile
404 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700405
Sunny Goyalf7258242015-10-19 16:59:07 -0700406 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700407 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 mModel = app.setLauncher(this);
409 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700410
Joe Onorato41a12d22009-10-31 18:30:00 -0400411 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700412 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Sunny Goyalffe83f12014-08-14 17:39:34 -0700414 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700415
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
417 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700419 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
420 // this also ensures that any synchronous binding below doesn't re-trigger another
421 // LauncherModel load.
422 mPaused = false;
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200425 android.os.Debug.startMethodTracing(
426 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700430
Tony Wickhameef44322015-10-20 13:24:36 -0700431 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
432 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700434 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700435 mExtractedColors = new ExtractedColors();
436 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437
Tony Wickhame2217252016-03-22 16:34:23 -0700438 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
439 .addAccessibilityStateChangeListener(this);
440
Joe Onorato7c312c12009-08-13 21:36:53 -0700441 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 mSavedState = savedInstanceState;
444 restoreState(mSavedState);
445
446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700451 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700454 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700458 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800469 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700470 // In case we are on a device with locked rotation, we should look at preferences to check
471 // if the user has specifically allowed rotation.
472 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700473 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700474 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
475 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700476 }
477
478 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
479 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400480 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700481
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onCreate(savedInstanceState);
484 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700485
486 if (shouldShowIntroScreen()) {
487 showIntroScreen();
488 } else {
489 showFirstRunActivity();
490 showFirstRunClings();
491 }
Adam Cohen9211d422014-10-07 18:14:53 -0700492 }
493
Sunny Goyal7779d622015-06-11 16:18:39 -0700494 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700495 public void onExtractedColorsChanged() {
496 loadExtractedColorsAndColorItems();
497 }
498
499 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700500 // TODO: do this in pre-N as well, once the extraction part is complete.
501 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700502 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700503 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700504 if (mPageIndicator != null) {
505 mPageIndicator.updateColor(mExtractedColors);
506 }
Tony Wickham827cef22016-03-17 15:39:39 -0700507 }
508 }
509
Adam Cohen9211d422014-10-07 18:14:53 -0700510 private LauncherCallbacks mLauncherCallbacks;
511
512 public void onPostCreate(Bundle savedInstanceState) {
513 super.onPostCreate(savedInstanceState);
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onPostCreate(savedInstanceState);
516 }
517 }
518
Sunny Goyal32554d12015-12-03 15:31:25 -0800519 /**
520 * Call this after onCreate to set or clear overlay.
521 */
522 public void setLauncherOverlay(LauncherOverlay overlay) {
523 if (overlay != null) {
524 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
525 }
526 mWorkspace.setLauncherOverlay(overlay);
527 }
528
Adam Cohen9211d422014-10-07 18:14:53 -0700529 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
530 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700532 private boolean mWorkspaceImportanceStored = false;
533 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800535 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
537
538 @Override
539 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 return;
542 }
543 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700544 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspace != null) {
546 mWorkspaceImportanceForAccessibility =
547 mWorkspace.getImportantForAccessibility();
548 mWorkspace.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mWorkspaceImportanceStored = true;
551 }
552 if (mHotseat != null) {
553 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
554 mHotseat.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mHotseatImportanceStored = true;
557 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 }
559
560 @Override
561 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspaceImportanceStored && mWorkspace != null) {
563 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
564 }
565 if (mHotseatImportanceStored && mHotseat != null) {
566 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
567 }
568 mWorkspaceImportanceStored = false;
569 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571 });
Adam Cohen9211d422014-10-07 18:14:53 -0700572 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700573 }
574
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700576 public void onLauncherProviderChange() {
577 if (mLauncherCallbacks != null) {
578 mLauncherCallbacks.onLauncherProviderChange();
579 }
580 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581
Adam Cohen9211d422014-10-07 18:14:53 -0700582 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700583 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700584 if (mLauncherCallbacks != null) {
585 return mLauncherCallbacks.hasCustomContentToLeft();
586 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 return false;
588 }
589
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500591 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 * {@link #addToCustomContentPage}. This will only be invoked if
593 * {@link #hasCustomContentToLeft()} is {@code true}.
594 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.populateCustomContentContainer();
598 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 }
600
601 /**
602 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
603 * ensure the custom content page is added or removed if necessary.
604 */
605 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600606 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 // Not bound yet, wait for bindScreens to be called.
608 return;
609 }
610
611 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
612 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 mWorkspace.createCustomContentContainer();
614 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
616 mWorkspace.removeCustomContentPage();
617 }
618 }
619
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800620 /**
621 * Logger object is a singleton and does not have to be coupled with the foreground activity.
622 * Since most user event logging is done on the UI, the object is retrieved from the
623 * callback for convenience.
624 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700625 private UserEventDispatcher createUserEventDispatcher() {
626 return new UserEventDispatcher() {
627 @Override
628 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
629 if (!DEBUG_LOGGING) {
630 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800631 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 Log.d("UserEvent", String.format(Locale.US,
633 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
634 LoggerUtils.getActionStr(ev.action),
635 LoggerUtils.getTargetStr(ev.srcTarget[0]),
636 LoggerUtils.getTargetStr(ev.srcTarget[1]),
637 ev.elapsedContainerMillis,
638 ev.elapsedSessionMillis));
639 }
640 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
642
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 public UserEventDispatcher getUserEventDispatcher() {
644 if (mLauncherCallbacks != null) {
645 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
646 if (dispatcher != null) {
647 return dispatcher;
648 }
649 }
650
651 if (mUserEventDispatcher == null) {
652 mUserEventDispatcher = createUserEventDispatcher();
653 }
654 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800655 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100656
Hyunyoung Song3f471442015-04-08 19:01:34 -0700657 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
659 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700660 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 }
662
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000663 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700664 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
665 // This cast is safe as long as the id < 0x00FFFFFF
666 // Since we jail all the dynamically generated views, there should be no clashes
667 // with any other views.
668 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000669 }
670
671 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
673 * a configuration step, this allows the proper animations to run after other transitions.
674 */
Adam Cohendb364c32014-05-20 14:23:40 -0700675 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700676 long screenId = args.screenId;
677 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
678 // When the screen id represents an actual screen (as opposed to a rank) we make sure
679 // that the drop page actually exists.
680 screenId = ensurePendingDropLayoutExists(args.screenId);
681 }
Adam Cohendb364c32014-05-20 14:23:40 -0700682
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700685 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700689 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700691 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700692 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700693 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700694 case REQUEST_BIND_PENDING_APPWIDGET: {
695 int widgetId = args.appWidgetId;
696 LauncherAppWidgetInfo info =
697 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
698 if (info != null) {
699 // Since the view was just bound, also launch the configure activity if needed
700 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
701 .getLauncherAppWidgetInfo(widgetId);
702 if (provider != null && provider.configure != null) {
703 startRestoredWidgetReconfigActivity(provider, info);
704 }
705 }
706 break;
707 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 }
Michael Jurka27614d22012-04-02 06:40:22 -0700709 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
710 // if you turned the screen off and then back while in All Apps, Launcher would not
711 // return to the workspace. Clearing mAddInfo.container here fixes this issue
712 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700713 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
715
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700718 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700719 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700720 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800721 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700732 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
735 if (resultCode == RESULT_CANCELED) {
736 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800740 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700741 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700742
743 // When the user has granted permission to bind widgets, we should check to see if
744 // we can inflate the default search bar widget.
745 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 }
747 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200748 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
749 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700750 // User could have free-scrolled between pages before picking a wallpaper; make sure
751 // we move to the closest one now.
752 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700753 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 }
755 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700759 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700760
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 // We have special handling for widgets
763 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 final int appWidgetId;
765 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
766 : -1;
767 if (widgetId < 0) {
768 appWidgetId = pendingAddWidgetId;
769 } else {
770 appWidgetId = widgetId;
771 }
772
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800774 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700775 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
776 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 result = RESULT_CANCELED;
778 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 @Override
781 public void run() {
782 exitSpringLoadedDragModeDelayed(false, 0, null);
783 }
784 };
Adam Cohendb364c32014-05-20 14:23:40 -0700785 if (workspaceLocked) {
786 // No need to remove the empty screen if we're mid-binding, as the
787 // the bind will not add the empty screen.
788 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
789 } else {
790 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
792 }
Winson Chung5aaab772012-04-27 15:24:02 -0700793 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700794 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700795 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
796 // When the screen id represents an actual screen (as opposed to a rank)
797 // we make sure that the drop page actually exists.
798 mPendingAddInfo.screenId =
799 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
800 }
Adam Cohendb364c32014-05-20 14:23:40 -0700801 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
802
803 dropLayout.setDropPending(true);
804 final Runnable onComplete = new Runnable() {
805 @Override
806 public void run() {
807 completeTwoStageWidgetDrop(resultCode, appWidgetId);
808 dropLayout.setDropPending(false);
809 }
810 };
811 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 } else {
814 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
815 mPendingAddInfo);
816 sPendingAddItem = args;
817 }
Winson Chung5aaab772012-04-27 15:24:02 -0700818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 return;
820 }
821
Sunny Goyald478c832016-04-01 12:04:16 -0700822 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
823 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700824 if (resultCode == RESULT_OK) {
825 // Update the widget view.
826 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
827 pendingAddWidgetId, mPendingAddInfo);
828 if (workspaceLocked) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 }
833 }
834 // Leave the widget in the pending state if the user canceled the configure.
835 return;
836 }
837
Sunny Goyalaad90582015-07-09 14:22:50 -0700838 if (requestCode == REQUEST_CREATE_SHORTCUT) {
839 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
840 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
841 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
842 mPendingAddInfo);
843 if (isWorkspaceLocked()) {
844 sPendingAddItem = args;
845 } else {
846 completeAdd(args);
847 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
848 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
849 }
850 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700851 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
852 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800855 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800856
857 }
858
859 @Override
860 protected void onActivityResult(
861 final int requestCode, final int resultCode, final Intent data) {
862 handleActivityResult(requestCode, resultCode, data);
863 if (mLauncherCallbacks != null) {
864 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600868 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700869 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600870 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700871 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
872 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
873 View v = null;
874 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
875 if (layout != null) {
876 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
877 }
878 Intent intent = sPendingAddItem.intent;
879 sPendingAddItem = null;
880 if (grantResults.length > 0
881 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
882 startActivity(v, intent, null);
883 } else {
884 // TODO: Show a snack bar with link to settings
885 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
886 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
887 }
888 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600889 if (mLauncherCallbacks != null) {
890 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
891 grantResults);
892 }
893 }
894
Adam Cohendb364c32014-05-20 14:23:40 -0700895 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
896 appWidgetId, ItemInfo info) {
897 PendingAddArguments args = new PendingAddArguments();
898 args.requestCode = requestCode;
899 args.intent = data;
900 args.container = info.container;
901 args.screenId = info.screenId;
902 args.cellX = info.cellX;
903 args.cellY = info.cellY;
904 args.appWidgetId = appWidgetId;
905 return args;
906 }
907
908 /**
909 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
910 *
911 * @param screenId the screen id to check
912 * @return the new screen, or screenId if it exists
913 */
914 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800915 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700916 if (dropLayout == null) {
917 // it's possible that the add screen was removed because it was
918 // empty and a re-bind occurred
919 mWorkspace.addExtraEmptyScreen();
920 return mWorkspace.commitExtraEmptyScreen();
921 } else {
922 return screenId;
923 }
924 }
925
Adam Cohen091440a2015-03-18 14:16:05 -0700926 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800927 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 Runnable onCompleteRunnable = null;
929 int animationType = 0;
930
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 if (resultCode == RESULT_OK) {
933 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
934 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 mPendingAddWidgetInfo);
936 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 onCompleteRunnable = new Runnable() {
938 @Override
939 public void run() {
940 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700942 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
943 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
945 };
946 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800947 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 if (mDragLayer.getAnimatedView() != null) {
951 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
952 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
953 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700954 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 // The animated view may be null in the case of a rotation during widget configuration
956 onCompleteRunnable.run();
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 protected void onStop() {
962 super.onStop();
963 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStop();
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
971 protected void onStart() {
972 super.onStart();
973 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700974
975 if (mLauncherCallbacks != null) {
976 mLauncherCallbacks.onStart();
977 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200982 long startTime = 0;
983 if (DEBUG_RESUME_TIME) {
984 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400985 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200986 }
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.preOnResume();
990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700993 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700994
Winson Chung4a2afa32012-07-19 14:53:05 -0700995 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700996 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800998 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700999 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001000 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001001 // view after launching an app, as they may be depending on the UI to be static to
1002 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001003 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001004 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001005 } else if (mOnResumeState == State.WIDGETS) {
1006 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001007 }
1008 mOnResumeState = State.NONE;
1009
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001010 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001011 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1012 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001013
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001014 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001015 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001016 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001017 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001022 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1023 // execute them here
1024 long startTimeCallbacks = 0;
1025 if (DEBUG_RESUME_TIME) {
1026 startTimeCallbacks = System.currentTimeMillis();
1027 }
1028
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1030 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001031 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001033 if (DEBUG_RESUME_TIME) {
1034 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1035 (System.currentTimeMillis() - startTimeCallbacks));
1036 }
Michael Jurka447bf842013-05-15 14:52:15 +02001037 }
Michael Jurka54554252013-08-01 12:52:23 +02001038 if (mOnResumeCallbacks.size() > 0) {
1039 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1040 mOnResumeCallbacks.get(i).run();
1041 }
1042 mOnResumeCallbacks.clear();
1043 }
Winson Chunge4e50662012-01-23 14:45:13 -08001044
1045 // Reset the pressed state of icons that were locked in the press state while activities
1046 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001047 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001048 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001049 mWaitingForResume.setStayPressed(false);
1050 }
Winson Chung82963d52013-10-09 11:20:57 -07001051
Adam Cohen06dff352012-06-01 17:17:08 -07001052 // It is possible that widgets can receive updates while launcher is not in the foreground.
1053 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1054 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1055 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001056 if (!isWorkspaceLoading()) {
1057 getWorkspace().reinflateWidgetsIfNecessary();
1058 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001059 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 // We want to suppress callbacks about CustomContent being shown if we have just received
1066 // onNewIntent while the user was present within launcher. In that case, we post a call
1067 // to move the user to the main screen (which will occur after onResume). We don't want to
1068 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1069 // suppress here.
1070 if (mWorkspace.getCustomContentCallbacks() != null
1071 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001073 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1075 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 }
1078 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001079 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001080 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001081 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001082
Sunny Goyal756adbc2015-04-16 15:20:51 -07001083 if (!isWorkspaceLoading()) {
1084 // Process any items that were added while Launcher was away.
1085 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1086 }
Adam Cohen9211d422014-10-07 18:14:53 -07001087
1088 if (mLauncherCallbacks != null) {
1089 mLauncherCallbacks.onResume();
1090 }
Adam Cohen06dff352012-06-01 17:17:08 -07001091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001095 // Ensure that items added to Launcher are queued until Launcher returns
1096 InstallShortcutReceiver.enableInstallQueue();
1097
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001099 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001100 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001101 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001102
1103 // We call onHide() aggressively. The custom content callbacks should be able to
1104 // debounce excess onHide calls.
1105 if (mWorkspace.getCustomContentCallbacks() != null) {
1106 mWorkspace.getCustomContentCallbacks().onHide();
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Adam Cohen9211d422014-10-07 18:14:53 -07001109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
1114 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001115 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1116 // by a onResume or by scrolling otherwise.
1117 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001118
1119 // Custom content is completely hidden
1120 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001121
1122 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1123 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001124
1125 // Indicates whether the user is allowed to scroll away from the custom content.
1126 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlay {
1130
1131 /**
1132 * Touch interaction leading to overscroll has begun
1133 */
1134 public void onScrollInteractionBegin();
1135
1136 /**
1137 * Touch interaction related to overscroll has ended
1138 */
1139 public void onScrollInteractionEnd();
1140
1141 /**
1142 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1143 * screen (or in the case of RTL, the rightmost screen).
1144 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001145 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146
1147 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001148 * Called when the launcher is ready to use the overlay
1149 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001151 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
Jun Mukai8af0cd82015-05-12 12:32:12 -07001154 public interface LauncherSearchCallbacks {
1155 /**
1156 * Called when the search overlay is shown.
1157 */
1158 public void onSearchOverlayOpened();
1159
1160 /**
1161 * Called when the search overlay is dismissed.
1162 */
1163 public void onSearchOverlayClosed();
1164 }
1165
Adam Cohenc2d6e892014-10-16 09:49:24 -07001166 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 }
1169
1170 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1171
Sunny Goyalc86df472016-02-25 09:19:38 -08001172 public void onScrollChanged(float progress) {
1173 if (mWorkspace != null) {
1174 mWorkspace.onOverlayScrollChanged(progress);
1175 }
1176 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001180 if (mLauncherCallbacks != null) {
1181 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001182 } else {
1183 // On devices with a locked orientation, we will at least have the allow rotation
1184 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001185 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001186 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 }
1188
Adam Cohen9211d422014-10-07 18:14:53 -07001189 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001190 CustomContentCallbacks callbacks, String description) {
1191 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001192 }
1193
Adam Cohenedb40762013-07-18 16:45:45 -07001194 // The custom content needs to offset its content to account for the QSB
1195 public int getTopOffsetForCustomContent() {
1196 return mWorkspace.getPaddingTop();
1197 }
1198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 @Override
1200 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 if (mModel.isCurrentCallbacks(this)) {
1203 mModel.stopLoader();
1204 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001205 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1206
Romain Guy13c2e7b2010-03-10 19:45:00 -08001207 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 @Override
1212 public void onWindowFocusChanged(boolean hasFocus) {
1213 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001214 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001215
1216 if (mLauncherCallbacks != null) {
1217 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 private boolean acceptFilter() {
1222 final InputMethodManager inputManager = (InputMethodManager)
1223 getSystemService(Context.INPUT_METHOD_SERVICE);
1224 return !inputManager.isFullscreenMode();
1225 }
1226
1227 @Override
1228 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001229 final int uniChar = event.getUnicodeChar();
1230 final boolean handled = super.onKeyDown(keyCode, event);
1231 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1232 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1234 keyCode, event);
1235 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001237 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // showSearchDialog()
1239 // If there are multiple keystrokes before the search dialog takes focus,
1240 // onSearchRequested() will be called for every keystroke,
1241 // but it is idempotent, so it's fine.
1242 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Joe Onorato8a9625e2010-01-28 15:55:35 -08001246 // Eat the long press event so the keyboard doesn't come up.
1247 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1248 return true;
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return handled;
1252 }
1253
Winson46163472015-09-22 17:31:56 -07001254 @Override
1255 public boolean onKeyUp(int keyCode, KeyEvent event) {
1256 if (keyCode == KeyEvent.KEYCODE_MENU) {
1257 // Ignore the menu key if we are currently dragging or are on the custom content screen
1258 if (!isOnCustomContent() && !mDragController.isDragging()) {
1259 // Close any open folders
1260 closeFolder();
1261
1262 // Stop resizing any widgets
1263 mWorkspace.exitWidgetResizeMode();
1264
1265 // Show the overview mode if we are on the workspace
1266 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1267 !mWorkspace.isSwitchingState()) {
1268 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001269 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001270 }
1271 }
1272 return true;
1273 }
1274 return super.onKeyUp(keyCode, event);
1275 }
1276
Karl Rosaen138a0412009-04-23 19:00:21 -07001277 private String getTypedText() {
1278 return mDefaultKeySsb.toString();
1279 }
1280
1281 private void clearTypedText() {
1282 mDefaultKeySsb.clear();
1283 mDefaultKeySsb.clearSpans();
1284 Selection.setSelection(mDefaultKeySsb, 0);
1285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001288 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1289 * State
1290 */
1291 private static State intToState(int stateOrdinal) {
1292 State state = State.WORKSPACE;
1293 final State[] stateValues = State.values();
1294 for (int i = 0; i < stateValues.length; i++) {
1295 if (stateValues[i].ordinal() == stateOrdinal) {
1296 state = stateValues[i];
1297 break;
1298 }
1299 }
1300 return state;
1301 }
1302
1303 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 * Restores the previous state, if it exists.
1305 *
1306 * @param savedState The previous state.
1307 */
1308 private void restoreState(Bundle savedState) {
1309 if (savedState == null) {
1310 return;
1311 }
1312
Michael Jurka883f55b2010-10-21 15:47:14 -07001313 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001314 if (state == State.APPS || state == State.WIDGETS) {
1315 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001316 }
1317
Adam Cohen21cd0022013-10-09 18:57:02 -07001318 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1319 PagedView.INVALID_RESTORE_PAGE);
1320 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001321 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyal756cd262015-08-20 12:33:21 -07001324 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1325 if (itemValues != null) {
1326 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001327 AppWidgetProviderInfo info = savedState.getParcelable(
1328 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001329 mPendingAddWidgetInfo = info == null ?
1330 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1331
Adam Cohen4637b5a2013-11-04 18:21:24 -08001332 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001333 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 mRestoring = true;
1335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
1338 /**
1339 * Finds all the views we need and configure them properly.
1340 */
1341 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001342 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001343
Craig Mautner360310b2012-10-26 15:13:08 -07001344 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001345 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001346 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1347 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001348 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001349
Sunny Goyal784f9c32016-03-23 16:56:54 -07001350 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1351 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1352 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
Winson Chung4c98d922011-05-31 16:50:48 -07001355 // Setup the drag layer
1356 mDragLayer.setup(this, dragController);
1357
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 // Setup the hotseat
1359 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1360 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001361 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 }
1363
Winsone9f27272015-10-13 10:47:51 -07001364 // Setup the overview panel
1365 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366
Winson Chung4c98d922011-05-31 16:50:48 -07001367 // Setup the workspace
1368 mWorkspace.setHapticFeedbackEnabled(false);
1369 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001370 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001371 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001372
Tony Wickham34d2c912015-09-11 09:27:58 -07001373 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001374 mSearchDropTargetBar = (SearchDropTargetBar)
1375 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001376 // Get the app info bar
1377 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1378 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001379
Winson Chungef7f8742015-06-04 17:18:17 -07001380 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001381 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001382 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001383 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1384 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1385 } else {
1386 mAppsView.setSearchBarController(new DefaultAppSearchController());
1387 }
Craig Mautner360310b2012-10-26 15:13:08 -07001388
Winson Chung3d503fb2011-07-13 17:25:49 -07001389 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001390 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001391 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001392 dragController.setMoveTarget(mWorkspace);
1393 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001394 if (mSearchDropTargetBar != null) {
1395 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001396 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001397 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001398 if (mAppInfoDropTargetBar != null) {
1399 mAppInfoDropTargetBar.setup(this, dragController);
1400 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001401
Sunny Goyal322d5562015-06-25 19:35:49 -07001402 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1403 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
Winsone9f27272015-10-13 10:47:51 -07001407 private void setupOverviewPanel() {
1408 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1409
1410 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1411 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1412 @Override
1413 public boolean onLongClick(View v) {
1414 return v.performClick();
1415 }
1416 };
1417
1418 // Bind wallpaper button actions
1419 View wallpaperButton = findViewById(R.id.wallpaper_button);
1420 wallpaperButton.setOnClickListener(new OnClickListener() {
1421 @Override
1422 public void onClick(View view) {
1423 if (!mWorkspace.isSwitchingState()) {
1424 onClickWallpaperPicker(view);
1425 }
1426 }
1427 });
1428 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1429 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430
1431 // Bind widget button actions
1432 mWidgetsButton = findViewById(R.id.widget_button);
1433 mWidgetsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickAddWidgetButton(view);
1438 }
1439 }
1440 });
1441 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1442 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443
1444 // Bind settings actions
1445 View settingsButton = findViewById(R.id.settings_button);
1446 boolean hasSettings = hasSettings();
1447 if (hasSettings) {
1448 settingsButton.setOnClickListener(new OnClickListener() {
1449 @Override
1450 public void onClick(View view) {
1451 if (!mWorkspace.isSwitchingState()) {
1452 onClickSettingsButton(view);
1453 }
1454 }
1455 });
1456 settingsButton.setOnLongClickListener(performClickOnLongClick);
1457 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1458 } else {
1459 settingsButton.setVisibility(View.GONE);
1460 }
1461
1462 mOverviewPanel.setAlpha(0f);
1463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001466 * Sets the all apps button. This method is called from {@link Hotseat}.
1467 */
1468 public void setAllAppsButton(View allAppsButton) {
1469 mAllAppsButton = allAppsButton;
1470 }
1471
1472 public View getAllAppsButton() {
1473 return mAllAppsButton;
1474 }
1475
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001476 public View getWidgetsButton() {
1477 return mWidgetsButton;
1478 }
1479
Anjali Koppal5ad44842014-03-10 20:34:39 -07001480 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 * Creates a view representing a shortcut.
1482 *
1483 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001486 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * @param parent The group the shortcut belongs to.
1493 * @param info The data structure describing the shortcut.
1494 *
1495 * @return A View inflated from layoutResId.
1496 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001497 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001498 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 parent, false);
1500 favorite.applyFromShortcutInfo(info, mIconCache);
1501 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001518 if (info == null) {
1519 return;
1520 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 final View view = createShortcut(info);
1522
Sunny Goyal5c97f512015-05-19 16:03:28 -07001523 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 }
1544
1545 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 return;
1548 }
1549
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001561 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 */
Adam Cohen091440a2015-03-18 14:16:05 -07001563 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001564 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1565
1566 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001568 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001589 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001590 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 hostView.setVisibility(View.VISIBLE);
1592 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001594 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 private void addAppWidgetToWorkspace(
1598 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setTag(item);
1601 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001602
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 hostView.setFocusable(true);
1604 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001606 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001607 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1608
1609 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001610 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611 }
1612 }
1613
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1615 @Override
1616 public void onReceive(Context context, Intent intent) {
1617 final String action = intent.getAction();
1618 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1619 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001620 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001621 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001622
Winson Chungc100e8e2011-08-09 16:02:43 -07001623 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001624 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001625 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001626 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001627 if (!showWorkspace(false)) {
1628 // If we are already on the workspace, then manually reset all apps
1629 mAppsView.reset();
1630 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1633 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001634 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 }
1636 }
1637 };
1638
1639 @Override
1640 public void onAttachedToWindow() {
1641 super.onAttachedToWindow();
1642
1643 // Listen for broadcasts related to user-presence
1644 final IntentFilter filter = new IntentFilter();
1645 filter.addAction(Intent.ACTION_SCREEN_OFF);
1646 filter.addAction(Intent.ACTION_USER_PRESENT);
1647 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001648 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001649 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001651
1652 if (mLauncherCallbacks != null) {
1653 mLauncherCallbacks.onAttachedToWindow();
1654 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 }
1656
1657 @Override
1658 public void onDetachedFromWindow() {
1659 super.onDetachedFromWindow();
1660 mVisible = false;
1661
Adam Cohend113e0c2010-11-11 10:48:05 -08001662 if (mAttached) {
1663 unregisterReceiver(mReceiver);
1664 mAttached = false;
1665 }
Winson Chungb745afb2015-03-02 11:51:23 -08001666 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001667
1668 if (mLauncherCallbacks != null) {
1669 mLauncherCallbacks.onDetachedFromWindow();
1670 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672
1673 public void onWindowVisibilityChanged(int visibility) {
1674 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001675 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 // The following code used to be in onResume, but it turns out onResume is called when
1677 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1678 // is a more appropriate event to handle
1679 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001680 if (!mWorkspaceLoading) {
1681 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001682 // We want to let Launcher draw itself at least once before we force it to build
1683 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001684 // apps is nice and speedy.
1685 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001687 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 if (mStarted) return;
1689 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001690 // We delay the layer building a bit in order to give
1691 // other message processing a time to run. In particular
1692 // this avoids a delay in hiding the IME if it was
1693 // currently shown, because doing that may involve
1694 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001695 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 final ViewTreeObserver.OnDrawListener listener = this;
1697 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001698 public void run() {
1699 if (mWorkspace != null &&
1700 mWorkspace.getViewTreeObserver() != null) {
1701 mWorkspace.getViewTreeObserver().
1702 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001703 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001704 }
1705 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001706 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001707 }
1708 });
1709 }
1710 clearTypedText();
1711 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 }
1713
Adam Cohen091440a2015-03-18 14:16:05 -07001714 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 mHandler.removeMessages(ADVANCE_MSG);
1716 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1717 mHandler.sendMessageDelayed(msg, delay);
1718 mAutoAdvanceSentTime = System.currentTimeMillis();
1719 }
1720
Adam Cohen091440a2015-03-18 14:16:05 -07001721 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1723 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1724 mAutoAdvanceRunning = autoAdvanceRunning;
1725 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001726 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 sendAdvanceMessage(delay);
1728 } else {
1729 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001730 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1732 }
1733 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001734 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736 }
1737 }
1738
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1740
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 if (msg.what == ADVANCE_MSG) {
1744 int i = 0;
1745 for (View key: mWidgetsToAdvance.keySet()) {
1746 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001747 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 public void run() {
1751 ((Advanceable) v).advance();
1752 }
1753 }, delay);
1754 }
1755 i++;
1756 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001757 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001761 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001762
Winsonc0b52fe2015-09-09 16:38:15 -07001763 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001764 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1766 if (v instanceof Advanceable) {
1767 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001768 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001769 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
1771 }
1772
Winsonc0b52fe2015-09-09 16:38:15 -07001773 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 if (mWidgetsToAdvance.containsKey(hostView)) {
1775 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001776 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001778 }
1779
Hyunyoung Song3f471442015-04-08 19:01:34 -07001780 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001781 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1782 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 }
1784
Winson Chunga6945242014-01-08 14:04:34 -08001785 public DragLayer getDragLayer() {
1786 return mDragLayer;
1787 }
1788
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001789 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001790 return mAppsView;
1791 }
1792
Hyunyoung Song3f471442015-04-08 19:01:34 -07001793 public WidgetsContainerView getWidgetsView() {
1794 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001795 }
1796
Winson Chunga6945242014-01-08 14:04:34 -08001797 public Workspace getWorkspace() {
1798 return mWorkspace;
1799 }
1800
1801 public Hotseat getHotseat() {
1802 return mHotseat;
1803 }
1804
Jorim Jaggid017f882014-01-14 17:08:48 -08001805 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001806 return mOverviewPanel;
1807 }
1808
Winson Chung006ee262015-08-03 14:40:11 -07001809 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001810 return mSearchDropTargetBar;
1811 }
1812
Tony Wickham34d2c912015-09-11 09:27:58 -07001813 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1814 return mAppInfoDropTargetBar;
1815 }
1816
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001817 public LauncherAppWidgetHost getAppWidgetHost() {
1818 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Winson Chunga9abd0e2010-10-27 17:18:37 -07001821 public LauncherModel getModel() {
1822 return mModel;
1823 }
1824
Adam Cohen79d90c52016-04-22 13:29:20 -07001825 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001826 return mSharedPrefs;
1827 }
1828
Adam Cohen2e6da152015-05-06 11:42:25 -07001829 public DeviceProfile getDeviceProfile() {
1830 return mDeviceProfile;
1831 }
1832
Bjorn Bringertc459e522013-06-07 19:36:01 +01001833 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001834 getWindow().closeAllPanels();
1835
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001837 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001838 }
1839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 @Override
1841 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001842 long startTime = 0;
1843 if (DEBUG_RESUME_TIME) {
1844 startTime = System.currentTimeMillis();
1845 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 super.onNewIntent(intent);
1847
1848 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001849 Folder openFolder = mWorkspace.getOpenFolder();
1850 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1851 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1852 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1853 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1854 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001855 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001857
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 if (mWorkspace == null) {
1859 // Can be cases where mWorkspace is null, this prevents a NPE
1860 return;
1861 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 // In all these cases, only animate if we're already on home
1863 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001864
Sunny Goyal935fca12015-10-13 10:19:01 -07001865 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 exitSpringLoadedDragMode();
1867
1868 // If we are already on home, then just animate back to the workspace,
1869 // otherwise, just wait until onResume to set the state back to Workspace
1870 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001871 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 } else {
1873 mOnResumeState = State.WORKSPACE;
1874 }
1875
1876 final View v = getWindow().peekDecorView();
1877 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001878 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001879 INPUT_METHOD_SERVICE);
1880 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1881 }
1882
Winson Chungb745afb2015-03-02 11:51:23 -08001883 // Reset the apps view
1884 if (!alreadyOnHome && mAppsView != null) {
1885 mAppsView.scrollToTop();
1886 }
1887
Hyunyoung Song3f471442015-04-08 19:01:34 -07001888 // Reset the widgets view
1889 if (!alreadyOnHome && mWidgetsView != null) {
1890 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001892
Adam Cohen9211d422014-10-07 18:14:53 -07001893 if (mLauncherCallbacks != null) {
1894 mLauncherCallbacks.onHomeIntent();
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
Adam Cohened307df2013-10-02 09:37:31 -07001897
Adam Cohen9211d422014-10-07 18:14:53 -07001898 if (mLauncherCallbacks != null) {
1899 mLauncherCallbacks.onNewIntent(intent);
1900 }
Winson15f8b172015-08-19 11:02:39 -07001901
1902 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1903 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1904 // animation.
1905 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001906 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1907 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001908 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1909 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001910
1911 // We use this flag to suppress noisy callbacks above custom content state
1912 // from onResume.
1913 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001914 mWorkspace.post(new Runnable() {
1915 @Override
1916 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001917 if (mWorkspace != null) {
1918 mWorkspace.moveToDefaultScreen(true);
1919 }
Winson15f8b172015-08-19 11:02:39 -07001920 }
1921 });
1922 }
1923 }
1924
1925 if (DEBUG_RESUME_TIME) {
1926 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1927 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001928 }
1929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001931 public void onRestoreInstanceState(Bundle state) {
1932 super.onRestoreInstanceState(state);
1933 for (int page: mSynchronouslyBoundPages) {
1934 mWorkspace.restoreInstanceStateForChild(page);
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
1938 @Override
1939 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001940 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001941 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1942 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001943 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001944 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
Michael Jurka883f55b2010-10-21 15:47:14 -07001946 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001947 // We close any open folder since it will not be re-opened, and we need to make sure
1948 // this state is reflected.
1949 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1950 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001954 ContentValues itemValues = new ContentValues();
1955 mPendingAddInfo.writeToValues(itemValues);
1956 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001957 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001958 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onSaveInstanceState(outState);
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 @Override
1967 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Winson Chunge7a03942011-08-05 15:05:12 -07001970 // Remove all pending runnables
1971 mHandler.removeMessages(ADVANCE_MSG);
1972 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001973 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001974 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001975
Winson Chungcd2b0142011-06-08 16:02:26 -07001976 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001977 // It's possible to receive onDestroy after a new Launcher activity has
1978 // been created. In this case, don't interfere with the new Launcher.
1979 if (mModel.isCurrentCallbacks(this)) {
1980 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001981 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001982 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001983
Sunny Goyal745bad92016-05-02 10:54:12 -07001984 if (mRotationPrefChangeHandler != null) {
1985 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001991 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993 mAppWidgetHost = null;
1994
1995 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
1997 TextKeyListener.getInstance().release();
1998
Tony Wickhame2217252016-03-22 16:34:23 -07001999 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2000 .removeAccessibilityStateChangeListener(this);
2001
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002002 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002003
Michael Jurka2ecf9952012-06-18 12:52:28 -07002004 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002005
2006 if (mLauncherCallbacks != null) {
2007 mLauncherCallbacks.onDestroy();
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 public DragController getDragController() {
2012 return mDragController;
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
2016 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002017 onStartForResult(requestCode);
2018 super.startActivityForResult(intent, requestCode);
2019 }
2020
2021 @Override
2022 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2023 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2024 onStartForResult(requestCode);
2025 try {
2026 super.startIntentSenderForResult(intent, requestCode,
2027 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2028 } catch (IntentSender.SendIntentException e) {
2029 throw new ActivityNotFoundException();
2030 }
2031 }
2032
2033 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002034 if (requestCode >= 0) {
2035 setWaitingForResult(true);
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Winson Chung70d72102011-08-12 11:24:35 -07002039 /**
2040 * Indicates that we want global search for this activity by setting the globalSearch
2041 * argument for {@link #startSearch} to true.
2042 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002044 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002046
Karl Rosaen138a0412009-04-23 19:00:21 -07002047 if (initialQuery == null) {
2048 // Use any text typed in the launcher as the initial query
2049 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 if (appSearchData == null) {
2052 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002053 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
Winson Chungadf0c182012-08-23 14:59:07 -07002055 Rect sourceBounds = new Rect();
2056 if (mSearchDropTargetBar != null) {
2057 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2058 }
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 if (clearTextImmediately) {
2063 clearTypedText();
2064 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002065
2066 // We need to show the workspace after starting the search
2067 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 }
2069
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 /**
2071 * Start a text search.
2072 *
2073 * @return {@code true} if the search will start immediately, so any further keypresses
2074 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2075 * to buffer keypresses.
2076 */
2077 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002079 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2080 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2081 sourceBounds);
2082 }
2083
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 }
2088
2089 /**
2090 * Starts the global search activity. This code is a copied from SearchManager
2091 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002094 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2096 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2097 if (globalSearchActivity == null) {
2098 Log.w(TAG, "No global search activity found.");
2099 return;
2100 }
2101 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2103 intent.setComponent(globalSearchActivity);
2104 // Make sure that we have a Bundle to put source in
2105 if (appSearchData == null) {
2106 appSearchData = new Bundle();
2107 } else {
2108 appSearchData = new Bundle(appSearchData);
2109 }
Adam Cohen9211d422014-10-07 18:14:53 -07002110 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 if (!appSearchData.containsKey("source")) {
2112 appSearchData.putString("source", getPackageName());
2113 }
2114 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2115 if (!TextUtils.isEmpty(initialQuery)) {
2116 intent.putExtra(SearchManager.QUERY, initialQuery);
2117 }
2118 if (selectInitialQuery) {
2119 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2120 }
2121 intent.setSourceBounds(sourceBounds);
2122 try {
2123 startActivity(intent);
2124 } catch (ActivityNotFoundException ex) {
2125 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
2128
Yura4f93ec62014-02-04 14:15:21 +00002129 public boolean isOnCustomContent() {
2130 return mWorkspace.isOnOrMovingToCustomContent();
2131 }
2132
Winson Chung70d72102011-08-12 11:24:35 -07002133 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002134 public boolean onPrepareOptionsMenu(Menu menu) {
2135 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002136 if (mLauncherCallbacks != null) {
2137 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2138 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002139 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 @Override
2143 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002144 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 }
2148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 public boolean isWorkspaceLocked() {
2150 return mWorkspaceLoading || mWaitingForResult;
2151 }
2152
Adam Cohen517a7f52014-03-01 12:12:59 -08002153 public boolean isWorkspaceLoading() {
2154 return mWorkspaceLoading;
2155 }
2156
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002157 private void setWorkspaceLoading(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWorkspaceLoading = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 private void setWaitingForResult(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWaitingForResult = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
Adam Cohen9211d422014-10-07 18:14:53 -07002173 protected void onWorkspaceLockedChanged() {
2174 if (mLauncherCallbacks != null) {
2175 mLauncherCallbacks.onWorkspaceLockedChanged();
2176 }
2177 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2183 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002184 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002185 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002186
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002188 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002189 if (LOGD) {
2190 Log.d(TAG, "Adding widget from drop");
2191 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2193 }
2194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002196 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2197 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 if (appWidgetInfo.configure != null) {
2199 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002200 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002201
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002203 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2204 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 Runnable onComplete = new Runnable() {
2209 @Override
2210 public void run() {
2211 // Exit spring loaded mode if necessary after adding the widget
2212 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2213 null);
2214 }
2215 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002218 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002223 // Close any folders that may be open.
2224 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002225 mWorkspace.moveToCustomContentScreen(animate);
2226 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002227
2228 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2229 int[] cell, int spanX, int spanY) {
2230 switch (info.itemType) {
2231 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2232 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2233 int span[] = new int[2];
2234 span[0] = spanX;
2235 span[1] = spanY;
2236 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2237 container, screenId, cell, span);
2238 break;
2239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2240 processShortcutFromDrop(info.componentName, container, screenId, cell);
2241 break;
2242 default:
2243 throw new IllegalStateException("Unknown item type: " + info.itemType);
2244 }
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a shortcut drop.
2249 *
2250 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2255 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259
2260 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.cellX = cell[0];
2262 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264
2265 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2266 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002267 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 /**
2271 * Process a widget drop.
2272 *
2273 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002277 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2278 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = info.minSpanX;
2283 mPendingAddInfo.minSpanY = info.minSpanY;
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.cellX = cell[0];
2287 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002289 if (span != null) {
2290 mPendingAddInfo.spanX = span[0];
2291 mPendingAddInfo.spanY = span[1];
2292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 AppWidgetHostView hostView = info.boundWidget;
2295 int appWidgetId;
2296 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002297 // In the case where we've prebound the widget, we remove it from the DragLayer
2298 if (LOGD) {
2299 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2300 }
2301 getDragLayer().removeView(hostView);
2302
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002304 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002305
2306 // Clear the boundWidget so that it doesn't get destroyed.
2307 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002309 // In this case, we either need to start an activity to get permission to bind
2310 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002312 Bundle options = info.bindOptions;
2313
Sunny Goyalffe83f12014-08-14 17:39:34 -07002314 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2315 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002316 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002317 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002319 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002323 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2324 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002325 // TODO: we need to make sure that this accounts for the options bundle.
2326 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002327 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2328 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 }
2331
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002334 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 folderInfo.title = getText(R.string.folder_name);
2336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002338 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2339 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
2341 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 FolderIcon newFolder =
2343 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002346 // Force measure the new folder icon
2347 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2348 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002349 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
Winsonc0b52fe2015-09-09 16:38:15 -07002352 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * Unbinds the view for the specified item, and removes the item and all its children.
2354 *
2355 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002356 * @param itemInfo the {@link ItemInfo} for this view.
2357 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002358 */
Winson2949fb52015-09-24 09:56:11 -07002359 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002361 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002362 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2363 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002364 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002365 } else {
2366 mWorkspace.removeWorkspaceItem(v);
2367 }
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (deleteFromDb) {
2369 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2370 }
2371 } else if (itemInfo instanceof FolderInfo) {
2372 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002373 if (v instanceof FolderIcon) {
2374 ((FolderIcon) v).removeListeners();
2375 }
Winsonc0b52fe2015-09-09 16:38:15 -07002376 mWorkspace.removeWorkspaceItem(v);
2377 if (deleteFromDb) {
2378 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2379 }
2380 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2381 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002382 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002383 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002386 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002387
Winsonc0b52fe2015-09-09 16:38:15 -07002388 } else {
2389 return false;
2390 }
2391 return true;
2392 }
2393
2394 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002396 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002398 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002399 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002400 // Deleting an app widget ID is a void call but writes to disk before returning
2401 // to the caller...
2402 new AsyncTask<Void, Void, Void>() {
2403 public Void doInBackground(Void ... args) {
2404 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2405 return null;
2406 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002407 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002408 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 @Override
2413 public boolean dispatchKeyEvent(KeyEvent event) {
2414 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2415 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002418 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002419 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002420 dumpState();
2421 return true;
2422 }
2423 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 }
2425 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2426 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 return true;
2429 }
2430 }
2431
2432 return super.dispatchKeyEvent(event);
2433 }
2434
Joe Onorato88ec0992009-11-19 13:16:06 -08002435 @Override
2436 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002437 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2438 return;
2439 }
2440
Sunny Goyal45478022015-06-08 16:52:41 -07002441 if (mDragController.isDragging()) {
2442 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002443 return;
2444 }
2445
Winson Chungb745afb2015-03-02 11:51:23 -08002446 if (isAppsViewVisible()) {
2447 showWorkspace(true);
2448 } else if (isWidgetsViewVisible()) {
2449 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002450 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002451 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002453 Folder openFolder = mWorkspace.getOpenFolder();
2454 if (openFolder.isEditingName()) {
2455 openFolder.dismissEditingName();
2456 } else {
2457 closeFolder();
2458 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002459 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002460 mWorkspace.exitWidgetResizeMode();
2461
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 // Back button is a no-op here, but give at least some feedback for the button press
2463 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002464 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002465 }
2466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002468 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002469 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002470 @Override
2471 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002472 if (mAppWidgetHost != null) {
2473 mAppWidgetHost.startListening();
2474 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002475
2476 // Recreate the QSB, as the widget has been reset.
2477 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002478 }
2479
2480 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 * Launches the intent referred by the clicked shortcut.
2482 *
2483 * @param v The view representing the clicked shortcut.
2484 */
2485 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002486 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2487 // view has detached (it's possible for this to happen if the view is removed mid touch).
2488 if (v.getWindowToken() == null) {
2489 return;
2490 }
2491
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002492 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002493 return;
2494 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002495
Adam Cohen1697b792013-09-17 19:08:21 -07002496 if (v instanceof Workspace) {
2497 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002498 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002499 }
2500 return;
2501 }
2502
2503 if (v instanceof CellLayout) {
2504 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002505 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2506 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002507 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002508 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 }
2510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002512 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002515 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002517 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002518 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002519 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002520 } else if (tag instanceof AppInfo) {
2521 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002522 } else if (tag instanceof LauncherAppWidgetInfo) {
2523 if (v instanceof PendingAppWidgetHostView) {
2524 onClickPendingWidget((PendingAppWidgetHostView) v);
2525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527 }
2528
Sunny Goyal70660032015-05-14 00:07:08 -07002529 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002530 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002531 return false;
2532 }
2533
Michael Jurkaaf442092010-06-10 17:01:57 -07002534 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002535 * Event handler for the app widget view which has not fully restored.
2536 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002537 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002538 if (mIsSafeModeEnabled) {
2539 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2540 return;
2541 }
2542
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002543 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002544 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002545 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2546 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2547 // This should not happen, as we make sure that an Id is allocated during bind.
2548 return;
2549 }
2550 LauncherAppWidgetProviderInfo appWidgetInfo =
2551 mAppWidgetManager.findProvider(info.providerName, info.user);
2552 if (appWidgetInfo != null) {
2553 mPendingAddWidgetId = info.appWidgetId;
2554 mPendingAddInfo.copyFrom(info);
2555 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002556
Sunny Goyald478c832016-04-01 12:04:16 -07002557 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2558 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2559 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2560 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2561 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2562 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2563 }
2564 } else {
2565 LauncherAppWidgetProviderInfo appWidgetInfo =
2566 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2567 if (appWidgetInfo != null) {
2568 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2569 }
Sunny Goyalff572272014-07-23 13:58:07 -07002570 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 } else if (info.installProgress < 0) {
2572 // The install has not been queued
2573 final String packageName = info.providerName.getPackageName();
2574 showBrokenAppInstallDialog(packageName,
2575 new DialogInterface.OnClickListener() {
2576 public void onClick(DialogInterface dialog, int id) {
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2578 }
2579 });
2580 } else {
2581 // Download has started.
2582 final String packageName = info.providerName.getPackageName();
2583 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002584 }
2585 }
2586
Sunny Goyald478c832016-04-01 12:04:16 -07002587 private void startRestoredWidgetReconfigActivity(
2588 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2589 mPendingAddWidgetInfo = provider;
2590 mPendingAddInfo.copyFrom(info);
2591 mPendingAddWidgetId = info.appWidgetId;
2592 mAppWidgetManager.startConfigActivity(provider,
2593 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2594 }
2595
Sunny Goyalff572272014-07-23 13:58:07 -07002596 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002597 * Event handler for the "grid" button that appears on the home screen, which
2598 * enters all apps mode.
2599 *
2600 * @param v The view that was clicked.
2601 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 protected void onClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002604 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002605 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002606 true /* updatePredictedApps */, false /* focusSearchBar */);
2607 }
2608 }
2609
2610 protected void onLongClickAllAppsButton(View v) {
2611 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2612 if (!isAppsViewVisible()) {
2613 showAppsView(true /* animated */, false /* resetListToTop */,
2614 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 }
2616 }
2617
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002618 private void showBrokenAppInstallDialog(final String packageName,
2619 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002620 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002621 .setTitle(R.string.abandoned_promises_title)
2622 .setMessage(R.string.abandoned_promise_explanation)
2623 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2624 .setNeutralButton(R.string.abandoned_clean_this,
2625 new DialogInterface.OnClickListener() {
2626 public void onClick(DialogInterface dialog, int id) {
2627 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2628 mWorkspace.removeAbandonedPromise(packageName, user);
2629 }
2630 })
2631 .create().show();
2632 return;
2633 }
2634
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 /**
2636 * Event handler for an app shortcut click.
2637 *
2638 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2639 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2642 Object tag = v.getTag();
2643 if (!(tag instanceof ShortcutInfo)) {
2644 throw new IllegalArgumentException("Input must be a Shortcut");
2645 }
2646
2647 // Open shortcut
2648 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002649
2650 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002651 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2652 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002653 // Launch activity anyway, framework will tell the user why the app is suspended.
2654 } else {
2655 int error = R.string.activity_not_available;
2656 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2657 error = R.string.safemode_shortcut_error;
2658 }
2659 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2660 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002661 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002662 }
2663
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002665 if ((v instanceof BubbleTextView)
2666 && shortcut.isPromise()
2667 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002668 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002669 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 new DialogInterface.OnClickListener() {
2671 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 return;
2676 }
2677
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 }
2681
Adam Cohen091440a2015-03-18 14:16:05 -07002682 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002684 final ShortcutInfo shortcut;
2685 final Intent intent;
2686 if (tag instanceof ShortcutInfo) {
2687 shortcut = (ShortcutInfo) tag;
2688 intent = shortcut.intent;
2689 int[] pos = new int[2];
2690 v.getLocationOnScreen(pos);
2691 intent.setSourceBounds(new Rect(pos[0], pos[1],
2692 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002693
Sunny Goyal508da152014-08-14 10:53:27 -07002694 } else if (tag instanceof AppInfo) {
2695 shortcut = null;
2696 intent = ((AppInfo) tag).intent;
2697 } else {
2698 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2699 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700
2701 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002702 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703
2704 if (success && v instanceof BubbleTextView) {
2705 mWaitingForResume = (BubbleTextView) v;
2706 mWaitingForResume.setStayPressed(true);
2707 }
2708 }
2709
2710 /**
2711 * Event handler for a folder icon click.
2712 *
2713 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2714 */
2715 protected void onClickFolderIcon(View v) {
2716 if (LOGD) Log.d(TAG, "onClickFolder");
2717 if (!(v instanceof FolderIcon)){
2718 throw new IllegalArgumentException("Input must be a FolderIcon");
2719 }
2720
2721 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002722 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002724 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 }
Michael Jurka5130e402011-10-13 04:55:35 -07002726 }
2727
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002728 /**
2729 * Event handler for the (Add) Widgets button that appears after a long press
2730 * on the home screen.
2731 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002732 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002734 if (mIsSafeModeEnabled) {
2735 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2736 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002737 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002738 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002739 }
2740
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 /**
2742 * Event handler for the wallpaper picker button that appears after a long press
2743 * on the home screen.
2744 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002745 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002746 if (!Utilities.isWallapaperAllowed(this)) {
2747 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2748 return;
2749 }
2750
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002751 String pickerPackage = getString(R.string.wallpaper_picker_package);
2752 if (TextUtils.isEmpty(pickerPackage)) {
2753 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2754 }
2755
Tony Wickham785f7a52015-08-31 17:28:32 -07002756 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2757 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002758 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2759 .setPackage(pickerPackage)
2760 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2761 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 }
2763
2764 /**
2765 * Event handler for a click on the settings button that appears after a long press
2766 * on the home screen.
2767 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002768 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002770 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2771 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 }
2773
Adam Cohen61f560d2013-09-30 15:58:20 -07002774 public View.OnTouchListener getHapticFeedbackTouchListener() {
2775 if (mHapticFeedbackTouchListener == null) {
2776 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002777 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002778 @Override
2779 public boolean onTouch(View v, MotionEvent event) {
2780 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2781 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2782 }
2783 return false;
2784 }
2785 };
2786 }
2787 return mHapticFeedbackTouchListener;
2788 }
2789
Tony Wickhame2217252016-03-22 16:34:23 -07002790 @Override
2791 public void onAccessibilityStateChanged(boolean enabled) {
2792 mDragLayer.onAccessibilityStateChanged(enabled);
2793 }
2794
Adam Cohen9211d422014-10-07 18:14:53 -07002795 public void onDragStarted(View view) {
2796 if (isOnCustomContent()) {
2797 // Custom content screen doesn't participate in drag and drop. If on custom
2798 // content screen, move to default.
2799 moveWorkspaceToDefaultScreen();
2800 }
Adam Cohen9211d422014-10-07 18:14:53 -07002801 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002802
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002803 /**
2804 * Called when the user stops interacting with the launcher.
2805 * This implies that the user is now on the homescreen and is not doing housekeeping.
2806 */
Adam Cohen9211d422014-10-07 18:14:53 -07002807 protected void onInteractionEnd() {
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onInteractionEnd();
2810 }
2811 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002812
2813 /**
2814 * Called when the user starts interacting with the launcher.
2815 * The possible interactions are:
2816 * - open all apps
2817 * - reorder an app shortcut, or a widget
2818 * - open the overview mode.
2819 * This is a good time to stop doing things that only make sense
2820 * when the user is on the homescreen and not doing housekeeping.
2821 */
Adam Cohen9211d422014-10-07 18:14:53 -07002822 protected void onInteractionBegin() {
2823 if (mLauncherCallbacks != null) {
2824 mLauncherCallbacks.onInteractionBegin();
2825 }
2826 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002827
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 /** Updates the interaction state. */
2829 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002830 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002831 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002832 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2833 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002834 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002835 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002836 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 onInteractionEnd();
2838 }
2839 }
2840
Winson Chung8f1eff72015-05-28 17:33:40 -07002841 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002842 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002844 // Only launch using the new animation if the shortcut has not opted out (this is a
2845 // private contract between launcher and may be ignored in the future).
2846 boolean useLaunchAnimation = (v != null) &&
2847 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002848 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2849 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002850
Kenny Guy1317e2d2014-05-08 18:52:50 +01002851 UserHandleCompat user = null;
2852 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2853 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2854 user = userManager.getUserForSerialNumber(serialNumber);
2855 }
2856
2857 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002858 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002859 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002860 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002861 int left = 0, top = 0;
2862 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2863 if (v instanceof TextView) {
2864 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002865 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2866 if (icon != null) {
2867 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002868 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002869 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002870 width = bounds.width();
2871 height = bounds.height();
2872 }
2873 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002874 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2875 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002876 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002877 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002878 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002879 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002880 // On L devices, we use the device default slide-up transition.
2881 // On L MR1 devices, we a custom version of the slide-up transition which
2882 // doesn't have the delay present in the device default.
2883 opts = ActivityOptions.makeCustomAnimation(this,
2884 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002885 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002886 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002887 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002888
2889 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2890 // Could be launching some bookkeeping activity
2891 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002892 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002893 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002894 launcherApps.startActivityForProfile(intent.getComponent(), user,
2895 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002896 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002897 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002899 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2900 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2901 // corresponding permission. Show the appropriate permission prompt if that
2902 // is the case.
2903 if (intent.getComponent() == null
2904 && Intent.ACTION_CALL.equals(intent.getAction())
2905 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2906 PackageManager.PERMISSION_GRANTED) {
2907 // TODO: Rename sPendingAddItem to a generic name.
2908 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2909 0, (ItemInfo) tag);
2910 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2911 REQUEST_PERMISSION_CALL_PHONE);
2912 return false;
2913 }
2914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002916 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002918 "or use the exported attribute for this activity. "
2919 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002921 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002923
Winson Chungbedf9232015-07-10 12:38:30 -07002924 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002925 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002926 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2927 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2928 return false;
2929 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002930 try {
2931 success = startActivity(v, intent, tag);
2932 } catch (ActivityNotFoundException e) {
2933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2934 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2935 }
2936 return success;
2937 }
2938
Adam Cohen268c4752012-06-06 17:47:33 -07002939 /**
2940 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2941 * in the DragLayer in the exact absolute location of the original FolderIcon.
2942 */
2943 private void copyFolderIconToImage(FolderIcon fi) {
2944 final int width = fi.getMeasuredWidth();
2945 final int height = fi.getMeasuredHeight();
2946
2947 // Lazy load ImageView, Bitmap and Canvas
2948 if (mFolderIconImageView == null) {
2949 mFolderIconImageView = new ImageView(this);
2950 }
2951 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2952 mFolderIconBitmap.getHeight() != height) {
2953 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2954 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2955 }
2956
2957 DragLayer.LayoutParams lp;
2958 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2959 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2960 } else {
2961 lp = new DragLayer.LayoutParams(width, height);
2962 }
2963
Adam Cohen307fe232012-08-16 17:55:58 -07002964 // The layout from which the folder is being opened may be scaled, adjust the starting
2965 // view size by this scale factor.
2966 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002967 lp.customPosition = true;
2968 lp.x = mRectForFolderAnimation.left;
2969 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002970 lp.width = (int) (scale * width);
2971 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002972
2973 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2974 fi.draw(mFolderIconCanvas);
2975 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002976 if (fi.getFolder() != null) {
2977 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2978 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002979 }
Adam Cohen268c4752012-06-06 17:47:33 -07002980 // Just in case this image view is still in the drag layer from a previous animation,
2981 // we remove it and re-add it.
2982 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2983 mDragLayer.removeView(mFolderIconImageView);
2984 }
2985 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002986 if (fi.getFolder() != null) {
2987 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002988 }
Adam Cohen268c4752012-06-06 17:47:33 -07002989 }
2990
Adam Cohen37b2a492016-04-06 18:36:06 -07002991 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002992 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002993 FolderInfo info = (FolderInfo) fi.getTag();
2994 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2995 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002996 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2997 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002998 }
2999
Adam Cohen268c4752012-06-06 17:47:33 -07003000 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3001 copyFolderIconToImage(fi);
3002 fi.setVisibility(View.INVISIBLE);
3003
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003004 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3005 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003006 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003007 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3008 }
3009 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003010 oa.start();
3011 }
3012
Sunny Goyal935fca12015-10-13 10:19:01 -07003013 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003014 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003015 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003016
Adam Cohen268c4752012-06-06 17:47:33 -07003017 // We remove and re-draw the FolderIcon in-case it has changed
3018 mDragLayer.removeView(mFolderIconImageView);
3019 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003020
3021 if (cl != null) {
3022 cl.clearFolderLeaveBehind();
3023 }
3024
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003025 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003026 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003027 oa.addListener(new AnimatorListenerAdapter() {
3028 @Override
3029 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003030 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003031 // Remove the ImageView copy of the FolderIcon and make the original visible.
3032 mDragLayer.removeView(mFolderIconImageView);
3033 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003034 }
3035 }
3036 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003038 if (!animate) {
3039 oa.end();
3040 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 }
3042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003044 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 * is animated relative to the specified View. If the View is null, no animation
3046 * is played.
3047 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003048 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003050 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003051
Adam Cohenfb91f302012-06-11 15:45:18 -07003052 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003053 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3054 if (openFolder != null && openFolder != folder) {
3055 // Close any open folder before opening a folder.
3056 closeFolder();
3057 }
3058
Adam Cohena9cf38f2011-05-02 15:36:58 -07003059 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060
Adam Cohena9cf38f2011-05-02 15:36:58 -07003061 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062
Sunny Goyalf4066152015-04-15 09:42:19 -07003063 // While the folder is open, the position of the icon cannot change.
3064 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3065
Adam Cohen4554ee12011-08-03 16:13:21 -07003066 // Just verify that the folder hasn't already been added to the DragLayer.
3067 // There was a one-off crash where the folder had a parent already.
3068 if (folder.getParent() == null) {
3069 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003070 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003071 } else {
3072 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3073 folder.getParent() + ").");
3074 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003075 folder.animateOpen();
3076
3077 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003078
3079 // Notify the accessibility manager that this folder "window" has appeared and occluded
3080 // the workspace items
3081 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3082 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 }
3084
3085 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 closeFolder(true);
3087 }
3088
3089 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003090 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003091 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003092 if (folder.isEditingName()) {
3093 folder.dismissEditingName();
3094 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 }
3097 }
3098
Sunny Goyal935fca12015-10-13 10:19:01 -07003099 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003100 animate &= !Utilities.isPowerSaverOn(this);
3101
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 folder.getInfo().opened = false;
3103
3104 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3105 if (parent != null) {
3106 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003107 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003108 if (fi != null) {
3109 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3110 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003111 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003112 if (animate) {
3113 folder.animateClosed();
3114 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003115 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003116 }
Winson Chung83ca4802013-04-12 15:10:52 -07003117
Sunny Goyal935fca12015-10-13 10:19:01 -07003118 // Notify the accessibility manager that this folder "window" has disappeared and no
3119 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003120 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122
Tony Wickhamdadb3042016-02-24 11:07:00 -08003123 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003125 if (!isDraggingEnabled()) return false;
3126 if (isWorkspaceLocked()) return false;
3127 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128
Winson Chung76648c52015-07-10 14:33:23 -07003129 if (v == mAllAppsButton) {
3130 onLongClickAllAppsButton(v);
3131 return true;
3132 }
3133
Adam Cohen1697b792013-09-17 19:08:21 -07003134 if (v instanceof Workspace) {
3135 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003136 if (!mWorkspace.isTouchActive()) {
3137 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003138 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3139 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3140 return true;
3141 } else {
3142 return false;
3143 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003144 } else {
3145 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003146 }
Adam Cohen1697b792013-09-17 19:08:21 -07003147 }
3148
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003149 CellLayout.CellInfo longClickCellInfo = null;
3150 View itemUnderLongClick = null;
3151 if (v.getTag() instanceof ItemInfo) {
3152 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003153 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003154 itemUnderLongClick = longClickCellInfo.cell;
3155 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 }
3157
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 // The hotseat touch handling does not go through Workspace, and we always allow long press
3159 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003160 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003161 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003162 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003163 // User long pressed on empty space
3164 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3165 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003166 if (mWorkspace.isInOverviewMode()) {
3167 mWorkspace.startReordering(v);
3168 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003169 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003172 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3173 mHotseat.getOrderInHotseat(
3174 longClickCellInfo.cellX,
3175 longClickCellInfo.cellY));
3176 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003178 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 }
3180 }
3181 }
3182 return true;
3183 }
3184
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 boolean isHotseatLayout(View layout) {
3186 return mHotseat != null && layout != null &&
3187 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3188 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003189
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 /**
3191 * Returns the CellLayout of the specified container at the specified screen.
3192 */
Sunny Goyal92820592015-03-02 11:31:35 -08003193 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3195 if (mHotseat != null) {
3196 return mHotseat.getLayout();
3197 } else {
3198 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003199 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003201 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003202 }
3203 }
3204
Winson Chungb745afb2015-03-02 11:51:23 -08003205 /**
3206 * For overridden classes.
3207 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003208 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003209 return isAppsViewVisible();
3210 }
3211
3212 public boolean isAppsViewVisible() {
3213 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3214 }
3215
3216 public boolean isWidgetsViewVisible() {
3217 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003218 }
3219
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003220 private void setWorkspaceBackground(int background) {
3221 switch (background) {
3222 case WORKSPACE_BACKGROUND_TRANSPARENT:
3223 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3224 break;
3225 case WORKSPACE_BACKGROUND_BLACK:
3226 getWindow().setBackgroundDrawable(null);
3227 break;
3228 default:
3229 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3230 }
Craig Mautner360310b2012-10-26 15:13:08 -07003231 }
3232
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003233 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003234 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3235 int curflags = getWindow().getAttributes().flags
3236 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3237 if (wpflags != curflags) {
3238 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3239 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003240 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003241 }
3242
Michael Jurkae326f182011-11-21 14:05:46 -08003243 @Override
3244 public void onTrimMemory(int level) {
3245 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003246 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3247 // The widget preview db can result in holding onto over
3248 // 3MB of memory for caching which isn't necessary.
3249 SQLiteDatabase.releaseMemory();
3250
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003251 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003252 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003253 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003254 if (mLauncherCallbacks != null) {
3255 mLauncherCallbacks.onTrimMemory(level);
3256 }
Michael Jurkae326f182011-11-21 14:05:46 -08003257 }
3258
Winson5c6bdbb2015-09-03 11:36:19 -07003259 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003260 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003261 }
3262
Winson5c6bdbb2015-09-03 11:36:19 -07003263 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003264 boolean changed = mState != State.WORKSPACE ||
3265 mWorkspace.getState() != Workspace.State.NORMAL;
3266 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003267 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003268 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003269 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003270
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 // Set focus to the AppsCustomize button
3272 if (mAllAppsButton != null) {
3273 mAllAppsButton.requestFocus();
3274 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003275 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003276
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003277 // Change the state *after* we've called all the transition code
3278 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003279
Winson Chung5fb63472011-02-02 17:03:37 -08003280 // Resume the auto-advance of widgets
3281 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003282 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003283
Winson Chung0f785722015-04-08 10:27:49 -07003284 if (changed) {
3285 // Send an accessibility event to announce the context change
3286 getWindow().getDecorView()
3287 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003288 }
Winson5c6bdbb2015-09-03 11:36:19 -07003289 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003290 }
3291
Winsone9f27272015-10-13 10:47:51 -07003292 /**
3293 * Shows the overview button.
3294 */
Adam Cohened307df2013-10-02 09:37:31 -07003295 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003296 showOverviewMode(animated, false);
3297 }
3298
3299 /**
3300 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3301 * onto one of the overview panel buttons.
3302 */
3303 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3304 Runnable postAnimRunnable = null;
3305 if (requestButtonFocus) {
3306 postAnimRunnable = new Runnable() {
3307 @Override
3308 public void run() {
3309 // Hitting the menu button when in touch mode does not trigger touch mode to
3310 // be disabled, so if requested, force focus on one of the overview panel
3311 // buttons.
3312 mOverviewPanel.requestFocusFromTouch();
3313 }
3314 };
3315 }
Adam Cohened307df2013-10-02 09:37:31 -07003316 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003317 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003318 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003319 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003320 }
3321
Winson Chungb745afb2015-03-02 11:51:23 -08003322 /**
3323 * Shows the apps view.
3324 */
Winson Chung76648c52015-07-10 14:33:23 -07003325 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3326 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003327 if (resetListToTop) {
3328 mAppsView.scrollToTop();
3329 }
Winson Chung4ac30062015-05-08 17:34:17 -07003330 if (updatePredictedApps) {
3331 tryAndUpdatePredictedApps();
3332 }
Winson Chung76648c52015-07-10 14:33:23 -07003333 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003334 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003335
Winson Chungb745afb2015-03-02 11:51:23 -08003336 /**
3337 * Shows the widgets view.
3338 */
3339 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003340 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003341 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003342 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003343 }
Winson Chung76648c52015-07-10 14:33:23 -07003344 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003345
3346 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003347 @Override
3348 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003349 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003350 }
3351 });
Winson Chungb745afb2015-03-02 11:51:23 -08003352 }
3353
3354 /**
3355 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003356 *
3357 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003358 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003359 // TODO: calling method should use the return value so that when {@code false} is returned
3360 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003361 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003362 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3363 mState != State.WIDGETS_SPRING_LOADED) {
3364 return false;
3365 }
3366 if (toState != State.APPS && toState != State.WIDGETS) {
3367 return false;
3368 }
Winson Chungb745afb2015-03-02 11:51:23 -08003369
3370 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003371 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3372 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003373 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003374 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003375 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003376
Michael Jurkab3e22d92011-10-31 15:58:33 -07003377 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003378 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379
3380 // Pause the auto-advance of widgets until we are out of AllApps
3381 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003382 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383 closeFolder();
3384
3385 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003386 getWindow().getDecorView()
3387 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003388 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389 }
3390
Winson Chungcd99cd32015-04-29 11:03:24 -07003391 /**
3392 * Updates the workspace and interaction state on state change, and return the animation to this
3393 * new state.
3394 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003395 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003396 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003397 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003398 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003399 updateInteraction(fromState, toState);
3400 return anim;
3401 }
3402
Jun Mukaif0033da2015-08-04 18:34:30 -07003403 public void onLauncherClingShown() {
3404 // When a launcher cling appears, it should cover the underlying layers, so their focus
3405 // should be blocked.
3406 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3407 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3408 }
3409 }
3410
3411 public void onLauncherClingDismissed() {
3412 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3413 }
3414
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003416 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003417 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003418 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003419 }
Winson Chungb745afb2015-03-02 11:51:23 -08003420
Winson Chung006ee262015-08-03 14:40:11 -07003421 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003422 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003423 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003424
3425 if (isAppsViewVisible()) {
3426 mState = State.APPS_SPRING_LOADED;
3427 } else if (isWidgetsViewVisible()) {
3428 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003429 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003430 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003431 } else {
3432 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003433 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003434 }
Adam Cohen7777d962011-08-18 18:58:38 -07003435
Hyunyoung Song3f471442015-04-08 19:01:34 -07003436 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003437 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003438 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003439
Winson Chunge7a03942011-08-05 15:05:12 -07003440 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003441 @Override
3442 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003443 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003444 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3445 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003446 // Before we show workspace, hide all apps again because
3447 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3448 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003449 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003450 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003451 } else {
3452 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003453 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003454 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003455 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003456 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457
Tony Wickhame0c33232016-02-08 11:37:04 -08003458 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003459 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3460 || mState == State.WIDGETS_SPRING_LOADED;
3461 }
3462
Michael Jurkad3ef3062010-11-23 16:23:58 -08003463 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003464 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003465 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003466 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003467 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003468 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003469 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3470 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003471 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003472 }
3473
Winson Chung4ac30062015-05-08 17:34:17 -07003474 /**
3475 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3476 * resumed.
3477 */
3478 private void tryAndUpdatePredictedApps() {
3479 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003480 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003481 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003482 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003483 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003484 }
3485 }
3486 }
3487
Michael Jurkab3e22d92011-10-31 15:58:33 -07003488 void lockAllApps() {
3489 // TODO
3490 }
3491
3492 void unlockAllApps() {
3493 // TODO
3494 }
3495
Winsonf768d932015-09-25 16:12:35 -07003496 public boolean launcherCallbacksProvidesSearch() {
3497 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3498 }
3499
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003500 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003501 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003502 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003503 }
3504
Adam Cohen24ce0b32014-01-14 16:18:14 -08003505 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003506 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3507 if (searchProvider == null) {
3508 return null;
3509 }
3510
3511 Bundle opts = new Bundle();
3512 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003513 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003514
Tony Wickham3a3517f2015-10-06 11:27:04 -07003515 // Determine the min and max dimensions of the widget.
3516 LauncherAppState app = LauncherAppState.getInstance();
3517 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3518 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3519 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003520 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3521 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003522 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003523 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003524 int minWidth = maxWidth;
3525 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003526 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3527 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3528 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3529 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3530 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003531 }
3532 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3533 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3534 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3535 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003536 if (LOGD) {
3537 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3538 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3539 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003540
Tony Wickham775455c2015-10-16 09:49:32 -07003541 if (mLauncherCallbacks != null) {
3542 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3543 }
3544
Sunny Goyalf7258242015-10-19 16:59:07 -07003545 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003546 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003547 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003548 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003549 || (widgetInfo == null)
3550 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003551 // A valid widget is not already bound.
3552 if (widgetId > -1) {
3553 mAppWidgetHost.deleteAppWidgetId(widgetId);
3554 widgetId = -1;
3555 }
3556
3557 // Try to bind a new widget
3558 widgetId = mAppWidgetHost.allocateAppWidgetId();
3559
3560 if (!AppWidgetManagerCompat.getInstance(this)
3561 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3562 mAppWidgetHost.deleteAppWidgetId(widgetId);
3563 widgetId = -1;
3564 }
3565
Sunny Goyalf7258242015-10-19 16:59:07 -07003566 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003567 .putInt(QSB_WIDGET_ID, widgetId)
3568 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003569 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003570 }
3571
Sunny Goyal8d595092015-07-06 12:22:14 -07003572 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003573 if (widgetId != -1) {
3574 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003575 mQsb.setId(R.id.qsb_widget);
Sunny Goyal3f732192016-05-13 13:48:44 -07003576 if (!Utilities.containsAll(
3577 AppWidgetManager.getInstance(this).getAppWidgetOptions(widgetId), opts)) {
3578 // Launcher should not be updating the options often.
3579 FileLog.d(TAG, "Options for QSB were not same");
3580 mQsb.updateAppWidgetOptions(opts);
3581 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 mQsb.setPadding(0, 0, 0, 0);
3583 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003584 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003585 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003586 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003587 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003588 }
3589
Sunny Goyal22235bc2015-04-03 09:23:43 -07003590 private void reinflateQSBIfNecessary() {
3591 if (mQsb instanceof LauncherAppWidgetHostView &&
3592 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3593 mSearchDropTargetBar.removeView(mQsb);
3594 mQsb = null;
3595 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3596 }
3597 }
3598
Michael Jurkad7c28052012-04-27 15:43:36 -07003599 @Override
3600 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003601 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003602 final List<CharSequence> text = event.getText();
3603 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003604 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003605 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003606 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003607 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003608 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003609 } else if (mWorkspace != null) {
3610 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003611 } else {
3612 text.add(getString(R.string.all_apps_home_button_label));
3613 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003614 return result;
3615 }
3616
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003617 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003618 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003619 */
Adam Cohen091440a2015-03-18 14:16:05 -07003620 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003621 @Override
3622 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003623 closeSystemDialogs();
3624 }
3625 }
3626
3627 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003628 * If the activity is currently paused, signal that we need to run the passed Runnable
3629 * in onResume.
3630 *
3631 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003632 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3633 * wrong when we're not running, and if the activity comes back to what the configuration was
3634 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003635 *
3636 * Implementation of the method from LauncherModel.Callbacks.
3637 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003638 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003639 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003640 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003642 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003643 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003644 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003645 }
3646 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003647 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 return true;
3649 } else {
3650 return false;
3651 }
3652 }
3653
Michael Jurkac402cd92013-05-20 15:49:32 +02003654 private boolean waitUntilResume(Runnable run) {
3655 return waitUntilResume(run, false);
3656 }
3657
Michael Jurka1e2f4652013-07-08 18:03:46 -07003658 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003659 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003660 }
3661
Michael Jurka7607c2f2013-04-03 14:33:19 -07003662 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003663 * If the activity is currently paused, signal that we need to re-run the loader
3664 * in onResume.
3665 *
3666 * This needs to be called from incoming places where resources might have been loaded
3667 * while we are paused. That is becaues the Configuration might be wrong
3668 * when we're not running, and if it comes back to what it was when we
3669 * were paused, we are not restarted.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 *
3673 * @return true if we are currently paused. The caller might be able to
3674 * skip some work in that case since we will come back again.
3675 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003676 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003677 public boolean setLoadOnResume() {
3678 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003679 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003680 mOnResumeNeedsLoad = true;
3681 return true;
3682 } else {
3683 return false;
3684 }
3685 }
3686
3687 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003689 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003690 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003692 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003693 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003694 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003695 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003696 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003698
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003700 * Clear any pending bind callbacks. This is called when is loader is planning to
3701 * perform a full rebind from scratch.
3702 */
3703 @Override
3704 public void clearPendingBinds() {
3705 mBindOnResumeCallbacks.clear();
3706 if (mPendingExecutor != null) {
3707 mPendingExecutor.markCompleted();
3708 mPendingExecutor = null;
3709 }
3710 }
3711
3712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 * Refreshes the shortcuts shown on the workspace.
3714 *
3715 * Implementation of the method from LauncherModel.Callbacks.
3716 */
3717 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003718 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003719
Winson Chungd64d1762013-08-20 14:37:16 -07003720 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003721 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003722 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003723
Michael Jurka05bf644e2011-11-30 20:28:53 -08003724 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003725 if (mHotseat != null) {
3726 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 }
3728 }
3729
Adam Cohendcd297f2013-06-18 13:13:40 -07003730 @Override
3731 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003732 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003733
Adam Cohen5084cba2013-09-03 12:01:16 -07003734 // If there are no screens, we need to have an empty screen
3735 if (orderedScreenIds.size() == 0) {
3736 mWorkspace.addExtraEmptyScreen();
3737 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003738
3739 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003740 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003741 if (hasCustomContentToLeft()) {
3742 mWorkspace.createCustomContentContainer();
3743 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003744 }
Winson Chung64359a52013-07-08 17:17:08 -07003745 }
3746
3747 @Override
3748 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 int count = orderedScreenIds.size();
3750 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003751 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003752 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 }
3754
Winson Chungd64d1762013-08-20 14:37:16 -07003755 public void bindAppsAdded(final ArrayList<Long> newScreens,
3756 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003757 final ArrayList<ItemInfo> addAnimated,
3758 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003759 Runnable r = new Runnable() {
3760 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003761 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003762 }
3763 };
3764 if (waitUntilResume(r)) {
3765 return;
3766 }
3767
Winson Chungd64d1762013-08-20 14:37:16 -07003768 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003769 if (newScreens != null) {
3770 bindAddScreens(newScreens);
3771 }
Winson Chungd64d1762013-08-20 14:37:16 -07003772
3773 // We add the items without animation on non-visible pages, and with
3774 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003775 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003776 bindItems(addNotAnimated, 0,
3777 addNotAnimated.size(), false);
3778 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003779 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003780 bindItems(addAnimated, 0,
3781 addAnimated.size(), true);
3782 }
Winson Chungc58497e2013-09-03 17:48:37 -07003783
Winson Chung87412982013-10-03 18:34:14 -07003784 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003785 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003786
Winson Chungb745afb2015-03-02 11:51:23 -08003787 if (addedApps != null && mAppsView != null) {
3788 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003789 }
Winson Chungd64d1762013-08-20 14:37:16 -07003790 }
3791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 * Bind the items start-end from the list.
3794 *
3795 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003796 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003797 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003798 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3799 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 Runnable r = new Runnable() {
3801 public void run() {
3802 bindItems(shortcuts, start, end, forceAnimateIcons);
3803 }
3804 };
3805 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 return;
3807 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003808
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003810 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3811 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003812 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003813 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003814 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003815 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003817
3818 // Short circuit if we are loading dock items for a configuration which has no dock
3819 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3820 mHotseat == null) {
3821 continue;
3822 }
3823
Sunny Goyal639e9062015-08-19 19:17:06 -07003824 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 switch (item.itemType) {
3826 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3827 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003828 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003829 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003830
Winson Chung64359a52013-07-08 17:17:08 -07003831 /*
3832 * TODO: FIX collision case
3833 */
Winson Chungce376632013-07-11 16:12:41 -07003834 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3835 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3836 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003837 View v = cl.getChildAt(item.cellX, item.cellY);
3838 Object tag = v.getTag();
3839 String desc = "Collision while binding workspace item: " + item
3840 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003841 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003842 throw (new RuntimeException(desc));
3843 } else {
3844 Log.d(TAG, desc);
3845 }
Winson Chungce376632013-07-11 16:12:41 -07003846 }
Winson Chung64359a52013-07-08 17:17:08 -07003847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003849 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003850 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003851 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003852 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003854 default:
3855 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003857
3858 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3859 item.cellY, 1, 1);
3860 if (animateIcons) {
3861 // Animate all the applications up now
3862 view.setAlpha(0f);
3863 view.setScaleX(0f);
3864 view.setScaleY(0f);
3865 bounceAnims.add(createNewAppBounceAnimation(view, i));
3866 newShortcutsScreenId = item.screenId;
3867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003868 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003869
Winson Chung997a9232013-07-24 15:33:46 -07003870 if (animateIcons) {
3871 // Animate to the correct page
3872 if (newShortcutsScreenId > -1) {
3873 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003874 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003875 final Runnable startBounceAnimRunnable = new Runnable() {
3876 public void run() {
3877 anim.playTogether(bounceAnims);
3878 anim.start();
3879 }
3880 };
Winson Chung997a9232013-07-24 15:33:46 -07003881 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003882 // We post the animation slightly delayed to prevent slowdowns
3883 // when we are loading right after we return to launcher.
3884 mWorkspace.postDelayed(new Runnable() {
3885 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003886 if (mWorkspace != null) {
3887 mWorkspace.snapToPage(newScreenIndex);
3888 mWorkspace.postDelayed(startBounceAnimRunnable,
3889 NEW_APPS_ANIMATION_DELAY);
3890 }
Winson Chung94d67682013-09-25 16:29:40 -07003891 }
3892 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003893 } else {
3894 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003895 }
3896 }
Winson Chung64359a52013-07-08 17:17:08 -07003897 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003899 }
3900
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003901 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3902 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3903 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003904 view.updateAppWidget(null);
3905 view.setOnClickListener(this);
3906 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003907 mWorkspace.requestLayout();
3908 }
3909
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 /**
3911 * Add the views for a widget to the workspace.
3912 *
3913 * Implementation of the method from LauncherModel.Callbacks.
3914 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003915 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003916 Runnable r = new Runnable() {
3917 public void run() {
3918 bindAppWidget(item);
3919 }
3920 };
3921 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003922 return;
3923 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003924
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003925 if (mIsSafeModeEnabled) {
3926 bindSafeModeWidget(item);
3927 return;
3928 }
3929
Daniel Sandler843e8602010-06-07 14:59:01 -04003930 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3931 if (DEBUG_WIDGETS) {
3932 Log.d(TAG, "bindAppWidget: " + item);
3933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003935 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003936
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003937 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3938 // If the provider is not ready, bind as a pending widget.
3939 appWidgetInfo = null;
3940 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3941 // The widget id is not valid. Try to find the widget based on the provider info.
3942 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3943 } else {
3944 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3945 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003946
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003947 // If the provider is ready, but the width is not yet restored, try to restore it.
3948 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3949 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003950 if (appWidgetInfo == null) {
3951 if (DEBUG_WIDGETS) {
3952 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3953 + " belongs to component " + item.providerName
3954 + ", as the povider is null");
3955 }
3956 LauncherModel.deleteItemFromDatabase(this, item);
3957 return;
3958 }
Sunny Goyalff572272014-07-23 13:58:07 -07003959
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003960 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003961 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003962 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3963 // Id has not been allocated yet. Allocate a new id.
3964 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3965 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003966
Sunny Goyald478c832016-04-01 12:04:16 -07003967 // Also try to bind the widget. If the bind fails, the user will be shown
3968 // a click to setup UI, which will ask for the bind permission.
3969 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3970 pendingInfo.spanX = item.spanX;
3971 pendingInfo.spanY = item.spanY;
3972 pendingInfo.minSpanX = item.minSpanX;
3973 pendingInfo.minSpanY = item.minSpanY;
3974 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3975 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3976 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003977
Sunny Goyald478c832016-04-01 12:04:16 -07003978 // Bind succeeded
3979 if (success) {
3980 // If the widget has a configure activity, it is still needs to set it up,
3981 // otherwise the widget is ready to go.
3982 item.restoreStatus = (appWidgetInfo.configure == null)
3983 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3984 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003985 }
Sunny Goyald478c832016-04-01 12:04:16 -07003986
3987 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003988 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003989 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003990 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003991 // The widget was marked as UI not ready, but there is no configure activity to
3992 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003993 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3994 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003995 }
Sunny Goyalff572272014-07-23 13:58:07 -07003996 }
3997
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003998 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003999 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004000 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4001 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004002 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004003
Sunny Goyal56c73602015-09-25 12:55:01 -07004004 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004005 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07004006 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004007 deleteWidgetInfo(item);
4008 return;
4009 }
4010
Sunny Goyal233ee962015-08-03 13:05:01 -07004011 item.minSpanX = appWidgetInfo.minSpanX;
4012 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07004013 addAppWidgetToWorkspace(
4014 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
4015 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004016 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07004017 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004018 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07004019 view.updateAppWidget(null);
4020 view.setOnClickListener(this);
4021 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004022 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004023 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024
Daniel Sandler843e8602010-06-07 14:59:01 -04004025 if (DEBUG_WIDGETS) {
4026 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4027 + (SystemClock.uptimeMillis()-start) + "ms");
4028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029 }
4030
Sunny Goyalff572272014-07-23 13:58:07 -07004031 /**
4032 * Restores a pending widget.
4033 *
4034 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004035 */
Sunny Goyald478c832016-04-01 12:04:16 -07004036 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07004037 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4038 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4039 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07004040 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07004041 }
4042
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004043 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07004044 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07004045
4046 mWorkspace.reinflateWidgetsIfNecessary();
4047 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004048 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004049 }
4050
Adam Cohen1462de32012-07-24 22:34:36 -07004051 public void onPageBoundSynchronously(int page) {
4052 mSynchronouslyBoundPages.add(page);
4053 }
4054
Sunny Goyal527c7d32015-08-28 15:19:36 -07004055 @Override
4056 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4057 if (mPendingExecutor != null) {
4058 mPendingExecutor.markCompleted();
4059 }
4060 mPendingExecutor = executor;
4061 executor.attachTo(this);
4062 }
4063
4064 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4065 if (mPendingExecutor == executor) {
4066 mPendingExecutor = null;
4067 }
4068 }
4069
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 /**
4071 * Callback saying that there aren't any more items to bind.
4072 *
4073 * Implementation of the method from LauncherModel.Callbacks.
4074 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004075 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004076 Runnable r = new Runnable() {
4077 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004078 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004079 }
4080 };
4081 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004082 return;
4083 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 if (mSavedState != null) {
4085 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004086 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004088
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 mSavedState = null;
4090 }
4091
Adam Cohen1462de32012-07-24 22:34:36 -07004092 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004094 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004095
4096 // If we received the result of any pending adds while the loader was running (e.g. the
4097 // widget configuration forced an orientation change), process them now.
4098 if (sPendingAddItem != null) {
4099 final long screenId = completeAdd(sPendingAddItem);
4100
4101 // TODO: this moves the user to the page where the pending item was added. Ideally,
4102 // the screen would be guaranteed to exist after bind, and the page would be set through
4103 // the workspace restore process.
4104 mWorkspace.post(new Runnable() {
4105 @Override
4106 public void run() {
4107 mWorkspace.snapToScreenId(screenId);
4108 }
4109 });
4110 sPendingAddItem = null;
4111 }
4112
Sunny Goyal756adbc2015-04-16 15:20:51 -07004113 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004114
4115 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004116 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004117 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004118 }
4119
Winson Chunga0b7e862013-09-05 16:03:15 -07004120 public boolean isAllAppsButtonRank(int rank) {
4121 if (mHotseat != null) {
4122 return mHotseat.isAllAppsButtonRank(rank);
4123 }
4124 return false;
4125 }
4126
Winson Chunga2413752012-04-03 14:22:34 -07004127 private boolean canRunNewAppsAnimation() {
4128 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004129 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4130 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004131 }
4132
Winson Chungc9168342013-06-26 14:54:55 -07004133 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004134 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004135 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4136 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004137 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004138 return bounceAnim;
4139 }
4140
Adam Cohen27772732013-11-11 14:00:56 +00004141 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004142 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004143 }
4144
Tony Wickham3a3517f2015-10-06 11:27:04 -07004145 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004146 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004147 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004148 }
4149
Tony Wickham55616cd2015-09-23 14:55:17 -07004150 public int getSearchBarHeight() {
4151 if (mLauncherCallbacks != null) {
4152 return mLauncherCallbacks.getSearchBarHeight();
4153 }
4154 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4155 }
4156
Winson Chung88fa7412015-08-03 14:25:28 -07004157 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004158 if (mSearchDropTargetBar == null) {
4159 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004160 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004161 if (mQsb != null) {
4162 mSearchDropTargetBar.removeView(mQsb);
4163 mQsb = null;
4164 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004165 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004166 }
4167
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004168 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004169 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4170 * multiple calls to bind the same list.)
4171 */
4172 @Thunk ArrayList<AppInfo> mTmpAppsList;
4173 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4174 public void run() {
4175 bindAllApplications(mTmpAppsList);
4176 mTmpAppsList = null;
4177 }
4178 };
4179
4180 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004181 * Add the icons for all apps.
4182 *
4183 * Implementation of the method from LauncherModel.Callbacks.
4184 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004185 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004186 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4187 mTmpAppsList = apps;
4188 return;
4189 }
4190
Winson Chungb745afb2015-03-02 11:51:23 -08004191 if (mAppsView != null) {
4192 mAppsView.setApps(apps);
4193 }
Adam Cohen9211d422014-10-07 18:14:53 -07004194 if (mLauncherCallbacks != null) {
4195 mLauncherCallbacks.bindAllApplications(apps);
4196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004197 }
4198
4199 /**
4200 * A package was updated.
4201 *
4202 * Implementation of the method from LauncherModel.Callbacks.
4203 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004204 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004205 Runnable r = new Runnable() {
4206 public void run() {
4207 bindAppsUpdated(apps);
4208 }
4209 };
4210 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004211 return;
4212 }
4213
Winson Chungb745afb2015-03-02 11:51:23 -08004214 if (mAppsView != null) {
4215 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004217 }
4218
Sunny Goyal4390ace2014-10-13 11:33:11 -07004219 @Override
4220 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4221 Runnable r = new Runnable() {
4222 public void run() {
4223 bindWidgetsRestored(widgets);
4224 }
4225 };
4226 if (waitUntilResume(r)) {
4227 return;
4228 }
4229 mWorkspace.widgetsRestored(widgets);
4230 }
4231
Joe Onorato9c1289c2009-08-17 11:03:03 -04004232 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004233 * Some shortcuts were updated in the background.
4234 *
4235 * Implementation of the method from LauncherModel.Callbacks.
4236 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004237 @Override
4238 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4239 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004240 Runnable r = new Runnable() {
4241 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004242 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004243 }
4244 };
4245 if (waitUntilResume(r)) {
4246 return;
4247 }
4248
Sunny Goyal4390ace2014-10-13 11:33:11 -07004249 if (!updated.isEmpty()) {
4250 mWorkspace.updateShortcuts(updated);
4251 }
4252
4253 if (!removed.isEmpty()) {
4254 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4255 for (ShortcutInfo si : removed) {
4256 removedComponents.add(si.getTargetComponent());
4257 }
4258 mWorkspace.removeItemsByComponentName(removedComponents, user);
4259 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004260 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004261 }
4262 }
4263
4264 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004265 * Update the state of a package, typically related to install state.
4266 *
4267 * Implementation of the method from LauncherModel.Callbacks.
4268 */
Sunny Goyale755d462014-07-22 13:48:29 -07004269 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004270 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4271 Runnable r = new Runnable() {
4272 public void run() {
4273 bindRestoreItemsChange(updates);
4274 }
4275 };
4276 if (waitUntilResume(r)) {
4277 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004278 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004279
Sunny Goyal756adbc2015-04-16 15:20:51 -07004280 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004281 }
4282
4283 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004284 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004285 * in addition to specific applications to remove, the reason being that
4286 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004287 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004288 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004289 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004290 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004291 public void bindWorkspaceComponentsRemoved(
4292 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4293 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004294 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004295 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004296 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004297 }
Winson Chungd64d1762013-08-20 14:37:16 -07004298 };
4299 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004300 return;
4301 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004302 if (!packageNames.isEmpty()) {
4303 mWorkspace.removeItemsByPackageName(packageNames, user);
4304 }
4305 if (!components.isEmpty()) {
4306 mWorkspace.removeItemsByComponentName(components, user);
4307 }
4308 // Notify the drag controller
4309 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004310
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004311 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004312
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004313 @Override
4314 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4315 Runnable r = new Runnable() {
4316 public void run() {
4317 bindAppInfosRemoved(appInfos);
4318 }
4319 };
4320 if (waitUntilResume(r)) {
4321 return;
Joe Onorato36115782010-06-17 13:28:48 -04004322 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004323
Winson Chungdf95eb12013-10-16 14:57:07 -07004324 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004325 if (mAppsView != null) {
4326 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328 }
Joe Onoratobe386092009-11-17 17:32:16 -08004329
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004330 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004331 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004332 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004333 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004334 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004335
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004336 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004337 public void bindWidgetsModel(WidgetsModel model) {
4338 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004339 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004340 return;
4341 }
4342
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004343 if (mWidgetsView != null && model != null) {
4344 mWidgetsView.addWidgets(model);
4345 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004346 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004347 }
4348
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004349 @Override
4350 public void notifyWidgetProvidersChanged() {
4351 if (mWorkspace.getState().shouldUpdateWidget) {
4352 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4353 }
4354 }
4355
Winson Chung400438b2011-01-16 17:53:48 -08004356 private int mapConfigurationOriActivityInfoOri(int configOri) {
4357 final Display d = getWindowManager().getDefaultDisplay();
4358 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4359 switch (d.getRotation()) {
4360 case Surface.ROTATION_0:
4361 case Surface.ROTATION_180:
4362 // We are currently in the same basic orientation as the natural orientation
4363 naturalOri = configOri;
4364 break;
4365 case Surface.ROTATION_90:
4366 case Surface.ROTATION_270:
4367 // We are currently in the other basic orientation to the natural orientation
4368 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4369 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4370 break;
4371 }
4372
4373 int[] oriMap = {
4374 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4375 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4376 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4377 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4378 };
4379 // Since the map starts at portrait, we need to offset if this device's natural orientation
4380 // is landscape.
4381 int indexOffset = 0;
4382 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4383 indexOffset = 1;
4384 }
4385 return oriMap[(d.getRotation() + indexOffset) % 4];
4386 }
Adam Cohen446e9402011-09-15 18:21:21 -07004387
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004388 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004389 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004390 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004391 if (Utilities.ATLEAST_JB_MR2) {
4392 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4393 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004394 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4395 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004396 }
Winson Chung4b919f82012-05-01 10:44:08 -07004397 }
4398 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004399
Winson Chung4b919f82012-05-01 10:44:08 -07004400 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004401 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004402 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004403 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004404 } else {
4405 mHandler.postDelayed(new Runnable() {
4406 public void run() {
4407 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4408 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004409 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004410 }
Winson Chung4b919f82012-05-01 10:44:08 -07004411 }
Winson Chung400438b2011-01-16 17:53:48 -08004412 }
4413
Adam Cohenea90f832014-05-21 15:03:34 -07004414 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004415 * To be overridden by subclasses to indicate that there is an activity to launch
4416 * before showing the standard launcher experience.
4417 */
4418 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004419 if (mLauncherCallbacks != null) {
4420 return mLauncherCallbacks.hasFirstRunActivity();
4421 }
Adam Cohen432609a2014-03-13 17:03:22 -07004422 return false;
4423 }
4424
4425 /**
4426 * To be overridden by subclasses to launch any first run activity
4427 */
4428 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004429 if (mLauncherCallbacks != null) {
4430 return mLauncherCallbacks.getFirstRunActivity();
4431 }
Adam Cohen432609a2014-03-13 17:03:22 -07004432 return null;
4433 }
4434
Winson Chunga6945242014-01-08 14:04:34 -08004435 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004436 return !ActivityManager.isRunningInTestHarness() &&
4437 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439
Adam Cohen4a9423d2014-03-28 14:22:31 -07004440 protected boolean hasRunFirstRunActivity() {
4441 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4442 }
4443
Adam Cohen432609a2014-03-13 17:03:22 -07004444 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 if (shouldRunFirstRunActivity() &&
4446 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004447 Intent firstRunIntent = getFirstRunActivity();
4448 if (firstRunIntent != null) {
4449 startActivity(firstRunIntent);
4450 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004452 }
4453 }
Adam Cohen432609a2014-03-13 17:03:22 -07004454 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004455 }
4456
4457 private void markFirstRunActivityShown() {
4458 SharedPreferences.Editor editor = mSharedPrefs.edit();
4459 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4460 editor.apply();
4461 }
4462
Adam Cohen432609a2014-03-13 17:03:22 -07004463 /**
4464 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4465 * screen that must be displayed and dismissed.
4466 */
4467 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.hasDismissableIntroScreen();
4470 }
Adam Cohen432609a2014-03-13 17:03:22 -07004471 return false;
4472 }
4473
4474 /**
4475 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4476 */
4477 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.getIntroScreen();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return null;
4482 }
4483
4484 /**
4485 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4486 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4487 */
4488 private boolean shouldShowIntroScreen() {
4489 return hasDismissableIntroScreen() &&
4490 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4491 }
4492
4493 protected void showIntroScreen() {
4494 View introScreen = getIntroScreen();
4495 changeWallpaperVisiblity(false);
4496 if (introScreen != null) {
4497 mDragLayer.showOverlayView(introScreen);
4498 }
4499 }
4500
4501 public void dismissIntroScreen() {
4502 markIntroScreenDismissed();
4503 if (showFirstRunActivity()) {
4504 // We delay hiding the intro view until the first run activity is showing. This
4505 // avoids a blip.
4506 mWorkspace.postDelayed(new Runnable() {
4507 @Override
4508 public void run() {
4509 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004510 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512 }, ACTIVITY_START_DELAY);
4513 } else {
4514 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004515 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004516 }
4517 changeWallpaperVisiblity(true);
4518 }
4519
4520 private void markIntroScreenDismissed() {
4521 SharedPreferences.Editor editor = mSharedPrefs.edit();
4522 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4523 editor.apply();
4524 }
4525
Adam Cohen091440a2015-03-18 14:16:05 -07004526 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004527 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4528 // on the device, then we always show the first run cling experience (or if there is no
4529 // launcher2). Otherwise, we prompt the user upon started for migration
4530 LauncherClings launcherClings = new LauncherClings(this);
4531 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004532 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004533 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004534 }
4535 }
4536
Winson Chung5ffd51d2015-06-25 11:36:50 -07004537 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004538 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004539 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004540 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004541 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004542 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004543 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4544 if (userHandle != null) {
4545 user = UserHandleCompat.fromUser(userHandle);
4546 }
4547 }
4548 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004549 }
4550
4551 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004552 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004553 if (user == null) {
4554 user = UserHandleCompat.myUserHandle();
4555 }
4556
4557 // Called from search suggestion, add the profile extra to the intent to ensure that we
4558 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004559 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004560 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004561 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004562 return null;
4563 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004564 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004565 }
4566
Winson Chung5ffd51d2015-06-25 11:36:50 -07004567 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004568 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4569 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004570 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004571 UserHandleCompat.myUserHandle());
4572 }
4573
Winson Chung5ffd51d2015-06-25 11:36:50 -07004574 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004575 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4576 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004577 mWorkspace.onExternalDragStartedWithItem(dragView);
4578 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004579 }
4580
Winson Chung5ffd51d2015-06-25 11:36:50 -07004581 protected void moveWorkspaceToDefaultScreen() {
4582 mWorkspace.moveToDefaultScreen(false);
4583 }
4584
Winson Chung80baf5a2010-08-09 16:03:15 -07004585 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004586 * Returns a FastBitmapDrawable with the icon, accurately sized.
4587 */
4588 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4589 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4590 d.setFilterBitmap(true);
4591 resizeIconDrawable(d);
4592 return d;
4593 }
4594
4595 /**
4596 * Resizes an icon drawable to the correct icon size.
4597 */
Winson5fbe0742015-09-30 15:33:00 -07004598 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004599 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004600 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004601 }
4602
4603 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004604 * Prints out out state for debugging.
4605 */
4606 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004607 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004608 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004609 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4610 Log.d(TAG, "mRestoring=" + mRestoring);
4611 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004612 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004613 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004614
Daniel Sandler325dc232013-06-05 22:57:57 -04004615 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004616 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004617
4618 @Override
4619 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4620 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004621 // Dump workspace
4622 writer.println(prefix + "Workspace Items");
4623 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4624 writer.println(prefix + " Homescreen " + i);
4625
4626 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4627 for (int j = 0; j < layout.getChildCount(); j++) {
4628 Object tag = layout.getChildAt(j).getTag();
4629 if (tag != null) {
4630 writer.println(prefix + " " + tag.toString());
4631 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004632 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004633 }
Sunny Goyala1365452015-10-01 15:46:24 -07004634
4635 writer.println(prefix + " Hotseat");
4636 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4637 for (int j = 0; j < layout.getChildCount(); j++) {
4638 Object tag = layout.getChildAt(j).getTag();
4639 if (tag != null) {
4640 writer.println(prefix + " " + tag.toString());
4641 }
4642 }
4643
Sunny Goyal713edfc2016-05-06 09:58:34 -07004644 try {
4645 FileLog.flushAll(writer);
4646 } catch (Exception e) {
4647 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004648 }
4649
Adam Cohen9211d422014-10-07 18:14:53 -07004650 if (mLauncherCallbacks != null) {
4651 mLauncherCallbacks.dump(prefix, fd, writer, args);
4652 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004653 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004654
Adam Cohen59400422014-03-05 18:07:04 -08004655 public static CustomAppWidget getCustomAppWidget(String name) {
4656 return sCustomAppWidgets.get(name);
4657 }
4658
4659 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4660 return sCustomAppWidgets;
4661 }
4662
Sunny Goyal29538332016-02-18 09:10:19 -08004663 public static List<View> getFolderContents(View icon) {
4664 if (icon instanceof FolderIcon) {
4665 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4666 } else {
4667 return Collections.EMPTY_LIST;
4668 }
4669 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004670
4671 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4672
4673 @Override
4674 public void onSharedPreferenceChanged(
4675 SharedPreferences sharedPreferences, String key) {
4676 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4677 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4678 if (!waitUntilResume(this, true)) {
4679 run();
4680 }
4681 }
4682 }
4683
4684 @Override
4685 public void run() {
4686 setOrientation();
4687 }
4688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004689}