blob: 3ce07e3ea8c5d45285cf9f1751682645e2b6fe0c [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;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070088import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800112import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700113import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700114import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700117import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700118import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700119import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700121import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800134import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700140 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
141 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700150 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700188 // Type: Content Values / parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Sunny Goyal594d76d2014-11-06 10:12:54 -0800198 private static final String QSB_WIDGET_ID = "qsb_widget_id";
199 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
200
Winson Chunga6945242014-01-08 14:04:34 -0800201 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
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;
Winson Chunga6945242014-01-08 14:04:34 -0800227 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700230
231 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Sunny Goyalffe83f12014-08-14 17:39:34 -0700233 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700236 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800237 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700249 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
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 Wickham827cef22016-03-17 15:39:39 -0700504 }
505 }
506
Adam Cohen9211d422014-10-07 18:14:53 -0700507 private LauncherCallbacks mLauncherCallbacks;
508
509 public void onPostCreate(Bundle savedInstanceState) {
510 super.onPostCreate(savedInstanceState);
511 if (mLauncherCallbacks != null) {
512 mLauncherCallbacks.onPostCreate(savedInstanceState);
513 }
514 }
515
Sunny Goyal32554d12015-12-03 15:31:25 -0800516 /**
517 * Call this after onCreate to set or clear overlay.
518 */
519 public void setLauncherOverlay(LauncherOverlay overlay) {
520 if (overlay != null) {
521 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
522 }
523 mWorkspace.setLauncherOverlay(overlay);
524 }
525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
527 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700528 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700529 private boolean mWorkspaceImportanceStored = false;
530 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800532 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700533 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
534
535 @Override
536 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700537 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 return;
539 }
540 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700541 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700542 if (mWorkspace != null) {
543 mWorkspaceImportanceForAccessibility =
544 mWorkspace.getImportantForAccessibility();
545 mWorkspace.setImportantForAccessibility(
546 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
547 mWorkspaceImportanceStored = true;
548 }
549 if (mHotseat != null) {
550 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
551 mHotseat.setImportantForAccessibility(
552 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
553 mHotseatImportanceStored = true;
554 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 }
556
557 @Override
558 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700559 if (mWorkspaceImportanceStored && mWorkspace != null) {
560 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
561 }
562 if (mHotseatImportanceStored && mHotseat != null) {
563 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
564 }
565 mWorkspaceImportanceStored = false;
566 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700567 }
568 });
Adam Cohen9211d422014-10-07 18:14:53 -0700569 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700570 }
571
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700572 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700573 public void onLauncherProviderChange() {
574 if (mLauncherCallbacks != null) {
575 mLauncherCallbacks.onLauncherProviderChange();
576 }
577 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578
Adam Cohen9211d422014-10-07 18:14:53 -0700579 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700580 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700581 if (mLauncherCallbacks != null) {
582 return mLauncherCallbacks.hasCustomContentToLeft();
583 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700584 return false;
585 }
586
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500588 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 * {@link #addToCustomContentPage}. This will only be invoked if
590 * {@link #hasCustomContentToLeft()} is {@code true}.
591 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500592 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700593 if (mLauncherCallbacks != null) {
594 mLauncherCallbacks.populateCustomContentContainer();
595 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 }
597
598 /**
599 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
600 * ensure the custom content page is added or removed if necessary.
601 */
602 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600603 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 // Not bound yet, wait for bindScreens to be called.
605 return;
606 }
607
608 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
609 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500610 mWorkspace.createCustomContentContainer();
611 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
613 mWorkspace.removeCustomContentPage();
614 }
615 }
616
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800617 /**
618 * Logger object is a singleton and does not have to be coupled with the foreground activity.
619 * Since most user event logging is done on the UI, the object is retrieved from the
620 * callback for convenience.
621 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700622 private UserEventDispatcher createUserEventDispatcher() {
623 return new UserEventDispatcher() {
624 @Override
625 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
626 if (!DEBUG_LOGGING) {
627 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800628 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700629 Log.d("UserEvent", String.format(Locale.US,
630 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
631 LoggerUtils.getActionStr(ev.action),
632 LoggerUtils.getTargetStr(ev.srcTarget[0]),
633 LoggerUtils.getTargetStr(ev.srcTarget[1]),
634 ev.elapsedContainerMillis,
635 ev.elapsedSessionMillis));
636 }
637 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800638 }
639
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 public UserEventDispatcher getUserEventDispatcher() {
641 if (mLauncherCallbacks != null) {
642 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
643 if (dispatcher != null) {
644 return dispatcher;
645 }
646 }
647
648 if (mUserEventDispatcher == null) {
649 mUserEventDispatcher = createUserEventDispatcher();
650 }
651 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800652 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100653
Hyunyoung Song3f471442015-04-08 19:01:34 -0700654 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
656 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700657 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 }
659
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000660 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700661 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
662 // This cast is safe as long as the id < 0x00FFFFFF
663 // Since we jail all the dynamically generated views, there should be no clashes
664 // with any other views.
665 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000666 }
667
668 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
670 * a configuration step, this allows the proper animations to run after other transitions.
671 */
Adam Cohendb364c32014-05-20 14:23:40 -0700672 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700673 long screenId = args.screenId;
674 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
675 // When the screen id represents an actual screen (as opposed to a rank) we make sure
676 // that the drop page actually exists.
677 screenId = ensurePendingDropLayoutExists(args.screenId);
678 }
Adam Cohendb364c32014-05-20 14:23:40 -0700679
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700682 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700686 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700688 case REQUEST_RECONFIGURE_APPWIDGET:
689 completeRestoreAppWidget(args.appWidgetId);
690 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
Michael Jurka27614d22012-04-02 06:40:22 -0700692 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
693 // if you turned the screen off and then back while in All Apps, Launcher would not
694 // return to the workspace. Clearing mAddInfo.container here fixes this issue
695 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700696 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
698
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800699 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700701 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700702 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700703 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800704 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800723 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700725
726 // When the user has granted permission to bind widgets, we should check to see if
727 // we can inflate the default search bar widget.
728 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
730 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
732 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700733 // User could have free-scrolled between pages before picking a wallpaper; make sure
734 // we move to the closest one now.
735 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700736 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737 }
738 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700742 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700743
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 // We have special handling for widgets
746 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 final int appWidgetId;
748 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
749 : -1;
750 if (widgetId < 0) {
751 appWidgetId = pendingAddWidgetId;
752 } else {
753 appWidgetId = widgetId;
754 }
755
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800757 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700758 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
759 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 result = RESULT_CANCELED;
761 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 @Override
764 public void run() {
765 exitSpringLoadedDragModeDelayed(false, 0, null);
766 }
767 };
Adam Cohendb364c32014-05-20 14:23:40 -0700768 if (workspaceLocked) {
769 // No need to remove the empty screen if we're mid-binding, as the
770 // the bind will not add the empty screen.
771 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
772 } else {
773 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
775 }
Winson Chung5aaab772012-04-27 15:24:02 -0700776 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700778 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
779 // When the screen id represents an actual screen (as opposed to a rank)
780 // we make sure that the drop page actually exists.
781 mPendingAddInfo.screenId =
782 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
783 }
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
785
786 dropLayout.setDropPending(true);
787 final Runnable onComplete = new Runnable() {
788 @Override
789 public void run() {
790 completeTwoStageWidgetDrop(resultCode, appWidgetId);
791 dropLayout.setDropPending(false);
792 }
793 };
794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 } else {
797 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
798 mPendingAddInfo);
799 sPendingAddItem = args;
800 }
Winson Chung5aaab772012-04-27 15:24:02 -0700801 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 return;
803 }
804
Sunny Goyalff572272014-07-23 13:58:07 -0700805 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
806 if (resultCode == RESULT_OK) {
807 // Update the widget view.
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
809 pendingAddWidgetId, mPendingAddInfo);
810 if (workspaceLocked) {
811 sPendingAddItem = args;
812 } else {
813 completeAdd(args);
814 }
815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
822 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
823 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
824 mPendingAddInfo);
825 if (isWorkspaceLocked()) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 }
832 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800838
839 }
840
841 @Override
842 protected void onActivityResult(
843 final int requestCode, final int resultCode, final Intent data) {
844 handleActivityResult(requestCode, resultCode, data);
845 if (mLauncherCallbacks != null) {
846 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 }
849
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700851 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
854 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
855 View v = null;
856 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
857 if (layout != null) {
858 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
859 }
860 Intent intent = sPendingAddItem.intent;
861 sPendingAddItem = null;
862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
864 startActivity(v, intent, null);
865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
868 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
878 appWidgetId, ItemInfo info) {
879 PendingAddArguments args = new PendingAddArguments();
880 args.requestCode = requestCode;
881 args.intent = data;
882 args.container = info.container;
883 args.screenId = info.screenId;
884 args.cellX = info.cellX;
885 args.cellY = info.cellY;
886 args.appWidgetId = appWidgetId;
887 return args;
888 }
889
890 /**
891 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
892 *
893 * @param screenId the screen id to check
894 * @return the new screen, or screenId if it exists
895 */
896 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800909 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200964 long startTime = 0;
965 if (DEBUG_RESUME_TIME) {
966 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400967 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200968 }
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.preOnResume();
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700975 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700976
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700978 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800980 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700981 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700982 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700983 // view after launching an app, as they may be depending on the UI to be static to
984 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700985 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700986 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800987 } else if (mOnResumeState == State.WIDGETS) {
988 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 }
990 mOnResumeState = State.NONE;
991
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700992 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700993 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
994 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700995
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800996 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700998 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700999 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1005 // execute them here
1006 long startTimeCallbacks = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTimeCallbacks = System.currentTimeMillis();
1009 }
1010
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1012 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 if (DEBUG_RESUME_TIME) {
1016 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1017 (System.currentTimeMillis() - startTimeCallbacks));
1018 }
Michael Jurka447bf842013-05-15 14:52:15 +02001019 }
Michael Jurka54554252013-08-01 12:52:23 +02001020 if (mOnResumeCallbacks.size() > 0) {
1021 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1022 mOnResumeCallbacks.get(i).run();
1023 }
1024 mOnResumeCallbacks.clear();
1025 }
Winson Chunge4e50662012-01-23 14:45:13 -08001026
1027 // Reset the pressed state of icons that were locked in the press state while activities
1028 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001030 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 mWaitingForResume.setStayPressed(false);
1032 }
Winson Chung82963d52013-10-09 11:20:57 -07001033
Adam Cohen06dff352012-06-01 17:17:08 -07001034 // It is possible that widgets can receive updates while launcher is not in the foreground.
1035 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1036 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1037 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001038 if (!isWorkspaceLoading()) {
1039 getWorkspace().reinflateWidgetsIfNecessary();
1040 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001041 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001042
Michael Jurka447bf842013-05-15 14:52:15 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1045 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001046
Adam Cohen4b66bf32015-09-18 12:15:19 -07001047 // We want to suppress callbacks about CustomContent being shown if we have just received
1048 // onNewIntent while the user was present within launcher. In that case, we post a call
1049 // to move the user to the main screen (which will occur after onResume). We don't want to
1050 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1051 // suppress here.
1052 if (mWorkspace.getCustomContentCallbacks() != null
1053 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001055 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1057 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001058 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 }
1060 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001061 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001062 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001063 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001064
Sunny Goyal756adbc2015-04-16 15:20:51 -07001065 if (!isWorkspaceLoading()) {
1066 // Process any items that were added while Launcher was away.
1067 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1068 }
Adam Cohen9211d422014-10-07 18:14:53 -07001069
1070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onResume();
1072 }
Adam Cohen06dff352012-06-01 17:17:08 -07001073 }
1074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001077 // Ensure that items added to Launcher are queued until Launcher returns
1078 InstallShortcutReceiver.enableInstallQueue();
1079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001081 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001082 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001083 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001084
1085 // We call onHide() aggressively. The custom content callbacks should be able to
1086 // debounce excess onHide calls.
1087 if (mWorkspace.getCustomContentCallbacks() != null) {
1088 mWorkspace.getCustomContentCallbacks().onHide();
1089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Adam Cohen9211d422014-10-07 18:14:53 -07001091 if (mLauncherCallbacks != null) {
1092 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
1095
1096 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1098 // by a onResume or by scrolling otherwise.
1099 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001100
1101 // Custom content is completely hidden
1102 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001103
1104 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1105 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001106
1107 // Indicates whether the user is allowed to scroll away from the custom content.
1108 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
Adam Cohenc2d6e892014-10-16 09:49:24 -07001111 public interface LauncherOverlay {
1112
1113 /**
1114 * Touch interaction leading to overscroll has begun
1115 */
1116 public void onScrollInteractionBegin();
1117
1118 /**
1119 * Touch interaction related to overscroll has ended
1120 */
1121 public void onScrollInteractionEnd();
1122
1123 /**
1124 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1125 * screen (or in the case of RTL, the rightmost screen).
1126 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001127 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128
1129 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001130 * Called when the launcher is ready to use the overlay
1131 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001133 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 }
1135
Jun Mukai8af0cd82015-05-12 12:32:12 -07001136 public interface LauncherSearchCallbacks {
1137 /**
1138 * Called when the search overlay is shown.
1139 */
1140 public void onSearchOverlayOpened();
1141
1142 /**
1143 * Called when the search overlay is dismissed.
1144 */
1145 public void onSearchOverlayClosed();
1146 }
1147
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 }
1151
1152 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1153
Sunny Goyalc86df472016-02-25 09:19:38 -08001154 public void onScrollChanged(float progress) {
1155 if (mWorkspace != null) {
1156 mWorkspace.onOverlayScrollChanged(progress);
1157 }
1158 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 }
1160
Jorim Jaggid017f882014-01-14 17:08:48 -08001161 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001162 if (mLauncherCallbacks != null) {
1163 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001164 } else {
1165 // On devices with a locked orientation, we will at least have the allow rotation
1166 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001167 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001168 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001169 }
1170
Adam Cohen9211d422014-10-07 18:14:53 -07001171 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001172 CustomContentCallbacks callbacks, String description) {
1173 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001174 }
1175
Adam Cohenedb40762013-07-18 16:45:45 -07001176 // The custom content needs to offset its content to account for the QSB
1177 public int getTopOffsetForCustomContent() {
1178 return mWorkspace.getPaddingTop();
1179 }
1180
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001181 @Override
1182 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001184 if (mModel.isCurrentCallbacks(this)) {
1185 mModel.stopLoader();
1186 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001187 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1188
Romain Guy13c2e7b2010-03-10 19:45:00 -08001189 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001190 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193 @Override
1194 public void onWindowFocusChanged(boolean hasFocus) {
1195 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001196 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001197
1198 if (mLauncherCallbacks != null) {
1199 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1200 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 private boolean acceptFilter() {
1204 final InputMethodManager inputManager = (InputMethodManager)
1205 getSystemService(Context.INPUT_METHOD_SERVICE);
1206 return !inputManager.isFullscreenMode();
1207 }
1208
1209 @Override
1210 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001211 final int uniChar = event.getUnicodeChar();
1212 final boolean handled = super.onKeyDown(keyCode, event);
1213 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1214 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1216 keyCode, event);
1217 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001219 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // showSearchDialog()
1221 // If there are multiple keystrokes before the search dialog takes focus,
1222 // onSearchRequested() will be called for every keystroke,
1223 // but it is idempotent, so it's fine.
1224 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226 }
1227
Joe Onorato8a9625e2010-01-28 15:55:35 -08001228 // Eat the long press event so the keyboard doesn't come up.
1229 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1230 return true;
1231 }
1232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 return handled;
1234 }
1235
Winson46163472015-09-22 17:31:56 -07001236 @Override
1237 public boolean onKeyUp(int keyCode, KeyEvent event) {
1238 if (keyCode == KeyEvent.KEYCODE_MENU) {
1239 // Ignore the menu key if we are currently dragging or are on the custom content screen
1240 if (!isOnCustomContent() && !mDragController.isDragging()) {
1241 // Close any open folders
1242 closeFolder();
1243
1244 // Stop resizing any widgets
1245 mWorkspace.exitWidgetResizeMode();
1246
1247 // Show the overview mode if we are on the workspace
1248 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1249 !mWorkspace.isSwitchingState()) {
1250 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001251 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001252 }
1253 }
1254 return true;
1255 }
1256 return super.onKeyUp(keyCode, event);
1257 }
1258
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 private String getTypedText() {
1260 return mDefaultKeySsb.toString();
1261 }
1262
1263 private void clearTypedText() {
1264 mDefaultKeySsb.clear();
1265 mDefaultKeySsb.clearSpans();
1266 Selection.setSelection(mDefaultKeySsb, 0);
1267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001270 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1271 * State
1272 */
1273 private static State intToState(int stateOrdinal) {
1274 State state = State.WORKSPACE;
1275 final State[] stateValues = State.values();
1276 for (int i = 0; i < stateValues.length; i++) {
1277 if (stateValues[i].ordinal() == stateOrdinal) {
1278 state = stateValues[i];
1279 break;
1280 }
1281 }
1282 return state;
1283 }
1284
1285 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 * Restores the previous state, if it exists.
1287 *
1288 * @param savedState The previous state.
1289 */
1290 private void restoreState(Bundle savedState) {
1291 if (savedState == null) {
1292 return;
1293 }
1294
Michael Jurka883f55b2010-10-21 15:47:14 -07001295 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001296 if (state == State.APPS || state == State.WIDGETS) {
1297 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001298 }
1299
Adam Cohen21cd0022013-10-09 18:57:02 -07001300 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1301 PagedView.INVALID_RESTORE_PAGE);
1302 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001303 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
Sunny Goyal756cd262015-08-20 12:33:21 -07001306 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1307 if (itemValues != null) {
1308 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001309 AppWidgetProviderInfo info = savedState.getParcelable(
1310 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001311 mPendingAddWidgetInfo = info == null ?
1312 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1313
Adam Cohen4637b5a2013-11-04 18:21:24 -08001314 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001315 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 mRestoring = true;
1317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 /**
1321 * Finds all the views we need and configure them properly.
1322 */
1323 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001324 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001325
Craig Mautner360310b2012-10-26 15:13:08 -07001326 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001327 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001328 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1329 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001330 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001331
Sunny Goyal784f9c32016-03-23 16:56:54 -07001332 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1333 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1334 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001335 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Winson Chung4c98d922011-05-31 16:50:48 -07001337 // Setup the drag layer
1338 mDragLayer.setup(this, dragController);
1339
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 // Setup the hotseat
1341 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1342 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001343 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001344 }
1345
Winsone9f27272015-10-13 10:47:51 -07001346 // Setup the overview panel
1347 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001348
Winson Chung4c98d922011-05-31 16:50:48 -07001349 // Setup the workspace
1350 mWorkspace.setHapticFeedbackEnabled(false);
1351 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001352 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001353 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001354
Tony Wickham34d2c912015-09-11 09:27:58 -07001355 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001356 mSearchDropTargetBar = (SearchDropTargetBar)
1357 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001358 // Get the app info bar
1359 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1360 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001361
Winson Chungef7f8742015-06-04 17:18:17 -07001362 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001363 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001364 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001365 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1366 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1367 } else {
1368 mAppsView.setSearchBarController(new DefaultAppSearchController());
1369 }
Craig Mautner360310b2012-10-26 15:13:08 -07001370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001372 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001373 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001374 dragController.setMoveTarget(mWorkspace);
1375 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001376 if (mSearchDropTargetBar != null) {
1377 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001378 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001379 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001380 if (mAppInfoDropTargetBar != null) {
1381 mAppInfoDropTargetBar.setup(this, dragController);
1382 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001383
Sunny Goyal322d5562015-06-25 19:35:49 -07001384 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1385 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
Winsone9f27272015-10-13 10:47:51 -07001389 private void setupOverviewPanel() {
1390 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1391
1392 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1393 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1394 @Override
1395 public boolean onLongClick(View v) {
1396 return v.performClick();
1397 }
1398 };
1399
1400 // Bind wallpaper button actions
1401 View wallpaperButton = findViewById(R.id.wallpaper_button);
1402 wallpaperButton.setOnClickListener(new OnClickListener() {
1403 @Override
1404 public void onClick(View view) {
1405 if (!mWorkspace.isSwitchingState()) {
1406 onClickWallpaperPicker(view);
1407 }
1408 }
1409 });
1410 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1411 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412
1413 // Bind widget button actions
1414 mWidgetsButton = findViewById(R.id.widget_button);
1415 mWidgetsButton.setOnClickListener(new OnClickListener() {
1416 @Override
1417 public void onClick(View view) {
1418 if (!mWorkspace.isSwitchingState()) {
1419 onClickAddWidgetButton(view);
1420 }
1421 }
1422 });
1423 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1424 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425
1426 // Bind settings actions
1427 View settingsButton = findViewById(R.id.settings_button);
1428 boolean hasSettings = hasSettings();
1429 if (hasSettings) {
1430 settingsButton.setOnClickListener(new OnClickListener() {
1431 @Override
1432 public void onClick(View view) {
1433 if (!mWorkspace.isSwitchingState()) {
1434 onClickSettingsButton(view);
1435 }
1436 }
1437 });
1438 settingsButton.setOnLongClickListener(performClickOnLongClick);
1439 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440 } else {
1441 settingsButton.setVisibility(View.GONE);
1442 }
1443
1444 mOverviewPanel.setAlpha(0f);
1445 }
1446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001448 * Sets the all apps button. This method is called from {@link Hotseat}.
1449 */
1450 public void setAllAppsButton(View allAppsButton) {
1451 mAllAppsButton = allAppsButton;
1452 }
1453
1454 public View getAllAppsButton() {
1455 return mAllAppsButton;
1456 }
1457
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001458 public View getWidgetsButton() {
1459 return mWidgetsButton;
1460 }
1461
Anjali Koppal5ad44842014-03-10 20:34:39 -07001462 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * Creates a view representing a shortcut.
1464 *
1465 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001467 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001468 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
1471 /**
1472 * Creates a view representing a shortcut inflated from the specified resource.
1473 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * @param parent The group the shortcut belongs to.
1475 * @param info The data structure describing the shortcut.
1476 *
1477 * @return A View inflated from layoutResId.
1478 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001480 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 parent, false);
1482 favorite.applyFromShortcutInfo(info, mIconCache);
1483 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001485 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return favorite;
1487 }
1488
1489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Add a shortcut to the workspace.
1491 *
1492 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 int cellY) {
1496 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001498
Sunny Goyal5c97f512015-05-19 16:03:28 -07001499 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001500 if (info == null) {
1501 return;
1502 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001503 final View view = createShortcut(info);
1504
Sunny Goyal5c97f512015-05-19 16:03:28 -07001505 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 // First we check if we already know the exact location where we want to add this item.
1507 if (cellX >= 0 && cellY >= 0) {
1508 cellXY[0] = cellX;
1509 cellXY[1] = cellY;
1510 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001511
1512 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001513 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 true, null,null)) {
1515 return;
1516 }
1517 DragObject dragObject = new DragObject();
1518 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001519 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1520 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 return;
1522 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001523 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001525 }
1526
1527 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001528 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 return;
1530 }
1531
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001532 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001541 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001543 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 */
Adam Cohen091440a2015-03-18 14:16:05 -07001545 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001546 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1547
1548 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001550 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 }
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Adam Cohen59400422014-03-05 18:07:04 -08001553 if (appWidgetInfo.isCustomWidget) {
1554 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001555 }
1556
Adam Cohen59400422014-03-05 18:07:04 -08001557 LauncherAppWidgetInfo launcherInfo;
1558 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1559 launcherInfo.spanX = info.spanX;
1560 launcherInfo.spanY = info.spanY;
1561 launcherInfo.minSpanX = info.minSpanX;
1562 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001563 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001566 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
1568 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 if (hostView == null) {
1570 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001573 hostView.setVisibility(View.VISIBLE);
1574 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001576 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001579 private void addAppWidgetToWorkspace(
1580 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 hostView.setTag(item);
1583 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setFocusable(true);
1586 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001589 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1590
1591 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001592 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593 }
1594 }
1595
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1597 @Override
1598 public void onReceive(Context context, Intent intent) {
1599 final String action = intent.getAction();
1600 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1601 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001602 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001603 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001604
Winson Chungc100e8e2011-08-09 16:02:43 -07001605 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001606 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001607 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001608 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001609 if (!showWorkspace(false)) {
1610 // If we are already on the workspace, then manually reset all apps
1611 mAppsView.reset();
1612 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1615 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001616 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001617 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1618 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001619 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001620 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1621 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1622 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001623 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001624 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1625 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627 }
1628 };
1629
1630 @Override
1631 public void onAttachedToWindow() {
1632 super.onAttachedToWindow();
1633
1634 // Listen for broadcasts related to user-presence
1635 final IntentFilter filter = new IntentFilter();
1636 filter.addAction(Intent.ACTION_SCREEN_OFF);
1637 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001638 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001639 if (ENABLE_DEBUG_INTENTS) {
1640 filter.addAction(DebugIntents.DELETE_DATABASE);
1641 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1642 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001644 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001645 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001647
1648 if (mLauncherCallbacks != null) {
1649 mLauncherCallbacks.onAttachedToWindow();
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652
1653 @Override
1654 public void onDetachedFromWindow() {
1655 super.onDetachedFromWindow();
1656 mVisible = false;
1657
Adam Cohend113e0c2010-11-11 10:48:05 -08001658 if (mAttached) {
1659 unregisterReceiver(mReceiver);
1660 mAttached = false;
1661 }
Winson Chungb745afb2015-03-02 11:51:23 -08001662 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001663
1664 if (mLauncherCallbacks != null) {
1665 mLauncherCallbacks.onDetachedFromWindow();
1666 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 }
1668
1669 public void onWindowVisibilityChanged(int visibility) {
1670 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001671 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 // The following code used to be in onResume, but it turns out onResume is called when
1673 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1674 // is a more appropriate event to handle
1675 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 if (!mWorkspaceLoading) {
1677 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 // We want to let Launcher draw itself at least once before we force it to build
1679 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 // apps is nice and speedy.
1681 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001682 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001683 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 if (mStarted) return;
1685 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001686 // We delay the layer building a bit in order to give
1687 // other message processing a time to run. In particular
1688 // this avoids a delay in hiding the IME if it was
1689 // currently shown, because doing that may involve
1690 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001691 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001692 final ViewTreeObserver.OnDrawListener listener = this;
1693 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001694 public void run() {
1695 if (mWorkspace != null &&
1696 mWorkspace.getViewTreeObserver() != null) {
1697 mWorkspace.getViewTreeObserver().
1698 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001699 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001700 }
1701 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001703 }
1704 });
1705 }
1706 clearTypedText();
1707 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 }
1709
Adam Cohen091440a2015-03-18 14:16:05 -07001710 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 mHandler.removeMessages(ADVANCE_MSG);
1712 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1713 mHandler.sendMessageDelayed(msg, delay);
1714 mAutoAdvanceSentTime = System.currentTimeMillis();
1715 }
1716
Adam Cohen091440a2015-03-18 14:16:05 -07001717 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1719 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1720 mAutoAdvanceRunning = autoAdvanceRunning;
1721 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001722 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 sendAdvanceMessage(delay);
1724 } else {
1725 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001726 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1728 }
1729 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001730 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
1732 }
1733 }
1734
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1736
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001738 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 if (msg.what == ADVANCE_MSG) {
1740 int i = 0;
1741 for (View key: mWidgetsToAdvance.keySet()) {
1742 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001743 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001745 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 public void run() {
1747 ((Advanceable) v).advance();
1748 }
1749 }, delay);
1750 }
1751 i++;
1752 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001753 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001755 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001758
Winsonc0b52fe2015-09-09 16:38:15 -07001759 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001760 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1762 if (v instanceof Advanceable) {
1763 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001764 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001765 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 }
1767 }
1768
Winsonc0b52fe2015-09-09 16:38:15 -07001769 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 if (mWidgetsToAdvance.containsKey(hostView)) {
1771 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001772 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 }
1775
Hyunyoung Song3f471442015-04-08 19:01:34 -07001776 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001777 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1778 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public DragLayer getDragLayer() {
1782 return mDragLayer;
1783 }
1784
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001785 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001786 return mAppsView;
1787 }
1788
Hyunyoung Song3f471442015-04-08 19:01:34 -07001789 public WidgetsContainerView getWidgetsView() {
1790 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001791 }
1792
Winson Chunga6945242014-01-08 14:04:34 -08001793 public Workspace getWorkspace() {
1794 return mWorkspace;
1795 }
1796
1797 public Hotseat getHotseat() {
1798 return mHotseat;
1799 }
1800
Jorim Jaggid017f882014-01-14 17:08:48 -08001801 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001802 return mOverviewPanel;
1803 }
1804
Winson Chung006ee262015-08-03 14:40:11 -07001805 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001806 return mSearchDropTargetBar;
1807 }
1808
Tony Wickham34d2c912015-09-11 09:27:58 -07001809 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1810 return mAppInfoDropTargetBar;
1811 }
1812
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001813 public LauncherAppWidgetHost getAppWidgetHost() {
1814 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Winson Chunga9abd0e2010-10-27 17:18:37 -07001817 public LauncherModel getModel() {
1818 return mModel;
1819 }
1820
Adam Cohen79d90c52016-04-22 13:29:20 -07001821 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001822 return mSharedPrefs;
1823 }
1824
Adam Cohen2e6da152015-05-06 11:42:25 -07001825 public DeviceProfile getDeviceProfile() {
1826 return mDeviceProfile;
1827 }
1828
Bjorn Bringertc459e522013-06-07 19:36:01 +01001829 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001830 getWindow().closeAllPanels();
1831
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001832 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001833 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 @Override
1837 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001838 long startTime = 0;
1839 if (DEBUG_RESUME_TIME) {
1840 startTime = System.currentTimeMillis();
1841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 super.onNewIntent(intent);
1843
1844 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001845 Folder openFolder = mWorkspace.getOpenFolder();
1846 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1847 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1848 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1849 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1850 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001851 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001853
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 if (mWorkspace == null) {
1855 // Can be cases where mWorkspace is null, this prevents a NPE
1856 return;
1857 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 // In all these cases, only animate if we're already on home
1859 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001860
Sunny Goyal935fca12015-10-13 10:19:01 -07001861 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 exitSpringLoadedDragMode();
1863
1864 // If we are already on home, then just animate back to the workspace,
1865 // otherwise, just wait until onResume to set the state back to Workspace
1866 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001867 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 } else {
1869 mOnResumeState = State.WORKSPACE;
1870 }
1871
1872 final View v = getWindow().peekDecorView();
1873 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001874 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 INPUT_METHOD_SERVICE);
1876 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1877 }
1878
Winson Chungb745afb2015-03-02 11:51:23 -08001879 // Reset the apps view
1880 if (!alreadyOnHome && mAppsView != null) {
1881 mAppsView.scrollToTop();
1882 }
1883
Hyunyoung Song3f471442015-04-08 19:01:34 -07001884 // Reset the widgets view
1885 if (!alreadyOnHome && mWidgetsView != null) {
1886 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001887 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001888
Adam Cohen9211d422014-10-07 18:14:53 -07001889 if (mLauncherCallbacks != null) {
1890 mLauncherCallbacks.onHomeIntent();
1891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
Adam Cohened307df2013-10-02 09:37:31 -07001893
Adam Cohen9211d422014-10-07 18:14:53 -07001894 if (mLauncherCallbacks != null) {
1895 mLauncherCallbacks.onNewIntent(intent);
1896 }
Winson15f8b172015-08-19 11:02:39 -07001897
1898 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1899 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1900 // animation.
1901 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001902 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1903 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001904 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1905 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001906
1907 // We use this flag to suppress noisy callbacks above custom content state
1908 // from onResume.
1909 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001910 mWorkspace.post(new Runnable() {
1911 @Override
1912 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001913 if (mWorkspace != null) {
1914 mWorkspace.moveToDefaultScreen(true);
1915 }
Winson15f8b172015-08-19 11:02:39 -07001916 }
1917 });
1918 }
1919 }
1920
1921 if (DEBUG_RESUME_TIME) {
1922 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1923 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001924 }
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001927 public void onRestoreInstanceState(Bundle state) {
1928 super.onRestoreInstanceState(state);
1929 for (int page: mSynchronouslyBoundPages) {
1930 mWorkspace.restoreInstanceStateForChild(page);
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
1934 @Override
1935 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001936 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001937 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1938 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001939 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001940 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Michael Jurka883f55b2010-10-21 15:47:14 -07001942 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001943 // We close any open folder since it will not be re-opened, and we need to make sure
1944 // this state is reflected.
1945 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1946 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001949 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001950 ContentValues itemValues = new ContentValues();
1951 mPendingAddInfo.writeToValues(itemValues);
1952 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001953 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001954 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956
Adam Cohen9211d422014-10-07 18:14:53 -07001957 if (mLauncherCallbacks != null) {
1958 mLauncherCallbacks.onSaveInstanceState(outState);
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
1962 @Override
1963 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Winson Chunge7a03942011-08-05 15:05:12 -07001966 // Remove all pending runnables
1967 mHandler.removeMessages(ADVANCE_MSG);
1968 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001969 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001970
Winson Chungcd2b0142011-06-08 16:02:26 -07001971 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001972 // It's possible to receive onDestroy after a new Launcher activity has
1973 // been created. In this case, don't interfere with the new Launcher.
1974 if (mModel.isCurrentCallbacks(this)) {
1975 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001976 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001977 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001978
Sunny Goyal745bad92016-05-02 10:54:12 -07001979 if (mRotationPrefChangeHandler != null) {
1980 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1981 }
1982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001984 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001986 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001988 mAppWidgetHost = null;
1989
1990 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991
1992 TextKeyListener.getInstance().release();
1993
Tony Wickhame2217252016-03-22 16:34:23 -07001994 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1995 .removeAccessibilityStateChangeListener(this);
1996
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001997 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001998
Michael Jurka2ecf9952012-06-18 12:52:28 -07001999 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002000
2001 if (mLauncherCallbacks != null) {
2002 mLauncherCallbacks.onDestroy();
2003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
Adam Cohena9cf38f2011-05-02 15:36:58 -07002006 public DragController getDragController() {
2007 return mDragController;
2008 }
2009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 @Override
2011 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002012 onStartForResult(requestCode);
2013 super.startActivityForResult(intent, requestCode);
2014 }
2015
2016 @Override
2017 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2018 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2019 onStartForResult(requestCode);
2020 try {
2021 super.startIntentSenderForResult(intent, requestCode,
2022 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2023 } catch (IntentSender.SendIntentException e) {
2024 throw new ActivityNotFoundException();
2025 }
2026 }
2027
2028 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002029 if (requestCode >= 0) {
2030 setWaitingForResult(true);
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033
Winson Chung70d72102011-08-12 11:24:35 -07002034 /**
2035 * Indicates that we want global search for this activity by setting the globalSearch
2036 * argument for {@link #startSearch} to true.
2037 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002039 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002041
Karl Rosaen138a0412009-04-23 19:00:21 -07002042 if (initialQuery == null) {
2043 // Use any text typed in the launcher as the initial query
2044 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 if (appSearchData == null) {
2047 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002048 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
Winson Chungadf0c182012-08-23 14:59:07 -07002050 Rect sourceBounds = new Rect();
2051 if (mSearchDropTargetBar != null) {
2052 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2053 }
Romain Guycbb89e42009-06-08 15:52:54 -07002054
Ian Parkinson047036e2014-09-01 15:40:53 +01002055 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002057 if (clearTextImmediately) {
2058 clearTypedText();
2059 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002060
2061 // We need to show the workspace after starting the search
2062 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 }
2064
Ian Parkinson047036e2014-09-01 15:40:53 +01002065 /**
2066 * Start a text search.
2067 *
2068 * @return {@code true} if the search will start immediately, so any further keypresses
2069 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2070 * to buffer keypresses.
2071 */
2072 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002074 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2075 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2076 sourceBounds);
2077 }
2078
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002081 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 }
2083
2084 /**
2085 * Starts the global search activity. This code is a copied from SearchManager
2086 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002087 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002089 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2091 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2092 if (globalSearchActivity == null) {
2093 Log.w(TAG, "No global search activity found.");
2094 return;
2095 }
2096 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2097 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2098 intent.setComponent(globalSearchActivity);
2099 // Make sure that we have a Bundle to put source in
2100 if (appSearchData == null) {
2101 appSearchData = new Bundle();
2102 } else {
2103 appSearchData = new Bundle(appSearchData);
2104 }
Adam Cohen9211d422014-10-07 18:14:53 -07002105 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002106 if (!appSearchData.containsKey("source")) {
2107 appSearchData.putString("source", getPackageName());
2108 }
2109 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2110 if (!TextUtils.isEmpty(initialQuery)) {
2111 intent.putExtra(SearchManager.QUERY, initialQuery);
2112 }
2113 if (selectInitialQuery) {
2114 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2115 }
2116 intent.setSourceBounds(sourceBounds);
2117 try {
2118 startActivity(intent);
2119 } catch (ActivityNotFoundException ex) {
2120 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123
Yura4f93ec62014-02-04 14:15:21 +00002124 public boolean isOnCustomContent() {
2125 return mWorkspace.isOnOrMovingToCustomContent();
2126 }
2127
Winson Chung70d72102011-08-12 11:24:35 -07002128 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002129 public boolean onPrepareOptionsMenu(Menu menu) {
2130 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002131 if (mLauncherCallbacks != null) {
2132 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2133 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002134 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002137 @Override
2138 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002139 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002140 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002141 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 }
2143
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 public boolean isWorkspaceLocked() {
2145 return mWorkspaceLoading || mWaitingForResult;
2146 }
2147
Adam Cohen517a7f52014-03-01 12:12:59 -08002148 public boolean isWorkspaceLoading() {
2149 return mWorkspaceLoading;
2150 }
2151
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152 private void setWorkspaceLoading(boolean value) {
2153 boolean isLocked = isWorkspaceLocked();
2154 mWorkspaceLoading = value;
2155 if (isLocked != isWorkspaceLocked()) {
2156 onWorkspaceLockedChanged();
2157 }
2158 }
2159
2160 private void setWaitingForResult(boolean value) {
2161 boolean isLocked = isWorkspaceLocked();
2162 mWaitingForResult = value;
2163 if (isLocked != isWorkspaceLocked()) {
2164 onWorkspaceLockedChanged();
2165 }
2166 }
2167
Adam Cohen9211d422014-10-07 18:14:53 -07002168 protected void onWorkspaceLockedChanged() {
2169 if (mLauncherCallbacks != null) {
2170 mLauncherCallbacks.onWorkspaceLockedChanged();
2171 }
2172 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002177 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2178 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002179 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002181
Tony Wickhama0628cc2015-10-14 15:23:04 -07002182 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002183 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002184 if (LOGD) {
2185 Log.d(TAG, "Adding widget from drop");
2186 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002187 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2188 }
2189
Sunny Goyale6b63a32015-01-16 16:14:29 -08002190 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002191 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2192 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002193 if (appWidgetInfo.configure != null) {
2194 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002195 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002196
Bjorn Bringert7984c942009-12-09 15:38:25 +00002197 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002198 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2199 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002203 Runnable onComplete = new Runnable() {
2204 @Override
2205 public void run() {
2206 // Exit spring loaded mode if necessary after adding the widget
2207 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2208 null);
2209 }
2210 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002212 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002213 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 }
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002217 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002218 // Close any folders that may be open.
2219 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002220 mWorkspace.moveToCustomContentScreen(animate);
2221 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002222
2223 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2224 int[] cell, int spanX, int spanY) {
2225 switch (info.itemType) {
2226 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2227 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2228 int span[] = new int[2];
2229 span[0] = spanX;
2230 span[1] = spanY;
2231 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2232 container, screenId, cell, span);
2233 break;
2234 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2235 processShortcutFromDrop(info.componentName, container, screenId, cell);
2236 break;
2237 default:
2238 throw new IllegalStateException("Unknown item type: " + info.itemType);
2239 }
2240 }
2241
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 /**
2243 * Process a shortcut drop.
2244 *
2245 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002249 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2250 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254
2255 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.cellX = cell[0];
2257 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259
2260 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2261 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002262 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 }
2264
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 /**
2266 * Process a widget drop.
2267 *
2268 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002272 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2273 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002277 mPendingAddInfo.minSpanX = info.minSpanX;
2278 mPendingAddInfo.minSpanY = info.minSpanY;
2279
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = cell[0];
2282 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002284 if (span != null) {
2285 mPendingAddInfo.spanX = span[0];
2286 mPendingAddInfo.spanY = span[1];
2287 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 AppWidgetHostView hostView = info.boundWidget;
2290 int appWidgetId;
2291 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002292 // In the case where we've prebound the widget, we remove it from the DragLayer
2293 if (LOGD) {
2294 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2295 }
2296 getDragLayer().removeView(hostView);
2297
Adam Cohened66b2b2012-01-23 17:28:51 -08002298 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002299 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002300
2301 // Clear the boundWidget so that it doesn't get destroyed.
2302 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002304 // In this case, we either need to start an activity to get permission to bind
2305 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002307 Bundle options = info.bindOptions;
2308
Sunny Goyalffe83f12014-08-14 17:39:34 -07002309 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2310 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002311 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002312 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002314 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2316 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2317 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002318 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2319 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002320 // TODO: we need to make sure that this accounts for the options bundle.
2321 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002322 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2323 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 }
2326
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002329 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 folderInfo.title = getText(R.string.folder_name);
2331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002333 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2334 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335
2336 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 FolderIcon newFolder =
2338 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002341 // Force measure the new folder icon
2342 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2343 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002344 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 }
Romain Guycbb89e42009-06-08 15:52:54 -07002346
Winsonc0b52fe2015-09-09 16:38:15 -07002347 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002348 * Unbinds the view for the specified item, and removes the item and all its children.
2349 *
2350 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002351 * @param itemInfo the {@link ItemInfo} for this view.
2352 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002353 */
Winson2949fb52015-09-24 09:56:11 -07002354 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002355 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002356 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002357 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2358 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002359 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002360 } else {
2361 mWorkspace.removeWorkspaceItem(v);
2362 }
Winsonc0b52fe2015-09-09 16:38:15 -07002363 if (deleteFromDb) {
2364 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2365 }
2366 } else if (itemInfo instanceof FolderInfo) {
2367 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002368 mWorkspace.removeWorkspaceItem(v);
2369 if (deleteFromDb) {
2370 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2371 }
2372 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2373 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002374 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002375 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002377 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002378 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002379
Winsonc0b52fe2015-09-09 16:38:15 -07002380 } else {
2381 return false;
2382 }
2383 return true;
2384 }
2385
2386 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002387 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002388 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002389 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002390 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002391 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002392 // Deleting an app widget ID is a void call but writes to disk before returning
2393 // to the caller...
2394 new AsyncTask<Void, Void, Void>() {
2395 public Void doInBackground(Void ... args) {
2396 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2397 return null;
2398 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002399 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002401 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002402 }
2403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 @Override
2405 public boolean dispatchKeyEvent(KeyEvent event) {
2406 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2407 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002409 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002410 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002411 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002412 dumpState();
2413 return true;
2414 }
2415 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002416 }
2417 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2418 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002419 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 return true;
2421 }
2422 }
2423
2424 return super.dispatchKeyEvent(event);
2425 }
2426
Joe Onorato88ec0992009-11-19 13:16:06 -08002427 @Override
2428 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002429 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2430 return;
2431 }
2432
Sunny Goyal45478022015-06-08 16:52:41 -07002433 if (mDragController.isDragging()) {
2434 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002435 return;
2436 }
2437
Winson Chungb745afb2015-03-02 11:51:23 -08002438 if (isAppsViewVisible()) {
2439 showWorkspace(true);
2440 } else if (isWidgetsViewVisible()) {
2441 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002442 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002443 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002444 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002445 Folder openFolder = mWorkspace.getOpenFolder();
2446 if (openFolder.isEditingName()) {
2447 openFolder.dismissEditingName();
2448 } else {
2449 closeFolder();
2450 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002452 mWorkspace.exitWidgetResizeMode();
2453
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002454 // Back button is a no-op here, but give at least some feedback for the button press
2455 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002456 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002457 }
2458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002460 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002461 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002462 @Override
2463 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002464 if (mAppWidgetHost != null) {
2465 mAppWidgetHost.startListening();
2466 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002467
2468 // Recreate the QSB, as the widget has been reset.
2469 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002470 }
2471
2472 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 * Launches the intent referred by the clicked shortcut.
2474 *
2475 * @param v The view representing the clicked shortcut.
2476 */
2477 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2479 // view has detached (it's possible for this to happen if the view is removed mid touch).
2480 if (v.getWindowToken() == null) {
2481 return;
2482 }
2483
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002484 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002485 return;
2486 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002487
Adam Cohen1697b792013-09-17 19:08:21 -07002488 if (v instanceof Workspace) {
2489 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002490 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 }
2492 return;
2493 }
2494
2495 if (v instanceof CellLayout) {
2496 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002497 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2498 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002499 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002500 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002501 }
2502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002504 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002507 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002508 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002509 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002510 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002511 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002512 } else if (tag instanceof AppInfo) {
2513 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002514 } else if (tag instanceof LauncherAppWidgetInfo) {
2515 if (v instanceof PendingAppWidgetHostView) {
2516 onClickPendingWidget((PendingAppWidgetHostView) v);
2517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 }
2519 }
2520
Sunny Goyal70660032015-05-14 00:07:08 -07002521 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002522 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002523 return false;
2524 }
2525
Michael Jurkaaf442092010-06-10 17:01:57 -07002526 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002527 * Event handler for the app widget view which has not fully restored.
2528 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002529 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002530 if (mIsSafeModeEnabled) {
2531 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2532 return;
2533 }
2534
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002535 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002536 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002537 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002538 LauncherAppWidgetProviderInfo appWidgetInfo =
2539 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002540 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002541 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002542 mPendingAddInfo.copyFrom(info);
2543 mPendingAddWidgetId = widgetId;
2544
Sunny Goyalffe83f12014-08-14 17:39:34 -07002545 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2546 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 } else if (info.installProgress < 0) {
2549 // The install has not been queued
2550 final String packageName = info.providerName.getPackageName();
2551 showBrokenAppInstallDialog(packageName,
2552 new DialogInterface.OnClickListener() {
2553 public void onClick(DialogInterface dialog, int id) {
2554 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2555 }
2556 });
2557 } else {
2558 // Download has started.
2559 final String packageName = info.providerName.getPackageName();
2560 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 }
2562 }
2563
2564 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002565 * Event handler for the "grid" button that appears on the home screen, which
2566 * enters all apps mode.
2567 *
2568 * @param v The view that was clicked.
2569 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002570 protected void onClickAllAppsButton(View v) {
2571 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002572 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002573 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002574 true /* updatePredictedApps */, false /* focusSearchBar */);
2575 }
2576 }
2577
2578 protected void onLongClickAllAppsButton(View v) {
2579 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2580 if (!isAppsViewVisible()) {
2581 showAppsView(true /* animated */, false /* resetListToTop */,
2582 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002583 }
2584 }
2585
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 private void showBrokenAppInstallDialog(final String packageName,
2587 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002588 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 .setTitle(R.string.abandoned_promises_title)
2590 .setMessage(R.string.abandoned_promise_explanation)
2591 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2592 .setNeutralButton(R.string.abandoned_clean_this,
2593 new DialogInterface.OnClickListener() {
2594 public void onClick(DialogInterface dialog, int id) {
2595 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2596 mWorkspace.removeAbandonedPromise(packageName, user);
2597 }
2598 })
2599 .create().show();
2600 return;
2601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 /**
2604 * Event handler for an app shortcut click.
2605 *
2606 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2607 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002608 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2610 Object tag = v.getTag();
2611 if (!(tag instanceof ShortcutInfo)) {
2612 throw new IllegalArgumentException("Input must be a Shortcut");
2613 }
2614
2615 // Open shortcut
2616 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002617
2618 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002619 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2620 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002621 // Launch activity anyway, framework will tell the user why the app is suspended.
2622 } else {
2623 int error = R.string.activity_not_available;
2624 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2625 error = R.string.safemode_shortcut_error;
2626 }
2627 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2628 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002629 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002630 }
2631
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002633 if ((v instanceof BubbleTextView)
2634 && shortcut.isPromise()
2635 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002636 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002637 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 new DialogInterface.OnClickListener() {
2639 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002640 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 return;
2644 }
2645
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002647 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 }
2649
Adam Cohen091440a2015-03-18 14:16:05 -07002650 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002652 final ShortcutInfo shortcut;
2653 final Intent intent;
2654 if (tag instanceof ShortcutInfo) {
2655 shortcut = (ShortcutInfo) tag;
2656 intent = shortcut.intent;
2657 int[] pos = new int[2];
2658 v.getLocationOnScreen(pos);
2659 intent.setSourceBounds(new Rect(pos[0], pos[1],
2660 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002661
Sunny Goyal508da152014-08-14 10:53:27 -07002662 } else if (tag instanceof AppInfo) {
2663 shortcut = null;
2664 intent = ((AppInfo) tag).intent;
2665 } else {
2666 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2667 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668
2669 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002670 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671
2672 if (success && v instanceof BubbleTextView) {
2673 mWaitingForResume = (BubbleTextView) v;
2674 mWaitingForResume.setStayPressed(true);
2675 }
2676 }
2677
2678 /**
2679 * Event handler for a folder icon click.
2680 *
2681 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2682 */
2683 protected void onClickFolderIcon(View v) {
2684 if (LOGD) Log.d(TAG, "onClickFolder");
2685 if (!(v instanceof FolderIcon)){
2686 throw new IllegalArgumentException("Input must be a FolderIcon");
2687 }
2688
2689 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002690 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002692 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002693 }
Michael Jurka5130e402011-10-13 04:55:35 -07002694 }
2695
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002696 /**
2697 * Event handler for the (Add) Widgets button that appears after a long press
2698 * on the home screen.
2699 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002700 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002702 if (mIsSafeModeEnabled) {
2703 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2704 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002705 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002706 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002707 }
2708
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 /**
2710 * Event handler for the wallpaper picker button that appears after a long press
2711 * on the home screen.
2712 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002713 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002714 if (!Utilities.isWallapaperAllowed(this)) {
2715 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2716 return;
2717 }
2718
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002719 String pickerPackage = getString(R.string.wallpaper_picker_package);
2720 if (TextUtils.isEmpty(pickerPackage)) {
2721 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2722 }
2723
Tony Wickham785f7a52015-08-31 17:28:32 -07002724 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2725 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002726 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2727 .setPackage(pickerPackage)
2728 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2729 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 }
2731
2732 /**
2733 * Event handler for a click on the settings button that appears after a long press
2734 * on the home screen.
2735 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002736 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002738 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2739 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 }
2741
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 public View.OnTouchListener getHapticFeedbackTouchListener() {
2743 if (mHapticFeedbackTouchListener == null) {
2744 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002745 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002746 @Override
2747 public boolean onTouch(View v, MotionEvent event) {
2748 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2749 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2750 }
2751 return false;
2752 }
2753 };
2754 }
2755 return mHapticFeedbackTouchListener;
2756 }
2757
Tony Wickhame2217252016-03-22 16:34:23 -07002758 @Override
2759 public void onAccessibilityStateChanged(boolean enabled) {
2760 mDragLayer.onAccessibilityStateChanged(enabled);
2761 }
2762
Adam Cohen9211d422014-10-07 18:14:53 -07002763 public void onDragStarted(View view) {
2764 if (isOnCustomContent()) {
2765 // Custom content screen doesn't participate in drag and drop. If on custom
2766 // content screen, move to default.
2767 moveWorkspaceToDefaultScreen();
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002770
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002771 /**
2772 * Called when the user stops interacting with the launcher.
2773 * This implies that the user is now on the homescreen and is not doing housekeeping.
2774 */
Adam Cohen9211d422014-10-07 18:14:53 -07002775 protected void onInteractionEnd() {
2776 if (mLauncherCallbacks != null) {
2777 mLauncherCallbacks.onInteractionEnd();
2778 }
2779 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002780
2781 /**
2782 * Called when the user starts interacting with the launcher.
2783 * The possible interactions are:
2784 * - open all apps
2785 * - reorder an app shortcut, or a widget
2786 * - open the overview mode.
2787 * This is a good time to stop doing things that only make sense
2788 * when the user is on the homescreen and not doing housekeeping.
2789 */
Adam Cohen9211d422014-10-07 18:14:53 -07002790 protected void onInteractionBegin() {
2791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onInteractionBegin();
2793 }
2794 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002795
Winson Chungcd99cd32015-04-29 11:03:24 -07002796 /** Updates the interaction state. */
2797 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002798 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002799 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002800 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2801 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002804 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002805 onInteractionEnd();
2806 }
2807 }
2808
Winson Chung8f1eff72015-05-28 17:33:40 -07002809 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002810 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002812 // Only launch using the new animation if the shortcut has not opted out (this is a
2813 // private contract between launcher and may be ignored in the future).
2814 boolean useLaunchAnimation = (v != null) &&
2815 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002816 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2817 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002818
Kenny Guy1317e2d2014-05-08 18:52:50 +01002819 UserHandleCompat user = null;
2820 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2821 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2822 user = userManager.getUserForSerialNumber(serialNumber);
2823 }
2824
2825 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002826 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002827 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002828 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002829 int left = 0, top = 0;
2830 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2831 if (v instanceof TextView) {
2832 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002833 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2834 if (icon != null) {
2835 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002836 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002837 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002838 width = bounds.width();
2839 height = bounds.height();
2840 }
2841 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002842 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2843 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002844 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002845 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002846 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002847 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002848 // On L devices, we use the device default slide-up transition.
2849 // On L MR1 devices, we a custom version of the slide-up transition which
2850 // doesn't have the delay present in the device default.
2851 opts = ActivityOptions.makeCustomAnimation(this,
2852 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002853 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002854 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002855 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002856
2857 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2858 // Could be launching some bookkeeping activity
2859 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002860 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002861 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002862 launcherApps.startActivityForProfile(intent.getComponent(), user,
2863 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002864 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002865 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002867 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2868 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2869 // corresponding permission. Show the appropriate permission prompt if that
2870 // is the case.
2871 if (intent.getComponent() == null
2872 && Intent.ACTION_CALL.equals(intent.getAction())
2873 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2874 PackageManager.PERMISSION_GRANTED) {
2875 // TODO: Rename sPendingAddItem to a generic name.
2876 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2877 0, (ItemInfo) tag);
2878 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2879 REQUEST_PERMISSION_CALL_PHONE);
2880 return false;
2881 }
2882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002884 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002886 "or use the exported attribute for this activity. "
2887 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002889 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002891
Winson Chungbedf9232015-07-10 12:38:30 -07002892 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002893 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002894 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2895 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2896 return false;
2897 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002898 try {
2899 success = startActivity(v, intent, tag);
2900 } catch (ActivityNotFoundException e) {
2901 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2902 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2903 }
2904 return success;
2905 }
2906
Adam Cohen268c4752012-06-06 17:47:33 -07002907 /**
2908 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2909 * in the DragLayer in the exact absolute location of the original FolderIcon.
2910 */
2911 private void copyFolderIconToImage(FolderIcon fi) {
2912 final int width = fi.getMeasuredWidth();
2913 final int height = fi.getMeasuredHeight();
2914
2915 // Lazy load ImageView, Bitmap and Canvas
2916 if (mFolderIconImageView == null) {
2917 mFolderIconImageView = new ImageView(this);
2918 }
2919 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2920 mFolderIconBitmap.getHeight() != height) {
2921 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2922 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2923 }
2924
2925 DragLayer.LayoutParams lp;
2926 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2927 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2928 } else {
2929 lp = new DragLayer.LayoutParams(width, height);
2930 }
2931
Adam Cohen307fe232012-08-16 17:55:58 -07002932 // The layout from which the folder is being opened may be scaled, adjust the starting
2933 // view size by this scale factor.
2934 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002935 lp.customPosition = true;
2936 lp.x = mRectForFolderAnimation.left;
2937 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002938 lp.width = (int) (scale * width);
2939 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002940
2941 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2942 fi.draw(mFolderIconCanvas);
2943 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002944 if (fi.getFolder() != null) {
2945 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2946 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002947 }
Adam Cohen268c4752012-06-06 17:47:33 -07002948 // Just in case this image view is still in the drag layer from a previous animation,
2949 // we remove it and re-add it.
2950 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2951 mDragLayer.removeView(mFolderIconImageView);
2952 }
2953 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002954 if (fi.getFolder() != null) {
2955 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002956 }
Adam Cohen268c4752012-06-06 17:47:33 -07002957 }
2958
Adam Cohen37b2a492016-04-06 18:36:06 -07002959 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002960 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002961 FolderInfo info = (FolderInfo) fi.getTag();
2962 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2963 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002964 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2965 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002966 }
2967
Adam Cohen268c4752012-06-06 17:47:33 -07002968 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2969 copyFolderIconToImage(fi);
2970 fi.setVisibility(View.INVISIBLE);
2971
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002972 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2973 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002974 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002975 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2976 }
2977 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002978 oa.start();
2979 }
2980
Sunny Goyal935fca12015-10-13 10:19:01 -07002981 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002982 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002983 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002984
Adam Cohen268c4752012-06-06 17:47:33 -07002985 // We remove and re-draw the FolderIcon in-case it has changed
2986 mDragLayer.removeView(mFolderIconImageView);
2987 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002988
2989 if (cl != null) {
2990 cl.clearFolderLeaveBehind();
2991 }
2992
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002993 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002994 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002995 oa.addListener(new AnimatorListenerAdapter() {
2996 @Override
2997 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002998 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002999 // Remove the ImageView copy of the FolderIcon and make the original visible.
3000 mDragLayer.removeView(mFolderIconImageView);
3001 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003002 }
3003 }
3004 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003005 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003006 if (!animate) {
3007 oa.end();
3008 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 }
3010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003012 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013 * is animated relative to the specified View. If the View is null, no animation
3014 * is played.
3015 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003016 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003018 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003019
Adam Cohenfb91f302012-06-11 15:45:18 -07003020 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003021 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3022 if (openFolder != null && openFolder != folder) {
3023 // Close any open folder before opening a folder.
3024 closeFolder();
3025 }
3026
Adam Cohena9cf38f2011-05-02 15:36:58 -07003027 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028
Adam Cohena9cf38f2011-05-02 15:36:58 -07003029 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030
Sunny Goyalf4066152015-04-15 09:42:19 -07003031 // While the folder is open, the position of the icon cannot change.
3032 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3033
Adam Cohen4554ee12011-08-03 16:13:21 -07003034 // Just verify that the folder hasn't already been added to the DragLayer.
3035 // There was a one-off crash where the folder had a parent already.
3036 if (folder.getParent() == null) {
3037 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003038 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003039 } else {
3040 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3041 folder.getParent() + ").");
3042 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003043 folder.animateOpen();
3044
3045 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003046
3047 // Notify the accessibility manager that this folder "window" has appeared and occluded
3048 // the workspace items
3049 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3050 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003051 }
3052
3053 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003054 closeFolder(true);
3055 }
3056
3057 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003058 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003059 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003060 if (folder.isEditingName()) {
3061 folder.dismissEditingName();
3062 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003063 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003064 }
3065 }
3066
Sunny Goyal935fca12015-10-13 10:19:01 -07003067 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003068 animate &= !Utilities.isPowerSaverOn(this);
3069
Adam Cohen4554ee12011-08-03 16:13:21 -07003070 folder.getInfo().opened = false;
3071
3072 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3073 if (parent != null) {
3074 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003075 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003076 if (fi != null) {
3077 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3078 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003080 if (animate) {
3081 folder.animateClosed();
3082 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003083 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003084 }
Winson Chung83ca4802013-04-12 15:10:52 -07003085
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 // Notify the accessibility manager that this folder "window" has disappeared and no
3087 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003088 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 }
3090
Tony Wickhamdadb3042016-02-24 11:07:00 -08003091 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003093 if (!isDraggingEnabled()) return false;
3094 if (isWorkspaceLocked()) return false;
3095 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096
Winson Chung76648c52015-07-10 14:33:23 -07003097 if (v == mAllAppsButton) {
3098 onLongClickAllAppsButton(v);
3099 return true;
3100 }
3101
Adam Cohen1697b792013-09-17 19:08:21 -07003102 if (v instanceof Workspace) {
3103 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003104 if (!mWorkspace.isTouchActive()) {
3105 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003106 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3107 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3108 return true;
3109 } else {
3110 return false;
3111 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003112 } else {
3113 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003114 }
Adam Cohen1697b792013-09-17 19:08:21 -07003115 }
3116
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003117 CellLayout.CellInfo longClickCellInfo = null;
3118 View itemUnderLongClick = null;
3119 if (v.getTag() instanceof ItemInfo) {
3120 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003121 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003122 itemUnderLongClick = longClickCellInfo.cell;
3123 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 }
3125
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 // The hotseat touch handling does not go through Workspace, and we always allow long press
3127 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003128 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003129 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003130 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003131 // User long pressed on empty space
3132 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3133 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003134 if (mWorkspace.isInOverviewMode()) {
3135 mWorkspace.startReordering(v);
3136 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003137 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003140 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3141 mHotseat.getOrderInHotseat(
3142 longClickCellInfo.cellX,
3143 longClickCellInfo.cellY));
3144 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003146 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 }
3148 }
3149 }
3150 return true;
3151 }
3152
Winson Chung3d503fb2011-07-13 17:25:49 -07003153 boolean isHotseatLayout(View layout) {
3154 return mHotseat != null && layout != null &&
3155 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3156 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003157
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 /**
3159 * Returns the CellLayout of the specified container at the specified screen.
3160 */
Sunny Goyal92820592015-03-02 11:31:35 -08003161 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3163 if (mHotseat != null) {
3164 return mHotseat.getLayout();
3165 } else {
3166 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003167 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003169 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003170 }
3171 }
3172
Winson Chungb745afb2015-03-02 11:51:23 -08003173 /**
3174 * For overridden classes.
3175 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003176 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003177 return isAppsViewVisible();
3178 }
3179
3180 public boolean isAppsViewVisible() {
3181 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3182 }
3183
3184 public boolean isWidgetsViewVisible() {
3185 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003186 }
3187
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003188 private void setWorkspaceBackground(int background) {
3189 switch (background) {
3190 case WORKSPACE_BACKGROUND_TRANSPARENT:
3191 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3192 break;
3193 case WORKSPACE_BACKGROUND_BLACK:
3194 getWindow().setBackgroundDrawable(null);
3195 break;
3196 default:
3197 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3198 }
Craig Mautner360310b2012-10-26 15:13:08 -07003199 }
3200
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003201 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003202 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3203 int curflags = getWindow().getAttributes().flags
3204 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3205 if (wpflags != curflags) {
3206 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3207 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003208 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003209 }
3210
Michael Jurkae326f182011-11-21 14:05:46 -08003211 @Override
3212 public void onTrimMemory(int level) {
3213 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003214 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3215 // The widget preview db can result in holding onto over
3216 // 3MB of memory for caching which isn't necessary.
3217 SQLiteDatabase.releaseMemory();
3218
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003219 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003220 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003221 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003222 if (mLauncherCallbacks != null) {
3223 mLauncherCallbacks.onTrimMemory(level);
3224 }
Michael Jurkae326f182011-11-21 14:05:46 -08003225 }
3226
Winson5c6bdbb2015-09-03 11:36:19 -07003227 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003228 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003229 }
3230
Winson5c6bdbb2015-09-03 11:36:19 -07003231 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003232 boolean changed = mState != State.WORKSPACE ||
3233 mWorkspace.getState() != Workspace.State.NORMAL;
3234 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003235 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003236 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003237 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003238
Michael Jurkab3e22d92011-10-31 15:58:33 -07003239 // Set focus to the AppsCustomize button
3240 if (mAllAppsButton != null) {
3241 mAllAppsButton.requestFocus();
3242 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003243 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003244
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003245 // Change the state *after* we've called all the transition code
3246 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003247
Winson Chung5fb63472011-02-02 17:03:37 -08003248 // Resume the auto-advance of widgets
3249 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003250 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003251
Winson Chung0f785722015-04-08 10:27:49 -07003252 if (changed) {
3253 // Send an accessibility event to announce the context change
3254 getWindow().getDecorView()
3255 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003256 }
Winson5c6bdbb2015-09-03 11:36:19 -07003257 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003258 }
3259
Winsone9f27272015-10-13 10:47:51 -07003260 /**
3261 * Shows the overview button.
3262 */
Adam Cohened307df2013-10-02 09:37:31 -07003263 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003264 showOverviewMode(animated, false);
3265 }
3266
3267 /**
3268 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3269 * onto one of the overview panel buttons.
3270 */
3271 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3272 Runnable postAnimRunnable = null;
3273 if (requestButtonFocus) {
3274 postAnimRunnable = new Runnable() {
3275 @Override
3276 public void run() {
3277 // Hitting the menu button when in touch mode does not trigger touch mode to
3278 // be disabled, so if requested, force focus on one of the overview panel
3279 // buttons.
3280 mOverviewPanel.requestFocusFromTouch();
3281 }
3282 };
3283 }
Adam Cohened307df2013-10-02 09:37:31 -07003284 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003285 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003286 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003287 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003288 }
3289
Winson Chungb745afb2015-03-02 11:51:23 -08003290 /**
3291 * Shows the apps view.
3292 */
Winson Chung76648c52015-07-10 14:33:23 -07003293 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3294 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003295 if (resetListToTop) {
3296 mAppsView.scrollToTop();
3297 }
Winson Chung4ac30062015-05-08 17:34:17 -07003298 if (updatePredictedApps) {
3299 tryAndUpdatePredictedApps();
3300 }
Winson Chung76648c52015-07-10 14:33:23 -07003301 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003302 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303
Winson Chungb745afb2015-03-02 11:51:23 -08003304 /**
3305 * Shows the widgets view.
3306 */
3307 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003308 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003309 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003310 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003311 }
Winson Chung76648c52015-07-10 14:33:23 -07003312 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003313
3314 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003315 @Override
3316 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003318 }
3319 });
Winson Chungb745afb2015-03-02 11:51:23 -08003320 }
3321
3322 /**
3323 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003324 *
3325 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003326 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003327 // TODO: calling method should use the return value so that when {@code false} is returned
3328 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003329 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003330 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3331 mState != State.WIDGETS_SPRING_LOADED) {
3332 return false;
3333 }
3334 if (toState != State.APPS && toState != State.WIDGETS) {
3335 return false;
3336 }
Winson Chungb745afb2015-03-02 11:51:23 -08003337
3338 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003339 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3340 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003341 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003342 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003343 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344
Michael Jurkab3e22d92011-10-31 15:58:33 -07003345 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003346 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347
3348 // Pause the auto-advance of widgets until we are out of AllApps
3349 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003350 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351 closeFolder();
3352
3353 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003354 getWindow().getDecorView()
3355 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003356 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357 }
3358
Winson Chungcd99cd32015-04-29 11:03:24 -07003359 /**
3360 * Updates the workspace and interaction state on state change, and return the animation to this
3361 * new state.
3362 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003363 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003364 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003365 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003366 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003367 updateInteraction(fromState, toState);
3368 return anim;
3369 }
3370
Jun Mukaif0033da2015-08-04 18:34:30 -07003371 public void onLauncherClingShown() {
3372 // When a launcher cling appears, it should cover the underlying layers, so their focus
3373 // should be blocked.
3374 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3375 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3376 }
3377 }
3378
3379 public void onLauncherClingDismissed() {
3380 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3381 }
3382
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003384 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003385 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003386 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003387 }
Winson Chungb745afb2015-03-02 11:51:23 -08003388
Winson Chung006ee262015-08-03 14:40:11 -07003389 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003390 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003391 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003392
3393 if (isAppsViewVisible()) {
3394 mState = State.APPS_SPRING_LOADED;
3395 } else if (isWidgetsViewVisible()) {
3396 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003397 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003398 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003399 } else {
3400 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003401 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003402 }
Adam Cohen7777d962011-08-18 18:58:38 -07003403
Hyunyoung Song3f471442015-04-08 19:01:34 -07003404 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003405 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003406 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003407
Winson Chunge7a03942011-08-05 15:05:12 -07003408 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003409 @Override
3410 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003411 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3413 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003414 // Before we show workspace, hide all apps again because
3415 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3416 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003417 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003418 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003419 } else {
3420 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003421 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003422 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003423 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003424 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
Tony Wickhame0c33232016-02-08 11:37:04 -08003426 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003427 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3428 || mState == State.WIDGETS_SPRING_LOADED;
3429 }
3430
Michael Jurkad3ef3062010-11-23 16:23:58 -08003431 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003432 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003433 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003434 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003435 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003436 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003437 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3438 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003439 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003440 }
3441
Winson Chung4ac30062015-05-08 17:34:17 -07003442 /**
3443 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3444 * resumed.
3445 */
3446 private void tryAndUpdatePredictedApps() {
3447 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003448 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003449 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003450 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003451 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003452 }
3453 }
3454 }
3455
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456 void lockAllApps() {
3457 // TODO
3458 }
3459
3460 void unlockAllApps() {
3461 // TODO
3462 }
3463
Winsonf768d932015-09-25 16:12:35 -07003464 public boolean launcherCallbacksProvidesSearch() {
3465 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3466 }
3467
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003468 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003469 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003470 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003471 }
3472
Adam Cohen24ce0b32014-01-14 16:18:14 -08003473 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003474 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3475 if (searchProvider == null) {
3476 return null;
3477 }
3478
3479 Bundle opts = new Bundle();
3480 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003481 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003482
Tony Wickham3a3517f2015-10-06 11:27:04 -07003483 // Determine the min and max dimensions of the widget.
3484 LauncherAppState app = LauncherAppState.getInstance();
3485 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3486 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3487 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003488 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3489 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003490 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003491 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003492 int minWidth = maxWidth;
3493 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003494 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3495 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3496 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3497 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3498 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003499 }
3500 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3501 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3502 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3503 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003504 if (LOGD) {
3505 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3506 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3507 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003508
Tony Wickham775455c2015-10-16 09:49:32 -07003509 if (mLauncherCallbacks != null) {
3510 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3511 }
3512
Sunny Goyalf7258242015-10-19 16:59:07 -07003513 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003514 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003515 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003516 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003517 || (widgetInfo == null)
3518 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003519 // A valid widget is not already bound.
3520 if (widgetId > -1) {
3521 mAppWidgetHost.deleteAppWidgetId(widgetId);
3522 widgetId = -1;
3523 }
3524
3525 // Try to bind a new widget
3526 widgetId = mAppWidgetHost.allocateAppWidgetId();
3527
3528 if (!AppWidgetManagerCompat.getInstance(this)
3529 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3530 mAppWidgetHost.deleteAppWidgetId(widgetId);
3531 widgetId = -1;
3532 }
3533
Sunny Goyalf7258242015-10-19 16:59:07 -07003534 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003535 .putInt(QSB_WIDGET_ID, widgetId)
3536 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003537 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003538 }
3539
Sunny Goyal8d595092015-07-06 12:22:14 -07003540 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003541 if (widgetId != -1) {
3542 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003543 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003544 mQsb.updateAppWidgetOptions(opts);
3545 mQsb.setPadding(0, 0, 0, 0);
3546 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003547 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003549 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003550 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003551 }
3552
Sunny Goyal22235bc2015-04-03 09:23:43 -07003553 private void reinflateQSBIfNecessary() {
3554 if (mQsb instanceof LauncherAppWidgetHostView &&
3555 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3556 mSearchDropTargetBar.removeView(mQsb);
3557 mQsb = null;
3558 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3559 }
3560 }
3561
Michael Jurkad7c28052012-04-27 15:43:36 -07003562 @Override
3563 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003564 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003565 final List<CharSequence> text = event.getText();
3566 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003567 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003568 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003569 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003570 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003571 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003572 } else if (mWorkspace != null) {
3573 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003574 } else {
3575 text.add(getString(R.string.all_apps_home_button_label));
3576 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003577 return result;
3578 }
3579
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003580 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003581 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003582 */
Adam Cohen091440a2015-03-18 14:16:05 -07003583 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003584 @Override
3585 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003586 closeSystemDialogs();
3587 }
3588 }
3589
3590 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 * If the activity is currently paused, signal that we need to run the passed Runnable
3592 * in onResume.
3593 *
3594 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003595 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3596 * wrong when we're not running, and if the activity comes back to what the configuration was
3597 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003598 *
3599 * Implementation of the method from LauncherModel.Callbacks.
3600 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003601 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003603 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003605 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003606 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003608 }
3609 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003610 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 return true;
3612 } else {
3613 return false;
3614 }
3615 }
3616
Michael Jurkac402cd92013-05-20 15:49:32 +02003617 private boolean waitUntilResume(Runnable run) {
3618 return waitUntilResume(run, false);
3619 }
3620
Michael Jurka1e2f4652013-07-08 18:03:46 -07003621 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003622 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003623 }
3624
Michael Jurka7607c2f2013-04-03 14:33:19 -07003625 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003626 * If the activity is currently paused, signal that we need to re-run the loader
3627 * in onResume.
3628 *
3629 * This needs to be called from incoming places where resources might have been loaded
3630 * while we are paused. That is becaues the Configuration might be wrong
3631 * when we're not running, and if it comes back to what it was when we
3632 * were paused, we are not restarted.
3633 *
3634 * Implementation of the method from LauncherModel.Callbacks.
3635 *
3636 * @return true if we are currently paused. The caller might be able to
3637 * skip some work in that case since we will come back again.
3638 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003639 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003640 public boolean setLoadOnResume() {
3641 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003642 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003643 mOnResumeNeedsLoad = true;
3644 return true;
3645 } else {
3646 return false;
3647 }
3648 }
3649
3650 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003653 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003655 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003656 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003657 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003658 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003659 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003661
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003663 * Clear any pending bind callbacks. This is called when is loader is planning to
3664 * perform a full rebind from scratch.
3665 */
3666 @Override
3667 public void clearPendingBinds() {
3668 mBindOnResumeCallbacks.clear();
3669 if (mPendingExecutor != null) {
3670 mPendingExecutor.markCompleted();
3671 mPendingExecutor = null;
3672 }
3673 }
3674
3675 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 * Refreshes the shortcuts shown on the workspace.
3677 *
3678 * Implementation of the method from LauncherModel.Callbacks.
3679 */
3680 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003681 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003682
Winson Chungd64d1762013-08-20 14:37:16 -07003683 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003684 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003685 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003686
Michael Jurka05bf644e2011-11-30 20:28:53 -08003687 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003688 if (mHotseat != null) {
3689 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690 }
3691 }
3692
Adam Cohendcd297f2013-06-18 13:13:40 -07003693 @Override
3694 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003695 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003696
Adam Cohen5084cba2013-09-03 12:01:16 -07003697 // If there are no screens, we need to have an empty screen
3698 if (orderedScreenIds.size() == 0) {
3699 mWorkspace.addExtraEmptyScreen();
3700 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003701
3702 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003703 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003704 if (hasCustomContentToLeft()) {
3705 mWorkspace.createCustomContentContainer();
3706 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003707 }
Winson Chung64359a52013-07-08 17:17:08 -07003708 }
3709
3710 @Override
3711 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003712 int count = orderedScreenIds.size();
3713 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003714 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003715 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003716 }
3717
Winson Chungd64d1762013-08-20 14:37:16 -07003718 public void bindAppsAdded(final ArrayList<Long> newScreens,
3719 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003720 final ArrayList<ItemInfo> addAnimated,
3721 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003722 Runnable r = new Runnable() {
3723 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003724 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003725 }
3726 };
3727 if (waitUntilResume(r)) {
3728 return;
3729 }
3730
Winson Chungd64d1762013-08-20 14:37:16 -07003731 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003732 if (newScreens != null) {
3733 bindAddScreens(newScreens);
3734 }
Winson Chungd64d1762013-08-20 14:37:16 -07003735
3736 // We add the items without animation on non-visible pages, and with
3737 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003738 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003739 bindItems(addNotAnimated, 0,
3740 addNotAnimated.size(), false);
3741 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003742 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003743 bindItems(addAnimated, 0,
3744 addAnimated.size(), true);
3745 }
Winson Chungc58497e2013-09-03 17:48:37 -07003746
Winson Chung87412982013-10-03 18:34:14 -07003747 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003748 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003749
Winson Chungb745afb2015-03-02 11:51:23 -08003750 if (addedApps != null && mAppsView != null) {
3751 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003752 }
Winson Chungd64d1762013-08-20 14:37:16 -07003753 }
3754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003755 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 * Bind the items start-end from the list.
3757 *
3758 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003759 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003760 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003761 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3762 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003763 Runnable r = new Runnable() {
3764 public void run() {
3765 bindItems(shortcuts, start, end, forceAnimateIcons);
3766 }
3767 };
3768 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003769 return;
3770 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003771
Winson Chungf0c6ae02012-03-21 16:10:31 -07003772 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003773 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3774 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003775 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003776 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003777 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003778 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003780
3781 // Short circuit if we are loading dock items for a configuration which has no dock
3782 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3783 mHotseat == null) {
3784 continue;
3785 }
3786
Sunny Goyal639e9062015-08-19 19:17:06 -07003787 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 switch (item.itemType) {
3789 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3790 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003792 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003793
Winson Chung64359a52013-07-08 17:17:08 -07003794 /*
3795 * TODO: FIX collision case
3796 */
Winson Chungce376632013-07-11 16:12:41 -07003797 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3798 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3799 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003800 View v = cl.getChildAt(item.cellX, item.cellY);
3801 Object tag = v.getTag();
3802 String desc = "Collision while binding workspace item: " + item
3803 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003804 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003805 throw (new RuntimeException(desc));
3806 } else {
3807 Log.d(TAG, desc);
3808 }
Winson Chungce376632013-07-11 16:12:41 -07003809 }
Winson Chung64359a52013-07-08 17:17:08 -07003810 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003812 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003813 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003814 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003815 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003817 default:
3818 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003820
3821 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3822 item.cellY, 1, 1);
3823 if (animateIcons) {
3824 // Animate all the applications up now
3825 view.setAlpha(0f);
3826 view.setScaleX(0f);
3827 view.setScaleY(0f);
3828 bounceAnims.add(createNewAppBounceAnimation(view, i));
3829 newShortcutsScreenId = item.screenId;
3830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003831 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003832
Winson Chung997a9232013-07-24 15:33:46 -07003833 if (animateIcons) {
3834 // Animate to the correct page
3835 if (newShortcutsScreenId > -1) {
3836 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003837 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003838 final Runnable startBounceAnimRunnable = new Runnable() {
3839 public void run() {
3840 anim.playTogether(bounceAnims);
3841 anim.start();
3842 }
3843 };
Winson Chung997a9232013-07-24 15:33:46 -07003844 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003845 // We post the animation slightly delayed to prevent slowdowns
3846 // when we are loading right after we return to launcher.
3847 mWorkspace.postDelayed(new Runnable() {
3848 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003849 if (mWorkspace != null) {
3850 mWorkspace.snapToPage(newScreenIndex);
3851 mWorkspace.postDelayed(startBounceAnimRunnable,
3852 NEW_APPS_ANIMATION_DELAY);
3853 }
Winson Chung94d67682013-09-25 16:29:40 -07003854 }
3855 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003856 } else {
3857 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003858 }
3859 }
Winson Chung64359a52013-07-08 17:17:08 -07003860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003862 }
3863
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003864 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3865 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3866 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003867 view.updateAppWidget(null);
3868 view.setOnClickListener(this);
3869 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 mWorkspace.requestLayout();
3871 }
3872
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 /**
3874 * Add the views for a widget to the workspace.
3875 *
3876 * Implementation of the method from LauncherModel.Callbacks.
3877 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003878 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003879 Runnable r = new Runnable() {
3880 public void run() {
3881 bindAppWidget(item);
3882 }
3883 };
3884 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003885 return;
3886 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003887
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 if (mIsSafeModeEnabled) {
3889 bindSafeModeWidget(item);
3890 return;
3891 }
3892
Daniel Sandler843e8602010-06-07 14:59:01 -04003893 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3894 if (DEBUG_WIDGETS) {
3895 Log.d(TAG, "bindAppWidget: " + item);
3896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003898 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003899
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003900 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3901 // If the provider is not ready, bind as a pending widget.
3902 appWidgetInfo = null;
3903 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3904 // The widget id is not valid. Try to find the widget based on the provider info.
3905 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3906 } else {
3907 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3908 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003909
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003910 // If the provider is ready, but the width is not yet restored, try to restore it.
3911 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3912 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003913 if (appWidgetInfo == null) {
3914 if (DEBUG_WIDGETS) {
3915 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3916 + " belongs to component " + item.providerName
3917 + ", as the povider is null");
3918 }
3919 LauncherModel.deleteItemFromDatabase(this, item);
3920 return;
3921 }
Sunny Goyalff572272014-07-23 13:58:07 -07003922
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003923 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003924 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003925 // Note: This assumes that the id remap broadcast is received before this step.
3926 // If that is not the case, the id remap will be ignored and user may see the
3927 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003928 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003929 pendingInfo.spanX = item.spanX;
3930 pendingInfo.spanY = item.spanY;
3931 pendingInfo.minSpanX = item.minSpanX;
3932 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003933 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003934
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003935 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3936 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3937 newWidgetId, appWidgetInfo, options);
3938
3939 // TODO consider showing a permission dialog when the widget is clicked.
3940 if (!success) {
3941 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3942 if (DEBUG_WIDGETS) {
3943 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3944 + " belongs to component " + item.providerName
3945 + ", as the launcher is unable to bing a new widget id");
3946 }
3947 LauncherModel.deleteItemFromDatabase(this, item);
3948 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003949 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003950
3951 item.appWidgetId = newWidgetId;
3952
3953 // If the widget has a configure activity, it is still needs to set it up, otherwise
3954 // the widget is ready to go.
3955 item.restoreStatus = (appWidgetInfo.configure == null)
3956 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3957 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3958
3959 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003960 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003961 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003962 // The widget was marked as UI not ready, but there is no configure activity to
3963 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003964 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3965 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003966 }
Sunny Goyalff572272014-07-23 13:58:07 -07003967 }
3968
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003969 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003970 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003971 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3972 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003973 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003974
Sunny Goyal56c73602015-09-25 12:55:01 -07003975 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003976 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003977 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003978 deleteWidgetInfo(item);
3979 return;
3980 }
3981
Sunny Goyal233ee962015-08-03 13:05:01 -07003982 item.minSpanX = appWidgetInfo.minSpanX;
3983 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003984 addAppWidgetToWorkspace(
3985 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3986 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003988 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003989 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003990 view.updateAppWidget(null);
3991 view.setOnClickListener(this);
3992 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003994 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995
Daniel Sandler843e8602010-06-07 14:59:01 -04003996 if (DEBUG_WIDGETS) {
3997 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3998 + (SystemClock.uptimeMillis()-start) + "ms");
3999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004000 }
4001
Sunny Goyalff572272014-07-23 13:58:07 -07004002 /**
4003 * Restores a pending widget.
4004 *
4005 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004006 */
4007 private void completeRestoreAppWidget(final int appWidgetId) {
4008 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4009 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4010 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4011 return;
4012 }
4013
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004014 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004015 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4016
4017 mWorkspace.reinflateWidgetsIfNecessary();
4018 LauncherModel.updateItemInDatabase(this, info);
4019 }
4020
Adam Cohen1462de32012-07-24 22:34:36 -07004021 public void onPageBoundSynchronously(int page) {
4022 mSynchronouslyBoundPages.add(page);
4023 }
4024
Sunny Goyal527c7d32015-08-28 15:19:36 -07004025 @Override
4026 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4027 if (mPendingExecutor != null) {
4028 mPendingExecutor.markCompleted();
4029 }
4030 mPendingExecutor = executor;
4031 executor.attachTo(this);
4032 }
4033
4034 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4035 if (mPendingExecutor == executor) {
4036 mPendingExecutor = null;
4037 }
4038 }
4039
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040 /**
4041 * Callback saying that there aren't any more items to bind.
4042 *
4043 * Implementation of the method from LauncherModel.Callbacks.
4044 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004045 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004046 Runnable r = new Runnable() {
4047 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004048 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004049 }
4050 };
4051 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 return;
4053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 if (mSavedState != null) {
4055 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004056 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004058
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 mSavedState = null;
4060 }
4061
Adam Cohen1462de32012-07-24 22:34:36 -07004062 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004064 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004065
4066 // If we received the result of any pending adds while the loader was running (e.g. the
4067 // widget configuration forced an orientation change), process them now.
4068 if (sPendingAddItem != null) {
4069 final long screenId = completeAdd(sPendingAddItem);
4070
4071 // TODO: this moves the user to the page where the pending item was added. Ideally,
4072 // the screen would be guaranteed to exist after bind, and the page would be set through
4073 // the workspace restore process.
4074 mWorkspace.post(new Runnable() {
4075 @Override
4076 public void run() {
4077 mWorkspace.snapToScreenId(screenId);
4078 }
4079 });
4080 sPendingAddItem = null;
4081 }
4082
Sunny Goyal756adbc2015-04-16 15:20:51 -07004083 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004084
4085 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004086 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004087 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004088 }
4089
Winson Chunga0b7e862013-09-05 16:03:15 -07004090 public boolean isAllAppsButtonRank(int rank) {
4091 if (mHotseat != null) {
4092 return mHotseat.isAllAppsButtonRank(rank);
4093 }
4094 return false;
4095 }
4096
Winson Chunga2413752012-04-03 14:22:34 -07004097 private boolean canRunNewAppsAnimation() {
4098 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004099 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4100 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004101 }
4102
Winson Chungc9168342013-06-26 14:54:55 -07004103 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004104 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004105 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4106 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004107 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004108 return bounceAnim;
4109 }
4110
Adam Cohen27772732013-11-11 14:00:56 +00004111 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004112 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004113 }
4114
Tony Wickham3a3517f2015-10-06 11:27:04 -07004115 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004116 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004117 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004118 }
4119
Tony Wickham55616cd2015-09-23 14:55:17 -07004120 public int getSearchBarHeight() {
4121 if (mLauncherCallbacks != null) {
4122 return mLauncherCallbacks.getSearchBarHeight();
4123 }
4124 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4125 }
4126
Winson Chung88fa7412015-08-03 14:25:28 -07004127 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004128 if (mSearchDropTargetBar == null) {
4129 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004130 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004131 if (mQsb != null) {
4132 mSearchDropTargetBar.removeView(mQsb);
4133 mQsb = null;
4134 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004135 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004136 }
4137
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004138 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004139 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4140 * multiple calls to bind the same list.)
4141 */
4142 @Thunk ArrayList<AppInfo> mTmpAppsList;
4143 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4144 public void run() {
4145 bindAllApplications(mTmpAppsList);
4146 mTmpAppsList = null;
4147 }
4148 };
4149
4150 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004151 * Add the icons for all apps.
4152 *
4153 * Implementation of the method from LauncherModel.Callbacks.
4154 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004155 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004156 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4157 mTmpAppsList = apps;
4158 return;
4159 }
4160
Winson Chungb745afb2015-03-02 11:51:23 -08004161 if (mAppsView != null) {
4162 mAppsView.setApps(apps);
4163 }
Adam Cohen9211d422014-10-07 18:14:53 -07004164 if (mLauncherCallbacks != null) {
4165 mLauncherCallbacks.bindAllApplications(apps);
4166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 }
4168
4169 /**
4170 * A package was updated.
4171 *
4172 * Implementation of the method from LauncherModel.Callbacks.
4173 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004174 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004175 Runnable r = new Runnable() {
4176 public void run() {
4177 bindAppsUpdated(apps);
4178 }
4179 };
4180 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004181 return;
4182 }
4183
Winson Chungb745afb2015-03-02 11:51:23 -08004184 if (mAppsView != null) {
4185 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 }
4188
Sunny Goyal4390ace2014-10-13 11:33:11 -07004189 @Override
4190 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4191 Runnable r = new Runnable() {
4192 public void run() {
4193 bindWidgetsRestored(widgets);
4194 }
4195 };
4196 if (waitUntilResume(r)) {
4197 return;
4198 }
4199 mWorkspace.widgetsRestored(widgets);
4200 }
4201
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004203 * Some shortcuts were updated in the background.
4204 *
4205 * Implementation of the method from LauncherModel.Callbacks.
4206 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004207 @Override
4208 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4209 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 Runnable r = new Runnable() {
4211 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004212 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004213 }
4214 };
4215 if (waitUntilResume(r)) {
4216 return;
4217 }
4218
Sunny Goyal4390ace2014-10-13 11:33:11 -07004219 if (!updated.isEmpty()) {
4220 mWorkspace.updateShortcuts(updated);
4221 }
4222
4223 if (!removed.isEmpty()) {
4224 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4225 for (ShortcutInfo si : removed) {
4226 removedComponents.add(si.getTargetComponent());
4227 }
4228 mWorkspace.removeItemsByComponentName(removedComponents, user);
4229 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004230 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004231 }
4232 }
4233
4234 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004235 * Update the state of a package, typically related to install state.
4236 *
4237 * Implementation of the method from LauncherModel.Callbacks.
4238 */
Sunny Goyale755d462014-07-22 13:48:29 -07004239 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004240 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4241 Runnable r = new Runnable() {
4242 public void run() {
4243 bindRestoreItemsChange(updates);
4244 }
4245 };
4246 if (waitUntilResume(r)) {
4247 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004248 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004249
Sunny Goyal756adbc2015-04-16 15:20:51 -07004250 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004251 }
4252
4253 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004254 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004255 * in addition to specific applications to remove, the reason being that
4256 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004257 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004258 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004260 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004261 public void bindWorkspaceComponentsRemoved(
4262 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4263 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004264 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004265 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004266 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004267 }
Winson Chungd64d1762013-08-20 14:37:16 -07004268 };
4269 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004270 return;
4271 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004272 if (!packageNames.isEmpty()) {
4273 mWorkspace.removeItemsByPackageName(packageNames, user);
4274 }
4275 if (!components.isEmpty()) {
4276 mWorkspace.removeItemsByComponentName(components, user);
4277 }
4278 // Notify the drag controller
4279 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004280
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004281 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004282
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004283 @Override
4284 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4285 Runnable r = new Runnable() {
4286 public void run() {
4287 bindAppInfosRemoved(appInfos);
4288 }
4289 };
4290 if (waitUntilResume(r)) {
4291 return;
Joe Onorato36115782010-06-17 13:28:48 -04004292 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004293
Winson Chungdf95eb12013-10-16 14:57:07 -07004294 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004295 if (mAppsView != null) {
4296 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004298 }
Joe Onoratobe386092009-11-17 17:32:16 -08004299
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004300 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004301 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004302 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004303 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004304 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004305
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004306 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004307 public void bindWidgetsModel(WidgetsModel model) {
4308 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004309 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004310 return;
4311 }
4312
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004313 if (mWidgetsView != null && model != null) {
4314 mWidgetsView.addWidgets(model);
4315 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004316 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004317 }
4318
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004319 @Override
4320 public void notifyWidgetProvidersChanged() {
4321 if (mWorkspace.getState().shouldUpdateWidget) {
4322 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4323 }
4324 }
4325
Winson Chung400438b2011-01-16 17:53:48 -08004326 private int mapConfigurationOriActivityInfoOri(int configOri) {
4327 final Display d = getWindowManager().getDefaultDisplay();
4328 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4329 switch (d.getRotation()) {
4330 case Surface.ROTATION_0:
4331 case Surface.ROTATION_180:
4332 // We are currently in the same basic orientation as the natural orientation
4333 naturalOri = configOri;
4334 break;
4335 case Surface.ROTATION_90:
4336 case Surface.ROTATION_270:
4337 // We are currently in the other basic orientation to the natural orientation
4338 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4339 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4340 break;
4341 }
4342
4343 int[] oriMap = {
4344 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4345 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4346 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4347 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4348 };
4349 // Since the map starts at portrait, we need to offset if this device's natural orientation
4350 // is landscape.
4351 int indexOffset = 0;
4352 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4353 indexOffset = 1;
4354 }
4355 return oriMap[(d.getRotation() + indexOffset) % 4];
4356 }
Adam Cohen446e9402011-09-15 18:21:21 -07004357
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004358 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004359 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004360 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004361 if (Utilities.ATLEAST_JB_MR2) {
4362 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4363 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004364 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4365 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004366 }
Winson Chung4b919f82012-05-01 10:44:08 -07004367 }
4368 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004369
Winson Chung4b919f82012-05-01 10:44:08 -07004370 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004371 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004372 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004373 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004374 } else {
4375 mHandler.postDelayed(new Runnable() {
4376 public void run() {
4377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4378 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004379 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004380 }
Winson Chung4b919f82012-05-01 10:44:08 -07004381 }
Winson Chung400438b2011-01-16 17:53:48 -08004382 }
4383
Winson Chunge43a1e72014-01-15 10:33:02 -08004384 protected boolean isLauncherPreinstalled() {
4385 PackageManager pm = getPackageManager();
4386 try {
4387 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4388 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4389 return true;
4390 } else {
4391 return false;
4392 }
4393 } catch (NameNotFoundException e) {
4394 e.printStackTrace();
4395 return false;
4396 }
4397 }
4398
Adam Cohenea90f832014-05-21 15:03:34 -07004399 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004400 * To be overridden by subclasses to indicate that there is an activity to launch
4401 * before showing the standard launcher experience.
4402 */
4403 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004404 if (mLauncherCallbacks != null) {
4405 return mLauncherCallbacks.hasFirstRunActivity();
4406 }
Adam Cohen432609a2014-03-13 17:03:22 -07004407 return false;
4408 }
4409
4410 /**
4411 * To be overridden by subclasses to launch any first run activity
4412 */
4413 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004414 if (mLauncherCallbacks != null) {
4415 return mLauncherCallbacks.getFirstRunActivity();
4416 }
Adam Cohen432609a2014-03-13 17:03:22 -07004417 return null;
4418 }
4419
Winson Chunga6945242014-01-08 14:04:34 -08004420 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004421 return !ActivityManager.isRunningInTestHarness() &&
4422 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004423 }
4424
Adam Cohen4a9423d2014-03-28 14:22:31 -07004425 protected boolean hasRunFirstRunActivity() {
4426 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4427 }
4428
Adam Cohen432609a2014-03-13 17:03:22 -07004429 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004430 if (shouldRunFirstRunActivity() &&
4431 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004432 Intent firstRunIntent = getFirstRunActivity();
4433 if (firstRunIntent != null) {
4434 startActivity(firstRunIntent);
4435 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004436 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004437 }
4438 }
Adam Cohen432609a2014-03-13 17:03:22 -07004439 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004440 }
4441
4442 private void markFirstRunActivityShown() {
4443 SharedPreferences.Editor editor = mSharedPrefs.edit();
4444 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4445 editor.apply();
4446 }
4447
Adam Cohen432609a2014-03-13 17:03:22 -07004448 /**
4449 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4450 * screen that must be displayed and dismissed.
4451 */
4452 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004453 if (mLauncherCallbacks != null) {
4454 return mLauncherCallbacks.hasDismissableIntroScreen();
4455 }
Adam Cohen432609a2014-03-13 17:03:22 -07004456 return false;
4457 }
4458
4459 /**
4460 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4461 */
4462 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004463 if (mLauncherCallbacks != null) {
4464 return mLauncherCallbacks.getIntroScreen();
4465 }
Adam Cohen432609a2014-03-13 17:03:22 -07004466 return null;
4467 }
4468
4469 /**
4470 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4471 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4472 */
4473 private boolean shouldShowIntroScreen() {
4474 return hasDismissableIntroScreen() &&
4475 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4476 }
4477
4478 protected void showIntroScreen() {
4479 View introScreen = getIntroScreen();
4480 changeWallpaperVisiblity(false);
4481 if (introScreen != null) {
4482 mDragLayer.showOverlayView(introScreen);
4483 }
4484 }
4485
4486 public void dismissIntroScreen() {
4487 markIntroScreenDismissed();
4488 if (showFirstRunActivity()) {
4489 // We delay hiding the intro view until the first run activity is showing. This
4490 // avoids a blip.
4491 mWorkspace.postDelayed(new Runnable() {
4492 @Override
4493 public void run() {
4494 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004495 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004496 }
4497 }, ACTIVITY_START_DELAY);
4498 } else {
4499 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004500 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004501 }
4502 changeWallpaperVisiblity(true);
4503 }
4504
4505 private void markIntroScreenDismissed() {
4506 SharedPreferences.Editor editor = mSharedPrefs.edit();
4507 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4508 editor.apply();
4509 }
4510
Adam Cohen091440a2015-03-18 14:16:05 -07004511 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004512 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4513 // on the device, then we always show the first run cling experience (or if there is no
4514 // launcher2). Otherwise, we prompt the user upon started for migration
4515 LauncherClings launcherClings = new LauncherClings(this);
4516 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004517 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004518 if (mModel.canMigrateFromOldLauncherDb(this)) {
4519 launcherClings.showMigrationCling();
4520 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004521 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004522 }
4523 }
4524 }
4525
Winson Chunga6945242014-01-08 14:04:34 -08004526 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004527 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4528 if (mHotseat != null) mHotseat.setAlpha(1f);
4529 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004530 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4531 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004532 }
4533
4534 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004535 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4536 if (mHotseat != null) mHotseat.setAlpha(0f);
4537 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004538 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4539 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004540 }
4541
Winson Chung5ffd51d2015-06-25 11:36:50 -07004542 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004543 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004544 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004545 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004547 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004548 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4549 if (userHandle != null) {
4550 user = UserHandleCompat.fromUser(userHandle);
4551 }
4552 }
4553 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004554 }
4555
4556 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004557 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004558 if (user == null) {
4559 user = UserHandleCompat.myUserHandle();
4560 }
4561
4562 // Called from search suggestion, add the profile extra to the intent to ensure that we
4563 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004564 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004565 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004566 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 return null;
4568 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004569 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 }
4571
Winson Chung5ffd51d2015-06-25 11:36:50 -07004572 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4574 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004575 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004576 UserHandleCompat.myUserHandle());
4577 }
4578
Winson Chung5ffd51d2015-06-25 11:36:50 -07004579 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004580 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4581 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004582 mWorkspace.onExternalDragStartedWithItem(dragView);
4583 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004584 }
4585
Winson Chung5ffd51d2015-06-25 11:36:50 -07004586 protected void moveWorkspaceToDefaultScreen() {
4587 mWorkspace.moveToDefaultScreen(false);
4588 }
4589
Winson Chung80baf5a2010-08-09 16:03:15 -07004590 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004591 * Returns a FastBitmapDrawable with the icon, accurately sized.
4592 */
4593 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4594 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4595 d.setFilterBitmap(true);
4596 resizeIconDrawable(d);
4597 return d;
4598 }
4599
4600 /**
4601 * Resizes an icon drawable to the correct icon size.
4602 */
Winson5fbe0742015-09-30 15:33:00 -07004603 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004604 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004605 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004606 }
4607
4608 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004609 * Prints out out state for debugging.
4610 */
4611 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004612 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004613 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004614 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4615 Log.d(TAG, "mRestoring=" + mRestoring);
4616 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004617 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004618 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004619
Daniel Sandler325dc232013-06-05 22:57:57 -04004620 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004621 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004622
4623 @Override
4624 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4625 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004626 // Dump workspace
4627 writer.println(prefix + "Workspace Items");
4628 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4629 writer.println(prefix + " Homescreen " + i);
4630
4631 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4632 for (int j = 0; j < layout.getChildCount(); j++) {
4633 Object tag = layout.getChildAt(j).getTag();
4634 if (tag != null) {
4635 writer.println(prefix + " " + tag.toString());
4636 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004637 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004638 }
Sunny Goyala1365452015-10-01 15:46:24 -07004639
4640 writer.println(prefix + " Hotseat");
4641 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4642 for (int j = 0; j < layout.getChildCount(); j++) {
4643 Object tag = layout.getChildAt(j).getTag();
4644 if (tag != null) {
4645 writer.println(prefix + " " + tag.toString());
4646 }
4647 }
4648
Sunny Goyal713edfc2016-05-06 09:58:34 -07004649 try {
4650 FileLog.flushAll(writer);
4651 } catch (Exception e) {
4652 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004653 }
4654
Adam Cohen9211d422014-10-07 18:14:53 -07004655 if (mLauncherCallbacks != null) {
4656 mLauncherCallbacks.dump(prefix, fd, writer, args);
4657 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004658 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004659
Adam Cohen59400422014-03-05 18:07:04 -08004660 public static CustomAppWidget getCustomAppWidget(String name) {
4661 return sCustomAppWidgets.get(name);
4662 }
4663
4664 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4665 return sCustomAppWidgets;
4666 }
4667
Sunny Goyal29538332016-02-18 09:10:19 -08004668 public static List<View> getFolderContents(View icon) {
4669 if (icon instanceof FolderIcon) {
4670 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4671 } else {
4672 return Collections.EMPTY_LIST;
4673 }
4674 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004675
4676 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4677
4678 @Override
4679 public void onSharedPreferenceChanged(
4680 SharedPreferences sharedPreferences, String key) {
4681 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4682 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4683 if (!waitUntilResume(this, true)) {
4684 run();
4685 }
4686 }
4687 }
4688
4689 @Override
4690 public void run() {
4691 setOrientation();
4692 }
4693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004694}
Michael Jurka2763be32011-02-24 11:19:57 -08004695
Dan Sandlerd5024042014-01-09 15:01:33 -05004696interface DebugIntents {
4697 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4698 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004699}