blob: 7a4e4f066c9dbe5ec57a53982d07ac5eca777395 [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 Goyale26d1002016-06-20 14:52:14 -070067import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070087import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070097import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070099import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800100import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherActivityInfoCompat;
103import com.android.launcher3.compat.LauncherAppsCompat;
104import com.android.launcher3.compat.UserHandleCompat;
105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700107import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700108import com.android.launcher3.dragndrop.DragController;
109import com.android.launcher3.dragndrop.DragLayer;
110import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700111import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700114import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700115import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800116import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700118import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700119import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800120import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700122import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700124import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700126import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800135import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800139import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141/**
142 * Default launcher application.
143 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100144public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700145 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
146 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700147 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700148 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700149 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700151 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700152 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400153 static final boolean DEBUG_RESUME_TIME = 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;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800239 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700240
241 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalffe83f12014-08-14 17:39:34 -0700243 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700244 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700246 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800247 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800248 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700249
Michael Jurka0280c3b2010-09-17 15:00:07 -0700250 private int[] mTmpAddItemCellCoordinates = new int[2];
251
Sunny Goyal316490e2015-06-02 09:38:28 -0700252 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800253 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700254
Michael Jurka838a4ca2011-02-07 13:33:06 -0800255 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700256 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700257
Sunny Goyal47328fd2016-05-25 18:56:41 -0700258 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
260 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700261 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700262 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700263
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700264 // Main container view and the model for the widget tray screen.
265 @Thunk WidgetsContainerView mWidgetsView;
266 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700269 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
270 // scroll issues (because the workspace may not have been measured yet) and extra work.
271 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
272 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
274 private SpannableStringBuilder mDefaultKeySsb = null;
275
Adam Cohen091440a2015-03-18 14:16:05 -0700276 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800278 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private boolean mRestoring;
280 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700281 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700285 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800288 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700289 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700290 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700291 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700293 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800294 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700296 /** Maps launcher activity components to their list of shortcut ids. */
297 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
298
Sunny Goyal639e9062015-08-19 19:17:06 -0700299 private LauncherClings mClings;
300
Adam Cohen61f560d2013-09-30 15:58:20 -0700301 private View.OnTouchListener mHapticFeedbackTouchListener;
302
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 // Related to the auto-advancing of widgets
304 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 private static final int ADVANCE_INTERVAL = 20000;
306 private static final int ADVANCE_STAGGER = 250;
307
308 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700311 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700315 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800316
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700318
Adam Cohen1462de32012-07-24 22:34:36 -0700319 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700321
Winson Chung46353de2012-02-16 14:05:10 -0800322 // We only want to get the SharedPreferences once since it does an FS stat each time we get
323 // it from the context.
324 private SharedPreferences mSharedPrefs;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700333 private DeviceProfile mDeviceProfile;
334
Adam Cohen4b66bf32015-09-18 12:15:19 -0700335 private boolean mMoveToDefaultScreenFromNewIntent;
336
Winson Chung13eb5272015-05-11 16:30:13 -0700337 // This is set to the view that launched the activity that navigated the user away from
338 // launcher. Since there is no callback for when the activity has finished launching, enable
339 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 private BubbleTextView mWaitingForResume;
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
343 new HashMap<String, CustomAppWidget>();
344
Adam Cohen59400422014-03-05 18:07:04 -0800345 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700346 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
347 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800348 }
349 }
350
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700353 if (mWorkspace != null) {
354 mWorkspace.buildPageHardwareLayers();
355 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700356 }
357 };
358
Adam Cohendb364c32014-05-20 14:23:40 -0700359 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360
Adam Cohen091440a2015-03-18 14:16:05 -0700361 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700368 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 }
370
Hyunyoung Songaa953652016-04-19 18:30:24 -0700371 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800372
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700373 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375
376 @Thunk void setOrientation() {
377 if (mRotationEnabled) {
378 unlockScreenOrientation(true);
379 } else {
380 setRequestedOrientation(
381 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384
Sunny Goyal745bad92016-05-02 10:54:12 -0700385 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700403 if (LauncherAppState.PROFILE_STARTUP) {
404 Trace.beginSection("Launcher-onCreate");
405 }
Winson Chunga2413752012-04-03 14:22:34 -0700406
Adam Cohen9211d422014-10-07 18:14:53 -0700407 if (mLauncherCallbacks != null) {
408 mLauncherCallbacks.preOnCreate();
409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Adam Cohen2e6da152015-05-06 11:42:25 -0700415 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700416 mDeviceProfile = getResources().getConfiguration().orientation
417 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700418 app.getInvariantDeviceProfile().landscapeProfile
419 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700420
Sunny Goyalf7258242015-10-19 16:59:07 -0700421 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700425 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700428 mAllAppsController = new AllAppsTransitionController(this);
429 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 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
Sunny Goyale26d1002016-06-20 14:52:14 -0700456 if (LauncherAppState.PROFILE_STARTUP) {
457 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
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);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700515 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700516 }
517 }
518
Adam Cohen9211d422014-10-07 18:14:53 -0700519 private LauncherCallbacks mLauncherCallbacks;
520
521 public void onPostCreate(Bundle savedInstanceState) {
522 super.onPostCreate(savedInstanceState);
523 if (mLauncherCallbacks != null) {
524 mLauncherCallbacks.onPostCreate(savedInstanceState);
525 }
526 }
527
Sunny Goyal32554d12015-12-03 15:31:25 -0800528 /**
529 * Call this after onCreate to set or clear overlay.
530 */
531 public void setLauncherOverlay(LauncherOverlay overlay) {
532 if (overlay != null) {
533 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
534 }
535 mWorkspace.setLauncherOverlay(overlay);
536 }
537
Adam Cohen9211d422014-10-07 18:14:53 -0700538 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
539 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700541 private boolean mWorkspaceImportanceStored = false;
542 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800544 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
546
547 @Override
548 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700550 return;
551 }
552 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700553 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700554 if (mWorkspace != null) {
555 mWorkspaceImportanceForAccessibility =
556 mWorkspace.getImportantForAccessibility();
557 mWorkspace.setImportantForAccessibility(
558 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
559 mWorkspaceImportanceStored = true;
560 }
561 if (mHotseat != null) {
562 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
563 mHotseat.setImportantForAccessibility(
564 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
565 mHotseatImportanceStored = true;
566 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700567 }
568
569 @Override
570 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700571 if (mWorkspaceImportanceStored && mWorkspace != null) {
572 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
573 }
574 if (mHotseatImportanceStored && mHotseat != null) {
575 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
576 }
577 mWorkspaceImportanceStored = false;
578 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700579 }
580 });
Adam Cohen9211d422014-10-07 18:14:53 -0700581 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700582 }
583
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700585 public void onLauncherProviderChange() {
586 if (mLauncherCallbacks != null) {
587 mLauncherCallbacks.onLauncherProviderChange();
588 }
589 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700590
Adam Cohen9211d422014-10-07 18:14:53 -0700591 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700592 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700593 if (mLauncherCallbacks != null) {
594 return mLauncherCallbacks.hasCustomContentToLeft();
595 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700596 return false;
597 }
598
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 * {@link #addToCustomContentPage}. This will only be invoked if
602 * {@link #hasCustomContentToLeft()} is {@code true}.
603 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500604 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700605 if (mLauncherCallbacks != null) {
606 mLauncherCallbacks.populateCustomContentContainer();
607 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 }
609
610 /**
611 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
612 * ensure the custom content page is added or removed if necessary.
613 */
614 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600615 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600616 // Not bound yet, wait for bindScreens to be called.
617 return;
618 }
619
620 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
621 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500622 mWorkspace.createCustomContentContainer();
623 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600624 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
625 mWorkspace.removeCustomContentPage();
626 }
627 }
628
Hyunyoung Songaa953652016-04-19 18:30:24 -0700629 public UserEventDispatcher getUserEventDispatcher() {
630 if (mLauncherCallbacks != null) {
631 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
632 if (dispatcher != null) {
633 return dispatcher;
634 }
635 }
636
Sunny Goyal64976d52016-06-20 14:56:28 -0700637 // Logger object is a singleton and does not have to be coupled with the foreground
638 // activity. Since most user event logging is done on the UI, the object is retrieved
639 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700641 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700642 }
643 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800644 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100645
Hyunyoung Song3f471442015-04-08 19:01:34 -0700646 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
648 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700649 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700650 }
651
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000652 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700653 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
654 // This cast is safe as long as the id < 0x00FFFFFF
655 // Since we jail all the dynamically generated views, there should be no clashes
656 // with any other views.
657 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000658 }
659
660 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700661 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
662 * a configuration step, this allows the proper animations to run after other transitions.
663 */
Adam Cohendb364c32014-05-20 14:23:40 -0700664 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700665 long screenId = args.screenId;
666 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
667 // When the screen id represents an actual screen (as opposed to a rank) we make sure
668 // that the drop page actually exists.
669 screenId = ensurePendingDropLayoutExists(args.screenId);
670 }
Adam Cohendb364c32014-05-20 14:23:40 -0700671
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700674 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700676 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700678 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700680 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700681 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700682 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700683 case REQUEST_BIND_PENDING_APPWIDGET: {
684 int widgetId = args.appWidgetId;
685 LauncherAppWidgetInfo info =
686 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
687 if (info != null) {
688 // Since the view was just bound, also launch the configure activity if needed
689 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
690 .getLauncherAppWidgetInfo(widgetId);
691 if (provider != null && provider.configure != null) {
692 startRestoredWidgetReconfigActivity(provider, info);
693 }
694 }
695 break;
696 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700702 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800705 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700707 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700708 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700709 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800710 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700711
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 Runnable exitSpringLoaded = new Runnable() {
713 @Override
714 public void run() {
715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
716 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
717 }
718 };
719
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700721 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
724 if (resultCode == RESULT_CANCELED) {
725 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700726 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800729 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
732 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
734 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700735 // User could have free-scrolled between pages before picking a wallpaper; make sure
736 // we move to the closest one now.
737 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700738 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739 }
740 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700744 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700745
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 // We have special handling for widgets
748 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 final int appWidgetId;
750 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
751 : -1;
752 if (widgetId < 0) {
753 appWidgetId = pendingAddWidgetId;
754 } else {
755 appWidgetId = widgetId;
756 }
757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800759 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
761 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 result = RESULT_CANCELED;
763 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed(false, 0, null);
768 }
769 };
Adam Cohendb364c32014-05-20 14:23:40 -0700770 if (workspaceLocked) {
771 // No need to remove the empty screen if we're mid-binding, as the
772 // the bind will not add the empty screen.
773 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
774 } else {
775 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
776 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
777 }
Winson Chung5aaab772012-04-27 15:24:02 -0700778 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700779 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700780 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
781 // When the screen id represents an actual screen (as opposed to a rank)
782 // we make sure that the drop page actually exists.
783 mPendingAddInfo.screenId =
784 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
785 }
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
787
788 dropLayout.setDropPending(true);
789 final Runnable onComplete = new Runnable() {
790 @Override
791 public void run() {
792 completeTwoStageWidgetDrop(resultCode, appWidgetId);
793 dropLayout.setDropPending(false);
794 }
795 };
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 } else {
799 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
800 mPendingAddInfo);
801 sPendingAddItem = args;
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 return;
805 }
806
Sunny Goyald478c832016-04-01 12:04:16 -0700807 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
808 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700809 if (resultCode == RESULT_OK) {
810 // Update the widget view.
811 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
812 pendingAddWidgetId, mPendingAddInfo);
813 if (workspaceLocked) {
814 sPendingAddItem = args;
815 } else {
816 completeAdd(args);
817 }
818 }
819 // Leave the widget in the pending state if the user canceled the configure.
820 return;
821 }
822
Sunny Goyalaad90582015-07-09 14:22:50 -0700823 if (requestCode == REQUEST_CREATE_SHORTCUT) {
824 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
825 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
826 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
827 mPendingAddInfo);
828 if (isWorkspaceLocked()) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
834 }
835 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800841
842 }
843
844 @Override
845 protected void onActivityResult(
846 final int requestCode, final int resultCode, final Intent data) {
847 handleActivityResult(requestCode, resultCode, data);
848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
850 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800851 }
852
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700854 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700856 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
857 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
858 View v = null;
859 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
860 if (layout != null) {
861 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
862 }
863 Intent intent = sPendingAddItem.intent;
864 sPendingAddItem = null;
865 if (grantResults.length > 0
866 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700867 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700868 } else {
869 // TODO: Show a snack bar with link to settings
870 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
871 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
872 }
873 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
876 grantResults);
877 }
878 }
879
Adam Cohendb364c32014-05-20 14:23:40 -0700880 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
881 appWidgetId, ItemInfo info) {
882 PendingAddArguments args = new PendingAddArguments();
883 args.requestCode = requestCode;
884 args.intent = data;
885 args.container = info.container;
886 args.screenId = info.screenId;
887 args.cellX = info.cellX;
888 args.cellY = info.cellY;
889 args.appWidgetId = appWidgetId;
890 return args;
891 }
892
893 /**
894 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
895 *
896 * @param screenId the screen id to check
897 * @return the new screen, or screenId if it exists
898 */
899 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800900 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800912 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700953
954 if (Utilities.isNycMR1OrAbove()) {
955 mAppWidgetHost.stopListening();
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
960 protected void onStart() {
961 super.onStart();
962 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700963
964 if (mLauncherCallbacks != null) {
965 mLauncherCallbacks.onStart();
966 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700967
968 if (Utilities.isNycMR1OrAbove()) {
969 mAppWidgetHost.startListening();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200975 long startTime = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400978 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.preOnResume();
983 }
984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700986 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700987
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700989 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800991 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700992 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700993 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700994 // view after launching an app, as they may be depending on the UI to be static to
995 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700996 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700997 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800998 } else if (mOnResumeState == State.WIDGETS) {
999 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 }
1001 mOnResumeState = State.NONE;
1002
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001003 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001004 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1005 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001006
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001007 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001009 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001010 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1016 // execute them here
1017 long startTimeCallbacks = 0;
1018 if (DEBUG_RESUME_TIME) {
1019 startTimeCallbacks = System.currentTimeMillis();
1020 }
1021
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1023 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1028 (System.currentTimeMillis() - startTimeCallbacks));
1029 }
Michael Jurka447bf842013-05-15 14:52:15 +02001030 }
Michael Jurka54554252013-08-01 12:52:23 +02001031 if (mOnResumeCallbacks.size() > 0) {
1032 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1033 mOnResumeCallbacks.get(i).run();
1034 }
1035 mOnResumeCallbacks.clear();
1036 }
Winson Chunge4e50662012-01-23 14:45:13 -08001037
1038 // Reset the pressed state of icons that were locked in the press state while activities
1039 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001040 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001041 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 mWaitingForResume.setStayPressed(false);
1043 }
Winson Chung82963d52013-10-09 11:20:57 -07001044
Adam Cohen06dff352012-06-01 17:17:08 -07001045 // It is possible that widgets can receive updates while launcher is not in the foreground.
1046 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1047 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1048 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001049 if (!isWorkspaceLoading()) {
1050 getWorkspace().reinflateWidgetsIfNecessary();
1051 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001052
Michael Jurka447bf842013-05-15 14:52:15 +02001053 if (DEBUG_RESUME_TIME) {
1054 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1055 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001056
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 // We want to suppress callbacks about CustomContent being shown if we have just received
1058 // onNewIntent while the user was present within launcher. In that case, we post a call
1059 // to move the user to the main screen (which will occur after onResume). We don't want to
1060 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1061 // suppress here.
1062 if (mWorkspace.getCustomContentCallbacks() != null
1063 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001064 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001066 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1067 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001068 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001069 }
1070 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001071 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001072 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001073 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001074
Sunny Goyal756adbc2015-04-16 15:20:51 -07001075 if (!isWorkspaceLoading()) {
1076 // Process any items that were added while Launcher was away.
1077 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1078 }
Adam Cohen9211d422014-10-07 18:14:53 -07001079
1080 if (mLauncherCallbacks != null) {
1081 mLauncherCallbacks.onResume();
1082 }
Adam Cohen06dff352012-06-01 17:17:08 -07001083 }
1084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001087 // Ensure that items added to Launcher are queued until Launcher returns
1088 InstallShortcutReceiver.enableInstallQueue();
1089
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001091 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001092 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001093 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001094
1095 // We call onHide() aggressively. The custom content callbacks should be able to
1096 // debounce excess onHide calls.
1097 if (mWorkspace.getCustomContentCallbacks() != null) {
1098 mWorkspace.getCustomContentCallbacks().onHide();
1099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Adam Cohen9211d422014-10-07 18:14:53 -07001101 if (mLauncherCallbacks != null) {
1102 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001103 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
1105
1106 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001107 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1108 // by a onResume or by scrolling otherwise.
1109 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001110
1111 // Custom content is completely hidden
1112 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001113
1114 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1115 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001116
1117 // Indicates whether the user is allowed to scroll away from the custom content.
1118 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001119 }
1120
Adam Cohenc2d6e892014-10-16 09:49:24 -07001121 public interface LauncherOverlay {
1122
1123 /**
1124 * Touch interaction leading to overscroll has begun
1125 */
1126 public void onScrollInteractionBegin();
1127
1128 /**
1129 * Touch interaction related to overscroll has ended
1130 */
1131 public void onScrollInteractionEnd();
1132
1133 /**
1134 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1135 * screen (or in the case of RTL, the rightmost screen).
1136 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001137 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138
1139 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001140 * Called when the launcher is ready to use the overlay
1141 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001143 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001144 }
1145
Jun Mukai8af0cd82015-05-12 12:32:12 -07001146 public interface LauncherSearchCallbacks {
1147 /**
1148 * Called when the search overlay is shown.
1149 */
1150 public void onSearchOverlayOpened();
1151
1152 /**
1153 * Called when the search overlay is dismissed.
1154 */
1155 public void onSearchOverlayClosed();
1156 }
1157
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001159 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001160 }
1161
1162 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1163
Sunny Goyalc86df472016-02-25 09:19:38 -08001164 public void onScrollChanged(float progress) {
1165 if (mWorkspace != null) {
1166 mWorkspace.onOverlayScrollChanged(progress);
1167 }
1168 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 }
1170
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001172 if (mLauncherCallbacks != null) {
1173 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001174 } else {
1175 // On devices with a locked orientation, we will at least have the allow rotation
1176 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001177 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001178 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 }
1180
Adam Cohen9211d422014-10-07 18:14:53 -07001181 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001182 CustomContentCallbacks callbacks, String description) {
1183 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001184 }
1185
Adam Cohenedb40762013-07-18 16:45:45 -07001186 // The custom content needs to offset its content to account for the QSB
1187 public int getTopOffsetForCustomContent() {
1188 return mWorkspace.getPaddingTop();
1189 }
1190
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001191 @Override
1192 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001194 if (mModel.isCurrentCallbacks(this)) {
1195 mModel.stopLoader();
1196 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001197 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1198
Romain Guy13c2e7b2010-03-10 19:45:00 -08001199 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 @Override
1204 public void onWindowFocusChanged(boolean hasFocus) {
1205 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001206 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001207
1208 if (mLauncherCallbacks != null) {
1209 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1210 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 private boolean acceptFilter() {
1214 final InputMethodManager inputManager = (InputMethodManager)
1215 getSystemService(Context.INPUT_METHOD_SERVICE);
1216 return !inputManager.isFullscreenMode();
1217 }
1218
1219 @Override
1220 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001221 final int uniChar = event.getUnicodeChar();
1222 final boolean handled = super.onKeyDown(keyCode, event);
1223 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1224 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1226 keyCode, event);
1227 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001228 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001229 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001230 // showSearchDialog()
1231 // If there are multiple keystrokes before the search dialog takes focus,
1232 // onSearchRequested() will be called for every keystroke,
1233 // but it is idempotent, so it's fine.
1234 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236 }
1237
Joe Onorato8a9625e2010-01-28 15:55:35 -08001238 // Eat the long press event so the keyboard doesn't come up.
1239 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1240 return true;
1241 }
1242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 return handled;
1244 }
1245
Winson46163472015-09-22 17:31:56 -07001246 @Override
1247 public boolean onKeyUp(int keyCode, KeyEvent event) {
1248 if (keyCode == KeyEvent.KEYCODE_MENU) {
1249 // Ignore the menu key if we are currently dragging or are on the custom content screen
1250 if (!isOnCustomContent() && !mDragController.isDragging()) {
1251 // Close any open folders
1252 closeFolder();
1253
1254 // Stop resizing any widgets
1255 mWorkspace.exitWidgetResizeMode();
1256
1257 // Show the overview mode if we are on the workspace
1258 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1259 !mWorkspace.isSwitchingState()) {
1260 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001261 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001262 }
1263 }
1264 return true;
1265 }
1266 return super.onKeyUp(keyCode, event);
1267 }
1268
Karl Rosaen138a0412009-04-23 19:00:21 -07001269 private String getTypedText() {
1270 return mDefaultKeySsb.toString();
1271 }
1272
1273 private void clearTypedText() {
1274 mDefaultKeySsb.clear();
1275 mDefaultKeySsb.clearSpans();
1276 Selection.setSelection(mDefaultKeySsb, 0);
1277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001280 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1281 * State
1282 */
1283 private static State intToState(int stateOrdinal) {
1284 State state = State.WORKSPACE;
1285 final State[] stateValues = State.values();
1286 for (int i = 0; i < stateValues.length; i++) {
1287 if (stateValues[i].ordinal() == stateOrdinal) {
1288 state = stateValues[i];
1289 break;
1290 }
1291 }
1292 return state;
1293 }
1294
1295 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 * Restores the previous state, if it exists.
1297 *
1298 * @param savedState The previous state.
1299 */
1300 private void restoreState(Bundle savedState) {
1301 if (savedState == null) {
1302 return;
1303 }
1304
Michael Jurka883f55b2010-10-21 15:47:14 -07001305 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001306 if (state == State.APPS || state == State.WIDGETS) {
1307 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001308 }
1309
Adam Cohen21cd0022013-10-09 18:57:02 -07001310 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1311 PagedView.INVALID_RESTORE_PAGE);
1312 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001313 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
Sunny Goyal756cd262015-08-20 12:33:21 -07001316 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1317 if (itemValues != null) {
1318 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001319 AppWidgetProviderInfo info = savedState.getParcelable(
1320 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001321 mPendingAddWidgetInfo = info == null ?
1322 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1323
Adam Cohen4637b5a2013-11-04 18:21:24 -08001324 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001325 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 mRestoring = true;
1327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
1330 /**
1331 * Finds all the views we need and configure them properly.
1332 */
1333 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001334 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001335 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001336 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001337 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001338 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001339
Sunny Goyal784f9c32016-03-23 16:56:54 -07001340 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1341 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1342 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001343 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344
Winson Chung4c98d922011-05-31 16:50:48 -07001345 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001346
1347 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001348
Winson Chung3d503fb2011-07-13 17:25:49 -07001349 // Setup the hotseat
1350 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1351 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001352 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 }
1354
Winsone9f27272015-10-13 10:47:51 -07001355 // Setup the overview panel
1356 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001357
Winson Chung4c98d922011-05-31 16:50:48 -07001358 // Setup the workspace
1359 mWorkspace.setHapticFeedbackEnabled(false);
1360 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001361 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001362 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001363 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001364
Tony Wickham34d2c912015-09-11 09:27:58 -07001365 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001366 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001367
Winson Chungef7f8742015-06-04 17:18:17 -07001368 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001369 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001370 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001371 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1372 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1373 } else {
1374 mAppsView.setSearchBarController(new DefaultAppSearchController());
1375 }
Craig Mautner360310b2012-10-26 15:13:08 -07001376
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001378 mDragController.setDragScoller(mWorkspace);
1379 mDragController.setScrollView(mDragLayer);
1380 mDragController.setMoveTarget(mWorkspace);
1381 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001382 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001383
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001384 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1385
Sunny Goyal322d5562015-06-25 19:35:49 -07001386 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1387 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
Winsone9f27272015-10-13 10:47:51 -07001391 private void setupOverviewPanel() {
1392 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1393
1394 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1395 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1396 @Override
1397 public boolean onLongClick(View v) {
1398 return v.performClick();
1399 }
1400 };
1401
1402 // Bind wallpaper button actions
1403 View wallpaperButton = findViewById(R.id.wallpaper_button);
1404 wallpaperButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View view) {
1407 if (!mWorkspace.isSwitchingState()) {
1408 onClickWallpaperPicker(view);
1409 }
1410 }
1411 });
1412 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1413 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1414
1415 // Bind widget button actions
1416 mWidgetsButton = findViewById(R.id.widget_button);
1417 mWidgetsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickAddWidgetButton(view);
1422 }
1423 }
1424 });
1425 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1426 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind settings actions
1429 View settingsButton = findViewById(R.id.settings_button);
1430 boolean hasSettings = hasSettings();
1431 if (hasSettings) {
1432 settingsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickSettingsButton(view);
1437 }
1438 }
1439 });
1440 settingsButton.setOnLongClickListener(performClickOnLongClick);
1441 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442 } else {
1443 settingsButton.setVisibility(View.GONE);
1444 }
1445
1446 mOverviewPanel.setAlpha(0f);
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001451 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001452 */
1453 public void setAllAppsButton(View allAppsButton) {
1454 mAllAppsButton = allAppsButton;
1455 }
1456
Sunny Goyalbb011da2016-06-15 15:42:29 -07001457 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001458 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 }
1460
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001461 public View getWidgetsButton() {
1462 return mWidgetsButton;
1463 }
1464
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * Creates a view representing a shortcut.
1467 *
1468 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001470 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001471 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * @param parent The group the shortcut belongs to.
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from layoutResId.
1481 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001482 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001483 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 parent, false);
1485 favorite.applyFromShortcutInfo(info, mIconCache);
1486 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001488 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 return favorite;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Add a shortcut to the workspace.
1494 *
1495 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001500 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Sunny Goyal5c97f512015-05-19 16:03:28 -07001502 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001503 if (info == null) {
1504 return;
1505 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 final View view = createShortcut(info);
1507
Sunny Goyal5c97f512015-05-19 16:03:28 -07001508 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 // First we check if we already know the exact location where we want to add this item.
1510 if (cellX >= 0 && cellY >= 0) {
1511 cellXY[0] = cellX;
1512 cellXY[1] = cellY;
1513 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001514
1515 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001516 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 true, null,null)) {
1518 return;
1519 }
1520 DragObject dragObject = new DragObject();
1521 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1523 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 return;
1525 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 }
1529
1530 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 return;
1533 }
1534
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001535 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001539 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001544 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001546 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohen091440a2015-03-18 14:16:05 -07001548 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001549 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1550
1551 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001553 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohen59400422014-03-05 18:07:04 -08001556 if (appWidgetInfo.isCustomWidget) {
1557 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001558 }
1559
Adam Cohen59400422014-03-05 18:07:04 -08001560 LauncherAppWidgetInfo launcherInfo;
1561 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1562 launcherInfo.spanX = info.spanX;
1563 launcherInfo.spanY = info.spanY;
1564 launcherInfo.minSpanX = info.minSpanX;
1565 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001566 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001569 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (hostView == null) {
1573 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001576 hostView.setVisibility(View.VISIBLE);
1577 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001579 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 private void addAppWidgetToWorkspace(
1583 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setTag(item);
1586 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 hostView.setFocusable(true);
1589 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1593
1594 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 }
1597 }
1598
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1600 @Override
1601 public void onReceive(Context context, Intent intent) {
1602 final String action = intent.getAction();
1603 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1604 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001605 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001606 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001607
Winson Chungc100e8e2011-08-09 16:02:43 -07001608 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001609 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001610 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001611 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001612 if (!showWorkspace(false)) {
1613 // If we are already on the workspace, then manually reset all apps
1614 mAppsView.reset();
1615 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001616 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1618 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001619 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 }
1621 }
1622 };
1623
1624 @Override
1625 public void onAttachedToWindow() {
1626 super.onAttachedToWindow();
1627
1628 // Listen for broadcasts related to user-presence
1629 final IntentFilter filter = new IntentFilter();
1630 filter.addAction(Intent.ACTION_SCREEN_OFF);
1631 filter.addAction(Intent.ACTION_USER_PRESENT);
1632 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001633 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001634 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001636
1637 if (mLauncherCallbacks != null) {
1638 mLauncherCallbacks.onAttachedToWindow();
1639 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 }
1641
1642 @Override
1643 public void onDetachedFromWindow() {
1644 super.onDetachedFromWindow();
1645 mVisible = false;
1646
Adam Cohend113e0c2010-11-11 10:48:05 -08001647 if (mAttached) {
1648 unregisterReceiver(mReceiver);
1649 mAttached = false;
1650 }
Winson Chungb745afb2015-03-02 11:51:23 -08001651 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001652
1653 if (mLauncherCallbacks != null) {
1654 mLauncherCallbacks.onDetachedFromWindow();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 public void onWindowVisibilityChanged(int visibility) {
1659 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001661 // The following code used to be in onResume, but it turns out onResume is called when
1662 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1663 // is a more appropriate event to handle
1664 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001665 if (!mWorkspaceLoading) {
1666 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001667 // We want to let Launcher draw itself at least once before we force it to build
1668 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001669 // apps is nice and speedy.
1670 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001671 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001672 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001673 if (mStarted) return;
1674 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001675 // We delay the layer building a bit in order to give
1676 // other message processing a time to run. In particular
1677 // this avoids a delay in hiding the IME if it was
1678 // currently shown, because doing that may involve
1679 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001680 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 final ViewTreeObserver.OnDrawListener listener = this;
1682 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001683 public void run() {
1684 if (mWorkspace != null &&
1685 mWorkspace.getViewTreeObserver() != null) {
1686 mWorkspace.getViewTreeObserver().
1687 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001689 }
1690 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001691 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001692 }
1693 });
1694 }
1695 clearTypedText();
1696 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 }
1698
Adam Cohen091440a2015-03-18 14:16:05 -07001699 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 mHandler.removeMessages(ADVANCE_MSG);
1701 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1702 mHandler.sendMessageDelayed(msg, delay);
1703 mAutoAdvanceSentTime = System.currentTimeMillis();
1704 }
1705
Adam Cohen091440a2015-03-18 14:16:05 -07001706 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1708 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1709 mAutoAdvanceRunning = autoAdvanceRunning;
1710 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001711 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 sendAdvanceMessage(delay);
1713 } else {
1714 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001715 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1717 }
1718 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001719 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 }
1721 }
1722 }
1723
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1725
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001727 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 if (msg.what == ADVANCE_MSG) {
1729 int i = 0;
1730 for (View key: mWidgetsToAdvance.keySet()) {
1731 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001732 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 public void run() {
1736 ((Advanceable) v).advance();
1737 }
1738 }, delay);
1739 }
1740 i++;
1741 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001742 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001746 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001747
Winsonc0b52fe2015-09-09 16:38:15 -07001748 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001749 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1751 if (v instanceof Advanceable) {
1752 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001753 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001754 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
1756 }
1757
Winsonc0b52fe2015-09-09 16:38:15 -07001758 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 if (mWidgetsToAdvance.containsKey(hostView)) {
1760 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001761 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 }
1764
Hyunyoung Song3f471442015-04-08 19:01:34 -07001765 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001766 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1767 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769
Winson Chunga6945242014-01-08 14:04:34 -08001770 public DragLayer getDragLayer() {
1771 return mDragLayer;
1772 }
1773
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001774 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001775 return mAppsView;
1776 }
1777
Hyunyoung Song3f471442015-04-08 19:01:34 -07001778 public WidgetsContainerView getWidgetsView() {
1779 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001780 }
1781
Winson Chunga6945242014-01-08 14:04:34 -08001782 public Workspace getWorkspace() {
1783 return mWorkspace;
1784 }
1785
1786 public Hotseat getHotseat() {
1787 return mHotseat;
1788 }
1789
Jorim Jaggid017f882014-01-14 17:08:48 -08001790 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001791 return mOverviewPanel;
1792 }
1793
Sunny Goyal47328fd2016-05-25 18:56:41 -07001794 public DropTargetBar getDropTargetBar() {
1795 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001796 }
1797
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001798 public LauncherAppWidgetHost getAppWidgetHost() {
1799 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Winson Chunga9abd0e2010-10-27 17:18:37 -07001802 public LauncherModel getModel() {
1803 return mModel;
1804 }
1805
Adam Cohen79d90c52016-04-22 13:29:20 -07001806 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001807 return mSharedPrefs;
1808 }
1809
Adam Cohen2e6da152015-05-06 11:42:25 -07001810 public DeviceProfile getDeviceProfile() {
1811 return mDeviceProfile;
1812 }
1813
Bjorn Bringertc459e522013-06-07 19:36:01 +01001814 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001815 getWindow().closeAllPanels();
1816
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001817 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001818 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 @Override
1822 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001823 long startTime = 0;
1824 if (DEBUG_RESUME_TIME) {
1825 startTime = System.currentTimeMillis();
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 super.onNewIntent(intent);
1828
1829 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001830 Folder openFolder = mWorkspace.getOpenFolder();
1831 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1832 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1833 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1834 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1835 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001838
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 if (mWorkspace == null) {
1840 // Can be cases where mWorkspace is null, this prevents a NPE
1841 return;
1842 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001843 // In all these cases, only animate if we're already on home
1844 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001845
Sunny Goyal935fca12015-10-13 10:19:01 -07001846 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 exitSpringLoadedDragMode();
1848
1849 // If we are already on home, then just animate back to the workspace,
1850 // otherwise, just wait until onResume to set the state back to Workspace
1851 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001852 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001853 } else {
1854 mOnResumeState = State.WORKSPACE;
1855 }
1856
1857 final View v = getWindow().peekDecorView();
1858 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001859 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 INPUT_METHOD_SERVICE);
1861 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1862 }
1863
Winson Chungb745afb2015-03-02 11:51:23 -08001864 // Reset the apps view
1865 if (!alreadyOnHome && mAppsView != null) {
1866 mAppsView.scrollToTop();
1867 }
1868
Hyunyoung Song3f471442015-04-08 19:01:34 -07001869 // Reset the widgets view
1870 if (!alreadyOnHome && mWidgetsView != null) {
1871 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001873
Adam Cohen9211d422014-10-07 18:14:53 -07001874 if (mLauncherCallbacks != null) {
1875 mLauncherCallbacks.onHomeIntent();
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
Adam Cohened307df2013-10-02 09:37:31 -07001878
Adam Cohen9211d422014-10-07 18:14:53 -07001879 if (mLauncherCallbacks != null) {
1880 mLauncherCallbacks.onNewIntent(intent);
1881 }
Winson15f8b172015-08-19 11:02:39 -07001882
1883 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1884 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1885 // animation.
1886 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001887 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1888 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001889 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1890 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001891
1892 // We use this flag to suppress noisy callbacks above custom content state
1893 // from onResume.
1894 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001895 mWorkspace.post(new Runnable() {
1896 @Override
1897 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001898 if (mWorkspace != null) {
1899 mWorkspace.moveToDefaultScreen(true);
1900 }
Winson15f8b172015-08-19 11:02:39 -07001901 }
1902 });
1903 }
1904 }
1905
1906 if (DEBUG_RESUME_TIME) {
1907 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1908 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001912 public void onRestoreInstanceState(Bundle state) {
1913 super.onRestoreInstanceState(state);
1914 for (int page: mSynchronouslyBoundPages) {
1915 mWorkspace.restoreInstanceStateForChild(page);
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
1918
1919 @Override
1920 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001921 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001922 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1923 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001924
Adam Cohen21cd0022013-10-09 18:57:02 -07001925 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001926 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927
Michael Jurka883f55b2010-10-21 15:47:14 -07001928 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001929 // We close any open folder since it will not be re-opened, and we need to make sure
1930 // this state is reflected.
1931 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1932 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001936 ContentValues itemValues = new ContentValues();
1937 mPendingAddInfo.writeToValues(itemValues);
1938 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001939 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001940 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942
Adam Cohen9211d422014-10-07 18:14:53 -07001943 if (mLauncherCallbacks != null) {
1944 mLauncherCallbacks.onSaveInstanceState(outState);
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
1948 @Override
1949 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001951
Winson Chunge7a03942011-08-05 15:05:12 -07001952 // Remove all pending runnables
1953 mHandler.removeMessages(ADVANCE_MSG);
1954 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001955 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001956 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001957
Winson Chungcd2b0142011-06-08 16:02:26 -07001958 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001959 // It's possible to receive onDestroy after a new Launcher activity has
1960 // been created. In this case, don't interfere with the new Launcher.
1961 if (mModel.isCurrentCallbacks(this)) {
1962 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001963 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001964 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001965
Sunny Goyal745bad92016-05-02 10:54:12 -07001966 if (mRotationPrefChangeHandler != null) {
1967 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1968 }
1969
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001971 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001973 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001975 mAppWidgetHost = null;
1976
1977 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978
1979 TextKeyListener.getInstance().release();
1980
Tony Wickhame2217252016-03-22 16:34:23 -07001981 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1982 .removeAccessibilityStateChangeListener(this);
1983
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001984 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985
Michael Jurka2ecf9952012-06-18 12:52:28 -07001986 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001987
1988 if (mLauncherCallbacks != null) {
1989 mLauncherCallbacks.onDestroy();
1990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992
Sunny Goyalae502842016-06-17 08:43:56 -07001993 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1994 return mAccessibilityDelegate;
1995 }
1996
Adam Cohena9cf38f2011-05-02 15:36:58 -07001997 public DragController getDragController() {
1998 return mDragController;
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 @Override
2002 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002003 onStartForResult(requestCode);
2004 super.startActivityForResult(intent, requestCode);
2005 }
2006
2007 @Override
2008 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2009 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2010 onStartForResult(requestCode);
2011 try {
2012 super.startIntentSenderForResult(intent, requestCode,
2013 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2014 } catch (IntentSender.SendIntentException e) {
2015 throw new ActivityNotFoundException();
2016 }
2017 }
2018
2019 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002020 if (requestCode >= 0) {
2021 setWaitingForResult(true);
2022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
2024
Winson Chung70d72102011-08-12 11:24:35 -07002025 /**
2026 * Indicates that we want global search for this activity by setting the globalSearch
2027 * argument for {@link #startSearch} to true.
2028 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002030 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002032
Karl Rosaen138a0412009-04-23 19:00:21 -07002033 if (initialQuery == null) {
2034 // Use any text typed in the launcher as the initial query
2035 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 if (appSearchData == null) {
2038 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002039 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002041
2042 // TODO send proper bounds.
2043 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002044
Ian Parkinson047036e2014-09-01 15:40:53 +01002045 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002046 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002047 if (clearTextImmediately) {
2048 clearTypedText();
2049 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002050
2051 // We need to show the workspace after starting the search
2052 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002053 }
2054
Ian Parkinson047036e2014-09-01 15:40:53 +01002055 /**
2056 * Start a text search.
2057 *
2058 * @return {@code true} if the search will start immediately, so any further keypresses
2059 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2060 * to buffer keypresses.
2061 */
2062 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002064 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2065 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2066 sourceBounds);
2067 }
2068
Michael Jurkaa33411c2012-06-14 16:18:21 -07002069 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002071 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002072 }
2073
2074 /**
2075 * Starts the global search activity. This code is a copied from SearchManager
2076 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002078 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002079 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002080 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2081 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2082 if (globalSearchActivity == null) {
2083 Log.w(TAG, "No global search activity found.");
2084 return;
2085 }
2086 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2087 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2088 intent.setComponent(globalSearchActivity);
2089 // Make sure that we have a Bundle to put source in
2090 if (appSearchData == null) {
2091 appSearchData = new Bundle();
2092 } else {
2093 appSearchData = new Bundle(appSearchData);
2094 }
Adam Cohen9211d422014-10-07 18:14:53 -07002095 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 if (!appSearchData.containsKey("source")) {
2097 appSearchData.putString("source", getPackageName());
2098 }
2099 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2100 if (!TextUtils.isEmpty(initialQuery)) {
2101 intent.putExtra(SearchManager.QUERY, initialQuery);
2102 }
2103 if (selectInitialQuery) {
2104 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2105 }
2106 intent.setSourceBounds(sourceBounds);
2107 try {
2108 startActivity(intent);
2109 } catch (ActivityNotFoundException ex) {
2110 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113
Yura4f93ec62014-02-04 14:15:21 +00002114 public boolean isOnCustomContent() {
2115 return mWorkspace.isOnOrMovingToCustomContent();
2116 }
2117
Winson Chung70d72102011-08-12 11:24:35 -07002118 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002119 public boolean onPrepareOptionsMenu(Menu menu) {
2120 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002121 if (mLauncherCallbacks != null) {
2122 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2123 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002124 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002127 @Override
2128 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002129 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002130 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002131 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002132 }
2133
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 public boolean isWorkspaceLocked() {
2135 return mWorkspaceLoading || mWaitingForResult;
2136 }
2137
Adam Cohen517a7f52014-03-01 12:12:59 -08002138 public boolean isWorkspaceLoading() {
2139 return mWorkspaceLoading;
2140 }
2141
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002142 private void setWorkspaceLoading(boolean value) {
2143 boolean isLocked = isWorkspaceLocked();
2144 mWorkspaceLoading = value;
2145 if (isLocked != isWorkspaceLocked()) {
2146 onWorkspaceLockedChanged();
2147 }
2148 }
2149
2150 private void setWaitingForResult(boolean value) {
2151 boolean isLocked = isWorkspaceLocked();
2152 mWaitingForResult = value;
2153 if (isLocked != isWorkspaceLocked()) {
2154 onWorkspaceLockedChanged();
2155 }
2156 }
2157
Adam Cohen9211d422014-10-07 18:14:53 -07002158 protected void onWorkspaceLockedChanged() {
2159 if (mLauncherCallbacks != null) {
2160 mLauncherCallbacks.onWorkspaceLockedChanged();
2161 }
2162 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002163
Michael Jurka0280c3b2010-09-17 15:00:07 -07002164 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002165 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002166 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2168 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002169 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002170 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002171
Tony Wickhama0628cc2015-10-14 15:23:04 -07002172 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002173 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002174 if (LOGD) {
2175 Log.d(TAG, "Adding widget from drop");
2176 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002177 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2178 }
2179
Sunny Goyale6b63a32015-01-16 16:14:29 -08002180 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002181 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2182 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002183 if (appWidgetInfo.configure != null) {
2184 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002185 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002186
Bjorn Bringert7984c942009-12-09 15:38:25 +00002187 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002188 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2189 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002192 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002193 Runnable onComplete = new Runnable() {
2194 @Override
2195 public void run() {
2196 // Exit spring loaded mode if necessary after adding the widget
2197 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2198 null);
2199 }
2200 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002202 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002203 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 }
2205 }
Romain Guycbb89e42009-06-08 15:52:54 -07002206
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002207 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002208 // Close any folders that may be open.
2209 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002210 mWorkspace.moveToCustomContentScreen(animate);
2211 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002212
2213 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2214 int[] cell, int spanX, int spanY) {
2215 switch (info.itemType) {
2216 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2217 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2218 int span[] = new int[2];
2219 span[0] = spanX;
2220 span[1] = spanY;
2221 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2222 container, screenId, cell, span);
2223 break;
2224 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2225 processShortcutFromDrop(info.componentName, container, screenId, cell);
2226 break;
2227 default:
2228 throw new IllegalStateException("Unknown item type: " + info.itemType);
2229 }
2230 }
2231
Adam Cohenfbba09b2011-07-18 21:43:05 -07002232 /**
2233 * Process a shortcut drop.
2234 *
2235 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002239 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2240 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244
2245 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.cellX = cell[0];
2247 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249
2250 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2251 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002252 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 }
2254
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 /**
2256 * Process a widget drop.
2257 *
2258 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002262 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2263 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 mPendingAddInfo.minSpanX = info.minSpanX;
2268 mPendingAddInfo.minSpanY = info.minSpanY;
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.cellX = cell[0];
2272 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 if (span != null) {
2275 mPendingAddInfo.spanX = span[0];
2276 mPendingAddInfo.spanY = span[1];
2277 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278
Adam Cohened66b2b2012-01-23 17:28:51 -08002279 AppWidgetHostView hostView = info.boundWidget;
2280 int appWidgetId;
2281 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002282 // In the case where we've prebound the widget, we remove it from the DragLayer
2283 if (LOGD) {
2284 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2285 }
2286 getDragLayer().removeView(hostView);
2287
Adam Cohened66b2b2012-01-23 17:28:51 -08002288 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002289 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002290
2291 // Clear the boundWidget so that it doesn't get destroyed.
2292 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002294 // In this case, we either need to start an activity to get permission to bind
2295 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002297 Bundle options = info.bindOptions;
2298
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2300 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002302 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002304 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002305 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002308 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2309 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002310 // TODO: we need to make sure that this accounts for the options bundle.
2311 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002312 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2313 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002314 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 }
2316
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002319 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 folderInfo.title = getText(R.string.folder_name);
2321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002323 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2324 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325
2326 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 FolderIcon newFolder =
2328 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002331 // Force measure the new folder icon
2332 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2333 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002334 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
Romain Guycbb89e42009-06-08 15:52:54 -07002336
Winsonc0b52fe2015-09-09 16:38:15 -07002337 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002338 * Unbinds the view for the specified item, and removes the item and all its children.
2339 *
2340 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002341 * @param itemInfo the {@link ItemInfo} for this view.
2342 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002343 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002344 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002345 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002346 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002347 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2348 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002349 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002350 } else {
2351 mWorkspace.removeWorkspaceItem(v);
2352 }
Winsonc0b52fe2015-09-09 16:38:15 -07002353 if (deleteFromDb) {
2354 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2355 }
2356 } else if (itemInfo instanceof FolderInfo) {
2357 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002358 if (v instanceof FolderIcon) {
2359 ((FolderIcon) v).removeListeners();
2360 }
Winsonc0b52fe2015-09-09 16:38:15 -07002361 mWorkspace.removeWorkspaceItem(v);
2362 if (deleteFromDb) {
2363 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2364 }
2365 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2366 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002367 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002368 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002369 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002370 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002371 }
2372 } else {
2373 return false;
2374 }
2375 return true;
2376 }
2377
2378 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002379 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002380 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002381 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002382 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002383 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002384 // Deleting an app widget ID is a void call but writes to disk before returning
2385 // to the caller...
2386 new AsyncTask<Void, Void, Void>() {
2387 public Void doInBackground(Void ... args) {
2388 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2389 return null;
2390 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002391 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002392 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002394 }
2395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 @Override
2397 public boolean dispatchKeyEvent(KeyEvent event) {
2398 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2399 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002401 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002402 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002403 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002404 dumpState();
2405 return true;
2406 }
2407 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002408 }
2409 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2410 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 return true;
2413 }
2414 }
2415
2416 return super.dispatchKeyEvent(event);
2417 }
2418
Joe Onorato88ec0992009-11-19 13:16:06 -08002419 @Override
2420 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002421 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2422 return;
2423 }
2424
Sunny Goyal45478022015-06-08 16:52:41 -07002425 if (mDragController.isDragging()) {
2426 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002427 return;
2428 }
2429
Winson Chungb745afb2015-03-02 11:51:23 -08002430 if (isAppsViewVisible()) {
2431 showWorkspace(true);
2432 } else if (isWidgetsViewVisible()) {
2433 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002435 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002436 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002437 Folder openFolder = mWorkspace.getOpenFolder();
2438 if (openFolder.isEditingName()) {
2439 openFolder.dismissEditingName();
2440 } else {
2441 closeFolder();
2442 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002443 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002444 mWorkspace.exitWidgetResizeMode();
2445
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002446 // Back button is a no-op here, but give at least some feedback for the button press
2447 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002448 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 * Launches the intent referred by the clicked shortcut.
2453 *
2454 * @param v The view representing the clicked shortcut.
2455 */
2456 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002457 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2458 // view has detached (it's possible for this to happen if the view is removed mid touch).
2459 if (v.getWindowToken() == null) {
2460 return;
2461 }
2462
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002463 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002464 return;
2465 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002466
Adam Cohen1697b792013-09-17 19:08:21 -07002467 if (v instanceof Workspace) {
2468 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002469 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002470 }
2471 return;
2472 }
2473
2474 if (v instanceof CellLayout) {
2475 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002476 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2477 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002478 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002479 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002483 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002484 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002486 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002487 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002488 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002489 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002490 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002491 } else if (tag instanceof AppInfo) {
2492 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002493 } else if (tag instanceof LauncherAppWidgetInfo) {
2494 if (v instanceof PendingAppWidgetHostView) {
2495 onClickPendingWidget((PendingAppWidgetHostView) v);
2496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 }
2498 }
2499
Sunny Goyal70660032015-05-14 00:07:08 -07002500 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002501 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002502 return false;
2503 }
2504
Michael Jurkaaf442092010-06-10 17:01:57 -07002505 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002506 * Event handler for the app widget view which has not fully restored.
2507 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002508 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002509 if (mIsSafeModeEnabled) {
2510 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2511 return;
2512 }
2513
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002514 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002515 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002516 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2517 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2518 // This should not happen, as we make sure that an Id is allocated during bind.
2519 return;
2520 }
2521 LauncherAppWidgetProviderInfo appWidgetInfo =
2522 mAppWidgetManager.findProvider(info.providerName, info.user);
2523 if (appWidgetInfo != null) {
2524 mPendingAddWidgetId = info.appWidgetId;
2525 mPendingAddInfo.copyFrom(info);
2526 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002527
Sunny Goyald478c832016-04-01 12:04:16 -07002528 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2529 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2530 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2531 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2532 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2533 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2534 }
2535 } else {
2536 LauncherAppWidgetProviderInfo appWidgetInfo =
2537 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2538 if (appWidgetInfo != null) {
2539 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2540 }
Sunny Goyalff572272014-07-23 13:58:07 -07002541 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002542 } else if (info.installProgress < 0) {
2543 // The install has not been queued
2544 final String packageName = info.providerName.getPackageName();
2545 showBrokenAppInstallDialog(packageName,
2546 new DialogInterface.OnClickListener() {
2547 public void onClick(DialogInterface dialog, int id) {
2548 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2549 }
2550 });
2551 } else {
2552 // Download has started.
2553 final String packageName = info.providerName.getPackageName();
2554 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002555 }
2556 }
2557
Sunny Goyald478c832016-04-01 12:04:16 -07002558 private void startRestoredWidgetReconfigActivity(
2559 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2560 mPendingAddWidgetInfo = provider;
2561 mPendingAddInfo.copyFrom(info);
2562 mPendingAddWidgetId = info.appWidgetId;
2563 mAppWidgetManager.startConfigActivity(provider,
2564 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2565 }
2566
Sunny Goyalff572272014-07-23 13:58:07 -07002567 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002568 * Event handler for the "grid" button that appears on the home screen, which
2569 * enters all apps mode.
2570 *
2571 * @param v The view that was clicked.
2572 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002573 protected void onClickAllAppsButton(View v) {
2574 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002575 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002576 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002577 true /* updatePredictedApps */, false /* focusSearchBar */);
2578 }
2579 }
2580
2581 protected void onLongClickAllAppsButton(View v) {
2582 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2583 if (!isAppsViewVisible()) {
2584 showAppsView(true /* animated */, false /* resetListToTop */,
2585 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 }
2587 }
2588
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 private void showBrokenAppInstallDialog(final String packageName,
2590 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002591 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002592 .setTitle(R.string.abandoned_promises_title)
2593 .setMessage(R.string.abandoned_promise_explanation)
2594 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2595 .setNeutralButton(R.string.abandoned_clean_this,
2596 new DialogInterface.OnClickListener() {
2597 public void onClick(DialogInterface dialog, int id) {
2598 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2599 mWorkspace.removeAbandonedPromise(packageName, user);
2600 }
2601 })
2602 .create().show();
2603 return;
2604 }
2605
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 /**
2607 * Event handler for an app shortcut click.
2608 *
2609 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2610 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002611 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2613 Object tag = v.getTag();
2614 if (!(tag instanceof ShortcutInfo)) {
2615 throw new IllegalArgumentException("Input must be a Shortcut");
2616 }
2617
2618 // Open shortcut
2619 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002620
2621 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002622 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2623 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002624 // Launch activity anyway, framework will tell the user why the app is suspended.
2625 } else {
2626 int error = R.string.activity_not_available;
2627 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2628 error = R.string.safemode_shortcut_error;
2629 }
2630 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2631 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633 }
2634
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002636 if ((v instanceof BubbleTextView)
2637 && shortcut.isPromise()
2638 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002639 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002640 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 new DialogInterface.OnClickListener() {
2642 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002643 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002650 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 }
2652
Sunny Goyala7ce1662016-05-31 15:01:35 -07002653 private void startAppShortcutOrInfoActivity(View v) {
2654 ItemInfo item = (ItemInfo) v.getTag();
2655 Intent intent = item.getIntent();
2656 if (intent == null) {
2657 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002658 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002660 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661
2662 if (success && v instanceof BubbleTextView) {
2663 mWaitingForResume = (BubbleTextView) v;
2664 mWaitingForResume.setStayPressed(true);
2665 }
2666 }
2667
2668 /**
2669 * Event handler for a folder icon click.
2670 *
2671 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2672 */
2673 protected void onClickFolderIcon(View v) {
2674 if (LOGD) Log.d(TAG, "onClickFolder");
2675 if (!(v instanceof FolderIcon)){
2676 throw new IllegalArgumentException("Input must be a FolderIcon");
2677 }
2678
2679 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002680 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002681 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002682 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002683 }
Michael Jurka5130e402011-10-13 04:55:35 -07002684 }
2685
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002686 /**
2687 * Event handler for the (Add) Widgets button that appears after a long press
2688 * on the home screen.
2689 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002690 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002692 if (mIsSafeModeEnabled) {
2693 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2694 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002695 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002696 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002697 }
2698
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 /**
2700 * Event handler for the wallpaper picker button that appears after a long press
2701 * on the home screen.
2702 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002703 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002704 if (!Utilities.isWallapaperAllowed(this)) {
2705 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2706 return;
2707 }
2708
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002709 String pickerPackage = getString(R.string.wallpaper_picker_package);
2710 if (TextUtils.isEmpty(pickerPackage)) {
2711 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2712 }
2713
Tony Wickham785f7a52015-08-31 17:28:32 -07002714 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2715 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002716 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2717 .setPackage(pickerPackage)
2718 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2719 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 }
2721
2722 /**
2723 * Event handler for a click on the settings button that appears after a long press
2724 * on the home screen.
2725 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002726 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002728 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2729 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 }
2731
Adam Cohen61f560d2013-09-30 15:58:20 -07002732 public View.OnTouchListener getHapticFeedbackTouchListener() {
2733 if (mHapticFeedbackTouchListener == null) {
2734 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002735 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002736 @Override
2737 public boolean onTouch(View v, MotionEvent event) {
2738 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2739 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2740 }
2741 return false;
2742 }
2743 };
2744 }
2745 return mHapticFeedbackTouchListener;
2746 }
2747
Tony Wickhame2217252016-03-22 16:34:23 -07002748 @Override
2749 public void onAccessibilityStateChanged(boolean enabled) {
2750 mDragLayer.onAccessibilityStateChanged(enabled);
2751 }
2752
Adam Cohen9211d422014-10-07 18:14:53 -07002753 public void onDragStarted(View view) {
2754 if (isOnCustomContent()) {
2755 // Custom content screen doesn't participate in drag and drop. If on custom
2756 // content screen, move to default.
2757 moveWorkspaceToDefaultScreen();
2758 }
Adam Cohen9211d422014-10-07 18:14:53 -07002759 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002760
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002761 /**
2762 * Called when the user stops interacting with the launcher.
2763 * This implies that the user is now on the homescreen and is not doing housekeeping.
2764 */
Adam Cohen9211d422014-10-07 18:14:53 -07002765 protected void onInteractionEnd() {
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onInteractionEnd();
2768 }
2769 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002770
2771 /**
2772 * Called when the user starts interacting with the launcher.
2773 * The possible interactions are:
2774 * - open all apps
2775 * - reorder an app shortcut, or a widget
2776 * - open the overview mode.
2777 * This is a good time to stop doing things that only make sense
2778 * when the user is on the homescreen and not doing housekeeping.
2779 */
Adam Cohen9211d422014-10-07 18:14:53 -07002780 protected void onInteractionBegin() {
2781 if (mLauncherCallbacks != null) {
2782 mLauncherCallbacks.onInteractionBegin();
2783 }
2784 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002785
Winson Chungcd99cd32015-04-29 11:03:24 -07002786 /** Updates the interaction state. */
2787 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002788 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002789 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002790 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2791 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002792 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002793 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002794 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002795 onInteractionEnd();
2796 }
2797 }
2798
Sunny Goyala7ce1662016-05-31 15:01:35 -07002799 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002801 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2802 try {
2803 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2804 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2805 // is enabled by default on NYC.
2806 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2807 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002808
2809 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2810 String id = ((ShortcutInfo) info).getDeepShortcutId();
2811 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002812 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2813 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002814 } else {
2815 // Could be launching some bookkeeping activity
2816 startActivity(intent, optsBundle);
2817 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002818 } finally {
2819 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002822 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2823 // corresponding permission. Show the appropriate permission prompt if that
2824 // is the case.
2825 if (intent.getComponent() == null
2826 && Intent.ACTION_CALL.equals(intent.getAction())
2827 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2828 PackageManager.PERMISSION_GRANTED) {
2829 // TODO: Rename sPendingAddItem to a generic name.
2830 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2831 0, info);
2832 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2833 REQUEST_PERMISSION_CALL_PHONE);
2834 } else {
2835 // No idea why this was thrown.
2836 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 }
2839 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002840
Sunny Goyala7ce1662016-05-31 15:01:35 -07002841 private Bundle getActivityLaunchOptions(View v) {
2842 if (Utilities.ATLEAST_MARSHMALLOW) {
2843 int left = 0, top = 0;
2844 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2845 if (v instanceof TextView) {
2846 // Launch from center of icon, not entire view
2847 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2848 if (icon != null) {
2849 Rect bounds = icon.getBounds();
2850 left = (width - bounds.width()) / 2;
2851 top = v.getPaddingTop();
2852 width = bounds.width();
2853 height = bounds.height();
2854 }
2855 }
2856 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2857 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2858 // On L devices, we use the device default slide-up transition.
2859 // On L MR1 devices, we use a custom version of the slide-up transition which
2860 // doesn't have the delay present in the device default.
2861 return ActivityOptions.makeCustomAnimation(
2862 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2863 }
2864 return null;
2865 }
2866
2867 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002868 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2869 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2870 return false;
2871 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002872 // Only launch using the new animation if the shortcut has not opted out (this is a
2873 // private contract between launcher and may be ignored in the future).
2874 boolean useLaunchAnimation = (v != null) &&
2875 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2876 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2877
2878 UserHandleCompat user = null;
2879 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2880 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2881 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002882 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002883
2884 // Prepare intent
2885 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2886 if (v != null) {
2887 int[] pos = new int[2];
2888 v.getLocationOnScreen(pos);
2889 intent.setSourceBounds(
2890 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2891 }
2892 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002893 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2894 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2895 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002896 // Shortcuts need some special checks due to legacy reasons.
2897 startShortcutIntentSafely(intent, optsBundle, item);
2898 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2899 // Could be launching some bookkeeping activity
2900 startActivity(intent, optsBundle);
2901 } else {
2902 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2903 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2904 }
2905 return true;
2906 } catch (ActivityNotFoundException|SecurityException e) {
2907 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2908 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2909 }
2910 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002911 }
2912
Adam Cohen268c4752012-06-06 17:47:33 -07002913 /**
2914 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2915 * in the DragLayer in the exact absolute location of the original FolderIcon.
2916 */
2917 private void copyFolderIconToImage(FolderIcon fi) {
2918 final int width = fi.getMeasuredWidth();
2919 final int height = fi.getMeasuredHeight();
2920
2921 // Lazy load ImageView, Bitmap and Canvas
2922 if (mFolderIconImageView == null) {
2923 mFolderIconImageView = new ImageView(this);
2924 }
2925 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2926 mFolderIconBitmap.getHeight() != height) {
2927 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2928 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2929 }
2930
2931 DragLayer.LayoutParams lp;
2932 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2933 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2934 } else {
2935 lp = new DragLayer.LayoutParams(width, height);
2936 }
2937
Adam Cohen307fe232012-08-16 17:55:58 -07002938 // The layout from which the folder is being opened may be scaled, adjust the starting
2939 // view size by this scale factor.
2940 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002941 lp.customPosition = true;
2942 lp.x = mRectForFolderAnimation.left;
2943 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002944 lp.width = (int) (scale * width);
2945 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002946
2947 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2948 fi.draw(mFolderIconCanvas);
2949 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002950 if (fi.getFolder() != null) {
2951 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2952 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002953 }
Adam Cohen268c4752012-06-06 17:47:33 -07002954 // Just in case this image view is still in the drag layer from a previous animation,
2955 // we remove it and re-add it.
2956 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2957 mDragLayer.removeView(mFolderIconImageView);
2958 }
2959 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002960 if (fi.getFolder() != null) {
2961 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002962 }
Adam Cohen268c4752012-06-06 17:47:33 -07002963 }
2964
Adam Cohen37b2a492016-04-06 18:36:06 -07002965 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002966 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002967 FolderInfo info = (FolderInfo) fi.getTag();
2968 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2969 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002970 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2971 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002972 }
2973
Adam Cohen268c4752012-06-06 17:47:33 -07002974 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2975 copyFolderIconToImage(fi);
2976 fi.setVisibility(View.INVISIBLE);
2977
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002978 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2979 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002980 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002981 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2982 }
2983 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002984 oa.start();
2985 }
2986
Sunny Goyal935fca12015-10-13 10:19:01 -07002987 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002988 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002989 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 // We remove and re-draw the FolderIcon in-case it has changed
2992 mDragLayer.removeView(mFolderIconImageView);
2993 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002994
2995 if (cl != null) {
2996 cl.clearFolderLeaveBehind();
2997 }
2998
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002999 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003000 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003001 oa.addListener(new AnimatorListenerAdapter() {
3002 @Override
3003 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003004 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003005 // Remove the ImageView copy of the FolderIcon and make the original visible.
3006 mDragLayer.removeView(mFolderIconImageView);
3007 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003008 }
3009 }
3010 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003012 if (!animate) {
3013 oa.end();
3014 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003015 }
3016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003018 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 * is animated relative to the specified View. If the View is null, no animation
3020 * is played.
3021 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003022 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003024 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003025
Adam Cohenfb91f302012-06-11 15:45:18 -07003026 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003027 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3028 if (openFolder != null && openFolder != folder) {
3029 // Close any open folder before opening a folder.
3030 closeFolder();
3031 }
3032
Adam Cohena9cf38f2011-05-02 15:36:58 -07003033 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034
Adam Cohena9cf38f2011-05-02 15:36:58 -07003035 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036
Sunny Goyalf4066152015-04-15 09:42:19 -07003037 // While the folder is open, the position of the icon cannot change.
3038 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3039
Adam Cohen4554ee12011-08-03 16:13:21 -07003040 // Just verify that the folder hasn't already been added to the DragLayer.
3041 // There was a one-off crash where the folder had a parent already.
3042 if (folder.getParent() == null) {
3043 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003044 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003045 } else {
3046 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3047 folder.getParent() + ").");
3048 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003049 folder.animateOpen();
3050
3051 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003052
3053 // Notify the accessibility manager that this folder "window" has appeared and occluded
3054 // the workspace items
3055 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3056 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 }
3058
3059 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003060 closeFolder(true);
3061 }
3062
3063 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003064 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003065 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003066 if (folder.isEditingName()) {
3067 folder.dismissEditingName();
3068 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003069 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003070 }
3071 }
3072
Sunny Goyal935fca12015-10-13 10:19:01 -07003073 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003074 animate &= !Utilities.isPowerSaverOn(this);
3075
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 folder.getInfo().opened = false;
3077
3078 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3079 if (parent != null) {
3080 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003081 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003082 if (fi != null) {
3083 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3084 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 if (animate) {
3087 folder.animateClosed();
3088 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003089 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003090 }
Winson Chung83ca4802013-04-12 15:10:52 -07003091
Sunny Goyal935fca12015-10-13 10:19:01 -07003092 // Notify the accessibility manager that this folder "window" has disappeared and no
3093 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003094 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 }
3096
Tony Wickhamdadb3042016-02-24 11:07:00 -08003097 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003099 if (!isDraggingEnabled()) return false;
3100 if (isWorkspaceLocked()) return false;
3101 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102
Sunny Goyalbb011da2016-06-15 15:42:29 -07003103 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003104 onLongClickAllAppsButton(v);
3105 return true;
3106 }
3107
Adam Cohen1697b792013-09-17 19:08:21 -07003108 if (v instanceof Workspace) {
3109 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003110 if (!mWorkspace.isTouchActive()) {
3111 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003112 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3113 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3114 return true;
3115 } else {
3116 return false;
3117 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003118 } else {
3119 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003120 }
Adam Cohen1697b792013-09-17 19:08:21 -07003121 }
3122
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003123 CellLayout.CellInfo longClickCellInfo = null;
3124 View itemUnderLongClick = null;
3125 if (v.getTag() instanceof ItemInfo) {
3126 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003127 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003128 itemUnderLongClick = longClickCellInfo.cell;
3129 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 }
3131
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 // The hotseat touch handling does not go through Workspace, and we always allow long press
3133 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003134 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003135 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003136 // User long pressed on empty space
3137 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3138 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003139 if (mWorkspace.isInOverviewMode()) {
3140 mWorkspace.startReordering(v);
3141 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003142 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003143 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003146 final boolean isAllAppsButton =
3147 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3148 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3149 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003150 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003152 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
3154 }
3155 }
3156 return true;
3157 }
3158
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 boolean isHotseatLayout(View layout) {
3160 return mHotseat != null && layout != null &&
3161 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3162 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003163
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 /**
3165 * Returns the CellLayout of the specified container at the specified screen.
3166 */
Sunny Goyal92820592015-03-02 11:31:35 -08003167 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3169 if (mHotseat != null) {
3170 return mHotseat.getLayout();
3171 } else {
3172 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003173 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003174 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003175 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003176 }
3177 }
3178
Winson Chungb745afb2015-03-02 11:51:23 -08003179 /**
3180 * For overridden classes.
3181 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003182 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003183 return isAppsViewVisible();
3184 }
3185
3186 public boolean isAppsViewVisible() {
3187 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3188 }
3189
3190 public boolean isWidgetsViewVisible() {
3191 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003192 }
3193
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003194 private void setWorkspaceBackground(int background) {
3195 switch (background) {
3196 case WORKSPACE_BACKGROUND_TRANSPARENT:
3197 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3198 break;
3199 case WORKSPACE_BACKGROUND_BLACK:
3200 getWindow().setBackgroundDrawable(null);
3201 break;
3202 default:
3203 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3204 }
Craig Mautner360310b2012-10-26 15:13:08 -07003205 }
3206
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003207 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003208 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3209 int curflags = getWindow().getAttributes().flags
3210 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3211 if (wpflags != curflags) {
3212 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3213 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003214 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003215 }
3216
Michael Jurkae326f182011-11-21 14:05:46 -08003217 @Override
3218 public void onTrimMemory(int level) {
3219 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003220 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3221 // The widget preview db can result in holding onto over
3222 // 3MB of memory for caching which isn't necessary.
3223 SQLiteDatabase.releaseMemory();
3224
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003225 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003226 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003227 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003228 if (mLauncherCallbacks != null) {
3229 mLauncherCallbacks.onTrimMemory(level);
3230 }
Michael Jurkae326f182011-11-21 14:05:46 -08003231 }
3232
Winson5c6bdbb2015-09-03 11:36:19 -07003233 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003234 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003235 }
3236
Winson5c6bdbb2015-09-03 11:36:19 -07003237 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003238 boolean changed = mState != State.WORKSPACE ||
3239 mWorkspace.getState() != Workspace.State.NORMAL;
3240 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003241 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003242 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003243 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003244
Michael Jurkab3e22d92011-10-31 15:58:33 -07003245 // Set focus to the AppsCustomize button
3246 if (mAllAppsButton != null) {
3247 mAllAppsButton.requestFocus();
3248 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003249 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003250
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003251 // Change the state *after* we've called all the transition code
3252 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003253
Winson Chung5fb63472011-02-02 17:03:37 -08003254 // Resume the auto-advance of widgets
3255 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003256 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003257
Winson Chung0f785722015-04-08 10:27:49 -07003258 if (changed) {
3259 // Send an accessibility event to announce the context change
3260 getWindow().getDecorView()
3261 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003262 }
Winson5c6bdbb2015-09-03 11:36:19 -07003263 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003264 }
3265
Winsone9f27272015-10-13 10:47:51 -07003266 /**
3267 * Shows the overview button.
3268 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003269 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003270 showOverviewMode(animated, false);
3271 }
3272
3273 /**
3274 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3275 * onto one of the overview panel buttons.
3276 */
3277 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3278 Runnable postAnimRunnable = null;
3279 if (requestButtonFocus) {
3280 postAnimRunnable = new Runnable() {
3281 @Override
3282 public void run() {
3283 // Hitting the menu button when in touch mode does not trigger touch mode to
3284 // be disabled, so if requested, force focus on one of the overview panel
3285 // buttons.
3286 mOverviewPanel.requestFocusFromTouch();
3287 }
3288 };
3289 }
Adam Cohened307df2013-10-02 09:37:31 -07003290 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003291 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003292 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003293 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003294 }
3295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 /**
3297 * Shows the apps view.
3298 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003299 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003300 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003301 if (resetListToTop) {
3302 mAppsView.scrollToTop();
3303 }
Winson Chung4ac30062015-05-08 17:34:17 -07003304 if (updatePredictedApps) {
3305 tryAndUpdatePredictedApps();
3306 }
Winson Chung76648c52015-07-10 14:33:23 -07003307 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003308 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003309
Winson Chungb745afb2015-03-02 11:51:23 -08003310 /**
3311 * Shows the widgets view.
3312 */
3313 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003314 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003315 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003316 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003317 }
Winson Chung76648c52015-07-10 14:33:23 -07003318 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003319
3320 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003321 @Override
3322 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003323 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003324 }
3325 });
Winson Chungb745afb2015-03-02 11:51:23 -08003326 }
3327
3328 /**
3329 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003330 *
3331 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003332 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003333 // TODO: calling method should use the return value so that when {@code false} is returned
3334 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003335 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003336 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3337 mState != State.WIDGETS_SPRING_LOADED) {
3338 return false;
3339 }
3340 if (toState != State.APPS && toState != State.WIDGETS) {
3341 return false;
3342 }
Winson Chungb745afb2015-03-02 11:51:23 -08003343
3344 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003345 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3346 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003348 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003349 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003352 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353
3354 // Pause the auto-advance of widgets until we are out of AllApps
3355 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003356 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357 closeFolder();
3358
3359 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003360 getWindow().getDecorView()
3361 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003362 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363 }
3364
Winson Chungcd99cd32015-04-29 11:03:24 -07003365 /**
3366 * Updates the workspace and interaction state on state change, and return the animation to this
3367 * new state.
3368 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003369 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003370 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003371 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003372 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003373 updateInteraction(fromState, toState);
3374 return anim;
3375 }
3376
Jun Mukaif0033da2015-08-04 18:34:30 -07003377 public void onLauncherClingShown() {
3378 // When a launcher cling appears, it should cover the underlying layers, so their focus
3379 // should be blocked.
3380 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3381 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3382 }
3383 }
3384
3385 public void onLauncherClingDismissed() {
3386 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3387 }
3388
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003390 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003391 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003392 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003393 }
Winson Chungb745afb2015-03-02 11:51:23 -08003394
Winson Chung006ee262015-08-03 14:40:11 -07003395 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003396 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003397 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003398
3399 if (isAppsViewVisible()) {
3400 mState = State.APPS_SPRING_LOADED;
3401 } else if (isWidgetsViewVisible()) {
3402 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003403 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003405 } else {
3406 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003407 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003408 }
Adam Cohen7777d962011-08-18 18:58:38 -07003409
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003411 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003412 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003413
Winson Chunge7a03942011-08-05 15:05:12 -07003414 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003415 @Override
3416 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003417 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3419 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003420 // Before we show workspace, hide all apps again because
3421 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3422 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003424 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003425 } else {
3426 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003427 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003428 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003429 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003430 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003431
Tony Wickhame0c33232016-02-08 11:37:04 -08003432 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003433 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3434 || mState == State.WIDGETS_SPRING_LOADED;
3435 }
3436
Michael Jurkad3ef3062010-11-23 16:23:58 -08003437 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003438 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003439 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003440 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003441 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003442 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003443 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3444 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003445 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003446 }
3447
Winson Chung4ac30062015-05-08 17:34:17 -07003448 /**
3449 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3450 * resumed.
3451 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003452 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003453 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003454 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003455 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003456 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003457 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003458 }
3459 }
3460 }
3461
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462 void lockAllApps() {
3463 // TODO
3464 }
3465
3466 void unlockAllApps() {
3467 // TODO
3468 }
3469
Winsonf768d932015-09-25 16:12:35 -07003470 public boolean launcherCallbacksProvidesSearch() {
3471 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3472 }
3473
Michael Jurkad7c28052012-04-27 15:43:36 -07003474 @Override
3475 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003476 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003477 final List<CharSequence> text = event.getText();
3478 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003479 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003480 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003481 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003482 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003483 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003484 } else if (mWorkspace != null) {
3485 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003486 } else {
3487 text.add(getString(R.string.all_apps_home_button_label));
3488 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003489 return result;
3490 }
3491
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003492 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003493 * If the activity is currently paused, signal that we need to run the passed Runnable
3494 * in onResume.
3495 *
3496 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003497 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3498 * wrong when we're not running, and if the activity comes back to what the configuration was
3499 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003500 *
3501 * Implementation of the method from LauncherModel.Callbacks.
3502 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003503 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003505 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003506 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003507 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003508 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003509 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003510 }
3511 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003512 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003513 return true;
3514 } else {
3515 return false;
3516 }
3517 }
3518
Michael Jurkac402cd92013-05-20 15:49:32 +02003519 private boolean waitUntilResume(Runnable run) {
3520 return waitUntilResume(run, false);
3521 }
3522
Michael Jurka1e2f4652013-07-08 18:03:46 -07003523 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003524 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003525 }
3526
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003528 * If the activity is currently paused, signal that we need to re-run the loader
3529 * in onResume.
3530 *
3531 * This needs to be called from incoming places where resources might have been loaded
3532 * while we are paused. That is becaues the Configuration might be wrong
3533 * when we're not running, and if it comes back to what it was when we
3534 * were paused, we are not restarted.
3535 *
3536 * Implementation of the method from LauncherModel.Callbacks.
3537 *
3538 * @return true if we are currently paused. The caller might be able to
3539 * skip some work in that case since we will come back again.
3540 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003541 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003542 public boolean setLoadOnResume() {
3543 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003544 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003545 mOnResumeNeedsLoad = true;
3546 return true;
3547 } else {
3548 return false;
3549 }
3550 }
3551
3552 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003555 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003557 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003558 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003559 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003560 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003561 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003563
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003565 * Clear any pending bind callbacks. This is called when is loader is planning to
3566 * perform a full rebind from scratch.
3567 */
3568 @Override
3569 public void clearPendingBinds() {
3570 mBindOnResumeCallbacks.clear();
3571 if (mPendingExecutor != null) {
3572 mPendingExecutor.markCompleted();
3573 mPendingExecutor = null;
3574 }
3575 }
3576
3577 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 * Refreshes the shortcuts shown on the workspace.
3579 *
3580 * Implementation of the method from LauncherModel.Callbacks.
3581 */
3582 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003583 if (LauncherAppState.PROFILE_STARTUP) {
3584 Trace.beginSection("Starting page bind");
3585 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003586 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003587
Winson Chungd64d1762013-08-20 14:37:16 -07003588 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003589 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003590 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003591
Michael Jurka05bf644e2011-11-30 20:28:53 -08003592 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003593 if (mHotseat != null) {
3594 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003595 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003596 if (LauncherAppState.PROFILE_STARTUP) {
3597 Trace.endSection();
3598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 }
3600
Adam Cohendcd297f2013-06-18 13:13:40 -07003601 @Override
3602 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003603 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003604 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3605 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003606 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3607 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3608 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003609 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3610 // If there are no screens, we need to have an empty screen
3611 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003612 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003613 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003614
3615 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003616 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003617 if (hasCustomContentToLeft()) {
3618 mWorkspace.createCustomContentContainer();
3619 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003620 }
Winson Chung64359a52013-07-08 17:17:08 -07003621 }
3622
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003623 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003624 int count = orderedScreenIds.size();
3625 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003626 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003627 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003628 // No need to bind the first screen, as its always bound.
3629 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3630 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003631 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 }
3633
Winson Chungd64d1762013-08-20 14:37:16 -07003634 public void bindAppsAdded(final ArrayList<Long> newScreens,
3635 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003636 final ArrayList<ItemInfo> addAnimated,
3637 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003638 Runnable r = new Runnable() {
3639 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003640 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003641 }
3642 };
3643 if (waitUntilResume(r)) {
3644 return;
3645 }
3646
Winson Chungd64d1762013-08-20 14:37:16 -07003647 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003648 if (newScreens != null) {
3649 bindAddScreens(newScreens);
3650 }
Winson Chungd64d1762013-08-20 14:37:16 -07003651
3652 // We add the items without animation on non-visible pages, and with
3653 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003654 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003655 bindItems(addNotAnimated, 0,
3656 addNotAnimated.size(), false);
3657 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003658 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003659 bindItems(addAnimated, 0,
3660 addAnimated.size(), true);
3661 }
Winson Chungc58497e2013-09-03 17:48:37 -07003662
Winson Chung87412982013-10-03 18:34:14 -07003663 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003664 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003665
Winson Chungb745afb2015-03-02 11:51:23 -08003666 if (addedApps != null && mAppsView != null) {
3667 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003668 }
Winson Chungd64d1762013-08-20 14:37:16 -07003669 }
3670
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003671 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 * Bind the items start-end from the list.
3673 *
3674 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003675 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003676 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003677 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3678 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003679 Runnable r = new Runnable() {
3680 public void run() {
3681 bindItems(shortcuts, start, end, forceAnimateIcons);
3682 }
3683 };
3684 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 return;
3686 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003687
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003689 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3690 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003691 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003692 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003693 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003694 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003696
3697 // Short circuit if we are loading dock items for a configuration which has no dock
3698 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3699 mHotseat == null) {
3700 continue;
3701 }
3702
Sunny Goyal639e9062015-08-19 19:17:06 -07003703 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 switch (item.itemType) {
3705 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3706 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003707 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003709 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003710 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003711 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003712 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003713 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003714 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003716 default:
3717 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003719
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003720 /*
3721 * Remove colliding items.
3722 */
3723 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3724 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3725 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3726 View v = cl.getChildAt(item.cellX, item.cellY);
3727 Object tag = v.getTag();
3728 String desc = "Collision while binding workspace item: " + item
3729 + ". Collides with " + tag;
3730 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3731 throw (new RuntimeException(desc));
3732 } else {
3733 Log.d(TAG, desc);
3734 LauncherModel.deleteItemFromDatabase(this, item);
3735 continue;
3736 }
3737 }
3738 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003739 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3740 item.cellY, 1, 1);
3741 if (animateIcons) {
3742 // Animate all the applications up now
3743 view.setAlpha(0f);
3744 view.setScaleX(0f);
3745 view.setScaleY(0f);
3746 bounceAnims.add(createNewAppBounceAnimation(view, i));
3747 newShortcutsScreenId = item.screenId;
3748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003749 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750
Winson Chung997a9232013-07-24 15:33:46 -07003751 if (animateIcons) {
3752 // Animate to the correct page
3753 if (newShortcutsScreenId > -1) {
3754 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003755 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003756 final Runnable startBounceAnimRunnable = new Runnable() {
3757 public void run() {
3758 anim.playTogether(bounceAnims);
3759 anim.start();
3760 }
3761 };
Winson Chung997a9232013-07-24 15:33:46 -07003762 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003763 // We post the animation slightly delayed to prevent slowdowns
3764 // when we are loading right after we return to launcher.
3765 mWorkspace.postDelayed(new Runnable() {
3766 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003767 if (mWorkspace != null) {
3768 mWorkspace.snapToPage(newScreenIndex);
3769 mWorkspace.postDelayed(startBounceAnimRunnable,
3770 NEW_APPS_ANIMATION_DELAY);
3771 }
Winson Chung94d67682013-09-25 16:29:40 -07003772 }
3773 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003774 } else {
3775 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003776 }
3777 }
Winson Chung64359a52013-07-08 17:17:08 -07003778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003780 }
3781
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003782 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3783 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3784 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003785 view.updateAppWidget(null);
3786 view.setOnClickListener(this);
3787 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003788 mWorkspace.requestLayout();
3789 }
3790
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 /**
3792 * Add the views for a widget to the workspace.
3793 *
3794 * Implementation of the method from LauncherModel.Callbacks.
3795 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003797 Runnable r = new Runnable() {
3798 public void run() {
3799 bindAppWidget(item);
3800 }
3801 };
3802 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003803 return;
3804 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003805
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003806 if (mIsSafeModeEnabled) {
3807 bindSafeModeWidget(item);
3808 return;
3809 }
3810
Daniel Sandler843e8602010-06-07 14:59:01 -04003811 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3812 if (DEBUG_WIDGETS) {
3813 Log.d(TAG, "bindAppWidget: " + item);
3814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003817
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003818 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3819 // If the provider is not ready, bind as a pending widget.
3820 appWidgetInfo = null;
3821 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3822 // The widget id is not valid. Try to find the widget based on the provider info.
3823 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3824 } else {
3825 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3826 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003827
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003828 // If the provider is ready, but the width is not yet restored, try to restore it.
3829 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3830 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003831 if (appWidgetInfo == null) {
3832 if (DEBUG_WIDGETS) {
3833 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3834 + " belongs to component " + item.providerName
3835 + ", as the povider is null");
3836 }
3837 LauncherModel.deleteItemFromDatabase(this, item);
3838 return;
3839 }
Sunny Goyalff572272014-07-23 13:58:07 -07003840
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003841 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003842 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003843 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3844 // Id has not been allocated yet. Allocate a new id.
3845 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3846 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003847
Sunny Goyald478c832016-04-01 12:04:16 -07003848 // Also try to bind the widget. If the bind fails, the user will be shown
3849 // a click to setup UI, which will ask for the bind permission.
3850 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3851 pendingInfo.spanX = item.spanX;
3852 pendingInfo.spanY = item.spanY;
3853 pendingInfo.minSpanX = item.minSpanX;
3854 pendingInfo.minSpanY = item.minSpanY;
3855 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3856 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3857 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003858
Sunny Goyald478c832016-04-01 12:04:16 -07003859 // Bind succeeded
3860 if (success) {
3861 // If the widget has a configure activity, it is still needs to set it up,
3862 // otherwise the widget is ready to go.
3863 item.restoreStatus = (appWidgetInfo.configure == null)
3864 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3865 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003866 }
Sunny Goyald478c832016-04-01 12:04:16 -07003867
3868 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003869 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003871 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 // The widget was marked as UI not ready, but there is no configure activity to
3873 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003874 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3875 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003876 }
Sunny Goyalff572272014-07-23 13:58:07 -07003877 }
3878
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003880 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003881 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3882 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003883 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003884
Sunny Goyal56c73602015-09-25 12:55:01 -07003885 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003886 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003887 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003888 deleteWidgetInfo(item);
3889 return;
3890 }
3891
Sunny Goyal233ee962015-08-03 13:05:01 -07003892 item.minSpanX = appWidgetInfo.minSpanX;
3893 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003894 addAppWidgetToWorkspace(
3895 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3896 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003897 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003898 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003899 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003900 view.updateAppWidget(null);
3901 view.setOnClickListener(this);
3902 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003903 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003904 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905
Daniel Sandler843e8602010-06-07 14:59:01 -04003906 if (DEBUG_WIDGETS) {
3907 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3908 + (SystemClock.uptimeMillis()-start) + "ms");
3909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 }
3911
Sunny Goyalff572272014-07-23 13:58:07 -07003912 /**
3913 * Restores a pending widget.
3914 *
3915 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003916 */
Sunny Goyald478c832016-04-01 12:04:16 -07003917 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003918 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3919 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3920 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003921 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003922 }
3923
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003924 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003925 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003926
3927 mWorkspace.reinflateWidgetsIfNecessary();
3928 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003929 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003930 }
3931
Adam Cohen1462de32012-07-24 22:34:36 -07003932 public void onPageBoundSynchronously(int page) {
3933 mSynchronouslyBoundPages.add(page);
3934 }
3935
Sunny Goyal527c7d32015-08-28 15:19:36 -07003936 @Override
3937 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3938 if (mPendingExecutor != null) {
3939 mPendingExecutor.markCompleted();
3940 }
3941 mPendingExecutor = executor;
3942 executor.attachTo(this);
3943 }
3944
3945 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3946 if (mPendingExecutor == executor) {
3947 mPendingExecutor = null;
3948 }
3949 }
3950
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 /**
3952 * Callback saying that there aren't any more items to bind.
3953 *
3954 * Implementation of the method from LauncherModel.Callbacks.
3955 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003956 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003957 Runnable r = new Runnable() {
3958 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003959 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003960 }
3961 };
3962 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003963 return;
3964 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003965 if (LauncherAppState.PROFILE_STARTUP) {
3966 Trace.beginSection("Page bind completed");
3967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 if (mSavedState != null) {
3969 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003970 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003972
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 mSavedState = null;
3974 }
3975
Adam Cohen1462de32012-07-24 22:34:36 -07003976 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003978 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003979
3980 // If we received the result of any pending adds while the loader was running (e.g. the
3981 // widget configuration forced an orientation change), process them now.
3982 if (sPendingAddItem != null) {
3983 final long screenId = completeAdd(sPendingAddItem);
3984
3985 // TODO: this moves the user to the page where the pending item was added. Ideally,
3986 // the screen would be guaranteed to exist after bind, and the page would be set through
3987 // the workspace restore process.
3988 mWorkspace.post(new Runnable() {
3989 @Override
3990 public void run() {
3991 mWorkspace.snapToScreenId(screenId);
3992 }
3993 });
3994 sPendingAddItem = null;
3995 }
3996
Sunny Goyal756adbc2015-04-16 15:20:51 -07003997 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003998
3999 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004000 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004001 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004002 if (LauncherAppState.PROFILE_STARTUP) {
4003 Trace.endSection();
4004 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004005 }
4006
Winson Chunga2413752012-04-03 14:22:34 -07004007 private boolean canRunNewAppsAnimation() {
4008 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004009 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4010 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004011 }
4012
Winson Chungc9168342013-06-26 14:54:55 -07004013 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004014 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004015 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4016 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004017 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004018 return bounceAnim;
4019 }
4020
Adam Cohen27772732013-11-11 14:00:56 +00004021 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004022 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004023 }
4024
Tony Wickham55616cd2015-09-23 14:55:17 -07004025 public int getSearchBarHeight() {
4026 if (mLauncherCallbacks != null) {
4027 return mLauncherCallbacks.getSearchBarHeight();
4028 }
4029 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4030 }
4031
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004032 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004033 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4034 * multiple calls to bind the same list.)
4035 */
4036 @Thunk ArrayList<AppInfo> mTmpAppsList;
4037 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4038 public void run() {
4039 bindAllApplications(mTmpAppsList);
4040 mTmpAppsList = null;
4041 }
4042 };
4043
4044 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 * Add the icons for all apps.
4046 *
4047 * Implementation of the method from LauncherModel.Callbacks.
4048 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004049 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004050 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4051 mTmpAppsList = apps;
4052 return;
4053 }
4054
Winson Chungb745afb2015-03-02 11:51:23 -08004055 if (mAppsView != null) {
4056 mAppsView.setApps(apps);
4057 }
Adam Cohen9211d422014-10-07 18:14:53 -07004058 if (mLauncherCallbacks != null) {
4059 mLauncherCallbacks.bindAllApplications(apps);
4060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 }
4062
4063 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004064 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4065 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4066 */
4067 @Override
4068 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4069 mDeepShortcutMap = deepShortcutMapCopy;
4070 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4071 }
4072
4073 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 * A package was updated.
4075 *
4076 * Implementation of the method from LauncherModel.Callbacks.
4077 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004078 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004079 Runnable r = new Runnable() {
4080 public void run() {
4081 bindAppsUpdated(apps);
4082 }
4083 };
4084 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 return;
4086 }
4087
Winson Chungb745afb2015-03-02 11:51:23 -08004088 if (mAppsView != null) {
4089 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 }
4092
Sunny Goyal4390ace2014-10-13 11:33:11 -07004093 @Override
4094 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4095 Runnable r = new Runnable() {
4096 public void run() {
4097 bindWidgetsRestored(widgets);
4098 }
4099 };
4100 if (waitUntilResume(r)) {
4101 return;
4102 }
4103 mWorkspace.widgetsRestored(widgets);
4104 }
4105
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004107 * Some shortcuts were updated in the background.
4108 *
4109 * Implementation of the method from LauncherModel.Callbacks.
4110 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004111 @Override
4112 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4113 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004114 Runnable r = new Runnable() {
4115 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004116 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004117 }
4118 };
4119 if (waitUntilResume(r)) {
4120 return;
4121 }
4122
Sunny Goyal4390ace2014-10-13 11:33:11 -07004123 if (!updated.isEmpty()) {
4124 mWorkspace.updateShortcuts(updated);
4125 }
4126
4127 if (!removed.isEmpty()) {
4128 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4129 for (ShortcutInfo si : removed) {
4130 removedComponents.add(si.getTargetComponent());
4131 }
4132 mWorkspace.removeItemsByComponentName(removedComponents, user);
4133 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004134 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004135 }
4136 }
4137
4138 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004139 * Update the state of a package, typically related to install state.
4140 *
4141 * Implementation of the method from LauncherModel.Callbacks.
4142 */
Sunny Goyale755d462014-07-22 13:48:29 -07004143 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004144 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4145 Runnable r = new Runnable() {
4146 public void run() {
4147 bindRestoreItemsChange(updates);
4148 }
4149 };
4150 if (waitUntilResume(r)) {
4151 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004152 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004153
Sunny Goyal756adbc2015-04-16 15:20:51 -07004154 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004155 }
4156
4157 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004159 * in addition to specific applications to remove, the reason being that
4160 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004161 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004162 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004164 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004165 public void bindWorkspaceComponentsRemoved(
4166 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4167 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004168 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004169 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004170 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004171 }
Winson Chungd64d1762013-08-20 14:37:16 -07004172 };
4173 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004174 return;
4175 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004176 if (!packageNames.isEmpty()) {
4177 mWorkspace.removeItemsByPackageName(packageNames, user);
4178 }
4179 if (!components.isEmpty()) {
4180 mWorkspace.removeItemsByComponentName(components, user);
4181 }
4182 // Notify the drag controller
4183 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004184
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004185 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004186
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 @Override
4188 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4189 Runnable r = new Runnable() {
4190 public void run() {
4191 bindAppInfosRemoved(appInfos);
4192 }
4193 };
4194 if (waitUntilResume(r)) {
4195 return;
Joe Onorato36115782010-06-17 13:28:48 -04004196 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004197
Winson Chungdf95eb12013-10-16 14:57:07 -07004198 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004199 if (mAppsView != null) {
4200 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
Joe Onoratobe386092009-11-17 17:32:16 -08004203
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004204 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004205 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004206 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004207 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004208 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004209
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004210 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004211 public void bindWidgetsModel(WidgetsModel model) {
4212 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004213 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004214 return;
4215 }
4216
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004217 if (mWidgetsView != null && model != null) {
4218 mWidgetsView.addWidgets(model);
4219 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004220 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004221 }
4222
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004223 @Override
4224 public void notifyWidgetProvidersChanged() {
4225 if (mWorkspace.getState().shouldUpdateWidget) {
4226 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4227 }
4228 }
4229
Winson Chung400438b2011-01-16 17:53:48 -08004230 private int mapConfigurationOriActivityInfoOri(int configOri) {
4231 final Display d = getWindowManager().getDefaultDisplay();
4232 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4233 switch (d.getRotation()) {
4234 case Surface.ROTATION_0:
4235 case Surface.ROTATION_180:
4236 // We are currently in the same basic orientation as the natural orientation
4237 naturalOri = configOri;
4238 break;
4239 case Surface.ROTATION_90:
4240 case Surface.ROTATION_270:
4241 // We are currently in the other basic orientation to the natural orientation
4242 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4243 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4244 break;
4245 }
4246
4247 int[] oriMap = {
4248 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4249 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4250 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4251 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4252 };
4253 // Since the map starts at portrait, we need to offset if this device's natural orientation
4254 // is landscape.
4255 int indexOffset = 0;
4256 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4257 indexOffset = 1;
4258 }
4259 return oriMap[(d.getRotation() + indexOffset) % 4];
4260 }
Adam Cohen446e9402011-09-15 18:21:21 -07004261
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004262 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004263 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004264 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004265 if (Utilities.ATLEAST_JB_MR2) {
4266 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4267 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004268 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4269 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004270 }
Winson Chung4b919f82012-05-01 10:44:08 -07004271 }
4272 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004273
Winson Chung4b919f82012-05-01 10:44:08 -07004274 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004275 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004276 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004277 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004278 } else {
4279 mHandler.postDelayed(new Runnable() {
4280 public void run() {
4281 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4282 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004283 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004284 }
Winson Chung4b919f82012-05-01 10:44:08 -07004285 }
Winson Chung400438b2011-01-16 17:53:48 -08004286 }
4287
Adam Cohenea90f832014-05-21 15:03:34 -07004288 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004289 * To be overridden by subclasses to indicate that there is an activity to launch
4290 * before showing the standard launcher experience.
4291 */
4292 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004293 if (mLauncherCallbacks != null) {
4294 return mLauncherCallbacks.hasFirstRunActivity();
4295 }
Adam Cohen432609a2014-03-13 17:03:22 -07004296 return false;
4297 }
4298
4299 /**
4300 * To be overridden by subclasses to launch any first run activity
4301 */
4302 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004303 if (mLauncherCallbacks != null) {
4304 return mLauncherCallbacks.getFirstRunActivity();
4305 }
Adam Cohen432609a2014-03-13 17:03:22 -07004306 return null;
4307 }
4308
Winson Chunga6945242014-01-08 14:04:34 -08004309 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004310 return !ActivityManager.isRunningInTestHarness() &&
4311 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004312 }
4313
Adam Cohen4a9423d2014-03-28 14:22:31 -07004314 protected boolean hasRunFirstRunActivity() {
4315 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4316 }
4317
Adam Cohen432609a2014-03-13 17:03:22 -07004318 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004319 if (shouldRunFirstRunActivity() &&
4320 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004321 Intent firstRunIntent = getFirstRunActivity();
4322 if (firstRunIntent != null) {
4323 startActivity(firstRunIntent);
4324 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004325 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004326 }
4327 }
Adam Cohen432609a2014-03-13 17:03:22 -07004328 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004329 }
4330
4331 private void markFirstRunActivityShown() {
4332 SharedPreferences.Editor editor = mSharedPrefs.edit();
4333 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4334 editor.apply();
4335 }
4336
Adam Cohen432609a2014-03-13 17:03:22 -07004337 /**
4338 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4339 * screen that must be displayed and dismissed.
4340 */
4341 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004342 if (mLauncherCallbacks != null) {
4343 return mLauncherCallbacks.hasDismissableIntroScreen();
4344 }
Adam Cohen432609a2014-03-13 17:03:22 -07004345 return false;
4346 }
4347
4348 /**
4349 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4350 */
4351 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004352 if (mLauncherCallbacks != null) {
4353 return mLauncherCallbacks.getIntroScreen();
4354 }
Adam Cohen432609a2014-03-13 17:03:22 -07004355 return null;
4356 }
4357
4358 /**
4359 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4360 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4361 */
4362 private boolean shouldShowIntroScreen() {
4363 return hasDismissableIntroScreen() &&
4364 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4365 }
4366
4367 protected void showIntroScreen() {
4368 View introScreen = getIntroScreen();
4369 changeWallpaperVisiblity(false);
4370 if (introScreen != null) {
4371 mDragLayer.showOverlayView(introScreen);
4372 }
4373 }
4374
4375 public void dismissIntroScreen() {
4376 markIntroScreenDismissed();
4377 if (showFirstRunActivity()) {
4378 // We delay hiding the intro view until the first run activity is showing. This
4379 // avoids a blip.
4380 mWorkspace.postDelayed(new Runnable() {
4381 @Override
4382 public void run() {
4383 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004384 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004385 }
4386 }, ACTIVITY_START_DELAY);
4387 } else {
4388 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004389 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004390 }
4391 changeWallpaperVisiblity(true);
4392 }
4393
4394 private void markIntroScreenDismissed() {
4395 SharedPreferences.Editor editor = mSharedPrefs.edit();
4396 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4397 editor.apply();
4398 }
4399
Adam Cohen091440a2015-03-18 14:16:05 -07004400 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004401 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4402 // on the device, then we always show the first run cling experience (or if there is no
4403 // launcher2). Otherwise, we prompt the user upon started for migration
4404 LauncherClings launcherClings = new LauncherClings(this);
4405 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004406 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004407 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004408 }
4409 }
4410
Winson Chung5ffd51d2015-06-25 11:36:50 -07004411 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004412 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004413 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004414 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004415 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004416 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004417 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4418 if (userHandle != null) {
4419 user = UserHandleCompat.fromUser(userHandle);
4420 }
4421 }
4422 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004423 }
4424
4425 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004426 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004427 if (user == null) {
4428 user = UserHandleCompat.myUserHandle();
4429 }
4430
4431 // Called from search suggestion, add the profile extra to the intent to ensure that we
4432 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004433 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004434 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004435 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004436 return null;
4437 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004438 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004439 }
4440
Winson Chung5ffd51d2015-06-25 11:36:50 -07004441 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4443 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004444 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004445 UserHandleCompat.myUserHandle());
4446 }
4447
Winson Chung5ffd51d2015-06-25 11:36:50 -07004448 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004449 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4450 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004451 mWorkspace.onExternalDragStartedWithItem(dragView);
4452 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004453 }
4454
Winson Chung5ffd51d2015-06-25 11:36:50 -07004455 protected void moveWorkspaceToDefaultScreen() {
4456 mWorkspace.moveToDefaultScreen(false);
4457 }
4458
Winson Chung80baf5a2010-08-09 16:03:15 -07004459 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004460 * Returns a FastBitmapDrawable with the icon, accurately sized.
4461 */
4462 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4463 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4464 d.setFilterBitmap(true);
4465 resizeIconDrawable(d);
4466 return d;
4467 }
4468
4469 /**
4470 * Resizes an icon drawable to the correct icon size.
4471 */
Winson5fbe0742015-09-30 15:33:00 -07004472 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004473 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004474 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004475 }
4476
4477 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004478 * Prints out out state for debugging.
4479 */
4480 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004481 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004482 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004483 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4484 Log.d(TAG, "mRestoring=" + mRestoring);
4485 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004486 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004487 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004488
Daniel Sandler325dc232013-06-05 22:57:57 -04004489 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004490 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004491
4492 @Override
4493 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4494 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004495 // Dump workspace
4496 writer.println(prefix + "Workspace Items");
4497 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4498 writer.println(prefix + " Homescreen " + i);
4499
4500 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4501 for (int j = 0; j < layout.getChildCount(); j++) {
4502 Object tag = layout.getChildAt(j).getTag();
4503 if (tag != null) {
4504 writer.println(prefix + " " + tag.toString());
4505 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004506 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004507 }
Sunny Goyala1365452015-10-01 15:46:24 -07004508
4509 writer.println(prefix + " Hotseat");
4510 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4511 for (int j = 0; j < layout.getChildCount(); j++) {
4512 Object tag = layout.getChildAt(j).getTag();
4513 if (tag != null) {
4514 writer.println(prefix + " " + tag.toString());
4515 }
4516 }
4517
Sunny Goyal713edfc2016-05-06 09:58:34 -07004518 try {
4519 FileLog.flushAll(writer);
4520 } catch (Exception e) {
4521 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004522 }
4523
Adam Cohen9211d422014-10-07 18:14:53 -07004524 if (mLauncherCallbacks != null) {
4525 mLauncherCallbacks.dump(prefix, fd, writer, args);
4526 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004527 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004528
Adam Cohen59400422014-03-05 18:07:04 -08004529 public static CustomAppWidget getCustomAppWidget(String name) {
4530 return sCustomAppWidgets.get(name);
4531 }
4532
4533 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4534 return sCustomAppWidgets;
4535 }
4536
Sunny Goyal29538332016-02-18 09:10:19 -08004537 public static List<View> getFolderContents(View icon) {
4538 if (icon instanceof FolderIcon) {
4539 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4540 } else {
4541 return Collections.EMPTY_LIST;
4542 }
4543 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004544
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004545 public static Launcher getLauncher(Context context) {
4546 if (context instanceof Launcher) {
4547 return (Launcher) context;
4548 }
4549 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4550 }
4551
Sunny Goyal745bad92016-05-02 10:54:12 -07004552 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4553
4554 @Override
4555 public void onSharedPreferenceChanged(
4556 SharedPreferences sharedPreferences, String key) {
4557 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4558 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4559 if (!waitUntilResume(this, true)) {
4560 run();
4561 }
4562 }
4563 }
4564
4565 @Override
4566 public void run() {
4567 setOrientation();
4568 }
4569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004570}