blob: 6124c895aee26dce2b52cf17a20c561ee729fbe5 [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;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070047import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800115import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700116import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700117import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700118import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800119import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800137import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700153 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700161 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700166 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
167 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
168 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
169
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700170 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
171
Mathew Inwood876a8462013-06-14 14:12:41 +0100172 /**
173 * IntentStarter uses request codes starting with this. This must be greater than all activity
174 * request codes used internally.
175 */
176 protected static final int REQUEST_LAST = 100;
177
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700179 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800180 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Winson Chung2672ff92012-05-04 16:22:30 -0700182 // The Intent extra that defines whether to ignore the launch animation
183 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400184 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700185
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700186 public static final String ACTION_APPWIDGET_HOST_RESET =
187 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
190 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700191 // Type: int
192 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700193 // Type: Content Values / parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
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
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700222 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
223
224 @Override
225 public void onReceive(Context context, Intent intent) {
226 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
227 closeSystemDialogs();
228 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
229 if (mAppWidgetHost != null) {
230 mAppWidgetHost.startListening();
231 }
232 }
233 }
234 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700237 private View mLauncherView;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700238 private PageIndicator mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800240 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700241
242 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Sunny Goyalffe83f12014-08-14 17:39:34 -0700244 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700245 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700247 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800248 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800249 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700250
Michael Jurka0280c3b2010-09-17 15:00:07 -0700251 private int[] mTmpAddItemCellCoordinates = new int[2];
252
Sunny Goyal316490e2015-06-02 09:38:28 -0700253 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700257 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700258
Sunny Goyal47328fd2016-05-25 18:56:41 -0700259 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
261 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700262 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700263 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700265 // Main container view and the model for the widget tray screen.
266 @Thunk WidgetsContainerView mWidgetsView;
267 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700270 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
271 // scroll issues (because the workspace may not have been measured yet) and extra work.
272 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
273 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
275 private SpannableStringBuilder mDefaultKeySsb = null;
276
Adam Cohen091440a2015-03-18 14:16:05 -0700277 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800279 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private boolean mRestoring;
281 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700282 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700286 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700290 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700291 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700292 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700294 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296
Sunny Goyal639e9062015-08-19 19:17:06 -0700297 private LauncherClings mClings;
298
Adam Cohen61f560d2013-09-30 15:58:20 -0700299 private View.OnTouchListener mHapticFeedbackTouchListener;
300
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 // Related to the auto-advancing of widgets
302 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int ADVANCE_INTERVAL = 20000;
304 private static final int ADVANCE_STAGGER = 250;
305
306 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700307 private long mAutoAdvanceSentTime;
308 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700309 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700310
Winson Chung400438b2011-01-16 17:53:48 -0800311 // Determines how long to wait after a rotation before restoring the screen orientation to
312 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700313 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800314
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700316
Adam Cohen1462de32012-07-24 22:34:36 -0700317 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700318 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Winson Chungf0c6ae02012-03-21 16:10:31 -0700324 // Holds the page that we need to animate to, and the icon views that we need to animate up
325 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700326 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700327 private Bitmap mFolderIconBitmap;
328 private Canvas mFolderIconCanvas;
329 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700331 private DeviceProfile mDeviceProfile;
332
Adam Cohen4b66bf32015-09-18 12:15:19 -0700333 private boolean mMoveToDefaultScreenFromNewIntent;
334
Winson Chung13eb5272015-05-11 16:30:13 -0700335 // This is set to the view that launched the activity that navigated the user away from
336 // launcher. Since there is no callback for when the activity has finished launching, enable
337 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 private BubbleTextView mWaitingForResume;
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
341 new HashMap<String, CustomAppWidget>();
342
Adam Cohen59400422014-03-05 18:07:04 -0800343 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700344 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
345 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800346 }
347 }
348
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700351 if (mWorkspace != null) {
352 mWorkspace.buildPageHardwareLayers();
353 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 }
355 };
356
Adam Cohendb364c32014-05-20 14:23:40 -0700357 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358
Adam Cohen091440a2015-03-18 14:16:05 -0700359 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int requestCode;
361 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700362 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700363 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int cellX;
365 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700366 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 }
368
Hyunyoung Songaa953652016-04-19 18:30:24 -0700369 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800370
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700371 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal745bad92016-05-02 10:54:12 -0700383 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 @Override
386 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700387 if (DEBUG_STRICT_MODE) {
388 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
389 .detectDiskReads()
390 .detectDiskWrites()
391 .detectNetwork() // or .detectAll() for all detectable problems
392 .penaltyLog()
393 .build());
394 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
395 .detectLeakedSqlLiteObjects()
396 .detectLeakedClosableObjects()
397 .penaltyLog()
398 .penaltyDeath()
399 .build());
400 }
401
Adam Cohen9211d422014-10-07 18:14:53 -0700402 if (mLauncherCallbacks != null) {
403 mLauncherCallbacks.preOnCreate();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400408 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Adam Cohen2e6da152015-05-06 11:42:25 -0700410 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700411 mDeviceProfile = getResources().getConfiguration().orientation
412 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700413 app.getInvariantDeviceProfile().landscapeProfile
414 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700415
Sunny Goyalf7258242015-10-19 16:59:07 -0700416 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700417 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700420 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700421
Joe Onorato41a12d22009-10-31 18:30:00 -0400422 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700423 mAllAppsController = new AllAppsTransitionController(this);
424 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Sunny Goyalffe83f12014-08-14 17:39:34 -0700426 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
429 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
432 // this also ensures that any synchronous binding below doesn't re-trigger another
433 // LauncherModel load.
434 mPaused = false;
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200437 android.os.Debug.startMethodTracing(
438 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700444 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700445 mExtractedColors = new ExtractedColors();
446 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Tony Wickhame2217252016-03-22 16:34:23 -0700448 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
449 .addAccessibilityStateChangeListener(this);
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700477 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
478 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700485 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
486 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700487 }
488
489 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
490 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400491 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 showFirstRunClings();
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
504
Sunny Goyal7779d622015-06-11 16:18:39 -0700505 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700506 public void onExtractedColorsChanged() {
507 loadExtractedColorsAndColorItems();
508 }
509
510 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 // TODO: do this in pre-N as well, once the extraction part is complete.
512 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700513 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700514 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700515 if (mPageIndicator != null) {
516 mPageIndicator.updateColor(mExtractedColors);
517 }
Tony Wickham827cef22016-03-17 15:39:39 -0700518 }
519 }
520
Adam Cohen9211d422014-10-07 18:14:53 -0700521 private LauncherCallbacks mLauncherCallbacks;
522
523 public void onPostCreate(Bundle savedInstanceState) {
524 super.onPostCreate(savedInstanceState);
525 if (mLauncherCallbacks != null) {
526 mLauncherCallbacks.onPostCreate(savedInstanceState);
527 }
528 }
529
Sunny Goyal32554d12015-12-03 15:31:25 -0800530 /**
531 * Call this after onCreate to set or clear overlay.
532 */
533 public void setLauncherOverlay(LauncherOverlay overlay) {
534 if (overlay != null) {
535 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
536 }
537 mWorkspace.setLauncherOverlay(overlay);
538 }
539
Adam Cohen9211d422014-10-07 18:14:53 -0700540 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
541 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 private boolean mWorkspaceImportanceStored = false;
544 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800546 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
548
549 @Override
550 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 return;
553 }
554 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700555 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 if (mWorkspace != null) {
557 mWorkspaceImportanceForAccessibility =
558 mWorkspace.getImportantForAccessibility();
559 mWorkspace.setImportantForAccessibility(
560 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
561 mWorkspaceImportanceStored = true;
562 }
563 if (mHotseat != null) {
564 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
565 mHotseat.setImportantForAccessibility(
566 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
567 mHotseatImportanceStored = true;
568 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700569 }
570
571 @Override
572 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700573 if (mWorkspaceImportanceStored && mWorkspace != null) {
574 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
575 }
576 if (mHotseatImportanceStored && mHotseat != null) {
577 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
578 }
579 mWorkspaceImportanceStored = false;
580 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700581 }
582 });
Adam Cohen9211d422014-10-07 18:14:53 -0700583 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700584 }
585
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700586 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700587 public void onLauncherProviderChange() {
588 if (mLauncherCallbacks != null) {
589 mLauncherCallbacks.onLauncherProviderChange();
590 }
591 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700592
Adam Cohen9211d422014-10-07 18:14:53 -0700593 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700595 if (mLauncherCallbacks != null) {
596 return mLauncherCallbacks.hasCustomContentToLeft();
597 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700598 return false;
599 }
600
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 * {@link #addToCustomContentPage}. This will only be invoked if
604 * {@link #hasCustomContentToLeft()} is {@code true}.
605 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500606 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700607 if (mLauncherCallbacks != null) {
608 mLauncherCallbacks.populateCustomContentContainer();
609 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 }
611
612 /**
613 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
614 * ensure the custom content page is added or removed if necessary.
615 */
616 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600617 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 // Not bound yet, wait for bindScreens to be called.
619 return;
620 }
621
622 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
623 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500624 mWorkspace.createCustomContentContainer();
625 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
627 mWorkspace.removeCustomContentPage();
628 }
629 }
630
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800631 /**
632 * Logger object is a singleton and does not have to be coupled with the foreground activity.
633 * Since most user event logging is done on the UI, the object is retrieved from the
634 * callback for convenience.
635 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700636 private UserEventDispatcher createUserEventDispatcher() {
637 return new UserEventDispatcher() {
638 @Override
639 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
640 if (!DEBUG_LOGGING) {
641 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800642 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 Log.d("UserEvent", String.format(Locale.US,
644 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
645 LoggerUtils.getActionStr(ev.action),
646 LoggerUtils.getTargetStr(ev.srcTarget[0]),
647 LoggerUtils.getTargetStr(ev.srcTarget[1]),
648 ev.elapsedContainerMillis,
649 ev.elapsedSessionMillis));
650 }
651 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800652 }
653
Hyunyoung Songaa953652016-04-19 18:30:24 -0700654 public UserEventDispatcher getUserEventDispatcher() {
655 if (mLauncherCallbacks != null) {
656 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
657 if (dispatcher != null) {
658 return dispatcher;
659 }
660 }
661
662 if (mUserEventDispatcher == null) {
663 mUserEventDispatcher = createUserEventDispatcher();
664 }
665 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800666 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100667
Hyunyoung Song3f471442015-04-08 19:01:34 -0700668 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700669 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
670 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700671 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700672 }
673
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000674 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700675 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
676 // This cast is safe as long as the id < 0x00FFFFFF
677 // Since we jail all the dynamically generated views, there should be no clashes
678 // with any other views.
679 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 }
681
682 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700683 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
684 * a configuration step, this allows the proper animations to run after other transitions.
685 */
Adam Cohendb364c32014-05-20 14:23:40 -0700686 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700687 long screenId = args.screenId;
688 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
689 // When the screen id represents an actual screen (as opposed to a rank) we make sure
690 // that the drop page actually exists.
691 screenId = ensurePendingDropLayoutExists(args.screenId);
692 }
Adam Cohendb364c32014-05-20 14:23:40 -0700693
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700696 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700697 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700698 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700700 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700701 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700702 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700703 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700704 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700705 case REQUEST_BIND_PENDING_APPWIDGET: {
706 int widgetId = args.appWidgetId;
707 LauncherAppWidgetInfo info =
708 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
709 if (info != null) {
710 // Since the view was just bound, also launch the configure activity if needed
711 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
712 .getLauncherAppWidgetInfo(widgetId);
713 if (provider != null && provider.configure != null) {
714 startRestoredWidgetReconfigActivity(provider, info);
715 }
716 }
717 break;
718 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 }
Michael Jurka27614d22012-04-02 06:40:22 -0700720 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
721 // if you turned the screen off and then back while in All Apps, Launcher would not
722 // return to the workspace. Clearing mAddInfo.container here fixes this issue
723 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700724 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 }
726
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800727 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700729 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700730 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700731 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800732 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700733
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 Runnable exitSpringLoaded = new Runnable() {
735 @Override
736 public void run() {
737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
738 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
739 }
740 };
741
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700743 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
746 if (resultCode == RESULT_CANCELED) {
747 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700748 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800751 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 }
754 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
756 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700757 // User could have free-scrolled between pages before picking a wallpaper; make sure
758 // we move to the closest one now.
759 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700760 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200761 }
762 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200764
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700766 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700767
Adam Cohendb364c32014-05-20 14:23:40 -0700768 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800769 // We have special handling for widgets
770 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800771 final int appWidgetId;
772 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
773 : -1;
774 if (widgetId < 0) {
775 appWidgetId = pendingAddWidgetId;
776 } else {
777 appWidgetId = widgetId;
778 }
779
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800781 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700782 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
783 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 result = RESULT_CANCELED;
785 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 @Override
788 public void run() {
789 exitSpringLoadedDragModeDelayed(false, 0, null);
790 }
791 };
Adam Cohendb364c32014-05-20 14:23:40 -0700792 if (workspaceLocked) {
793 // No need to remove the empty screen if we're mid-binding, as the
794 // the bind will not add the empty screen.
795 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
796 } else {
797 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
799 }
Winson Chung5aaab772012-04-27 15:24:02 -0700800 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700801 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700802 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
803 // When the screen id represents an actual screen (as opposed to a rank)
804 // we make sure that the drop page actually exists.
805 mPendingAddInfo.screenId =
806 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
807 }
Adam Cohendb364c32014-05-20 14:23:40 -0700808 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
809
810 dropLayout.setDropPending(true);
811 final Runnable onComplete = new Runnable() {
812 @Override
813 public void run() {
814 completeTwoStageWidgetDrop(resultCode, appWidgetId);
815 dropLayout.setDropPending(false);
816 }
817 };
818 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
819 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
820 } else {
821 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
822 mPendingAddInfo);
823 sPendingAddItem = args;
824 }
Winson Chung5aaab772012-04-27 15:24:02 -0700825 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800826 return;
827 }
828
Sunny Goyald478c832016-04-01 12:04:16 -0700829 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
830 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700831 if (resultCode == RESULT_OK) {
832 // Update the widget view.
833 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
834 pendingAddWidgetId, mPendingAddInfo);
835 if (workspaceLocked) {
836 sPendingAddItem = args;
837 } else {
838 completeAdd(args);
839 }
840 }
841 // Leave the widget in the pending state if the user canceled the configure.
842 return;
843 }
844
Sunny Goyalaad90582015-07-09 14:22:50 -0700845 if (requestCode == REQUEST_CREATE_SHORTCUT) {
846 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
847 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
848 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
849 mPendingAddInfo);
850 if (isWorkspaceLocked()) {
851 sPendingAddItem = args;
852 } else {
853 completeAdd(args);
854 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
855 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
856 }
857 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700858 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800863
864 }
865
866 @Override
867 protected void onActivityResult(
868 final int requestCode, final int resultCode, final Intent data) {
869 handleActivityResult(requestCode, resultCode, data);
870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 }
874
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600875 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700876 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600877 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700878 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
879 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
880 View v = null;
881 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
882 if (layout != null) {
883 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
884 }
885 Intent intent = sPendingAddItem.intent;
886 sPendingAddItem = null;
887 if (grantResults.length > 0
888 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700889 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700890 } else {
891 // TODO: Show a snack bar with link to settings
892 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
893 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
894 }
895 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600896 if (mLauncherCallbacks != null) {
897 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
898 grantResults);
899 }
900 }
901
Adam Cohendb364c32014-05-20 14:23:40 -0700902 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
903 appWidgetId, ItemInfo info) {
904 PendingAddArguments args = new PendingAddArguments();
905 args.requestCode = requestCode;
906 args.intent = data;
907 args.container = info.container;
908 args.screenId = info.screenId;
909 args.cellX = info.cellX;
910 args.cellY = info.cellY;
911 args.appWidgetId = appWidgetId;
912 return args;
913 }
914
915 /**
916 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
917 *
918 * @param screenId the screen id to check
919 * @return the new screen, or screenId if it exists
920 */
921 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800922 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700923 if (dropLayout == null) {
924 // it's possible that the add screen was removed because it was
925 // empty and a re-bind occurred
926 mWorkspace.addExtraEmptyScreen();
927 return mWorkspace.commitExtraEmptyScreen();
928 } else {
929 return screenId;
930 }
931 }
932
Adam Cohen091440a2015-03-18 14:16:05 -0700933 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800934 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 Runnable onCompleteRunnable = null;
936 int animationType = 0;
937
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700938 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 if (resultCode == RESULT_OK) {
940 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
941 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 mPendingAddWidgetInfo);
943 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 onCompleteRunnable = new Runnable() {
945 @Override
946 public void run() {
947 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700948 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
950 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 }
952 };
953 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800954 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800956 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 if (mDragLayer.getAnimatedView() != null) {
958 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
959 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
960 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700961 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700962 // The animated view may be null in the case of a rotation during widget configuration
963 onCompleteRunnable.run();
964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
967 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 protected void onStop() {
969 super.onStop();
970 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.onStop();
974 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700975 }
976
977 @Override
978 protected void onStart() {
979 super.onStart();
980 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700981
982 if (mLauncherCallbacks != null) {
983 mLauncherCallbacks.onStart();
984 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700985 }
986
987 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200989 long startTime = 0;
990 if (DEBUG_RESUME_TIME) {
991 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400992 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200993 }
Adam Cohen9211d422014-10-07 18:14:53 -0700994
995 if (mLauncherCallbacks != null) {
996 mLauncherCallbacks.preOnResume();
997 }
998
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -07001000 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001001
Winson Chung4a2afa32012-07-19 14:53:05 -07001002 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001003 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001004 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001005 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001006 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001007 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001008 // view after launching an app, as they may be depending on the UI to be static to
1009 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001010 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001011 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001012 } else if (mOnResumeState == State.WIDGETS) {
1013 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001014 }
1015 mOnResumeState = State.NONE;
1016
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001017 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001018 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1019 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001020
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001021 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001023 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001024 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001026 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001028 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001029 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1030 // execute them here
1031 long startTimeCallbacks = 0;
1032 if (DEBUG_RESUME_TIME) {
1033 startTimeCallbacks = System.currentTimeMillis();
1034 }
1035
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1037 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001038 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001039 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001040 if (DEBUG_RESUME_TIME) {
1041 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1042 (System.currentTimeMillis() - startTimeCallbacks));
1043 }
Michael Jurka447bf842013-05-15 14:52:15 +02001044 }
Michael Jurka54554252013-08-01 12:52:23 +02001045 if (mOnResumeCallbacks.size() > 0) {
1046 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1047 mOnResumeCallbacks.get(i).run();
1048 }
1049 mOnResumeCallbacks.clear();
1050 }
Winson Chunge4e50662012-01-23 14:45:13 -08001051
1052 // Reset the pressed state of icons that were locked in the press state while activities
1053 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001054 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001055 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001056 mWaitingForResume.setStayPressed(false);
1057 }
Winson Chung82963d52013-10-09 11:20:57 -07001058
Adam Cohen06dff352012-06-01 17:17:08 -07001059 // It is possible that widgets can receive updates while launcher is not in the foreground.
1060 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1061 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1062 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001063 if (!isWorkspaceLoading()) {
1064 getWorkspace().reinflateWidgetsIfNecessary();
1065 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001066
Michael Jurka447bf842013-05-15 14:52:15 +02001067 if (DEBUG_RESUME_TIME) {
1068 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1069 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001070
Adam Cohen4b66bf32015-09-18 12:15:19 -07001071 // We want to suppress callbacks about CustomContent being shown if we have just received
1072 // onNewIntent while the user was present within launcher. In that case, we post a call
1073 // to move the user to the main screen (which will occur after onResume). We don't want to
1074 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1075 // suppress here.
1076 if (mWorkspace.getCustomContentCallbacks() != null
1077 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001078 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001079 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001080 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1081 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001082 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001083 }
1084 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001085 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001086 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001087 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001088
Sunny Goyal756adbc2015-04-16 15:20:51 -07001089 if (!isWorkspaceLoading()) {
1090 // Process any items that were added while Launcher was away.
1091 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1092 }
Adam Cohen9211d422014-10-07 18:14:53 -07001093
1094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onResume();
1096 }
Adam Cohen06dff352012-06-01 17:17:08 -07001097 }
1098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001101 // Ensure that items added to Launcher are queued until Launcher returns
1102 InstallShortcutReceiver.enableInstallQueue();
1103
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001105 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001106 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001107 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001108
1109 // We call onHide() aggressively. The custom content callbacks should be able to
1110 // debounce excess onHide calls.
1111 if (mWorkspace.getCustomContentCallbacks() != null) {
1112 mWorkspace.getCustomContentCallbacks().onHide();
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Adam Cohen9211d422014-10-07 18:14:53 -07001115 if (mLauncherCallbacks != null) {
1116 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 }
1119
1120 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001121 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1122 // by a onResume or by scrolling otherwise.
1123 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001124
1125 // Custom content is completely hidden
1126 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001127
1128 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1129 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001130
1131 // Indicates whether the user is allowed to scroll away from the custom content.
1132 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001133 }
1134
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 public interface LauncherOverlay {
1136
1137 /**
1138 * Touch interaction leading to overscroll has begun
1139 */
1140 public void onScrollInteractionBegin();
1141
1142 /**
1143 * Touch interaction related to overscroll has ended
1144 */
1145 public void onScrollInteractionEnd();
1146
1147 /**
1148 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1149 * screen (or in the case of RTL, the rightmost screen).
1150 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152
1153 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001154 * Called when the launcher is ready to use the overlay
1155 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001157 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 }
1159
Jun Mukai8af0cd82015-05-12 12:32:12 -07001160 public interface LauncherSearchCallbacks {
1161 /**
1162 * Called when the search overlay is shown.
1163 */
1164 public void onSearchOverlayOpened();
1165
1166 /**
1167 * Called when the search overlay is dismissed.
1168 */
1169 public void onSearchOverlayClosed();
1170 }
1171
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001173 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001174 }
1175
1176 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1177
Sunny Goyalc86df472016-02-25 09:19:38 -08001178 public void onScrollChanged(float progress) {
1179 if (mWorkspace != null) {
1180 mWorkspace.onOverlayScrollChanged(progress);
1181 }
1182 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001183 }
1184
Jorim Jaggid017f882014-01-14 17:08:48 -08001185 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001186 if (mLauncherCallbacks != null) {
1187 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001188 } else {
1189 // On devices with a locked orientation, we will at least have the allow rotation
1190 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001191 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001192 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001193 }
1194
Adam Cohen9211d422014-10-07 18:14:53 -07001195 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001196 CustomContentCallbacks callbacks, String description) {
1197 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001198 }
1199
Adam Cohenedb40762013-07-18 16:45:45 -07001200 // The custom content needs to offset its content to account for the QSB
1201 public int getTopOffsetForCustomContent() {
1202 return mWorkspace.getPaddingTop();
1203 }
1204
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 @Override
1206 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001208 if (mModel.isCurrentCallbacks(this)) {
1209 mModel.stopLoader();
1210 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001211 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1212
Romain Guy13c2e7b2010-03-10 19:45:00 -08001213 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001214 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001215
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 @Override
1218 public void onWindowFocusChanged(boolean hasFocus) {
1219 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001220 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001221
1222 if (mLauncherCallbacks != null) {
1223 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1224 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 private boolean acceptFilter() {
1228 final InputMethodManager inputManager = (InputMethodManager)
1229 getSystemService(Context.INPUT_METHOD_SERVICE);
1230 return !inputManager.isFullscreenMode();
1231 }
1232
1233 @Override
1234 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001235 final int uniChar = event.getUnicodeChar();
1236 final boolean handled = super.onKeyDown(keyCode, event);
1237 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1238 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1240 keyCode, event);
1241 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001243 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001244 // showSearchDialog()
1245 // If there are multiple keystrokes before the search dialog takes focus,
1246 // onSearchRequested() will be called for every keystroke,
1247 // but it is idempotent, so it's fine.
1248 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250 }
1251
Joe Onorato8a9625e2010-01-28 15:55:35 -08001252 // Eat the long press event so the keyboard doesn't come up.
1253 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1254 return true;
1255 }
1256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 return handled;
1258 }
1259
Winson46163472015-09-22 17:31:56 -07001260 @Override
1261 public boolean onKeyUp(int keyCode, KeyEvent event) {
1262 if (keyCode == KeyEvent.KEYCODE_MENU) {
1263 // Ignore the menu key if we are currently dragging or are on the custom content screen
1264 if (!isOnCustomContent() && !mDragController.isDragging()) {
1265 // Close any open folders
1266 closeFolder();
1267
1268 // Stop resizing any widgets
1269 mWorkspace.exitWidgetResizeMode();
1270
1271 // Show the overview mode if we are on the workspace
1272 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1273 !mWorkspace.isSwitchingState()) {
1274 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001275 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001276 }
1277 }
1278 return true;
1279 }
1280 return super.onKeyUp(keyCode, event);
1281 }
1282
Karl Rosaen138a0412009-04-23 19:00:21 -07001283 private String getTypedText() {
1284 return mDefaultKeySsb.toString();
1285 }
1286
1287 private void clearTypedText() {
1288 mDefaultKeySsb.clear();
1289 mDefaultKeySsb.clearSpans();
1290 Selection.setSelection(mDefaultKeySsb, 0);
1291 }
1292
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001294 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1295 * State
1296 */
1297 private static State intToState(int stateOrdinal) {
1298 State state = State.WORKSPACE;
1299 final State[] stateValues = State.values();
1300 for (int i = 0; i < stateValues.length; i++) {
1301 if (stateValues[i].ordinal() == stateOrdinal) {
1302 state = stateValues[i];
1303 break;
1304 }
1305 }
1306 return state;
1307 }
1308
1309 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 * Restores the previous state, if it exists.
1311 *
1312 * @param savedState The previous state.
1313 */
1314 private void restoreState(Bundle savedState) {
1315 if (savedState == null) {
1316 return;
1317 }
1318
Michael Jurka883f55b2010-10-21 15:47:14 -07001319 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001320 if (state == State.APPS || state == State.WIDGETS) {
1321 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001322 }
1323
Adam Cohen21cd0022013-10-09 18:57:02 -07001324 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1325 PagedView.INVALID_RESTORE_PAGE);
1326 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001327 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
Sunny Goyal756cd262015-08-20 12:33:21 -07001330 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1331 if (itemValues != null) {
1332 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001333 AppWidgetProviderInfo info = savedState.getParcelable(
1334 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001335 mPendingAddWidgetInfo = info == null ?
1336 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1337
Adam Cohen4637b5a2013-11-04 18:21:24 -08001338 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001339 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 mRestoring = true;
1341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 /**
1345 * Finds all the views we need and configure them properly.
1346 */
1347 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001349 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001350 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1351
Winson Chung4c98d922011-05-31 16:50:48 -07001352 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001353 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001354
Sunny Goyal784f9c32016-03-23 16:56:54 -07001355 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1356 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1357 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001358 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359
Winson Chung4c98d922011-05-31 16:50:48 -07001360 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001361
1362 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001363
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 // Setup the hotseat
1365 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1366 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001367 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 }
1369
Winsone9f27272015-10-13 10:47:51 -07001370 // Setup the overview panel
1371 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001372
Winson Chung4c98d922011-05-31 16:50:48 -07001373 // Setup the workspace
1374 mWorkspace.setHapticFeedbackEnabled(false);
1375 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001376 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001377 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001378 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001379
Tony Wickham34d2c912015-09-11 09:27:58 -07001380 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001381 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001382
Winson Chungef7f8742015-06-04 17:18:17 -07001383 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001384 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001385 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001386 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1387 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1388 } else {
1389 mAppsView.setSearchBarController(new DefaultAppSearchController());
1390 }
Craig Mautner360310b2012-10-26 15:13:08 -07001391
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001393 mDragController.setDragScoller(mWorkspace);
1394 mDragController.setScrollView(mDragLayer);
1395 mDragController.setMoveTarget(mWorkspace);
1396 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001397 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001398
Sunny Goyal322d5562015-06-25 19:35:49 -07001399 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1400 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
Winsone9f27272015-10-13 10:47:51 -07001404 private void setupOverviewPanel() {
1405 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1406
1407 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1408 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1409 @Override
1410 public boolean onLongClick(View v) {
1411 return v.performClick();
1412 }
1413 };
1414
1415 // Bind wallpaper button actions
1416 View wallpaperButton = findViewById(R.id.wallpaper_button);
1417 wallpaperButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickWallpaperPicker(view);
1422 }
1423 }
1424 });
1425 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1426 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind widget button actions
1429 mWidgetsButton = findViewById(R.id.widget_button);
1430 mWidgetsButton.setOnClickListener(new OnClickListener() {
1431 @Override
1432 public void onClick(View view) {
1433 if (!mWorkspace.isSwitchingState()) {
1434 onClickAddWidgetButton(view);
1435 }
1436 }
1437 });
1438 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1439 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440
1441 // Bind settings actions
1442 View settingsButton = findViewById(R.id.settings_button);
1443 boolean hasSettings = hasSettings();
1444 if (hasSettings) {
1445 settingsButton.setOnClickListener(new OnClickListener() {
1446 @Override
1447 public void onClick(View view) {
1448 if (!mWorkspace.isSwitchingState()) {
1449 onClickSettingsButton(view);
1450 }
1451 }
1452 });
1453 settingsButton.setOnLongClickListener(performClickOnLongClick);
1454 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1455 } else {
1456 settingsButton.setVisibility(View.GONE);
1457 }
1458
1459 mOverviewPanel.setAlpha(0f);
1460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001463 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001464 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 */
1466 public void setAllAppsButton(View allAppsButton) {
1467 mAllAppsButton = allAppsButton;
1468 }
1469
Sunny Goyalbb011da2016-06-15 15:42:29 -07001470 public View getStartViewForAllAppsRevealAnimation() {
1471 return FeatureFlags.NO_ALL_APPS_ICON ? mPageIndicator : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001472 }
1473
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001474 public View getWidgetsButton() {
1475 return mWidgetsButton;
1476 }
1477
Anjali Koppal5ad44842014-03-10 20:34:39 -07001478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Creates a view representing a shortcut.
1480 *
1481 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001483 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 /**
1488 * Creates a view representing a shortcut inflated from the specified resource.
1489 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * @param parent The group the shortcut belongs to.
1491 * @param info The data structure describing the shortcut.
1492 *
1493 * @return A View inflated from layoutResId.
1494 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001495 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001496 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001497 parent, false);
1498 favorite.applyFromShortcutInfo(info, mIconCache);
1499 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001501 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 return favorite;
1503 }
1504
1505 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 * Add a shortcut to the workspace.
1507 *
1508 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 int cellY) {
1512 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001514
Sunny Goyal5c97f512015-05-19 16:03:28 -07001515 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001516 if (info == null) {
1517 return;
1518 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001519 final View view = createShortcut(info);
1520
Sunny Goyal5c97f512015-05-19 16:03:28 -07001521 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001522 // First we check if we already know the exact location where we want to add this item.
1523 if (cellX >= 0 && cellY >= 0) {
1524 cellXY[0] = cellX;
1525 cellXY[1] = cellY;
1526 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001527
1528 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001529 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001530 true, null,null)) {
1531 return;
1532 }
1533 DragObject dragObject = new DragObject();
1534 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001535 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1536 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001537 return;
1538 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001539 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 }
1542
1543 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001544 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 return;
1546 }
1547
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001548 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
1555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001557 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001559 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 */
Adam Cohen091440a2015-03-18 14:16:05 -07001561 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001562 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1563
1564 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001566 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 }
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Adam Cohen59400422014-03-05 18:07:04 -08001569 if (appWidgetInfo.isCustomWidget) {
1570 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001571 }
1572
Adam Cohen59400422014-03-05 18:07:04 -08001573 LauncherAppWidgetInfo launcherInfo;
1574 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1575 launcherInfo.spanX = info.spanX;
1576 launcherInfo.spanY = info.spanY;
1577 launcherInfo.minSpanX = info.minSpanX;
1578 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001579 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001582 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583
1584 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001585 if (hostView == null) {
1586 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001587 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001589 hostView.setVisibility(View.VISIBLE);
1590 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001592 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Romain Guycbb89e42009-06-08 15:52:54 -07001594
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 private void addAppWidgetToWorkspace(
1596 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 hostView.setTag(item);
1599 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001600
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001601 hostView.setFocusable(true);
1602 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001603
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1606
1607 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001608 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001609 }
1610 }
1611
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1613 @Override
1614 public void onReceive(Context context, Intent intent) {
1615 final String action = intent.getAction();
1616 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1617 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001618 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001619 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001620
Winson Chungc100e8e2011-08-09 16:02:43 -07001621 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001622 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001623 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001624 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001625 if (!showWorkspace(false)) {
1626 // If we are already on the workspace, then manually reset all apps
1627 mAppsView.reset();
1628 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001629 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1631 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001632 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 }
1634 }
1635 };
1636
1637 @Override
1638 public void onAttachedToWindow() {
1639 super.onAttachedToWindow();
1640
1641 // Listen for broadcasts related to user-presence
1642 final IntentFilter filter = new IntentFilter();
1643 filter.addAction(Intent.ACTION_SCREEN_OFF);
1644 filter.addAction(Intent.ACTION_USER_PRESENT);
1645 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001646 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001647 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001649
1650 if (mLauncherCallbacks != null) {
1651 mLauncherCallbacks.onAttachedToWindow();
1652 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 }
1654
1655 @Override
1656 public void onDetachedFromWindow() {
1657 super.onDetachedFromWindow();
1658 mVisible = false;
1659
Adam Cohend113e0c2010-11-11 10:48:05 -08001660 if (mAttached) {
1661 unregisterReceiver(mReceiver);
1662 mAttached = false;
1663 }
Winson Chungb745afb2015-03-02 11:51:23 -08001664 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001665
1666 if (mLauncherCallbacks != null) {
1667 mLauncherCallbacks.onDetachedFromWindow();
1668 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 }
1670
1671 public void onWindowVisibilityChanged(int visibility) {
1672 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001673 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 // The following code used to be in onResume, but it turns out onResume is called when
1675 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1676 // is a more appropriate event to handle
1677 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 if (!mWorkspaceLoading) {
1679 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001680 // We want to let Launcher draw itself at least once before we force it to build
1681 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 // apps is nice and speedy.
1683 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001685 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 if (mStarted) return;
1687 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001688 // We delay the layer building a bit in order to give
1689 // other message processing a time to run. In particular
1690 // this avoids a delay in hiding the IME if it was
1691 // currently shown, because doing that may involve
1692 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001693 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001694 final ViewTreeObserver.OnDrawListener listener = this;
1695 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001696 public void run() {
1697 if (mWorkspace != null &&
1698 mWorkspace.getViewTreeObserver() != null) {
1699 mWorkspace.getViewTreeObserver().
1700 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001702 }
1703 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001704 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001705 }
1706 });
1707 }
1708 clearTypedText();
1709 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 }
1711
Adam Cohen091440a2015-03-18 14:16:05 -07001712 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 mHandler.removeMessages(ADVANCE_MSG);
1714 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1715 mHandler.sendMessageDelayed(msg, delay);
1716 mAutoAdvanceSentTime = System.currentTimeMillis();
1717 }
1718
Adam Cohen091440a2015-03-18 14:16:05 -07001719 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1721 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1722 mAutoAdvanceRunning = autoAdvanceRunning;
1723 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001724 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 sendAdvanceMessage(delay);
1726 } else {
1727 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1730 }
1731 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001732 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734 }
1735 }
1736
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1738
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 if (msg.what == ADVANCE_MSG) {
1742 int i = 0;
1743 for (View key: mWidgetsToAdvance.keySet()) {
1744 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001745 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 public void run() {
1749 ((Advanceable) v).advance();
1750 }
1751 }, delay);
1752 }
1753 i++;
1754 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001755 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001760
Winsonc0b52fe2015-09-09 16:38:15 -07001761 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001762 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1764 if (v instanceof Advanceable) {
1765 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001766 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001767 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769 }
1770
Winsonc0b52fe2015-09-09 16:38:15 -07001771 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 if (mWidgetsToAdvance.containsKey(hostView)) {
1773 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001774 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 }
1777
Hyunyoung Song3f471442015-04-08 19:01:34 -07001778 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001779 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1780 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 }
1782
Winson Chunga6945242014-01-08 14:04:34 -08001783 public DragLayer getDragLayer() {
1784 return mDragLayer;
1785 }
1786
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001787 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001788 return mAppsView;
1789 }
1790
Hyunyoung Song3f471442015-04-08 19:01:34 -07001791 public WidgetsContainerView getWidgetsView() {
1792 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public Workspace getWorkspace() {
1796 return mWorkspace;
1797 }
1798
1799 public Hotseat getHotseat() {
1800 return mHotseat;
1801 }
1802
Jorim Jaggid017f882014-01-14 17:08:48 -08001803 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001804 return mOverviewPanel;
1805 }
1806
Sunny Goyal47328fd2016-05-25 18:56:41 -07001807 public DropTargetBar getDropTargetBar() {
1808 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001809 }
1810
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001811 public LauncherAppWidgetHost getAppWidgetHost() {
1812 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Winson Chunga9abd0e2010-10-27 17:18:37 -07001815 public LauncherModel getModel() {
1816 return mModel;
1817 }
1818
Adam Cohen79d90c52016-04-22 13:29:20 -07001819 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mSharedPrefs;
1821 }
1822
Adam Cohen2e6da152015-05-06 11:42:25 -07001823 public DeviceProfile getDeviceProfile() {
1824 return mDeviceProfile;
1825 }
1826
Bjorn Bringertc459e522013-06-07 19:36:01 +01001827 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001828 getWindow().closeAllPanels();
1829
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001830 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001831 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001832 }
1833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 @Override
1835 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001836 long startTime = 0;
1837 if (DEBUG_RESUME_TIME) {
1838 startTime = System.currentTimeMillis();
1839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 super.onNewIntent(intent);
1841
1842 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001843 Folder openFolder = mWorkspace.getOpenFolder();
1844 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1845 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1846 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1847 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1848 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001849 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001850 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001851
Adam Cohen6fecd412013-10-02 17:41:50 -07001852 if (mWorkspace == null) {
1853 // Can be cases where mWorkspace is null, this prevents a NPE
1854 return;
1855 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 // In all these cases, only animate if we're already on home
1857 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001858
Sunny Goyal935fca12015-10-13 10:19:01 -07001859 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 exitSpringLoadedDragMode();
1861
1862 // If we are already on home, then just animate back to the workspace,
1863 // otherwise, just wait until onResume to set the state back to Workspace
1864 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001865 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 } else {
1867 mOnResumeState = State.WORKSPACE;
1868 }
1869
1870 final View v = getWindow().peekDecorView();
1871 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001872 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 INPUT_METHOD_SERVICE);
1874 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1875 }
1876
Winson Chungb745afb2015-03-02 11:51:23 -08001877 // Reset the apps view
1878 if (!alreadyOnHome && mAppsView != null) {
1879 mAppsView.scrollToTop();
1880 }
1881
Hyunyoung Song3f471442015-04-08 19:01:34 -07001882 // Reset the widgets view
1883 if (!alreadyOnHome && mWidgetsView != null) {
1884 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001886
Adam Cohen9211d422014-10-07 18:14:53 -07001887 if (mLauncherCallbacks != null) {
1888 mLauncherCallbacks.onHomeIntent();
1889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
Adam Cohened307df2013-10-02 09:37:31 -07001891
Adam Cohen9211d422014-10-07 18:14:53 -07001892 if (mLauncherCallbacks != null) {
1893 mLauncherCallbacks.onNewIntent(intent);
1894 }
Winson15f8b172015-08-19 11:02:39 -07001895
1896 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1897 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1898 // animation.
1899 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001900 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1901 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001902 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1903 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001904
1905 // We use this flag to suppress noisy callbacks above custom content state
1906 // from onResume.
1907 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001908 mWorkspace.post(new Runnable() {
1909 @Override
1910 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001911 if (mWorkspace != null) {
1912 mWorkspace.moveToDefaultScreen(true);
1913 }
Winson15f8b172015-08-19 11:02:39 -07001914 }
1915 });
1916 }
1917 }
1918
1919 if (DEBUG_RESUME_TIME) {
1920 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1921 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001925 public void onRestoreInstanceState(Bundle state) {
1926 super.onRestoreInstanceState(state);
1927 for (int page: mSynchronouslyBoundPages) {
1928 mWorkspace.restoreInstanceStateForChild(page);
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
1932 @Override
1933 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001934 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001935 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1936 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001937
Adam Cohen21cd0022013-10-09 18:57:02 -07001938 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001939 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
Michael Jurka883f55b2010-10-21 15:47:14 -07001941 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001942 // We close any open folder since it will not be re-opened, and we need to make sure
1943 // this state is reflected.
1944 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1945 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001948 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001949 ContentValues itemValues = new ContentValues();
1950 mPendingAddInfo.writeToValues(itemValues);
1951 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001952 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001953 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
Adam Cohen9211d422014-10-07 18:14:53 -07001956 if (mLauncherCallbacks != null) {
1957 mLauncherCallbacks.onSaveInstanceState(outState);
1958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
1961 @Override
1962 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001964
Winson Chunge7a03942011-08-05 15:05:12 -07001965 // Remove all pending runnables
1966 mHandler.removeMessages(ADVANCE_MSG);
1967 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001968 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001969 mWorkspace.removeFolderListeners();
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
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001997 unregisterReceiver(mUiBroadcastReceiver);
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
Sunny Goyalae502842016-06-17 08:43:56 -07002006 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2007 return mAccessibilityDelegate;
2008 }
2009
Adam Cohena9cf38f2011-05-02 15:36:58 -07002010 public DragController getDragController() {
2011 return mDragController;
2012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 @Override
2015 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002016 onStartForResult(requestCode);
2017 super.startActivityForResult(intent, requestCode);
2018 }
2019
2020 @Override
2021 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2022 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2023 onStartForResult(requestCode);
2024 try {
2025 super.startIntentSenderForResult(intent, requestCode,
2026 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2027 } catch (IntentSender.SendIntentException e) {
2028 throw new ActivityNotFoundException();
2029 }
2030 }
2031
2032 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002033 if (requestCode >= 0) {
2034 setWaitingForResult(true);
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
Winson Chung70d72102011-08-12 11:24:35 -07002038 /**
2039 * Indicates that we want global search for this activity by setting the globalSearch
2040 * argument for {@link #startSearch} to true.
2041 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002043 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002045
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 if (initialQuery == null) {
2047 // Use any text typed in the launcher as the initial query
2048 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 if (appSearchData == null) {
2051 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002052 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002054
2055 // TODO send proper bounds.
2056 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002057
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 if (clearTextImmediately) {
2061 clearTypedText();
2062 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002063
2064 // We need to show the workspace after starting the search
2065 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 }
2067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 /**
2069 * Start a text search.
2070 *
2071 * @return {@code true} if the search will start immediately, so any further keypresses
2072 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2073 * to buffer keypresses.
2074 */
2075 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002077 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2078 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2079 sourceBounds);
2080 }
2081
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002083 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002084 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 }
2086
2087 /**
2088 * Starts the global search activity. This code is a copied from SearchManager
2089 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002092 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2094 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2095 if (globalSearchActivity == null) {
2096 Log.w(TAG, "No global search activity found.");
2097 return;
2098 }
2099 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2100 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2101 intent.setComponent(globalSearchActivity);
2102 // Make sure that we have a Bundle to put source in
2103 if (appSearchData == null) {
2104 appSearchData = new Bundle();
2105 } else {
2106 appSearchData = new Bundle(appSearchData);
2107 }
Adam Cohen9211d422014-10-07 18:14:53 -07002108 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 if (!appSearchData.containsKey("source")) {
2110 appSearchData.putString("source", getPackageName());
2111 }
2112 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2113 if (!TextUtils.isEmpty(initialQuery)) {
2114 intent.putExtra(SearchManager.QUERY, initialQuery);
2115 }
2116 if (selectInitialQuery) {
2117 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2118 }
2119 intent.setSourceBounds(sourceBounds);
2120 try {
2121 startActivity(intent);
2122 } catch (ActivityNotFoundException ex) {
2123 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
2126
Yura4f93ec62014-02-04 14:15:21 +00002127 public boolean isOnCustomContent() {
2128 return mWorkspace.isOnOrMovingToCustomContent();
2129 }
2130
Winson Chung70d72102011-08-12 11:24:35 -07002131 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002132 public boolean onPrepareOptionsMenu(Menu menu) {
2133 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002134 if (mLauncherCallbacks != null) {
2135 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2136 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002137 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002140 @Override
2141 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002142 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002143 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002144 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002145 }
2146
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 public boolean isWorkspaceLocked() {
2148 return mWorkspaceLoading || mWaitingForResult;
2149 }
2150
Adam Cohen517a7f52014-03-01 12:12:59 -08002151 public boolean isWorkspaceLoading() {
2152 return mWorkspaceLoading;
2153 }
2154
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002155 private void setWorkspaceLoading(boolean value) {
2156 boolean isLocked = isWorkspaceLocked();
2157 mWorkspaceLoading = value;
2158 if (isLocked != isWorkspaceLocked()) {
2159 onWorkspaceLockedChanged();
2160 }
2161 }
2162
2163 private void setWaitingForResult(boolean value) {
2164 boolean isLocked = isWorkspaceLocked();
2165 mWaitingForResult = value;
2166 if (isLocked != isWorkspaceLocked()) {
2167 onWorkspaceLockedChanged();
2168 }
2169 }
2170
Adam Cohen9211d422014-10-07 18:14:53 -07002171 protected void onWorkspaceLockedChanged() {
2172 if (mLauncherCallbacks != null) {
2173 mLauncherCallbacks.onWorkspaceLockedChanged();
2174 }
2175 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002176
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2181 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002182 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002183 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002184
Tony Wickhama0628cc2015-10-14 15:23:04 -07002185 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002186 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 if (LOGD) {
2188 Log.d(TAG, "Adding widget from drop");
2189 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002190 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2191 }
2192
Sunny Goyale6b63a32015-01-16 16:14:29 -08002193 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002194 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2195 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002196 if (appWidgetInfo.configure != null) {
2197 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002198 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002199
Bjorn Bringert7984c942009-12-09 15:38:25 +00002200 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002201 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2202 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002205 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002206 Runnable onComplete = new Runnable() {
2207 @Override
2208 public void run() {
2209 // Exit spring loaded mode if necessary after adding the widget
2210 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2211 null);
2212 }
2213 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002215 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002216 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 }
2218 }
Romain Guycbb89e42009-06-08 15:52:54 -07002219
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002220 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002221 // Close any folders that may be open.
2222 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002223 mWorkspace.moveToCustomContentScreen(animate);
2224 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002225
2226 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2227 int[] cell, int spanX, int spanY) {
2228 switch (info.itemType) {
2229 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2230 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2231 int span[] = new int[2];
2232 span[0] = spanX;
2233 span[1] = spanY;
2234 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2235 container, screenId, cell, span);
2236 break;
2237 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2238 processShortcutFromDrop(info.componentName, container, screenId, cell);
2239 break;
2240 default:
2241 throw new IllegalStateException("Unknown item type: " + info.itemType);
2242 }
2243 }
2244
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 /**
2246 * Process a shortcut drop.
2247 *
2248 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002252 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2253 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257
2258 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.cellX = cell[0];
2260 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262
2263 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2264 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002265 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266 }
2267
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 /**
2269 * Process a widget drop.
2270 *
2271 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002275 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2276 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002280 mPendingAddInfo.minSpanX = info.minSpanX;
2281 mPendingAddInfo.minSpanY = info.minSpanY;
2282
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002284 mPendingAddInfo.cellX = cell[0];
2285 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002287 if (span != null) {
2288 mPendingAddInfo.spanX = span[0];
2289 mPendingAddInfo.spanY = span[1];
2290 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 AppWidgetHostView hostView = info.boundWidget;
2293 int appWidgetId;
2294 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002295 // In the case where we've prebound the widget, we remove it from the DragLayer
2296 if (LOGD) {
2297 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2298 }
2299 getDragLayer().removeView(hostView);
2300
Adam Cohened66b2b2012-01-23 17:28:51 -08002301 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002302 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002303
2304 // Clear the boundWidget so that it doesn't get destroyed.
2305 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002307 // In this case, we either need to start an activity to get permission to bind
2308 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002309 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002310 Bundle options = info.bindOptions;
2311
Sunny Goyalffe83f12014-08-14 17:39:34 -07002312 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2313 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002314 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002315 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002316 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002317 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2319 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002321 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2322 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002323 // TODO: we need to make sure that this accounts for the options bundle.
2324 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002325 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2326 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 }
2329
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002332 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 folderInfo.title = getText(R.string.folder_name);
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002336 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2337 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338
2339 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 FolderIcon newFolder =
2341 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002344 // Force measure the new folder icon
2345 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2346 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002347 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 }
Romain Guycbb89e42009-06-08 15:52:54 -07002349
Winsonc0b52fe2015-09-09 16:38:15 -07002350 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002351 * Unbinds the view for the specified item, and removes the item and all its children.
2352 *
2353 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002354 * @param itemInfo the {@link ItemInfo} for this view.
2355 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002356 */
Winson2949fb52015-09-24 09:56:11 -07002357 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002358 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002359 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002360 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2361 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002362 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002363 } else {
2364 mWorkspace.removeWorkspaceItem(v);
2365 }
Winsonc0b52fe2015-09-09 16:38:15 -07002366 if (deleteFromDb) {
2367 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2368 }
2369 } else if (itemInfo instanceof FolderInfo) {
2370 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002371 if (v instanceof FolderIcon) {
2372 ((FolderIcon) v).removeListeners();
2373 }
Winsonc0b52fe2015-09-09 16:38:15 -07002374 mWorkspace.removeWorkspaceItem(v);
2375 if (deleteFromDb) {
2376 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2377 }
2378 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2379 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002380 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002381 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002382 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002383 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002385
Winsonc0b52fe2015-09-09 16:38:15 -07002386 } else {
2387 return false;
2388 }
2389 return true;
2390 }
2391
2392 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002394 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002396 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002397 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002398 // Deleting an app widget ID is a void call but writes to disk before returning
2399 // to the caller...
2400 new AsyncTask<Void, Void, Void>() {
2401 public Void doInBackground(Void ... args) {
2402 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2403 return null;
2404 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002405 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002406 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002407 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002408 }
2409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 @Override
2411 public boolean dispatchKeyEvent(KeyEvent event) {
2412 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2413 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002415 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002416 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002417 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002418 dumpState();
2419 return true;
2420 }
2421 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 }
2423 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2424 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002425 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 return true;
2427 }
2428 }
2429
2430 return super.dispatchKeyEvent(event);
2431 }
2432
Joe Onorato88ec0992009-11-19 13:16:06 -08002433 @Override
2434 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002435 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2436 return;
2437 }
2438
Sunny Goyal45478022015-06-08 16:52:41 -07002439 if (mDragController.isDragging()) {
2440 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002441 return;
2442 }
2443
Winson Chungb745afb2015-03-02 11:51:23 -08002444 if (isAppsViewVisible()) {
2445 showWorkspace(true);
2446 } else if (isWidgetsViewVisible()) {
2447 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002448 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002449 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002450 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002451 Folder openFolder = mWorkspace.getOpenFolder();
2452 if (openFolder.isEditingName()) {
2453 openFolder.dismissEditingName();
2454 } else {
2455 closeFolder();
2456 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002457 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002458 mWorkspace.exitWidgetResizeMode();
2459
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 // Back button is a no-op here, but give at least some feedback for the button press
2461 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002462 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002463 }
2464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 * Launches the intent referred by the clicked shortcut.
2467 *
2468 * @param v The view representing the clicked shortcut.
2469 */
2470 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002471 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2472 // view has detached (it's possible for this to happen if the view is removed mid touch).
2473 if (v.getWindowToken() == null) {
2474 return;
2475 }
2476
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002477 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 return;
2479 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002480
Adam Cohen1697b792013-09-17 19:08:21 -07002481 if (v instanceof Workspace) {
2482 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002483 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
2485 return;
2486 }
2487
2488 if (v instanceof CellLayout) {
2489 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002490 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2491 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002493 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
2495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002497 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002503 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002505 } else if (tag instanceof AppInfo) {
2506 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002507 } else if (tag instanceof LauncherAppWidgetInfo) {
2508 if (v instanceof PendingAppWidgetHostView) {
2509 onClickPendingWidget((PendingAppWidgetHostView) v);
2510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 }
2512 }
2513
Sunny Goyal70660032015-05-14 00:07:08 -07002514 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002515 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002516 return false;
2517 }
2518
Michael Jurkaaf442092010-06-10 17:01:57 -07002519 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002520 * Event handler for the app widget view which has not fully restored.
2521 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002522 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002523 if (mIsSafeModeEnabled) {
2524 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2525 return;
2526 }
2527
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002528 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002529 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002530 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2531 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2532 // This should not happen, as we make sure that an Id is allocated during bind.
2533 return;
2534 }
2535 LauncherAppWidgetProviderInfo appWidgetInfo =
2536 mAppWidgetManager.findProvider(info.providerName, info.user);
2537 if (appWidgetInfo != null) {
2538 mPendingAddWidgetId = info.appWidgetId;
2539 mPendingAddInfo.copyFrom(info);
2540 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002541
Sunny Goyald478c832016-04-01 12:04:16 -07002542 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2543 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2544 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2545 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2546 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2547 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2548 }
2549 } else {
2550 LauncherAppWidgetProviderInfo appWidgetInfo =
2551 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2552 if (appWidgetInfo != null) {
2553 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2554 }
Sunny Goyalff572272014-07-23 13:58:07 -07002555 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002556 } else if (info.installProgress < 0) {
2557 // The install has not been queued
2558 final String packageName = info.providerName.getPackageName();
2559 showBrokenAppInstallDialog(packageName,
2560 new DialogInterface.OnClickListener() {
2561 public void onClick(DialogInterface dialog, int id) {
2562 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2563 }
2564 });
2565 } else {
2566 // Download has started.
2567 final String packageName = info.providerName.getPackageName();
2568 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002569 }
2570 }
2571
Sunny Goyald478c832016-04-01 12:04:16 -07002572 private void startRestoredWidgetReconfigActivity(
2573 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2574 mPendingAddWidgetInfo = provider;
2575 mPendingAddInfo.copyFrom(info);
2576 mPendingAddWidgetId = info.appWidgetId;
2577 mAppWidgetManager.startConfigActivity(provider,
2578 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2579 }
2580
Sunny Goyalff572272014-07-23 13:58:07 -07002581 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002582 * Event handler for the "grid" button that appears on the home screen, which
2583 * enters all apps mode.
2584 *
2585 * @param v The view that was clicked.
2586 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 protected void onClickAllAppsButton(View v) {
2588 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002589 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002590 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002591 true /* updatePredictedApps */, false /* focusSearchBar */);
2592 }
2593 }
2594
2595 protected void onLongClickAllAppsButton(View v) {
2596 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2597 if (!isAppsViewVisible()) {
2598 showAppsView(true /* animated */, false /* resetListToTop */,
2599 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 }
2601 }
2602
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002603 private void showBrokenAppInstallDialog(final String packageName,
2604 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002605 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 .setTitle(R.string.abandoned_promises_title)
2607 .setMessage(R.string.abandoned_promise_explanation)
2608 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2609 .setNeutralButton(R.string.abandoned_clean_this,
2610 new DialogInterface.OnClickListener() {
2611 public void onClick(DialogInterface dialog, int id) {
2612 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2613 mWorkspace.removeAbandonedPromise(packageName, user);
2614 }
2615 })
2616 .create().show();
2617 return;
2618 }
2619
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 /**
2621 * Event handler for an app shortcut click.
2622 *
2623 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2624 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002625 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002626 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2627 Object tag = v.getTag();
2628 if (!(tag instanceof ShortcutInfo)) {
2629 throw new IllegalArgumentException("Input must be a Shortcut");
2630 }
2631
2632 // Open shortcut
2633 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002634
2635 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002636 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2637 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002638 // Launch activity anyway, framework will tell the user why the app is suspended.
2639 } else {
2640 int error = R.string.activity_not_available;
2641 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2642 error = R.string.safemode_shortcut_error;
2643 }
2644 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2645 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002646 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002647 }
2648
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002650 if ((v instanceof BubbleTextView)
2651 && shortcut.isPromise()
2652 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002653 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002654 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002655 new DialogInterface.OnClickListener() {
2656 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002657 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 return;
2661 }
2662
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002663 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002664 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 }
2666
Sunny Goyala7ce1662016-05-31 15:01:35 -07002667 private void startAppShortcutOrInfoActivity(View v) {
2668 ItemInfo item = (ItemInfo) v.getTag();
2669 Intent intent = item.getIntent();
2670 if (intent == null) {
2671 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002672 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002673 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002674 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675
2676 if (success && v instanceof BubbleTextView) {
2677 mWaitingForResume = (BubbleTextView) v;
2678 mWaitingForResume.setStayPressed(true);
2679 }
2680 }
2681
2682 /**
2683 * Event handler for a folder icon click.
2684 *
2685 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2686 */
2687 protected void onClickFolderIcon(View v) {
2688 if (LOGD) Log.d(TAG, "onClickFolder");
2689 if (!(v instanceof FolderIcon)){
2690 throw new IllegalArgumentException("Input must be a FolderIcon");
2691 }
2692
2693 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002694 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002696 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 }
Michael Jurka5130e402011-10-13 04:55:35 -07002698 }
2699
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002700 /**
2701 * Event handler for the (Add) Widgets button that appears after a long press
2702 * on the home screen.
2703 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002704 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002706 if (mIsSafeModeEnabled) {
2707 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2708 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002709 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002710 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002711 }
2712
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 /**
2714 * Event handler for the wallpaper picker button that appears after a long press
2715 * on the home screen.
2716 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002717 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002718 if (!Utilities.isWallapaperAllowed(this)) {
2719 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2720 return;
2721 }
2722
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002723 String pickerPackage = getString(R.string.wallpaper_picker_package);
2724 if (TextUtils.isEmpty(pickerPackage)) {
2725 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2726 }
2727
Tony Wickham785f7a52015-08-31 17:28:32 -07002728 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2729 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002730 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2731 .setPackage(pickerPackage)
2732 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2733 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 }
2735
2736 /**
2737 * Event handler for a click on the settings button that appears after a long press
2738 * on the home screen.
2739 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002740 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002742 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2743 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 }
2745
Adam Cohen61f560d2013-09-30 15:58:20 -07002746 public View.OnTouchListener getHapticFeedbackTouchListener() {
2747 if (mHapticFeedbackTouchListener == null) {
2748 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002749 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002750 @Override
2751 public boolean onTouch(View v, MotionEvent event) {
2752 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2753 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2754 }
2755 return false;
2756 }
2757 };
2758 }
2759 return mHapticFeedbackTouchListener;
2760 }
2761
Tony Wickhame2217252016-03-22 16:34:23 -07002762 @Override
2763 public void onAccessibilityStateChanged(boolean enabled) {
2764 mDragLayer.onAccessibilityStateChanged(enabled);
2765 }
2766
Adam Cohen9211d422014-10-07 18:14:53 -07002767 public void onDragStarted(View view) {
2768 if (isOnCustomContent()) {
2769 // Custom content screen doesn't participate in drag and drop. If on custom
2770 // content screen, move to default.
2771 moveWorkspaceToDefaultScreen();
2772 }
Adam Cohen9211d422014-10-07 18:14:53 -07002773 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002774
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775 /**
2776 * Called when the user stops interacting with the launcher.
2777 * This implies that the user is now on the homescreen and is not doing housekeeping.
2778 */
Adam Cohen9211d422014-10-07 18:14:53 -07002779 protected void onInteractionEnd() {
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onInteractionEnd();
2782 }
2783 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784
2785 /**
2786 * Called when the user starts interacting with the launcher.
2787 * The possible interactions are:
2788 * - open all apps
2789 * - reorder an app shortcut, or a widget
2790 * - open the overview mode.
2791 * This is a good time to stop doing things that only make sense
2792 * when the user is on the homescreen and not doing housekeeping.
2793 */
Adam Cohen9211d422014-10-07 18:14:53 -07002794 protected void onInteractionBegin() {
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onInteractionBegin();
2797 }
2798 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002799
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 /** Updates the interaction state. */
2801 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002804 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2805 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002808 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 onInteractionEnd();
2810 }
2811 }
2812
Sunny Goyala7ce1662016-05-31 15:01:35 -07002813 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002815 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2816 try {
2817 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2818 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2819 // is enabled by default on NYC.
2820 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2821 .penaltyLog().build());
2822 // Could be launching some bookkeeping activity
2823 startActivity(intent, optsBundle);
2824 } finally {
2825 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002828 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2829 // corresponding permission. Show the appropriate permission prompt if that
2830 // is the case.
2831 if (intent.getComponent() == null
2832 && Intent.ACTION_CALL.equals(intent.getAction())
2833 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2834 PackageManager.PERMISSION_GRANTED) {
2835 // TODO: Rename sPendingAddItem to a generic name.
2836 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2837 0, info);
2838 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2839 REQUEST_PERMISSION_CALL_PHONE);
2840 } else {
2841 // No idea why this was thrown.
2842 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 }
2845 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002846
Sunny Goyala7ce1662016-05-31 15:01:35 -07002847 private Bundle getActivityLaunchOptions(View v) {
2848 if (Utilities.ATLEAST_MARSHMALLOW) {
2849 int left = 0, top = 0;
2850 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2851 if (v instanceof TextView) {
2852 // Launch from center of icon, not entire view
2853 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2854 if (icon != null) {
2855 Rect bounds = icon.getBounds();
2856 left = (width - bounds.width()) / 2;
2857 top = v.getPaddingTop();
2858 width = bounds.width();
2859 height = bounds.height();
2860 }
2861 }
2862 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2863 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2864 // On L devices, we use the device default slide-up transition.
2865 // On L MR1 devices, we use a custom version of the slide-up transition which
2866 // doesn't have the delay present in the device default.
2867 return ActivityOptions.makeCustomAnimation(
2868 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2869 }
2870 return null;
2871 }
2872
2873 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002874 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2875 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2876 return false;
2877 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002878 // Only launch using the new animation if the shortcut has not opted out (this is a
2879 // private contract between launcher and may be ignored in the future).
2880 boolean useLaunchAnimation = (v != null) &&
2881 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2882 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2883
2884 UserHandleCompat user = null;
2885 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2886 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2887 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002888 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002889
2890 // Prepare intent
2891 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2892 if (v != null) {
2893 int[] pos = new int[2];
2894 v.getLocationOnScreen(pos);
2895 intent.setSourceBounds(
2896 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2897 }
2898 try {
2899 if (Utilities.ATLEAST_MARSHMALLOW &&
2900 item != null && item.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
2901 // Shortcuts need some special checks due to legacy reasons.
2902 startShortcutIntentSafely(intent, optsBundle, item);
2903 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2904 // Could be launching some bookkeeping activity
2905 startActivity(intent, optsBundle);
2906 } else {
2907 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2908 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2909 }
2910 return true;
2911 } catch (ActivityNotFoundException|SecurityException e) {
2912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2913 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2914 }
2915 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002916 }
2917
Adam Cohen268c4752012-06-06 17:47:33 -07002918 /**
2919 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2920 * in the DragLayer in the exact absolute location of the original FolderIcon.
2921 */
2922 private void copyFolderIconToImage(FolderIcon fi) {
2923 final int width = fi.getMeasuredWidth();
2924 final int height = fi.getMeasuredHeight();
2925
2926 // Lazy load ImageView, Bitmap and Canvas
2927 if (mFolderIconImageView == null) {
2928 mFolderIconImageView = new ImageView(this);
2929 }
2930 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2931 mFolderIconBitmap.getHeight() != height) {
2932 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2933 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2934 }
2935
2936 DragLayer.LayoutParams lp;
2937 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2938 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2939 } else {
2940 lp = new DragLayer.LayoutParams(width, height);
2941 }
2942
Adam Cohen307fe232012-08-16 17:55:58 -07002943 // The layout from which the folder is being opened may be scaled, adjust the starting
2944 // view size by this scale factor.
2945 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002946 lp.customPosition = true;
2947 lp.x = mRectForFolderAnimation.left;
2948 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002949 lp.width = (int) (scale * width);
2950 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002951
2952 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2953 fi.draw(mFolderIconCanvas);
2954 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002955 if (fi.getFolder() != null) {
2956 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2957 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002958 }
Adam Cohen268c4752012-06-06 17:47:33 -07002959 // Just in case this image view is still in the drag layer from a previous animation,
2960 // we remove it and re-add it.
2961 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2962 mDragLayer.removeView(mFolderIconImageView);
2963 }
2964 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002965 if (fi.getFolder() != null) {
2966 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002967 }
Adam Cohen268c4752012-06-06 17:47:33 -07002968 }
2969
Adam Cohen37b2a492016-04-06 18:36:06 -07002970 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002971 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002972 FolderInfo info = (FolderInfo) fi.getTag();
2973 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2974 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002975 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2976 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002977 }
2978
Adam Cohen268c4752012-06-06 17:47:33 -07002979 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2980 copyFolderIconToImage(fi);
2981 fi.setVisibility(View.INVISIBLE);
2982
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002983 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2984 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002985 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002986 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2987 }
2988 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002989 oa.start();
2990 }
2991
Sunny Goyal935fca12015-10-13 10:19:01 -07002992 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002993 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002994 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // We remove and re-draw the FolderIcon in-case it has changed
2997 mDragLayer.removeView(mFolderIconImageView);
2998 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002999
3000 if (cl != null) {
3001 cl.clearFolderLeaveBehind();
3002 }
3003
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003004 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003005 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003006 oa.addListener(new AnimatorListenerAdapter() {
3007 @Override
3008 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003009 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003010 // Remove the ImageView copy of the FolderIcon and make the original visible.
3011 mDragLayer.removeView(mFolderIconImageView);
3012 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003013 }
3014 }
3015 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003017 if (!animate) {
3018 oa.end();
3019 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003020 }
3021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003023 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 * is animated relative to the specified View. If the View is null, no animation
3025 * is played.
3026 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003027 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003029 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003030
Adam Cohenfb91f302012-06-11 15:45:18 -07003031 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003032 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3033 if (openFolder != null && openFolder != folder) {
3034 // Close any open folder before opening a folder.
3035 closeFolder();
3036 }
3037
Adam Cohena9cf38f2011-05-02 15:36:58 -07003038 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039
Adam Cohena9cf38f2011-05-02 15:36:58 -07003040 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041
Sunny Goyalf4066152015-04-15 09:42:19 -07003042 // While the folder is open, the position of the icon cannot change.
3043 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3044
Adam Cohen4554ee12011-08-03 16:13:21 -07003045 // Just verify that the folder hasn't already been added to the DragLayer.
3046 // There was a one-off crash where the folder had a parent already.
3047 if (folder.getParent() == null) {
3048 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003049 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003050 } else {
3051 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3052 folder.getParent() + ").");
3053 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003054 folder.animateOpen();
3055
3056 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003057
3058 // Notify the accessibility manager that this folder "window" has appeared and occluded
3059 // the workspace items
3060 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3061 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 }
3063
3064 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 closeFolder(true);
3066 }
3067
3068 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003069 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003070 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003071 if (folder.isEditingName()) {
3072 folder.dismissEditingName();
3073 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003074 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 }
3076 }
3077
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003079 animate &= !Utilities.isPowerSaverOn(this);
3080
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 folder.getInfo().opened = false;
3082
3083 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3084 if (parent != null) {
3085 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003087 if (fi != null) {
3088 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3089 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003090 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003091 if (animate) {
3092 folder.animateClosed();
3093 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003094 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 }
Winson Chung83ca4802013-04-12 15:10:52 -07003096
Sunny Goyal935fca12015-10-13 10:19:01 -07003097 // Notify the accessibility manager that this folder "window" has disappeared and no
3098 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003099 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 }
3101
Tony Wickhamdadb3042016-02-24 11:07:00 -08003102 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003104 if (!isDraggingEnabled()) return false;
3105 if (isWorkspaceLocked()) return false;
3106 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107
Sunny Goyalbb011da2016-06-15 15:42:29 -07003108 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003109 onLongClickAllAppsButton(v);
3110 return true;
3111 }
3112
Adam Cohen1697b792013-09-17 19:08:21 -07003113 if (v instanceof Workspace) {
3114 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003115 if (!mWorkspace.isTouchActive()) {
3116 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003117 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3118 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3119 return true;
3120 } else {
3121 return false;
3122 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003123 } else {
3124 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003125 }
Adam Cohen1697b792013-09-17 19:08:21 -07003126 }
3127
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003128 CellLayout.CellInfo longClickCellInfo = null;
3129 View itemUnderLongClick = null;
3130 if (v.getTag() instanceof ItemInfo) {
3131 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003132 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003133 itemUnderLongClick = longClickCellInfo.cell;
3134 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Winson Chung3d503fb2011-07-13 17:25:49 -07003137 // The hotseat touch handling does not go through Workspace, and we always allow long press
3138 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003139 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003140 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003141 // User long pressed on empty space
3142 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3143 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003144 if (mWorkspace.isInOverviewMode()) {
3145 mWorkspace.startReordering(v);
3146 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003147 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003148 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003151 final boolean isAllAppsButton =
3152 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3153 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3154 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003155 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003157 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 }
3159 }
3160 }
3161 return true;
3162 }
3163
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 boolean isHotseatLayout(View layout) {
3165 return mHotseat != null && layout != null &&
3166 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3167 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003168
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 /**
3170 * Returns the CellLayout of the specified container at the specified screen.
3171 */
Sunny Goyal92820592015-03-02 11:31:35 -08003172 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3174 if (mHotseat != null) {
3175 return mHotseat.getLayout();
3176 } else {
3177 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003178 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003180 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003181 }
3182 }
3183
Winson Chungb745afb2015-03-02 11:51:23 -08003184 /**
3185 * For overridden classes.
3186 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003187 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003188 return isAppsViewVisible();
3189 }
3190
3191 public boolean isAppsViewVisible() {
3192 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3193 }
3194
3195 public boolean isWidgetsViewVisible() {
3196 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003197 }
3198
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003199 private void setWorkspaceBackground(int background) {
3200 switch (background) {
3201 case WORKSPACE_BACKGROUND_TRANSPARENT:
3202 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3203 break;
3204 case WORKSPACE_BACKGROUND_BLACK:
3205 getWindow().setBackgroundDrawable(null);
3206 break;
3207 default:
3208 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3209 }
Craig Mautner360310b2012-10-26 15:13:08 -07003210 }
3211
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003212 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003213 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3214 int curflags = getWindow().getAttributes().flags
3215 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3216 if (wpflags != curflags) {
3217 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3218 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003219 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003220 }
3221
Michael Jurkae326f182011-11-21 14:05:46 -08003222 @Override
3223 public void onTrimMemory(int level) {
3224 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003225 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3226 // The widget preview db can result in holding onto over
3227 // 3MB of memory for caching which isn't necessary.
3228 SQLiteDatabase.releaseMemory();
3229
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003230 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003231 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003232 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003233 if (mLauncherCallbacks != null) {
3234 mLauncherCallbacks.onTrimMemory(level);
3235 }
Michael Jurkae326f182011-11-21 14:05:46 -08003236 }
3237
Winson5c6bdbb2015-09-03 11:36:19 -07003238 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003239 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003240 }
3241
Winson5c6bdbb2015-09-03 11:36:19 -07003242 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003243 boolean changed = mState != State.WORKSPACE ||
3244 mWorkspace.getState() != Workspace.State.NORMAL;
3245 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003246 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003247 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003248 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003249
Michael Jurkab3e22d92011-10-31 15:58:33 -07003250 // Set focus to the AppsCustomize button
3251 if (mAllAppsButton != null) {
3252 mAllAppsButton.requestFocus();
3253 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003254 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003255
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003256 // Change the state *after* we've called all the transition code
3257 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003258
Winson Chung5fb63472011-02-02 17:03:37 -08003259 // Resume the auto-advance of widgets
3260 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003261 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003262
Winson Chung0f785722015-04-08 10:27:49 -07003263 if (changed) {
3264 // Send an accessibility event to announce the context change
3265 getWindow().getDecorView()
3266 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003267 }
Winson5c6bdbb2015-09-03 11:36:19 -07003268 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003269 }
3270
Winsone9f27272015-10-13 10:47:51 -07003271 /**
3272 * Shows the overview button.
3273 */
Adam Cohened307df2013-10-02 09:37:31 -07003274 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003275 showOverviewMode(animated, false);
3276 }
3277
3278 /**
3279 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3280 * onto one of the overview panel buttons.
3281 */
3282 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3283 Runnable postAnimRunnable = null;
3284 if (requestButtonFocus) {
3285 postAnimRunnable = new Runnable() {
3286 @Override
3287 public void run() {
3288 // Hitting the menu button when in touch mode does not trigger touch mode to
3289 // be disabled, so if requested, force focus on one of the overview panel
3290 // buttons.
3291 mOverviewPanel.requestFocusFromTouch();
3292 }
3293 };
3294 }
Adam Cohened307df2013-10-02 09:37:31 -07003295 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003296 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003297 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003298 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003299 }
3300
Winson Chungb745afb2015-03-02 11:51:23 -08003301 /**
3302 * Shows the apps view.
3303 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003304 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003305 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003306 if (resetListToTop) {
3307 mAppsView.scrollToTop();
3308 }
Winson Chung4ac30062015-05-08 17:34:17 -07003309 if (updatePredictedApps) {
3310 tryAndUpdatePredictedApps();
3311 }
Winson Chung76648c52015-07-10 14:33:23 -07003312 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003313 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003314
Winson Chungb745afb2015-03-02 11:51:23 -08003315 /**
3316 * Shows the widgets view.
3317 */
3318 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003319 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003320 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003321 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003322 }
Winson Chung76648c52015-07-10 14:33:23 -07003323 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003324
3325 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003326 @Override
3327 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003328 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003329 }
3330 });
Winson Chungb745afb2015-03-02 11:51:23 -08003331 }
3332
3333 /**
3334 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003335 *
3336 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003337 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003338 // TODO: calling method should use the return value so that when {@code false} is returned
3339 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003340 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003341 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3342 mState != State.WIDGETS_SPRING_LOADED) {
3343 return false;
3344 }
3345 if (toState != State.APPS && toState != State.WIDGETS) {
3346 return false;
3347 }
Winson Chungb745afb2015-03-02 11:51:23 -08003348
3349 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003350 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3351 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003352 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003353 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003354 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003357 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358
3359 // Pause the auto-advance of widgets until we are out of AllApps
3360 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003361 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 closeFolder();
3363
3364 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003365 getWindow().getDecorView()
3366 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003367 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 }
3369
Winson Chungcd99cd32015-04-29 11:03:24 -07003370 /**
3371 * Updates the workspace and interaction state on state change, and return the animation to this
3372 * new state.
3373 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003374 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003375 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003377 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003378 updateInteraction(fromState, toState);
3379 return anim;
3380 }
3381
Jun Mukaif0033da2015-08-04 18:34:30 -07003382 public void onLauncherClingShown() {
3383 // When a launcher cling appears, it should cover the underlying layers, so their focus
3384 // should be blocked.
3385 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3386 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3387 }
3388 }
3389
3390 public void onLauncherClingDismissed() {
3391 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3392 }
3393
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003395 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003396 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003397 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003398 }
Winson Chungb745afb2015-03-02 11:51:23 -08003399
Winson Chung006ee262015-08-03 14:40:11 -07003400 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003401 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003402 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003403
3404 if (isAppsViewVisible()) {
3405 mState = State.APPS_SPRING_LOADED;
3406 } else if (isWidgetsViewVisible()) {
3407 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003408 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003409 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003410 } else {
3411 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003412 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003413 }
Adam Cohen7777d962011-08-18 18:58:38 -07003414
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003416 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003417 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003418
Winson Chunge7a03942011-08-05 15:05:12 -07003419 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003420 @Override
3421 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003422 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3424 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003425 // Before we show workspace, hide all apps again because
3426 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3427 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003428 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003429 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003430 } else {
3431 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003432 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003433 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003434 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003435 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436
Tony Wickhame0c33232016-02-08 11:37:04 -08003437 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003438 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3439 || mState == State.WIDGETS_SPRING_LOADED;
3440 }
3441
Michael Jurkad3ef3062010-11-23 16:23:58 -08003442 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003443 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003444 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003445 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003446 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003447 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003448 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3449 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003450 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003451 }
3452
Winson Chung4ac30062015-05-08 17:34:17 -07003453 /**
3454 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3455 * resumed.
3456 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003457 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003458 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003459 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003460 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003461 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003462 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003463 }
3464 }
3465 }
3466
Michael Jurkab3e22d92011-10-31 15:58:33 -07003467 void lockAllApps() {
3468 // TODO
3469 }
3470
3471 void unlockAllApps() {
3472 // TODO
3473 }
3474
Winsonf768d932015-09-25 16:12:35 -07003475 public boolean launcherCallbacksProvidesSearch() {
3476 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3477 }
3478
Michael Jurkad7c28052012-04-27 15:43:36 -07003479 @Override
3480 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003481 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003482 final List<CharSequence> text = event.getText();
3483 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003484 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003485 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003486 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003487 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003488 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003489 } else if (mWorkspace != null) {
3490 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003491 } else {
3492 text.add(getString(R.string.all_apps_home_button_label));
3493 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003494 return result;
3495 }
3496
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003497 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003498 * If the activity is currently paused, signal that we need to run the passed Runnable
3499 * in onResume.
3500 *
3501 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003502 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3503 * wrong when we're not running, and if the activity comes back to what the configuration was
3504 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003508 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003510 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003512 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003513 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003514 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003515 }
3516 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003517 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 return true;
3519 } else {
3520 return false;
3521 }
3522 }
3523
Michael Jurkac402cd92013-05-20 15:49:32 +02003524 private boolean waitUntilResume(Runnable run) {
3525 return waitUntilResume(run, false);
3526 }
3527
Michael Jurka1e2f4652013-07-08 18:03:46 -07003528 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003529 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003530 }
3531
Michael Jurka7607c2f2013-04-03 14:33:19 -07003532 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003533 * If the activity is currently paused, signal that we need to re-run the loader
3534 * in onResume.
3535 *
3536 * This needs to be called from incoming places where resources might have been loaded
3537 * while we are paused. That is becaues the Configuration might be wrong
3538 * when we're not running, and if it comes back to what it was when we
3539 * were paused, we are not restarted.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 *
3543 * @return true if we are currently paused. The caller might be able to
3544 * skip some work in that case since we will come back again.
3545 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003546 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003547 public boolean setLoadOnResume() {
3548 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003549 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003550 mOnResumeNeedsLoad = true;
3551 return true;
3552 } else {
3553 return false;
3554 }
3555 }
3556
3557 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003559 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003560 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003562 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003563 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003564 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003565 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003568
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003570 * Clear any pending bind callbacks. This is called when is loader is planning to
3571 * perform a full rebind from scratch.
3572 */
3573 @Override
3574 public void clearPendingBinds() {
3575 mBindOnResumeCallbacks.clear();
3576 if (mPendingExecutor != null) {
3577 mPendingExecutor.markCompleted();
3578 mPendingExecutor = null;
3579 }
3580 }
3581
3582 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 * Refreshes the shortcuts shown on the workspace.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
3587 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003588 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003589
Winson Chungd64d1762013-08-20 14:37:16 -07003590 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003591 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003592 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003593
Michael Jurka05bf644e2011-11-30 20:28:53 -08003594 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003595 if (mHotseat != null) {
3596 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597 }
3598 }
3599
Adam Cohendcd297f2013-06-18 13:13:40 -07003600 @Override
3601 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003602 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003603 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3604 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003605 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3606 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3607 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003608 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3609 // If there are no screens, we need to have an empty screen
3610 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003611 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003612 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003613
3614 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003615 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003616 if (hasCustomContentToLeft()) {
3617 mWorkspace.createCustomContentContainer();
3618 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003619 }
Winson Chung64359a52013-07-08 17:17:08 -07003620 }
3621
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003622 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 int count = orderedScreenIds.size();
3624 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003625 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003626 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003627 // No need to bind the first screen, as its always bound.
3628 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3629 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003630 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003631 }
3632
Winson Chungd64d1762013-08-20 14:37:16 -07003633 public void bindAppsAdded(final ArrayList<Long> newScreens,
3634 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003635 final ArrayList<ItemInfo> addAnimated,
3636 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003637 Runnable r = new Runnable() {
3638 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003639 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003640 }
3641 };
3642 if (waitUntilResume(r)) {
3643 return;
3644 }
3645
Winson Chungd64d1762013-08-20 14:37:16 -07003646 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003647 if (newScreens != null) {
3648 bindAddScreens(newScreens);
3649 }
Winson Chungd64d1762013-08-20 14:37:16 -07003650
3651 // We add the items without animation on non-visible pages, and with
3652 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003653 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003654 bindItems(addNotAnimated, 0,
3655 addNotAnimated.size(), false);
3656 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003657 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003658 bindItems(addAnimated, 0,
3659 addAnimated.size(), true);
3660 }
Winson Chungc58497e2013-09-03 17:48:37 -07003661
Winson Chung87412982013-10-03 18:34:14 -07003662 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003663 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003664
Winson Chungb745afb2015-03-02 11:51:23 -08003665 if (addedApps != null && mAppsView != null) {
3666 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003667 }
Winson Chungd64d1762013-08-20 14:37:16 -07003668 }
3669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 * Bind the items start-end from the list.
3672 *
3673 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003675 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003676 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3677 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003678 Runnable r = new Runnable() {
3679 public void run() {
3680 bindItems(shortcuts, start, end, forceAnimateIcons);
3681 }
3682 };
3683 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 return;
3685 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003686
Winson Chungf0c6ae02012-03-21 16:10:31 -07003687 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003688 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3689 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003690 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003692 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003695
3696 // Short circuit if we are loading dock items for a configuration which has no dock
3697 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3698 mHotseat == null) {
3699 continue;
3700 }
3701
Sunny Goyal639e9062015-08-19 19:17:06 -07003702 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 switch (item.itemType) {
3704 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3705 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003707 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003709 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003710 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003711 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003712 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003714 default:
3715 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003716 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003717
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003718 /*
3719 * Remove colliding items.
3720 */
3721 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3722 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3723 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3724 View v = cl.getChildAt(item.cellX, item.cellY);
3725 Object tag = v.getTag();
3726 String desc = "Collision while binding workspace item: " + item
3727 + ". Collides with " + tag;
3728 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3729 throw (new RuntimeException(desc));
3730 } else {
3731 Log.d(TAG, desc);
3732 LauncherModel.deleteItemFromDatabase(this, item);
3733 continue;
3734 }
3735 }
3736 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003737 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3738 item.cellY, 1, 1);
3739 if (animateIcons) {
3740 // Animate all the applications up now
3741 view.setAlpha(0f);
3742 view.setScaleX(0f);
3743 view.setScaleY(0f);
3744 bounceAnims.add(createNewAppBounceAnimation(view, i));
3745 newShortcutsScreenId = item.screenId;
3746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003747 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003748
Winson Chung997a9232013-07-24 15:33:46 -07003749 if (animateIcons) {
3750 // Animate to the correct page
3751 if (newShortcutsScreenId > -1) {
3752 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003753 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003754 final Runnable startBounceAnimRunnable = new Runnable() {
3755 public void run() {
3756 anim.playTogether(bounceAnims);
3757 anim.start();
3758 }
3759 };
Winson Chung997a9232013-07-24 15:33:46 -07003760 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003761 // We post the animation slightly delayed to prevent slowdowns
3762 // when we are loading right after we return to launcher.
3763 mWorkspace.postDelayed(new Runnable() {
3764 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003765 if (mWorkspace != null) {
3766 mWorkspace.snapToPage(newScreenIndex);
3767 mWorkspace.postDelayed(startBounceAnimRunnable,
3768 NEW_APPS_ANIMATION_DELAY);
3769 }
Winson Chung94d67682013-09-25 16:29:40 -07003770 }
3771 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003772 } else {
3773 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003774 }
3775 }
Winson Chung64359a52013-07-08 17:17:08 -07003776 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003778 }
3779
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003780 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3781 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3782 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003783 view.updateAppWidget(null);
3784 view.setOnClickListener(this);
3785 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003786 mWorkspace.requestLayout();
3787 }
3788
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 /**
3790 * Add the views for a widget to the workspace.
3791 *
3792 * Implementation of the method from LauncherModel.Callbacks.
3793 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003794 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003795 Runnable r = new Runnable() {
3796 public void run() {
3797 bindAppWidget(item);
3798 }
3799 };
3800 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003801 return;
3802 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003803
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003804 if (mIsSafeModeEnabled) {
3805 bindSafeModeWidget(item);
3806 return;
3807 }
3808
Daniel Sandler843e8602010-06-07 14:59:01 -04003809 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3810 if (DEBUG_WIDGETS) {
3811 Log.d(TAG, "bindAppWidget: " + item);
3812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003814 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003815
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3817 // If the provider is not ready, bind as a pending widget.
3818 appWidgetInfo = null;
3819 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3820 // The widget id is not valid. Try to find the widget based on the provider info.
3821 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3822 } else {
3823 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3824 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003825
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003826 // If the provider is ready, but the width is not yet restored, try to restore it.
3827 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3828 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003829 if (appWidgetInfo == null) {
3830 if (DEBUG_WIDGETS) {
3831 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3832 + " belongs to component " + item.providerName
3833 + ", as the povider is null");
3834 }
3835 LauncherModel.deleteItemFromDatabase(this, item);
3836 return;
3837 }
Sunny Goyalff572272014-07-23 13:58:07 -07003838
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003839 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003840 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003841 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3842 // Id has not been allocated yet. Allocate a new id.
3843 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3844 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003845
Sunny Goyald478c832016-04-01 12:04:16 -07003846 // Also try to bind the widget. If the bind fails, the user will be shown
3847 // a click to setup UI, which will ask for the bind permission.
3848 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3849 pendingInfo.spanX = item.spanX;
3850 pendingInfo.spanY = item.spanY;
3851 pendingInfo.minSpanX = item.minSpanX;
3852 pendingInfo.minSpanY = item.minSpanY;
3853 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3854 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3855 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003856
Sunny Goyald478c832016-04-01 12:04:16 -07003857 // Bind succeeded
3858 if (success) {
3859 // If the widget has a configure activity, it is still needs to set it up,
3860 // otherwise the widget is ready to go.
3861 item.restoreStatus = (appWidgetInfo.configure == null)
3862 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3863 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003864 }
Sunny Goyald478c832016-04-01 12:04:16 -07003865
3866 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003867 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003868 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003869 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 // The widget was marked as UI not ready, but there is no configure activity to
3871 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003872 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3873 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003874 }
Sunny Goyalff572272014-07-23 13:58:07 -07003875 }
3876
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003877 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003878 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003879 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3880 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003881 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003882
Sunny Goyal56c73602015-09-25 12:55:01 -07003883 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003884 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003885 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003886 deleteWidgetInfo(item);
3887 return;
3888 }
3889
Sunny Goyal233ee962015-08-03 13:05:01 -07003890 item.minSpanX = appWidgetInfo.minSpanX;
3891 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003892 addAppWidgetToWorkspace(
3893 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3894 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003895 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003896 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003897 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003898 view.updateAppWidget(null);
3899 view.setOnClickListener(this);
3900 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003901 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903
Daniel Sandler843e8602010-06-07 14:59:01 -04003904 if (DEBUG_WIDGETS) {
3905 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3906 + (SystemClock.uptimeMillis()-start) + "ms");
3907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 }
3909
Sunny Goyalff572272014-07-23 13:58:07 -07003910 /**
3911 * Restores a pending widget.
3912 *
3913 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003914 */
Sunny Goyald478c832016-04-01 12:04:16 -07003915 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003916 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3917 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3918 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003919 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003920 }
3921
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003922 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003923 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003924
3925 mWorkspace.reinflateWidgetsIfNecessary();
3926 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003927 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003928 }
3929
Adam Cohen1462de32012-07-24 22:34:36 -07003930 public void onPageBoundSynchronously(int page) {
3931 mSynchronouslyBoundPages.add(page);
3932 }
3933
Sunny Goyal527c7d32015-08-28 15:19:36 -07003934 @Override
3935 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3936 if (mPendingExecutor != null) {
3937 mPendingExecutor.markCompleted();
3938 }
3939 mPendingExecutor = executor;
3940 executor.attachTo(this);
3941 }
3942
3943 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3944 if (mPendingExecutor == executor) {
3945 mPendingExecutor = null;
3946 }
3947 }
3948
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 /**
3950 * Callback saying that there aren't any more items to bind.
3951 *
3952 * Implementation of the method from LauncherModel.Callbacks.
3953 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003954 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003955 Runnable r = new Runnable() {
3956 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003957 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003958 }
3959 };
3960 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003961 return;
3962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 if (mSavedState != null) {
3964 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003965 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003967
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 mSavedState = null;
3969 }
3970
Adam Cohen1462de32012-07-24 22:34:36 -07003971 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003973 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003974
3975 // If we received the result of any pending adds while the loader was running (e.g. the
3976 // widget configuration forced an orientation change), process them now.
3977 if (sPendingAddItem != null) {
3978 final long screenId = completeAdd(sPendingAddItem);
3979
3980 // TODO: this moves the user to the page where the pending item was added. Ideally,
3981 // the screen would be guaranteed to exist after bind, and the page would be set through
3982 // the workspace restore process.
3983 mWorkspace.post(new Runnable() {
3984 @Override
3985 public void run() {
3986 mWorkspace.snapToScreenId(screenId);
3987 }
3988 });
3989 sPendingAddItem = null;
3990 }
3991
Sunny Goyal756adbc2015-04-16 15:20:51 -07003992 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003993
3994 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003995 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003996 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003997 }
3998
Winson Chunga2413752012-04-03 14:22:34 -07003999 private boolean canRunNewAppsAnimation() {
4000 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004001 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4002 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004003 }
4004
Winson Chungc9168342013-06-26 14:54:55 -07004005 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004006 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004007 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4008 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004009 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004010 return bounceAnim;
4011 }
4012
Adam Cohen27772732013-11-11 14:00:56 +00004013 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004014 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004015 }
4016
Tony Wickham55616cd2015-09-23 14:55:17 -07004017 public int getSearchBarHeight() {
4018 if (mLauncherCallbacks != null) {
4019 return mLauncherCallbacks.getSearchBarHeight();
4020 }
4021 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4022 }
4023
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004024 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004025 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4026 * multiple calls to bind the same list.)
4027 */
4028 @Thunk ArrayList<AppInfo> mTmpAppsList;
4029 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4030 public void run() {
4031 bindAllApplications(mTmpAppsList);
4032 mTmpAppsList = null;
4033 }
4034 };
4035
4036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 * Add the icons for all apps.
4038 *
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004041 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004042 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4043 mTmpAppsList = apps;
4044 return;
4045 }
4046
Winson Chungb745afb2015-03-02 11:51:23 -08004047 if (mAppsView != null) {
4048 mAppsView.setApps(apps);
4049 }
Adam Cohen9211d422014-10-07 18:14:53 -07004050 if (mLauncherCallbacks != null) {
4051 mLauncherCallbacks.bindAllApplications(apps);
4052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 }
4054
4055 /**
4056 * A package was updated.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004060 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004061 Runnable r = new Runnable() {
4062 public void run() {
4063 bindAppsUpdated(apps);
4064 }
4065 };
4066 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 return;
4068 }
4069
Winson Chungb745afb2015-03-02 11:51:23 -08004070 if (mAppsView != null) {
4071 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 }
4074
Sunny Goyal4390ace2014-10-13 11:33:11 -07004075 @Override
4076 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4077 Runnable r = new Runnable() {
4078 public void run() {
4079 bindWidgetsRestored(widgets);
4080 }
4081 };
4082 if (waitUntilResume(r)) {
4083 return;
4084 }
4085 mWorkspace.widgetsRestored(widgets);
4086 }
4087
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004089 * Some shortcuts were updated in the background.
4090 *
4091 * Implementation of the method from LauncherModel.Callbacks.
4092 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004093 @Override
4094 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4095 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004096 Runnable r = new Runnable() {
4097 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004098 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004099 }
4100 };
4101 if (waitUntilResume(r)) {
4102 return;
4103 }
4104
Sunny Goyal4390ace2014-10-13 11:33:11 -07004105 if (!updated.isEmpty()) {
4106 mWorkspace.updateShortcuts(updated);
4107 }
4108
4109 if (!removed.isEmpty()) {
4110 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4111 for (ShortcutInfo si : removed) {
4112 removedComponents.add(si.getTargetComponent());
4113 }
4114 mWorkspace.removeItemsByComponentName(removedComponents, user);
4115 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004116 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004117 }
4118 }
4119
4120 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004121 * Update the state of a package, typically related to install state.
4122 *
4123 * Implementation of the method from LauncherModel.Callbacks.
4124 */
Sunny Goyale755d462014-07-22 13:48:29 -07004125 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004126 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4127 Runnable r = new Runnable() {
4128 public void run() {
4129 bindRestoreItemsChange(updates);
4130 }
4131 };
4132 if (waitUntilResume(r)) {
4133 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004134 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004135
Sunny Goyal756adbc2015-04-16 15:20:51 -07004136 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004137 }
4138
4139 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004140 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004141 * in addition to specific applications to remove, the reason being that
4142 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004143 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004144 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004145 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004146 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004147 public void bindWorkspaceComponentsRemoved(
4148 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4149 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004150 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004151 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004152 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004153 }
Winson Chungd64d1762013-08-20 14:37:16 -07004154 };
4155 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004156 return;
4157 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 if (!packageNames.isEmpty()) {
4159 mWorkspace.removeItemsByPackageName(packageNames, user);
4160 }
4161 if (!components.isEmpty()) {
4162 mWorkspace.removeItemsByComponentName(components, user);
4163 }
4164 // Notify the drag controller
4165 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004166
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004167 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004168
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004169 @Override
4170 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindAppInfosRemoved(appInfos);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
4177 return;
Joe Onorato36115782010-06-17 13:28:48 -04004178 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004179
Winson Chungdf95eb12013-10-16 14:57:07 -07004180 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004181 if (mAppsView != null) {
4182 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 }
Joe Onoratobe386092009-11-17 17:32:16 -08004185
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004186 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004187 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004188 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004189 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004190 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004191
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004192 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004193 public void bindWidgetsModel(WidgetsModel model) {
4194 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004195 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004196 return;
4197 }
4198
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004199 if (mWidgetsView != null && model != null) {
4200 mWidgetsView.addWidgets(model);
4201 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004202 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004203 }
4204
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004205 @Override
4206 public void notifyWidgetProvidersChanged() {
4207 if (mWorkspace.getState().shouldUpdateWidget) {
4208 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4209 }
4210 }
4211
Winson Chung400438b2011-01-16 17:53:48 -08004212 private int mapConfigurationOriActivityInfoOri(int configOri) {
4213 final Display d = getWindowManager().getDefaultDisplay();
4214 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4215 switch (d.getRotation()) {
4216 case Surface.ROTATION_0:
4217 case Surface.ROTATION_180:
4218 // We are currently in the same basic orientation as the natural orientation
4219 naturalOri = configOri;
4220 break;
4221 case Surface.ROTATION_90:
4222 case Surface.ROTATION_270:
4223 // We are currently in the other basic orientation to the natural orientation
4224 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4225 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4226 break;
4227 }
4228
4229 int[] oriMap = {
4230 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4231 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4232 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4233 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4234 };
4235 // Since the map starts at portrait, we need to offset if this device's natural orientation
4236 // is landscape.
4237 int indexOffset = 0;
4238 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4239 indexOffset = 1;
4240 }
4241 return oriMap[(d.getRotation() + indexOffset) % 4];
4242 }
Adam Cohen446e9402011-09-15 18:21:21 -07004243
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004244 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004245 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004246 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004247 if (Utilities.ATLEAST_JB_MR2) {
4248 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4249 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004250 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4251 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004252 }
Winson Chung4b919f82012-05-01 10:44:08 -07004253 }
4254 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004255
Winson Chung4b919f82012-05-01 10:44:08 -07004256 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004257 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004258 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004259 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004260 } else {
4261 mHandler.postDelayed(new Runnable() {
4262 public void run() {
4263 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4264 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004265 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004266 }
Winson Chung4b919f82012-05-01 10:44:08 -07004267 }
Winson Chung400438b2011-01-16 17:53:48 -08004268 }
4269
Adam Cohenea90f832014-05-21 15:03:34 -07004270 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004271 * To be overridden by subclasses to indicate that there is an activity to launch
4272 * before showing the standard launcher experience.
4273 */
4274 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004275 if (mLauncherCallbacks != null) {
4276 return mLauncherCallbacks.hasFirstRunActivity();
4277 }
Adam Cohen432609a2014-03-13 17:03:22 -07004278 return false;
4279 }
4280
4281 /**
4282 * To be overridden by subclasses to launch any first run activity
4283 */
4284 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004285 if (mLauncherCallbacks != null) {
4286 return mLauncherCallbacks.getFirstRunActivity();
4287 }
Adam Cohen432609a2014-03-13 17:03:22 -07004288 return null;
4289 }
4290
Winson Chunga6945242014-01-08 14:04:34 -08004291 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004292 return !ActivityManager.isRunningInTestHarness() &&
4293 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004294 }
4295
Adam Cohen4a9423d2014-03-28 14:22:31 -07004296 protected boolean hasRunFirstRunActivity() {
4297 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4298 }
4299
Adam Cohen432609a2014-03-13 17:03:22 -07004300 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004301 if (shouldRunFirstRunActivity() &&
4302 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004303 Intent firstRunIntent = getFirstRunActivity();
4304 if (firstRunIntent != null) {
4305 startActivity(firstRunIntent);
4306 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004307 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004308 }
4309 }
Adam Cohen432609a2014-03-13 17:03:22 -07004310 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004311 }
4312
4313 private void markFirstRunActivityShown() {
4314 SharedPreferences.Editor editor = mSharedPrefs.edit();
4315 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4316 editor.apply();
4317 }
4318
Adam Cohen432609a2014-03-13 17:03:22 -07004319 /**
4320 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4321 * screen that must be displayed and dismissed.
4322 */
4323 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004324 if (mLauncherCallbacks != null) {
4325 return mLauncherCallbacks.hasDismissableIntroScreen();
4326 }
Adam Cohen432609a2014-03-13 17:03:22 -07004327 return false;
4328 }
4329
4330 /**
4331 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4332 */
4333 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004334 if (mLauncherCallbacks != null) {
4335 return mLauncherCallbacks.getIntroScreen();
4336 }
Adam Cohen432609a2014-03-13 17:03:22 -07004337 return null;
4338 }
4339
4340 /**
4341 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4342 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4343 */
4344 private boolean shouldShowIntroScreen() {
4345 return hasDismissableIntroScreen() &&
4346 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4347 }
4348
4349 protected void showIntroScreen() {
4350 View introScreen = getIntroScreen();
4351 changeWallpaperVisiblity(false);
4352 if (introScreen != null) {
4353 mDragLayer.showOverlayView(introScreen);
4354 }
4355 }
4356
4357 public void dismissIntroScreen() {
4358 markIntroScreenDismissed();
4359 if (showFirstRunActivity()) {
4360 // We delay hiding the intro view until the first run activity is showing. This
4361 // avoids a blip.
4362 mWorkspace.postDelayed(new Runnable() {
4363 @Override
4364 public void run() {
4365 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004366 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004367 }
4368 }, ACTIVITY_START_DELAY);
4369 } else {
4370 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004371 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004372 }
4373 changeWallpaperVisiblity(true);
4374 }
4375
4376 private void markIntroScreenDismissed() {
4377 SharedPreferences.Editor editor = mSharedPrefs.edit();
4378 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4379 editor.apply();
4380 }
4381
Adam Cohen091440a2015-03-18 14:16:05 -07004382 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004383 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4384 // on the device, then we always show the first run cling experience (or if there is no
4385 // launcher2). Otherwise, we prompt the user upon started for migration
4386 LauncherClings launcherClings = new LauncherClings(this);
4387 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004388 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004389 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004390 }
4391 }
4392
Winson Chung5ffd51d2015-06-25 11:36:50 -07004393 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004394 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004395 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004396 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004397 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004398 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004399 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4400 if (userHandle != null) {
4401 user = UserHandleCompat.fromUser(userHandle);
4402 }
4403 }
4404 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004405 }
4406
4407 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004408 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004409 if (user == null) {
4410 user = UserHandleCompat.myUserHandle();
4411 }
4412
4413 // Called from search suggestion, add the profile extra to the intent to ensure that we
4414 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004415 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004416 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004417 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004418 return null;
4419 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004420 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004421 }
4422
Winson Chung5ffd51d2015-06-25 11:36:50 -07004423 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004424 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4425 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004426 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004427 UserHandleCompat.myUserHandle());
4428 }
4429
Winson Chung5ffd51d2015-06-25 11:36:50 -07004430 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004431 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4432 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004433 mWorkspace.onExternalDragStartedWithItem(dragView);
4434 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004435 }
4436
Winson Chung5ffd51d2015-06-25 11:36:50 -07004437 protected void moveWorkspaceToDefaultScreen() {
4438 mWorkspace.moveToDefaultScreen(false);
4439 }
4440
Winson Chung80baf5a2010-08-09 16:03:15 -07004441 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004442 * Returns a FastBitmapDrawable with the icon, accurately sized.
4443 */
4444 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4445 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4446 d.setFilterBitmap(true);
4447 resizeIconDrawable(d);
4448 return d;
4449 }
4450
4451 /**
4452 * Resizes an icon drawable to the correct icon size.
4453 */
Winson5fbe0742015-09-30 15:33:00 -07004454 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004455 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004456 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004457 }
4458
4459 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004460 * Prints out out state for debugging.
4461 */
4462 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004463 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004464 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004465 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4466 Log.d(TAG, "mRestoring=" + mRestoring);
4467 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004468 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004469 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004470
Daniel Sandler325dc232013-06-05 22:57:57 -04004471 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004472 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004473
4474 @Override
4475 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4476 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004477 // Dump workspace
4478 writer.println(prefix + "Workspace Items");
4479 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4480 writer.println(prefix + " Homescreen " + i);
4481
4482 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4483 for (int j = 0; j < layout.getChildCount(); j++) {
4484 Object tag = layout.getChildAt(j).getTag();
4485 if (tag != null) {
4486 writer.println(prefix + " " + tag.toString());
4487 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004488 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004489 }
Sunny Goyala1365452015-10-01 15:46:24 -07004490
4491 writer.println(prefix + " Hotseat");
4492 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4493 for (int j = 0; j < layout.getChildCount(); j++) {
4494 Object tag = layout.getChildAt(j).getTag();
4495 if (tag != null) {
4496 writer.println(prefix + " " + tag.toString());
4497 }
4498 }
4499
Sunny Goyal713edfc2016-05-06 09:58:34 -07004500 try {
4501 FileLog.flushAll(writer);
4502 } catch (Exception e) {
4503 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004504 }
4505
Adam Cohen9211d422014-10-07 18:14:53 -07004506 if (mLauncherCallbacks != null) {
4507 mLauncherCallbacks.dump(prefix, fd, writer, args);
4508 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004509 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004510
Adam Cohen59400422014-03-05 18:07:04 -08004511 public static CustomAppWidget getCustomAppWidget(String name) {
4512 return sCustomAppWidgets.get(name);
4513 }
4514
4515 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4516 return sCustomAppWidgets;
4517 }
4518
Sunny Goyal29538332016-02-18 09:10:19 -08004519 public static List<View> getFolderContents(View icon) {
4520 if (icon instanceof FolderIcon) {
4521 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4522 } else {
4523 return Collections.EMPTY_LIST;
4524 }
4525 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004526
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004527 public static Launcher getLauncher(Context context) {
4528 if (context instanceof Launcher) {
4529 return (Launcher) context;
4530 }
4531 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4532 }
4533
Sunny Goyal745bad92016-05-02 10:54:12 -07004534 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4535
4536 @Override
4537 public void onSharedPreferenceChanged(
4538 SharedPreferences sharedPreferences, String key) {
4539 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4540 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4541 if (!waitUntilResume(this, true)) {
4542 run();
4543 }
4544 }
4545 }
4546
4547 @Override
4548 public void run() {
4549 setOrientation();
4550 }
4551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004552}