blob: adf07a1ca74548e36177a6444b12c1ad3709bb1b [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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070066import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700118import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700119import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700122import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700124import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700126import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700127import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700128import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700130import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700131import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700132
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700133import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700134import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700136import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800137import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142/**
143 * Default launcher application.
144 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100145public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700146 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
147 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700148 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700149 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700150 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700152 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700153 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400154 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700159 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700162 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700163 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700164
Sunny Goyal28c6b962015-10-12 11:42:05 -0700165 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
166
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700167 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
168 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
169 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
170
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700171 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
172
Mathew Inwood876a8462013-06-14 14:12:41 +0100173 /**
174 * IntentStarter uses request codes starting with this. This must be greater than all activity
175 * request codes used internally.
176 */
177 protected static final int REQUEST_LAST = 100;
178
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700180 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800181 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Winson Chung2672ff92012-05-04 16:22:30 -0700183 // The Intent extra that defines whether to ignore the launch animation
184 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400185 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700186
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700187 public static final String ACTION_APPWIDGET_HOST_RESET =
188 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700194 // Type: Content Values / parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohen432609a2014-03-13 17:03:22 -0700201 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700203 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700224 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
225
226 @Override
227 public void onReceive(Context context, Intent intent) {
228 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
229 closeSystemDialogs();
230 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
231 if (mAppWidgetHost != null) {
232 mAppWidgetHost.startListening();
233 }
234 }
235 }
236 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800237
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700239 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800241 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700242 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700243
244 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Sunny Goyalffe83f12014-08-14 17:39:34 -0700246 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700247 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700249 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800250 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800251 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700252
Michael Jurka0280c3b2010-09-17 15:00:07 -0700253 private int[] mTmpAddItemCellCoordinates = new int[2];
254
Sunny Goyal316490e2015-06-02 09:38:28 -0700255 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800256 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700257
Michael Jurka838a4ca2011-02-07 13:33:06 -0800258 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700259 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700260
Sunny Goyal47328fd2016-05-25 18:56:41 -0700261 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
263 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700264 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700265 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700267 // Main container view and the model for the widget tray screen.
268 @Thunk WidgetsContainerView mWidgetsView;
269 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
273 // scroll issues (because the workspace may not have been measured yet) and extra work.
274 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
275 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
277 private SpannableStringBuilder mDefaultKeySsb = null;
278
Adam Cohen091440a2015-03-18 14:16:05 -0700279 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800281 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private boolean mRestoring;
283 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700284 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
Michael Jurka1e2f4652013-07-08 18:03:46 -0700286 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700288 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800291 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700292 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700293 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700294 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700295 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700297 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700300 /** Maps launcher activity components to their list of shortcut ids. */
301 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int ADVANCE_INTERVAL = 20000;
308 private static final int ADVANCE_STAGGER = 250;
309
310 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 private long mAutoAdvanceSentTime;
312 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700313 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700314
Winson Chung400438b2011-01-16 17:53:48 -0800315 // Determines how long to wait after a rotation before restoring the screen orientation to
316 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700317 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800318
Adam Cohen091440a2015-03-18 14:16:05 -0700319 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700320
Adam Cohen1462de32012-07-24 22:34:36 -0700321 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700323
Winson Chung46353de2012-02-16 14:05:10 -0800324 // We only want to get the SharedPreferences once since it does an FS stat each time we get
325 // it from the context.
326 private SharedPreferences mSharedPrefs;
327
Winson Chungf0c6ae02012-03-21 16:10:31 -0700328 // Holds the page that we need to animate to, and the icon views that we need to animate up
329 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700330 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700331 private Bitmap mFolderIconBitmap;
332 private Canvas mFolderIconCanvas;
333 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700334
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700335 private DeviceProfile mDeviceProfile;
336
Adam Cohen4b66bf32015-09-18 12:15:19 -0700337 private boolean mMoveToDefaultScreenFromNewIntent;
338
Winson Chung13eb5272015-05-11 16:30:13 -0700339 // This is set to the view that launched the activity that navigated the user away from
340 // launcher. Since there is no callback for when the activity has finished launching, enable
341 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 private BubbleTextView mWaitingForResume;
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
345 new HashMap<String, CustomAppWidget>();
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700348 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
349 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800350 }
351 }
352
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700353 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
354 // state transition. If another state transition happened during this delay,
355 // simply unregister this runnable.
356 private Runnable mExitSpringLoadedModeRunnable;
357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700360 if (mWorkspace != null) {
361 mWorkspace.buildPageHardwareLayers();
362 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 }
364 };
365
Adam Cohendb364c32014-05-20 14:23:40 -0700366 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367
Adam Cohen091440a2015-03-18 14:16:05 -0700368 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 int requestCode;
370 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700371 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700372 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373 int cellX;
374 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700375 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 }
377
Hyunyoung Songaa953652016-04-19 18:30:24 -0700378 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800379
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700380 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700381 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400382
383 @Thunk void setOrientation() {
384 if (mRotationEnabled) {
385 unlockScreenOrientation(true);
386 } else {
387 setRequestedOrientation(
388 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700389 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400390 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700391
Sunny Goyal745bad92016-05-02 10:54:12 -0700392 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 @Override
395 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700396 if (DEBUG_STRICT_MODE) {
397 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
398 .detectDiskReads()
399 .detectDiskWrites()
400 .detectNetwork() // or .detectAll() for all detectable problems
401 .penaltyLog()
402 .build());
403 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
404 .detectLeakedSqlLiteObjects()
405 .detectLeakedClosableObjects()
406 .penaltyLog()
407 .penaltyDeath()
408 .build());
409 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700410 if (LauncherAppState.PROFILE_STARTUP) {
411 Trace.beginSection("Launcher-onCreate");
412 }
Winson Chunga2413752012-04-03 14:22:34 -0700413
Adam Cohen9211d422014-10-07 18:14:53 -0700414 if (mLauncherCallbacks != null) {
415 mLauncherCallbacks.preOnCreate();
416 }
417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400419
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Adam Cohen2e6da152015-05-06 11:42:25 -0700422 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700423 mDeviceProfile = getResources().getConfiguration().orientation
424 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700425 app.getInvariantDeviceProfile().landscapeProfile
426 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700427
Sunny Goyalf7258242015-10-19 16:59:07 -0700428 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700429 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800430 mModel = app.setLauncher(this);
431 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700432 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700433
Joe Onorato41a12d22009-10-31 18:30:00 -0400434 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700435 mAllAppsController = new AllAppsTransitionController(this);
436 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Sunny Goyalffe83f12014-08-14 17:39:34 -0700438 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
441 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700443 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
444 // this also ensures that any synchronous binding below doesn't re-trigger another
445 // LauncherModel load.
446 mPaused = false;
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Winson1f064272016-07-18 17:18:02 -0700451 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700452 mExtractedColors = new ExtractedColors();
453 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454
Tony Wickhame2217252016-03-22 16:34:23 -0700455 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
456 .addAccessibilityStateChangeListener(this);
457
Joe Onorato7c312c12009-08-13 21:36:53 -0700458 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 mSavedState = savedInstanceState;
461 restoreState(mSavedState);
462
Sunny Goyale26d1002016-06-20 14:52:14 -0700463 if (LauncherAppState.PROFILE_STARTUP) {
464 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466
467 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700468 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 // If the user leaves launcher, then we should just load items asynchronously when
470 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700471 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 } else {
473 // We only load the page synchronously if the user rotates (or triggers a
474 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700475 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
476 // If we are not binding synchronously, show a fade in animation when
477 // the first page bind completes.
478 mDragLayer.setAlpha(0);
479 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
483 // For handling default keys
484 mDefaultKeySsb = new SpannableStringBuilder();
485 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800486
487 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700488 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
489 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800490
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800491 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700492 // In case we are on a device with locked rotation, we should look at preferences to check
493 // if the user has specifically allowed rotation.
494 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700495 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700496 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
497 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700498 }
499
500 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
501 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400502 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700503
Adam Cohen9211d422014-10-07 18:14:53 -0700504 if (mLauncherCallbacks != null) {
505 mLauncherCallbacks.onCreate(savedInstanceState);
506 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700507
508 if (shouldShowIntroScreen()) {
509 showIntroScreen();
510 } else {
511 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700512 }
Adam Cohen9211d422014-10-07 18:14:53 -0700513 }
514
Sunny Goyal7779d622015-06-11 16:18:39 -0700515 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700516 public void onExtractedColorsChanged() {
517 loadExtractedColorsAndColorItems();
518 }
519
520 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700521 // TODO: do this in pre-N as well, once the extraction part is complete.
522 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700523 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700524 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700525 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700526 }
527 }
528
Adam Cohen9211d422014-10-07 18:14:53 -0700529 private LauncherCallbacks mLauncherCallbacks;
530
531 public void onPostCreate(Bundle savedInstanceState) {
532 super.onPostCreate(savedInstanceState);
533 if (mLauncherCallbacks != null) {
534 mLauncherCallbacks.onPostCreate(savedInstanceState);
535 }
536 }
537
Winson1f064272016-07-18 17:18:02 -0700538 public void onInsetsChanged(Rect insets) {
539 mDeviceProfile.updateInsets(insets);
540 mDeviceProfile.layout(this, true /* notifyListeners */);
541 }
542
Sunny Goyal32554d12015-12-03 15:31:25 -0800543 /**
544 * Call this after onCreate to set or clear overlay.
545 */
546 public void setLauncherOverlay(LauncherOverlay overlay) {
547 if (overlay != null) {
548 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
549 }
550 mWorkspace.setLauncherOverlay(overlay);
551 }
552
Adam Cohen9211d422014-10-07 18:14:53 -0700553 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
554 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 private boolean mWorkspaceImportanceStored = false;
557 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800559 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700560 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
561
562 @Override
563 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700564 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 return;
566 }
567 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700568 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspace != null) {
570 mWorkspaceImportanceForAccessibility =
571 mWorkspace.getImportantForAccessibility();
572 mWorkspace.setImportantForAccessibility(
573 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
574 mWorkspaceImportanceStored = true;
575 }
576 if (mHotseat != null) {
577 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
578 mHotseat.setImportantForAccessibility(
579 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
580 mHotseatImportanceStored = true;
581 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700582 }
583
584 @Override
585 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700586 if (mWorkspaceImportanceStored && mWorkspace != null) {
587 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
588 }
589 if (mHotseatImportanceStored && mHotseat != null) {
590 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
591 }
592 mWorkspaceImportanceStored = false;
593 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700594 }
595 });
Adam Cohen9211d422014-10-07 18:14:53 -0700596 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700597 }
598
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700599 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700600 public void onLauncherProviderChange() {
601 if (mLauncherCallbacks != null) {
602 mLauncherCallbacks.onLauncherProviderChange();
603 }
604 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700605
Adam Cohen9211d422014-10-07 18:14:53 -0700606 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700607 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 return mLauncherCallbacks.hasCustomContentToLeft();
610 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700611 return false;
612 }
613
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500615 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600616 * {@link #addToCustomContentPage}. This will only be invoked if
617 * {@link #hasCustomContentToLeft()} is {@code true}.
618 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700620 if (mLauncherCallbacks != null) {
621 mLauncherCallbacks.populateCustomContentContainer();
622 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600623 }
624
625 /**
626 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
627 * ensure the custom content page is added or removed if necessary.
628 */
629 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600630 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600631 // Not bound yet, wait for bindScreens to be called.
632 return;
633 }
634
635 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
636 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500637 mWorkspace.createCustomContentContainer();
638 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600639 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
640 mWorkspace.removeCustomContentPage();
641 }
642 }
643
Hyunyoung Songaa953652016-04-19 18:30:24 -0700644 public UserEventDispatcher getUserEventDispatcher() {
645 if (mLauncherCallbacks != null) {
646 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
647 if (dispatcher != null) {
648 return dispatcher;
649 }
650 }
651
Sunny Goyal64976d52016-06-20 14:56:28 -0700652 // Logger object is a singleton and does not have to be coupled with the foreground
653 // activity. Since most user event logging is done on the UI, the object is retrieved
654 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700655 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700656 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700657 }
658 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800659 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100660
Hyunyoung Song3f471442015-04-08 19:01:34 -0700661 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700662 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
663 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700664 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700665 }
666
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000667 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700668 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
669 // This cast is safe as long as the id < 0x00FFFFFF
670 // Since we jail all the dynamically generated views, there should be no clashes
671 // with any other views.
672 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 }
674
675 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700676 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
677 * a configuration step, this allows the proper animations to run after other transitions.
678 */
Adam Cohendb364c32014-05-20 14:23:40 -0700679 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700680 long screenId = args.screenId;
681 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
682 // When the screen id represents an actual screen (as opposed to a rank) we make sure
683 // that the drop page actually exists.
684 screenId = ensurePendingDropLayoutExists(args.screenId);
685 }
Adam Cohendb364c32014-05-20 14:23:40 -0700686
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700689 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700691 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700693 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700694 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700695 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700696 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700697 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700698 case REQUEST_BIND_PENDING_APPWIDGET: {
699 int widgetId = args.appWidgetId;
700 LauncherAppWidgetInfo info =
701 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
702 if (info != null) {
703 // Since the view was just bound, also launch the configure activity if needed
704 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
705 .getLauncherAppWidgetInfo(widgetId);
706 if (provider != null && provider.configure != null) {
707 startRestoredWidgetReconfigActivity(provider, info);
708 }
709 }
710 break;
711 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800712 }
Michael Jurka27614d22012-04-02 06:40:22 -0700713 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
714 // if you turned the screen off and then back while in All Apps, Launcher would not
715 // return to the workspace. Clearing mAddInfo.container here fixes this issue
716 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700717 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800718 }
719
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800720 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700722 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700723 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700724 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800725 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700726
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 Runnable exitSpringLoaded = new Runnable() {
728 @Override
729 public void run() {
730 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
731 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
732 }
733 };
734
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700736 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
739 if (resultCode == RESULT_CANCELED) {
740 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700741 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700742 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800744 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 }
747 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200748 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
749 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700750 // User could have free-scrolled between pages before picking a wallpaper; make sure
751 // we move to the closest one now.
752 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700753 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 }
755 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700759 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700760
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 // We have special handling for widgets
763 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 final int appWidgetId;
765 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
766 : -1;
767 if (widgetId < 0) {
768 appWidgetId = pendingAddWidgetId;
769 } else {
770 appWidgetId = widgetId;
771 }
772
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800774 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700775 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
776 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 result = RESULT_CANCELED;
778 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 @Override
781 public void run() {
782 exitSpringLoadedDragModeDelayed(false, 0, null);
783 }
784 };
Adam Cohendb364c32014-05-20 14:23:40 -0700785 if (workspaceLocked) {
786 // No need to remove the empty screen if we're mid-binding, as the
787 // the bind will not add the empty screen.
788 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
789 } else {
790 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
792 }
Winson Chung5aaab772012-04-27 15:24:02 -0700793 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700794 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700795 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
796 // When the screen id represents an actual screen (as opposed to a rank)
797 // we make sure that the drop page actually exists.
798 mPendingAddInfo.screenId =
799 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
800 }
Adam Cohendb364c32014-05-20 14:23:40 -0700801 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
802
803 dropLayout.setDropPending(true);
804 final Runnable onComplete = new Runnable() {
805 @Override
806 public void run() {
807 completeTwoStageWidgetDrop(resultCode, appWidgetId);
808 dropLayout.setDropPending(false);
809 }
810 };
811 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 } else {
814 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
815 mPendingAddInfo);
816 sPendingAddItem = args;
817 }
Winson Chung5aaab772012-04-27 15:24:02 -0700818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 return;
820 }
821
Sunny Goyald478c832016-04-01 12:04:16 -0700822 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
823 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700824 if (resultCode == RESULT_OK) {
825 // Update the widget view.
826 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
827 pendingAddWidgetId, mPendingAddInfo);
828 if (workspaceLocked) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 }
833 }
834 // Leave the widget in the pending state if the user canceled the configure.
835 return;
836 }
837
Sunny Goyalaad90582015-07-09 14:22:50 -0700838 if (requestCode == REQUEST_CREATE_SHORTCUT) {
839 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
840 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
841 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
842 mPendingAddInfo);
843 if (isWorkspaceLocked()) {
844 sPendingAddItem = args;
845 } else {
846 completeAdd(args);
847 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
848 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
849 }
850 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700851 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
852 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800855 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800856
857 }
858
859 @Override
860 protected void onActivityResult(
861 final int requestCode, final int resultCode, final Intent data) {
862 handleActivityResult(requestCode, resultCode, data);
863 if (mLauncherCallbacks != null) {
864 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600868 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700869 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600870 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700871 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
872 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
873 View v = null;
874 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
875 if (layout != null) {
876 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
877 }
878 Intent intent = sPendingAddItem.intent;
879 sPendingAddItem = null;
880 if (grantResults.length > 0
881 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700882 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700883 } else {
884 // TODO: Show a snack bar with link to settings
885 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
886 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
887 }
888 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600889 if (mLauncherCallbacks != null) {
890 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
891 grantResults);
892 }
893 }
894
Adam Cohendb364c32014-05-20 14:23:40 -0700895 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
896 appWidgetId, ItemInfo info) {
897 PendingAddArguments args = new PendingAddArguments();
898 args.requestCode = requestCode;
899 args.intent = data;
900 args.container = info.container;
901 args.screenId = info.screenId;
902 args.cellX = info.cellX;
903 args.cellY = info.cellY;
904 args.appWidgetId = appWidgetId;
905 return args;
906 }
907
908 /**
909 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
910 *
911 * @param screenId the screen id to check
912 * @return the new screen, or screenId if it exists
913 */
914 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800915 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700916 if (dropLayout == null) {
917 // it's possible that the add screen was removed because it was
918 // empty and a re-bind occurred
919 mWorkspace.addExtraEmptyScreen();
920 return mWorkspace.commitExtraEmptyScreen();
921 } else {
922 return screenId;
923 }
924 }
925
Adam Cohen091440a2015-03-18 14:16:05 -0700926 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800927 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 Runnable onCompleteRunnable = null;
929 int animationType = 0;
930
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 if (resultCode == RESULT_OK) {
933 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
934 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 mPendingAddWidgetInfo);
936 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 onCompleteRunnable = new Runnable() {
938 @Override
939 public void run() {
940 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700942 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
943 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
945 };
946 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800947 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 if (mDragLayer.getAnimatedView() != null) {
951 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
952 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
953 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700954 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 // The animated view may be null in the case of a rotation during widget configuration
956 onCompleteRunnable.run();
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 protected void onStop() {
962 super.onStop();
963 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStop();
967 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700968
969 if (Utilities.isNycMR1OrAbove()) {
970 mAppWidgetHost.stopListening();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
975 protected void onStart() {
976 super.onStart();
977 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700978
979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.onStart();
981 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700982
983 if (Utilities.isNycMR1OrAbove()) {
984 mAppWidgetHost.startListening();
985 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700986 }
987
988 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200990 long startTime = 0;
991 if (DEBUG_RESUME_TIME) {
992 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400993 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200994 }
Adam Cohen9211d422014-10-07 18:14:53 -0700995
996 if (mLauncherCallbacks != null) {
997 mLauncherCallbacks.preOnResume();
998 }
999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -07001001 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001002
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001004 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001007 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001008 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001009 // view after launching an app, as they may be depending on the UI to be static to
1010 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07001011 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
1012 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001013 } else if (mOnResumeState == State.WIDGETS) {
1014 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001015 }
1016 mOnResumeState = State.NONE;
1017
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001018 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001019 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1020 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001021
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001022 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001024 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001025 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001027 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001030 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1031 // execute them here
1032 long startTimeCallbacks = 0;
1033 if (DEBUG_RESUME_TIME) {
1034 startTimeCallbacks = System.currentTimeMillis();
1035 }
1036
Michael Jurka1e2f4652013-07-08 18:03:46 -07001037 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1038 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001039 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001040 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001041 if (DEBUG_RESUME_TIME) {
1042 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1043 (System.currentTimeMillis() - startTimeCallbacks));
1044 }
Michael Jurka447bf842013-05-15 14:52:15 +02001045 }
Michael Jurka54554252013-08-01 12:52:23 +02001046 if (mOnResumeCallbacks.size() > 0) {
1047 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1048 mOnResumeCallbacks.get(i).run();
1049 }
1050 mOnResumeCallbacks.clear();
1051 }
Winson Chunge4e50662012-01-23 14:45:13 -08001052
1053 // Reset the pressed state of icons that were locked in the press state while activities
1054 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001055 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001056 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001057 mWaitingForResume.setStayPressed(false);
1058 }
Winson Chung82963d52013-10-09 11:20:57 -07001059
Adam Cohen06dff352012-06-01 17:17:08 -07001060 // It is possible that widgets can receive updates while launcher is not in the foreground.
1061 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1062 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1063 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001064 if (!isWorkspaceLoading()) {
1065 getWorkspace().reinflateWidgetsIfNecessary();
1066 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001067
Michael Jurka447bf842013-05-15 14:52:15 +02001068 if (DEBUG_RESUME_TIME) {
1069 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1070 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001071
Adam Cohen4b66bf32015-09-18 12:15:19 -07001072 // We want to suppress callbacks about CustomContent being shown if we have just received
1073 // onNewIntent while the user was present within launcher. In that case, we post a call
1074 // to move the user to the main screen (which will occur after onResume). We don't want to
1075 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1076 // suppress here.
1077 if (mWorkspace.getCustomContentCallbacks() != null
1078 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001080 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001081 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1082 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001083 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001084 }
1085 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001086 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001087 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001088 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001089
Sunny Goyal756adbc2015-04-16 15:20:51 -07001090 if (!isWorkspaceLoading()) {
1091 // Process any items that were added while Launcher was away.
1092 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1093 }
Adam Cohen9211d422014-10-07 18:14:53 -07001094
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001095 if (shouldShowDiscoveryBounce()) {
1096 mAllAppsController.showDiscoveryBounce();
1097 }
1098 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001099 if (mLauncherCallbacks != null) {
1100 mLauncherCallbacks.onResume();
1101 }
Adam Cohen06dff352012-06-01 17:17:08 -07001102 }
1103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001106 // Ensure that items added to Launcher are queued until Launcher returns
1107 InstallShortcutReceiver.enableInstallQueue();
1108
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001110 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001111 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001112 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001113
1114 // We call onHide() aggressively. The custom content callbacks should be able to
1115 // debounce excess onHide calls.
1116 if (mWorkspace.getCustomContentCallbacks() != null) {
1117 mWorkspace.getCustomContentCallbacks().onHide();
1118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohen9211d422014-10-07 18:14:53 -07001120 if (mLauncherCallbacks != null) {
1121 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001123 }
1124
1125 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001126 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1127 // by a onResume or by scrolling otherwise.
1128 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001129
1130 // Custom content is completely hidden
1131 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001132
1133 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1134 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001135
1136 // Indicates whether the user is allowed to scroll away from the custom content.
1137 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001138 }
1139
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 public interface LauncherOverlay {
1141
1142 /**
1143 * Touch interaction leading to overscroll has begun
1144 */
1145 public void onScrollInteractionBegin();
1146
1147 /**
1148 * Touch interaction related to overscroll has ended
1149 */
1150 public void onScrollInteractionEnd();
1151
1152 /**
1153 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1154 * screen (or in the case of RTL, the rightmost screen).
1155 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001156 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157
1158 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001159 * Called when the launcher is ready to use the overlay
1160 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001162 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001163 }
1164
Jun Mukai8af0cd82015-05-12 12:32:12 -07001165 public interface LauncherSearchCallbacks {
1166 /**
1167 * Called when the search overlay is shown.
1168 */
1169 public void onSearchOverlayOpened();
1170
1171 /**
1172 * Called when the search overlay is dismissed.
1173 */
1174 public void onSearchOverlayClosed();
1175 }
1176
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001178 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001179 }
1180
1181 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1182
Sunny Goyalc86df472016-02-25 09:19:38 -08001183 public void onScrollChanged(float progress) {
1184 if (mWorkspace != null) {
1185 mWorkspace.onOverlayScrollChanged(progress);
1186 }
1187 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001188 }
1189
Jorim Jaggid017f882014-01-14 17:08:48 -08001190 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001191 if (mLauncherCallbacks != null) {
1192 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001193 } else {
1194 // On devices with a locked orientation, we will at least have the allow rotation
1195 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001196 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001197 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001198 }
1199
Adam Cohen9211d422014-10-07 18:14:53 -07001200 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001201 CustomContentCallbacks callbacks, String description) {
1202 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001203 }
1204
Adam Cohenedb40762013-07-18 16:45:45 -07001205 // The custom content needs to offset its content to account for the QSB
1206 public int getTopOffsetForCustomContent() {
1207 return mWorkspace.getPaddingTop();
1208 }
1209
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001210 @Override
1211 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001213 if (mModel.isCurrentCallbacks(this)) {
1214 mModel.stopLoader();
1215 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001216 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1217
Romain Guy13c2e7b2010-03-10 19:45:00 -08001218 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001219 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 @Override
1223 public void onWindowFocusChanged(boolean hasFocus) {
1224 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001225 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001226
1227 if (mLauncherCallbacks != null) {
1228 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1229 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 private boolean acceptFilter() {
1233 final InputMethodManager inputManager = (InputMethodManager)
1234 getSystemService(Context.INPUT_METHOD_SERVICE);
1235 return !inputManager.isFullscreenMode();
1236 }
1237
1238 @Override
1239 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001240 final int uniChar = event.getUnicodeChar();
1241 final boolean handled = super.onKeyDown(keyCode, event);
1242 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1243 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1245 keyCode, event);
1246 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001248 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 // showSearchDialog()
1250 // If there are multiple keystrokes before the search dialog takes focus,
1251 // onSearchRequested() will be called for every keystroke,
1252 // but it is idempotent, so it's fine.
1253 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255 }
1256
Joe Onorato8a9625e2010-01-28 15:55:35 -08001257 // Eat the long press event so the keyboard doesn't come up.
1258 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1259 return true;
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 return handled;
1263 }
1264
Winson46163472015-09-22 17:31:56 -07001265 @Override
1266 public boolean onKeyUp(int keyCode, KeyEvent event) {
1267 if (keyCode == KeyEvent.KEYCODE_MENU) {
1268 // Ignore the menu key if we are currently dragging or are on the custom content screen
1269 if (!isOnCustomContent() && !mDragController.isDragging()) {
1270 // Close any open folders
1271 closeFolder();
1272
Tony Wickham49c8d292016-07-01 11:44:34 -07001273 // Close any shortcuts containers
1274 closeShortcutsContainer();
1275
Winson46163472015-09-22 17:31:56 -07001276 // Stop resizing any widgets
1277 mWorkspace.exitWidgetResizeMode();
1278
1279 // Show the overview mode if we are on the workspace
1280 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1281 !mWorkspace.isSwitchingState()) {
1282 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001283 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001284 }
1285 }
1286 return true;
1287 }
1288 return super.onKeyUp(keyCode, event);
1289 }
1290
Karl Rosaen138a0412009-04-23 19:00:21 -07001291 private String getTypedText() {
1292 return mDefaultKeySsb.toString();
1293 }
1294
1295 private void clearTypedText() {
1296 mDefaultKeySsb.clear();
1297 mDefaultKeySsb.clearSpans();
1298 Selection.setSelection(mDefaultKeySsb, 0);
1299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001302 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1303 * State
1304 */
1305 private static State intToState(int stateOrdinal) {
1306 State state = State.WORKSPACE;
1307 final State[] stateValues = State.values();
1308 for (int i = 0; i < stateValues.length; i++) {
1309 if (stateValues[i].ordinal() == stateOrdinal) {
1310 state = stateValues[i];
1311 break;
1312 }
1313 }
1314 return state;
1315 }
1316
1317 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 * Restores the previous state, if it exists.
1319 *
1320 * @param savedState The previous state.
1321 */
1322 private void restoreState(Bundle savedState) {
1323 if (savedState == null) {
1324 return;
1325 }
1326
Michael Jurka883f55b2010-10-21 15:47:14 -07001327 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001328 if (state == State.APPS || state == State.WIDGETS) {
1329 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001330 }
1331
Adam Cohen21cd0022013-10-09 18:57:02 -07001332 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1333 PagedView.INVALID_RESTORE_PAGE);
1334 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001335 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
Sunny Goyal756cd262015-08-20 12:33:21 -07001338 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1339 if (itemValues != null) {
1340 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001341 AppWidgetProviderInfo info = savedState.getParcelable(
1342 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001343 mPendingAddWidgetInfo = info == null ?
1344 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1345
Adam Cohen4637b5a2013-11-04 18:21:24 -08001346 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001347 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 mRestoring = true;
1349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
1352 /**
1353 * Finds all the views we need and configure them properly.
1354 */
1355 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001356 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001357 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001358 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001359 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001360 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1361 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001362 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001363
Sunny Goyal784f9c32016-03-23 16:56:54 -07001364 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1365 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1366 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001367 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Winson Chung4c98d922011-05-31 16:50:48 -07001369 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001370
1371 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001372
Winson Chung3d503fb2011-07-13 17:25:49 -07001373 // Setup the hotseat
1374 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1375 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001376 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 }
1378
Winsone9f27272015-10-13 10:47:51 -07001379 // Setup the overview panel
1380 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001381
Winson Chung4c98d922011-05-31 16:50:48 -07001382 // Setup the workspace
1383 mWorkspace.setHapticFeedbackEnabled(false);
1384 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001385 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001386 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1387 // default state, otherwise we will update to the wrong offsets in RTL
1388 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001389 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001390 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001391
Tony Wickham34d2c912015-09-11 09:27:58 -07001392 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001393 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001394
Winson Chungef7f8742015-06-04 17:18:17 -07001395 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001396 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001397 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001398 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1399 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1400 } else {
1401 mAppsView.setSearchBarController(new DefaultAppSearchController());
1402 }
Craig Mautner360310b2012-10-26 15:13:08 -07001403
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001405 mDragController.setDragScoller(mWorkspace);
1406 mDragController.setScrollView(mDragLayer);
1407 mDragController.setMoveTarget(mWorkspace);
1408 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001409 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001410
Peter Schiller310a9882016-06-30 13:52:36 -07001411 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1412 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1413 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001414
Sunny Goyal322d5562015-06-25 19:35:49 -07001415 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1416 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
Winsone9f27272015-10-13 10:47:51 -07001420 private void setupOverviewPanel() {
1421 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1422
1423 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1424 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1425 @Override
1426 public boolean onLongClick(View v) {
1427 return v.performClick();
1428 }
1429 };
1430
1431 // Bind wallpaper button actions
1432 View wallpaperButton = findViewById(R.id.wallpaper_button);
1433 wallpaperButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickWallpaperPicker(view);
1438 }
1439 }
1440 });
1441 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1442 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443
1444 // Bind widget button actions
1445 mWidgetsButton = findViewById(R.id.widget_button);
1446 mWidgetsButton.setOnClickListener(new OnClickListener() {
1447 @Override
1448 public void onClick(View view) {
1449 if (!mWorkspace.isSwitchingState()) {
1450 onClickAddWidgetButton(view);
1451 }
1452 }
1453 });
1454 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1455 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1456
1457 // Bind settings actions
1458 View settingsButton = findViewById(R.id.settings_button);
1459 boolean hasSettings = hasSettings();
1460 if (hasSettings) {
1461 settingsButton.setOnClickListener(new OnClickListener() {
1462 @Override
1463 public void onClick(View view) {
1464 if (!mWorkspace.isSwitchingState()) {
1465 onClickSettingsButton(view);
1466 }
1467 }
1468 });
1469 settingsButton.setOnLongClickListener(performClickOnLongClick);
1470 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1471 } else {
1472 settingsButton.setVisibility(View.GONE);
1473 }
1474
1475 mOverviewPanel.setAlpha(0f);
1476 }
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001479 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001480 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001481 */
1482 public void setAllAppsButton(View allAppsButton) {
1483 mAllAppsButton = allAppsButton;
1484 }
1485
Sunny Goyalbb011da2016-06-15 15:42:29 -07001486 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001487 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001488 }
1489
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001490 public View getWidgetsButton() {
1491 return mWidgetsButton;
1492 }
1493
Anjali Koppal5ad44842014-03-10 20:34:39 -07001494 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 * Creates a view representing a shortcut.
1496 *
1497 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001499 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001500 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
1502
1503 /**
1504 * Creates a view representing a shortcut inflated from the specified resource.
1505 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 * @param parent The group the shortcut belongs to.
1507 * @param info The data structure describing the shortcut.
1508 *
1509 * @return A View inflated from layoutResId.
1510 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001511 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001512 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001513 parent, false);
1514 favorite.applyFromShortcutInfo(info, mIconCache);
1515 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001517 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 return favorite;
1519 }
1520
1521 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 * Add a shortcut to the workspace.
1523 *
1524 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001526 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001527 int cellY) {
1528 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001530
Sunny Goyal5c97f512015-05-19 16:03:28 -07001531 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001532 if (info == null) {
1533 return;
1534 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 final View view = createShortcut(info);
1536
Sunny Goyal5c97f512015-05-19 16:03:28 -07001537 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 // First we check if we already know the exact location where we want to add this item.
1539 if (cellX >= 0 && cellY >= 0) {
1540 cellXY[0] = cellX;
1541 cellXY[1] = cellY;
1542 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001543
1544 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001545 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 true, null,null)) {
1547 return;
1548 }
1549 DragObject dragObject = new DragObject();
1550 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001551 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1552 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 return;
1554 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001555 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001557 }
1558
1559 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001560 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 return;
1562 }
1563
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001564 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570 }
1571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001573 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001575 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 */
Adam Cohen091440a2015-03-18 14:16:05 -07001577 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001578 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1579
1580 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001582 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Adam Cohen59400422014-03-05 18:07:04 -08001585 if (appWidgetInfo.isCustomWidget) {
1586 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001587 }
1588
Adam Cohen59400422014-03-05 18:07:04 -08001589 LauncherAppWidgetInfo launcherInfo;
1590 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1591 launcherInfo.spanX = info.spanX;
1592 launcherInfo.spanY = info.spanY;
1593 launcherInfo.minSpanX = info.minSpanX;
1594 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001595 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001598 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599
1600 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 if (hostView == null) {
1602 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001604 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001605 hostView.setVisibility(View.VISIBLE);
1606 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001608 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 }
Romain Guycbb89e42009-06-08 15:52:54 -07001610
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001611 private void addAppWidgetToWorkspace(
1612 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001613 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001614 hostView.setTag(item);
1615 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001616
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001617 hostView.setFocusable(true);
1618 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001619
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001620 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001621 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1622
1623 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001624 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001625 }
1626 }
1627
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1629 @Override
1630 public void onReceive(Context context, Intent intent) {
1631 final String action = intent.getAction();
1632 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1633 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001634 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001635 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001636
Winson Chungc100e8e2011-08-09 16:02:43 -07001637 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001638 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001639 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001640 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001641 if (!showWorkspace(false)) {
1642 // If we are already on the workspace, then manually reset all apps
1643 mAppsView.reset();
1644 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001645 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001646 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1648 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001649 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 }
1651 }
1652 };
1653
1654 @Override
1655 public void onAttachedToWindow() {
1656 super.onAttachedToWindow();
1657
1658 // Listen for broadcasts related to user-presence
1659 final IntentFilter filter = new IntentFilter();
1660 filter.addAction(Intent.ACTION_SCREEN_OFF);
1661 filter.addAction(Intent.ACTION_USER_PRESENT);
1662 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001663 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001664 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001666
1667 if (mLauncherCallbacks != null) {
1668 mLauncherCallbacks.onAttachedToWindow();
1669 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 }
1671
1672 @Override
1673 public void onDetachedFromWindow() {
1674 super.onDetachedFromWindow();
1675 mVisible = false;
1676
Adam Cohend113e0c2010-11-11 10:48:05 -08001677 if (mAttached) {
1678 unregisterReceiver(mReceiver);
1679 mAttached = false;
1680 }
Winson Chungb745afb2015-03-02 11:51:23 -08001681 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001682
1683 if (mLauncherCallbacks != null) {
1684 mLauncherCallbacks.onDetachedFromWindow();
1685 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 }
1687
1688 public void onWindowVisibilityChanged(int visibility) {
1689 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001690 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001691 // The following code used to be in onResume, but it turns out onResume is called when
1692 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1693 // is a more appropriate event to handle
1694 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001695 if (!mWorkspaceLoading) {
1696 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001697 // We want to let Launcher draw itself at least once before we force it to build
1698 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 // apps is nice and speedy.
1700 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001703 if (mStarted) return;
1704 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001705 // We delay the layer building a bit in order to give
1706 // other message processing a time to run. In particular
1707 // this avoids a delay in hiding the IME if it was
1708 // currently shown, because doing that may involve
1709 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001710 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001711 final ViewTreeObserver.OnDrawListener listener = this;
1712 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001713 public void run() {
1714 if (mWorkspace != null &&
1715 mWorkspace.getViewTreeObserver() != null) {
1716 mWorkspace.getViewTreeObserver().
1717 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001718 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001719 }
1720 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001721 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001722 }
1723 });
1724 }
1725 clearTypedText();
1726 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 }
1728
Adam Cohen091440a2015-03-18 14:16:05 -07001729 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 mHandler.removeMessages(ADVANCE_MSG);
1731 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1732 mHandler.sendMessageDelayed(msg, delay);
1733 mAutoAdvanceSentTime = System.currentTimeMillis();
1734 }
1735
Adam Cohen091440a2015-03-18 14:16:05 -07001736 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1738 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1739 mAutoAdvanceRunning = autoAdvanceRunning;
1740 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 sendAdvanceMessage(delay);
1743 } else {
1744 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001745 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1747 }
1748 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001749 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751 }
1752 }
1753
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1755
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (msg.what == ADVANCE_MSG) {
1759 int i = 0;
1760 for (View key: mWidgetsToAdvance.keySet()) {
1761 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001762 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001764 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 public void run() {
1766 ((Advanceable) v).advance();
1767 }
1768 }, delay);
1769 }
1770 i++;
1771 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001772 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001774 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001776 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001777
Winsonc0b52fe2015-09-09 16:38:15 -07001778 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001779 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1781 if (v instanceof Advanceable) {
1782 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001783 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001784 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
1786 }
1787
Winsonc0b52fe2015-09-09 16:38:15 -07001788 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001789 if (mWidgetsToAdvance.containsKey(hostView)) {
1790 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001791 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 }
1794
Hyunyoung Song3f471442015-04-08 19:01:34 -07001795 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001796 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1797 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 }
1799
Winson Chunga6945242014-01-08 14:04:34 -08001800 public DragLayer getDragLayer() {
1801 return mDragLayer;
1802 }
1803
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001804 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001805 return mAppsView;
1806 }
1807
Hyunyoung Song3f471442015-04-08 19:01:34 -07001808 public WidgetsContainerView getWidgetsView() {
1809 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001810 }
1811
Winson Chunga6945242014-01-08 14:04:34 -08001812 public Workspace getWorkspace() {
1813 return mWorkspace;
1814 }
1815
Sunny Goyal6178f132016-07-11 17:30:03 -07001816 public View getQsbContainer() {
1817 return mQsbContainer;
1818 }
1819
Winson Chunga6945242014-01-08 14:04:34 -08001820 public Hotseat getHotseat() {
1821 return mHotseat;
1822 }
1823
Jorim Jaggid017f882014-01-14 17:08:48 -08001824 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001825 return mOverviewPanel;
1826 }
1827
Sunny Goyal47328fd2016-05-25 18:56:41 -07001828 public DropTargetBar getDropTargetBar() {
1829 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001830 }
1831
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001832 public LauncherAppWidgetHost getAppWidgetHost() {
1833 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
Romain Guycbb89e42009-06-08 15:52:54 -07001835
Winson Chunga9abd0e2010-10-27 17:18:37 -07001836 public LauncherModel getModel() {
1837 return mModel;
1838 }
1839
Adam Cohen79d90c52016-04-22 13:29:20 -07001840 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001841 return mSharedPrefs;
1842 }
1843
Adam Cohen2e6da152015-05-06 11:42:25 -07001844 public DeviceProfile getDeviceProfile() {
1845 return mDeviceProfile;
1846 }
1847
Bjorn Bringertc459e522013-06-07 19:36:01 +01001848 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001849 getWindow().closeAllPanels();
1850
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001851 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001852 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001853 }
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 @Override
1856 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001857 long startTime = 0;
1858 if (DEBUG_RESUME_TIME) {
1859 startTime = System.currentTimeMillis();
1860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 super.onNewIntent(intent);
1862
1863 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001864 Folder openFolder = mWorkspace.getOpenFolder();
1865 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1866 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1867 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1868 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1869 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001870 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001871 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001872
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 if (mWorkspace == null) {
1874 // Can be cases where mWorkspace is null, this prevents a NPE
1875 return;
1876 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 // In all these cases, only animate if we're already on home
1878 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001879
Sunny Goyal935fca12015-10-13 10:19:01 -07001880 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001881 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001882 exitSpringLoadedDragMode();
1883
1884 // If we are already on home, then just animate back to the workspace,
1885 // otherwise, just wait until onResume to set the state back to Workspace
1886 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001887 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 } else {
1889 mOnResumeState = State.WORKSPACE;
1890 }
1891
1892 final View v = getWindow().peekDecorView();
1893 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001894 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001895 INPUT_METHOD_SERVICE);
1896 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1897 }
1898
Winson Chungb745afb2015-03-02 11:51:23 -08001899 // Reset the apps view
1900 if (!alreadyOnHome && mAppsView != null) {
1901 mAppsView.scrollToTop();
1902 }
1903
Hyunyoung Song3f471442015-04-08 19:01:34 -07001904 // Reset the widgets view
1905 if (!alreadyOnHome && mWidgetsView != null) {
1906 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001907 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001908
Adam Cohen9211d422014-10-07 18:14:53 -07001909 if (mLauncherCallbacks != null) {
1910 mLauncherCallbacks.onHomeIntent();
1911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Adam Cohened307df2013-10-02 09:37:31 -07001913
Adam Cohen9211d422014-10-07 18:14:53 -07001914 if (mLauncherCallbacks != null) {
1915 mLauncherCallbacks.onNewIntent(intent);
1916 }
Winson15f8b172015-08-19 11:02:39 -07001917
1918 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1919 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1920 // animation.
1921 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001922 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1923 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001924 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1925 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001926
1927 // We use this flag to suppress noisy callbacks above custom content state
1928 // from onResume.
1929 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001930 mWorkspace.post(new Runnable() {
1931 @Override
1932 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001933 if (mWorkspace != null) {
1934 mWorkspace.moveToDefaultScreen(true);
1935 }
Winson15f8b172015-08-19 11:02:39 -07001936 }
1937 });
1938 }
1939 }
1940
1941 if (DEBUG_RESUME_TIME) {
1942 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1943 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001944 }
1945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001947 public void onRestoreInstanceState(Bundle state) {
1948 super.onRestoreInstanceState(state);
1949 for (int page: mSynchronouslyBoundPages) {
1950 mWorkspace.restoreInstanceStateForChild(page);
1951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
1953
1954 @Override
1955 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001956 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001957 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1958 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001959
Adam Cohen21cd0022013-10-09 18:57:02 -07001960 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001961 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Michael Jurka883f55b2010-10-21 15:47:14 -07001963 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001964 // We close any open folder since it will not be re-opened, and we need to make sure
1965 // this state is reflected.
1966 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1967 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001968 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001969
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001971 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001972 ContentValues itemValues = new ContentValues();
1973 mPendingAddInfo.writeToValues(itemValues);
1974 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001975 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001976 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
Adam Cohen9211d422014-10-07 18:14:53 -07001979 if (mLauncherCallbacks != null) {
1980 mLauncherCallbacks.onSaveInstanceState(outState);
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001987
Winson Chunge7a03942011-08-05 15:05:12 -07001988 // Remove all pending runnables
1989 mHandler.removeMessages(ADVANCE_MSG);
1990 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001991 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001992 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001993
Winson Chungcd2b0142011-06-08 16:02:26 -07001994 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001995 // It's possible to receive onDestroy after a new Launcher activity has
1996 // been created. In this case, don't interfere with the new Launcher.
1997 if (mModel.isCurrentCallbacks(this)) {
1998 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001999 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07002000 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002001
Sunny Goyal745bad92016-05-02 10:54:12 -07002002 if (mRotationPrefChangeHandler != null) {
2003 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
2004 }
2005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002007 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002009 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002011 mAppWidgetHost = null;
2012
2013 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014
2015 TextKeyListener.getInstance().release();
2016
Tony Wickhame2217252016-03-22 16:34:23 -07002017 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2018 .removeAccessibilityStateChangeListener(this);
2019
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002020 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002021
Michael Jurka2ecf9952012-06-18 12:52:28 -07002022 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002023
2024 if (mLauncherCallbacks != null) {
2025 mLauncherCallbacks.onDestroy();
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
Sunny Goyalae502842016-06-17 08:43:56 -07002029 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2030 return mAccessibilityDelegate;
2031 }
2032
Adam Cohena9cf38f2011-05-02 15:36:58 -07002033 public DragController getDragController() {
2034 return mDragController;
2035 }
2036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002038 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002039 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002040 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002041 }
2042
2043 @Override
2044 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2045 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2046 onStartForResult(requestCode);
2047 try {
2048 super.startIntentSenderForResult(intent, requestCode,
2049 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2050 } catch (IntentSender.SendIntentException e) {
2051 throw new ActivityNotFoundException();
2052 }
2053 }
2054
2055 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002056 if (requestCode >= 0) {
2057 setWaitingForResult(true);
2058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060
Winson Chung70d72102011-08-12 11:24:35 -07002061 /**
2062 * Indicates that we want global search for this activity by setting the globalSearch
2063 * argument for {@link #startSearch} to true.
2064 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002066 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002068
Karl Rosaen138a0412009-04-23 19:00:21 -07002069 if (initialQuery == null) {
2070 // Use any text typed in the launcher as the initial query
2071 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 if (appSearchData == null) {
2074 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002075 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002077
2078 // TODO send proper bounds.
2079 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002080
Ian Parkinson047036e2014-09-01 15:40:53 +01002081 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002082 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002083 if (clearTextImmediately) {
2084 clearTypedText();
2085 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002086
2087 // We need to show the workspace after starting the search
2088 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 }
2090
Ian Parkinson047036e2014-09-01 15:40:53 +01002091 /**
2092 * Start a text search.
2093 *
2094 * @return {@code true} if the search will start immediately, so any further keypresses
2095 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2096 * to buffer keypresses.
2097 */
2098 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002100 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2101 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2102 sourceBounds);
2103 }
2104
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002106 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002107 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002108 }
2109
2110 /**
2111 * Starts the global search activity. This code is a copied from SearchManager
2112 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002114 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002115 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002116 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2117 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2118 if (globalSearchActivity == null) {
2119 Log.w(TAG, "No global search activity found.");
2120 return;
2121 }
2122 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2123 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2124 intent.setComponent(globalSearchActivity);
2125 // Make sure that we have a Bundle to put source in
2126 if (appSearchData == null) {
2127 appSearchData = new Bundle();
2128 } else {
2129 appSearchData = new Bundle(appSearchData);
2130 }
Adam Cohen9211d422014-10-07 18:14:53 -07002131 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 if (!appSearchData.containsKey("source")) {
2133 appSearchData.putString("source", getPackageName());
2134 }
2135 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2136 if (!TextUtils.isEmpty(initialQuery)) {
2137 intent.putExtra(SearchManager.QUERY, initialQuery);
2138 }
2139 if (selectInitialQuery) {
2140 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2141 }
2142 intent.setSourceBounds(sourceBounds);
2143 try {
2144 startActivity(intent);
2145 } catch (ActivityNotFoundException ex) {
2146 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 }
2149
Yura4f93ec62014-02-04 14:15:21 +00002150 public boolean isOnCustomContent() {
2151 return mWorkspace.isOnOrMovingToCustomContent();
2152 }
2153
Winson Chung70d72102011-08-12 11:24:35 -07002154 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002155 public boolean onPrepareOptionsMenu(Menu menu) {
2156 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002157 if (mLauncherCallbacks != null) {
2158 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2159 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002160 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 @Override
2164 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002165 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002166 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002167 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002168 }
2169
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 public boolean isWorkspaceLocked() {
2171 return mWorkspaceLoading || mWaitingForResult;
2172 }
2173
Adam Cohen517a7f52014-03-01 12:12:59 -08002174 public boolean isWorkspaceLoading() {
2175 return mWorkspaceLoading;
2176 }
2177
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178 private void setWorkspaceLoading(boolean value) {
2179 boolean isLocked = isWorkspaceLocked();
2180 mWorkspaceLoading = value;
2181 if (isLocked != isWorkspaceLocked()) {
2182 onWorkspaceLockedChanged();
2183 }
2184 }
2185
2186 private void setWaitingForResult(boolean value) {
2187 boolean isLocked = isWorkspaceLocked();
2188 mWaitingForResult = value;
2189 if (isLocked != isWorkspaceLocked()) {
2190 onWorkspaceLockedChanged();
2191 }
2192 }
2193
Adam Cohen9211d422014-10-07 18:14:53 -07002194 protected void onWorkspaceLockedChanged() {
2195 if (mLauncherCallbacks != null) {
2196 mLauncherCallbacks.onWorkspaceLockedChanged();
2197 }
2198 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002199
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002201 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002203 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2204 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002205 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002206 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002207
Tony Wickhama0628cc2015-10-14 15:23:04 -07002208 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002209 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002210 if (LOGD) {
2211 Log.d(TAG, "Adding widget from drop");
2212 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002213 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2214 }
2215
Sunny Goyale6b63a32015-01-16 16:14:29 -08002216 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002217 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2218 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002219 if (appWidgetInfo.configure != null) {
2220 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002221 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002222
Bjorn Bringert7984c942009-12-09 15:38:25 +00002223 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002224 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2225 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002228 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002229 Runnable onComplete = new Runnable() {
2230 @Override
2231 public void run() {
2232 // Exit spring loaded mode if necessary after adding the widget
2233 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2234 null);
2235 }
2236 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002238 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002239 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241 }
Romain Guycbb89e42009-06-08 15:52:54 -07002242
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002243 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002244 // Close any folders that may be open.
2245 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002246 mWorkspace.moveToCustomContentScreen(animate);
2247 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002248
2249 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2250 int[] cell, int spanX, int spanY) {
2251 switch (info.itemType) {
2252 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2253 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2254 int span[] = new int[2];
2255 span[0] = spanX;
2256 span[1] = spanY;
2257 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2258 container, screenId, cell, span);
2259 break;
2260 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2261 processShortcutFromDrop(info.componentName, container, screenId, cell);
2262 break;
2263 default:
2264 throw new IllegalStateException("Unknown item type: " + info.itemType);
2265 }
2266 }
2267
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 /**
2269 * Process a shortcut drop.
2270 *
2271 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002275 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2276 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280
2281 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.cellX = cell[0];
2283 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285
2286 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2287 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002288 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 }
2290
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 /**
2292 * Process a widget drop.
2293 *
2294 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002298 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2299 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002303 mPendingAddInfo.minSpanX = info.minSpanX;
2304 mPendingAddInfo.minSpanY = info.minSpanY;
2305
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 mPendingAddInfo.cellX = cell[0];
2308 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002310 if (span != null) {
2311 mPendingAddInfo.spanX = span[0];
2312 mPendingAddInfo.spanY = span[1];
2313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 AppWidgetHostView hostView = info.boundWidget;
2316 int appWidgetId;
2317 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002318 // In the case where we've prebound the widget, we remove it from the DragLayer
2319 if (LOGD) {
2320 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2321 }
2322 getDragLayer().removeView(hostView);
2323
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002325 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002326
2327 // Clear the boundWidget so that it doesn't get destroyed.
2328 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 // In this case, we either need to start an activity to get permission to bind
2331 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002332 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002333 Bundle options = info.bindOptions;
2334
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2336 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002337 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002338 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002339 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002340 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002341 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2342 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2343 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002344 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2345 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002346 // TODO: we need to make sure that this accounts for the options bundle.
2347 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002348 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2349 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 }
2352
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002355 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 folderInfo.title = getText(R.string.folder_name);
2357
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002359 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2360 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361
2362 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 FolderIcon newFolder =
2364 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002366 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002367 // Force measure the new folder icon
2368 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2369 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002370 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 }
Romain Guycbb89e42009-06-08 15:52:54 -07002372
Winsonc0b52fe2015-09-09 16:38:15 -07002373 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002374 * Unbinds the view for the specified item, and removes the item and all its children.
2375 *
2376 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002377 * @param itemInfo the {@link ItemInfo} for this view.
2378 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002379 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002380 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002381 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002382 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002383 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2384 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002385 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002386 } else {
2387 mWorkspace.removeWorkspaceItem(v);
2388 }
Winsonc0b52fe2015-09-09 16:38:15 -07002389 if (deleteFromDb) {
2390 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2391 }
2392 } else if (itemInfo instanceof FolderInfo) {
2393 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002394 if (v instanceof FolderIcon) {
2395 ((FolderIcon) v).removeListeners();
2396 }
Winsonc0b52fe2015-09-09 16:38:15 -07002397 mWorkspace.removeWorkspaceItem(v);
2398 if (deleteFromDb) {
2399 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2400 }
2401 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2402 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002403 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002404 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002405 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002406 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
2408 } else {
2409 return false;
2410 }
2411 return true;
2412 }
2413
2414 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002415 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002416 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002417 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002418 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002419 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002420 // Deleting an app widget ID is a void call but writes to disk before returning
2421 // to the caller...
2422 new AsyncTask<Void, Void, Void>() {
2423 public Void doInBackground(Void ... args) {
2424 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2425 return null;
2426 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002427 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002428 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002429 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002430 }
2431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 @Override
2433 public boolean dispatchKeyEvent(KeyEvent event) {
2434 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2435 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002438 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002439 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002440 dumpState();
2441 return true;
2442 }
2443 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002444 }
2445 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2446 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002447 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 return true;
2449 }
2450 }
2451
2452 return super.dispatchKeyEvent(event);
2453 }
2454
Joe Onorato88ec0992009-11-19 13:16:06 -08002455 @Override
2456 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002457 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2458 return;
2459 }
2460
Sunny Goyal45478022015-06-08 16:52:41 -07002461 if (mDragController.isDragging()) {
2462 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002463 return;
2464 }
2465
Tony Wickham49c8d292016-07-01 11:44:34 -07002466 if (getOpenShortcutsContainer() != null) {
2467 closeShortcutsContainer();
2468 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002469 showWorkspace(true);
2470 } else if (isWidgetsViewVisible()) {
2471 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002473 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002475 Folder openFolder = mWorkspace.getOpenFolder();
2476 if (openFolder.isEditingName()) {
2477 openFolder.dismissEditingName();
2478 } else {
2479 closeFolder();
2480 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002481 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002482 mWorkspace.exitWidgetResizeMode();
2483
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 // Back button is a no-op here, but give at least some feedback for the button press
2485 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002486 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 * Launches the intent referred by the clicked shortcut.
2491 *
2492 * @param v The view representing the clicked shortcut.
2493 */
2494 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002495 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2496 // view has detached (it's possible for this to happen if the view is removed mid touch).
2497 if (v.getWindowToken() == null) {
2498 return;
2499 }
2500
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002501 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002502 return;
2503 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002504
Adam Cohen1697b792013-09-17 19:08:21 -07002505 if (v instanceof Workspace) {
2506 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002507 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002508 }
2509 return;
2510 }
2511
2512 if (v instanceof CellLayout) {
2513 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002514 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2515 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002516 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002517 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002521 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002524 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002527 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2528 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002529 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002530 } else if (tag instanceof AppInfo) {
2531 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002532 } else if (tag instanceof LauncherAppWidgetInfo) {
2533 if (v instanceof PendingAppWidgetHostView) {
2534 onClickPendingWidget((PendingAppWidgetHostView) v);
2535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 }
2537 }
2538
Sunny Goyal70660032015-05-14 00:07:08 -07002539 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002540 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002541 return false;
2542 }
2543
Michael Jurkaaf442092010-06-10 17:01:57 -07002544 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002545 * Event handler for the app widget view which has not fully restored.
2546 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002548 if (mIsSafeModeEnabled) {
2549 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2550 return;
2551 }
2552
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002554 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002555 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2556 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2557 // This should not happen, as we make sure that an Id is allocated during bind.
2558 return;
2559 }
2560 LauncherAppWidgetProviderInfo appWidgetInfo =
2561 mAppWidgetManager.findProvider(info.providerName, info.user);
2562 if (appWidgetInfo != null) {
2563 mPendingAddWidgetId = info.appWidgetId;
2564 mPendingAddInfo.copyFrom(info);
2565 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002566
Sunny Goyald478c832016-04-01 12:04:16 -07002567 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2568 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2569 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2570 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2571 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2572 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2573 }
2574 } else {
2575 LauncherAppWidgetProviderInfo appWidgetInfo =
2576 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2577 if (appWidgetInfo != null) {
2578 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2579 }
Sunny Goyalff572272014-07-23 13:58:07 -07002580 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 } else if (info.installProgress < 0) {
2582 // The install has not been queued
2583 final String packageName = info.providerName.getPackageName();
2584 showBrokenAppInstallDialog(packageName,
2585 new DialogInterface.OnClickListener() {
2586 public void onClick(DialogInterface dialog, int id) {
2587 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2588 }
2589 });
2590 } else {
2591 // Download has started.
2592 final String packageName = info.providerName.getPackageName();
2593 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 }
2595 }
2596
Sunny Goyald478c832016-04-01 12:04:16 -07002597 private void startRestoredWidgetReconfigActivity(
2598 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2599 mPendingAddWidgetInfo = provider;
2600 mPendingAddInfo.copyFrom(info);
2601 mPendingAddWidgetId = info.appWidgetId;
2602 mAppWidgetManager.startConfigActivity(provider,
2603 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2604 }
2605
Sunny Goyalff572272014-07-23 13:58:07 -07002606 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002607 * Event handler for the "grid" button that appears on the home screen, which
2608 * enters all apps mode.
2609 *
2610 * @param v The view that was clicked.
2611 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 protected void onClickAllAppsButton(View v) {
2613 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002614 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002615 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002616 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002617 showAppsView(true /* animated */, true /* updatePredictedApps */,
2618 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002619 }
2620 }
2621
2622 protected void onLongClickAllAppsButton(View v) {
2623 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2624 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002625 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002626 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002627 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002628 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 }
2630 }
2631
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 private void showBrokenAppInstallDialog(final String packageName,
2633 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002634 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002635 .setTitle(R.string.abandoned_promises_title)
2636 .setMessage(R.string.abandoned_promise_explanation)
2637 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2638 .setNeutralButton(R.string.abandoned_clean_this,
2639 new DialogInterface.OnClickListener() {
2640 public void onClick(DialogInterface dialog, int id) {
2641 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2642 mWorkspace.removeAbandonedPromise(packageName, user);
2643 }
2644 })
2645 .create().show();
2646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 /**
2650 * Event handler for an app shortcut click.
2651 *
2652 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2653 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002655 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2656 Object tag = v.getTag();
2657 if (!(tag instanceof ShortcutInfo)) {
2658 throw new IllegalArgumentException("Input must be a Shortcut");
2659 }
2660
2661 // Open shortcut
2662 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002663
2664 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002665 if ((shortcut.isDisabled &
2666 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2667 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2668 // If the app is only disabled because of the above flags, launch activity anyway.
2669 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002670 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002671 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2672 // Use a message specific to this shortcut, if it has one.
2673 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2674 return;
2675 }
2676 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002677 int error = R.string.activity_not_available;
2678 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2679 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002680 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2681 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002682 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002683 }
2684 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2685 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002686 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002687 }
2688
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002690 if ((v instanceof BubbleTextView)
2691 && shortcut.isPromise()
2692 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002693 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002694 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 new DialogInterface.OnClickListener() {
2696 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002697 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 return;
2701 }
2702
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002704 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002705 }
2706
Sunny Goyala7ce1662016-05-31 15:01:35 -07002707 private void startAppShortcutOrInfoActivity(View v) {
2708 ItemInfo item = (ItemInfo) v.getTag();
2709 Intent intent = item.getIntent();
2710 if (intent == null) {
2711 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002712 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002713 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002714 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715
2716 if (success && v instanceof BubbleTextView) {
2717 mWaitingForResume = (BubbleTextView) v;
2718 mWaitingForResume.setStayPressed(true);
2719 }
2720 }
2721
2722 /**
2723 * Event handler for a folder icon click.
2724 *
2725 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2726 */
2727 protected void onClickFolderIcon(View v) {
2728 if (LOGD) Log.d(TAG, "onClickFolder");
2729 if (!(v instanceof FolderIcon)){
2730 throw new IllegalArgumentException("Input must be a FolderIcon");
2731 }
2732
2733 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002734 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002736 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 }
Michael Jurka5130e402011-10-13 04:55:35 -07002738 }
2739
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002740 /**
2741 * Event handler for the (Add) Widgets button that appears after a long press
2742 * on the home screen.
2743 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002744 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002746 if (mIsSafeModeEnabled) {
2747 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2748 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002749 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002750 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002751 }
2752
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002753 /**
2754 * Event handler for the wallpaper picker button that appears after a long press
2755 * on the home screen.
2756 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002757 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002758 if (!Utilities.isWallapaperAllowed(this)) {
2759 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2760 return;
2761 }
2762
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002763 String pickerPackage = getString(R.string.wallpaper_picker_package);
2764 if (TextUtils.isEmpty(pickerPackage)) {
2765 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2766 }
2767
Tony Wickham785f7a52015-08-31 17:28:32 -07002768 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2769 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002770
2771 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002772 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002773 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2774 intent.setSourceBounds(getViewBounds(v));
2775 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 }
2777
2778 /**
2779 * Event handler for a click on the settings button that appears after a long press
2780 * on the home screen.
2781 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002782 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002783 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002784 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2785 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 }
2787
Adam Cohen61f560d2013-09-30 15:58:20 -07002788 public View.OnTouchListener getHapticFeedbackTouchListener() {
2789 if (mHapticFeedbackTouchListener == null) {
2790 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002791 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002792 @Override
2793 public boolean onTouch(View v, MotionEvent event) {
2794 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2795 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2796 }
2797 return false;
2798 }
2799 };
2800 }
2801 return mHapticFeedbackTouchListener;
2802 }
2803
Tony Wickhame2217252016-03-22 16:34:23 -07002804 @Override
2805 public void onAccessibilityStateChanged(boolean enabled) {
2806 mDragLayer.onAccessibilityStateChanged(enabled);
2807 }
2808
Adam Cohen9211d422014-10-07 18:14:53 -07002809 public void onDragStarted(View view) {
2810 if (isOnCustomContent()) {
2811 // Custom content screen doesn't participate in drag and drop. If on custom
2812 // content screen, move to default.
2813 moveWorkspaceToDefaultScreen();
2814 }
Adam Cohen9211d422014-10-07 18:14:53 -07002815 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002816
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002817 /**
2818 * Called when the user stops interacting with the launcher.
2819 * This implies that the user is now on the homescreen and is not doing housekeeping.
2820 */
Adam Cohen9211d422014-10-07 18:14:53 -07002821 protected void onInteractionEnd() {
2822 if (mLauncherCallbacks != null) {
2823 mLauncherCallbacks.onInteractionEnd();
2824 }
2825 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002826
2827 /**
2828 * Called when the user starts interacting with the launcher.
2829 * The possible interactions are:
2830 * - open all apps
2831 * - reorder an app shortcut, or a widget
2832 * - open the overview mode.
2833 * This is a good time to stop doing things that only make sense
2834 * when the user is on the homescreen and not doing housekeeping.
2835 */
Adam Cohen9211d422014-10-07 18:14:53 -07002836 protected void onInteractionBegin() {
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onInteractionBegin();
2839 }
2840 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002841
Winson Chungcd99cd32015-04-29 11:03:24 -07002842 /** Updates the interaction state. */
2843 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002844 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002845 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002846 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2847 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002848 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002849 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002850 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002851 onInteractionEnd();
2852 }
2853 }
2854
Sunny Goyala7ce1662016-05-31 15:01:35 -07002855 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002857 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2858 try {
2859 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2860 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2861 // is enabled by default on NYC.
2862 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2863 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002864
2865 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2866 String id = ((ShortcutInfo) info).getDeepShortcutId();
2867 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002868 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2869 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002870 } else {
2871 // Could be launching some bookkeeping activity
2872 startActivity(intent, optsBundle);
2873 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002874 } finally {
2875 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002878 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2879 // corresponding permission. Show the appropriate permission prompt if that
2880 // is the case.
2881 if (intent.getComponent() == null
2882 && Intent.ACTION_CALL.equals(intent.getAction())
2883 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2884 PackageManager.PERMISSION_GRANTED) {
2885 // TODO: Rename sPendingAddItem to a generic name.
2886 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2887 0, info);
2888 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2889 REQUEST_PERMISSION_CALL_PHONE);
2890 } else {
2891 // No idea why this was thrown.
2892 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 }
2895 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002896
Sunny Goyala7ce1662016-05-31 15:01:35 -07002897 private Bundle getActivityLaunchOptions(View v) {
2898 if (Utilities.ATLEAST_MARSHMALLOW) {
2899 int left = 0, top = 0;
2900 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2901 if (v instanceof TextView) {
2902 // Launch from center of icon, not entire view
2903 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2904 if (icon != null) {
2905 Rect bounds = icon.getBounds();
2906 left = (width - bounds.width()) / 2;
2907 top = v.getPaddingTop();
2908 width = bounds.width();
2909 height = bounds.height();
2910 }
2911 }
2912 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2913 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2914 // On L devices, we use the device default slide-up transition.
2915 // On L MR1 devices, we use a custom version of the slide-up transition which
2916 // doesn't have the delay present in the device default.
2917 return ActivityOptions.makeCustomAnimation(
2918 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2919 }
2920 return null;
2921 }
2922
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002923 private Rect getViewBounds(View v) {
2924 int[] pos = new int[2];
2925 v.getLocationOnScreen(pos);
2926 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2927 }
2928
Sunny Goyala7ce1662016-05-31 15:01:35 -07002929 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002930 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2931 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2932 return false;
2933 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002934 // Only launch using the new animation if the shortcut has not opted out (this is a
2935 // private contract between launcher and may be ignored in the future).
2936 boolean useLaunchAnimation = (v != null) &&
2937 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2938 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2939
2940 UserHandleCompat user = null;
2941 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2942 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2943 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002944 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002945
2946 // Prepare intent
2947 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2948 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002949 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002950 }
2951 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002952 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2953 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2954 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002955 // Shortcuts need some special checks due to legacy reasons.
2956 startShortcutIntentSafely(intent, optsBundle, item);
2957 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2958 // Could be launching some bookkeeping activity
2959 startActivity(intent, optsBundle);
2960 } else {
2961 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2962 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2963 }
2964 return true;
2965 } catch (ActivityNotFoundException|SecurityException e) {
2966 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2967 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2968 }
2969 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002970 }
2971
Adam Cohen268c4752012-06-06 17:47:33 -07002972 /**
2973 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2974 * in the DragLayer in the exact absolute location of the original FolderIcon.
2975 */
2976 private void copyFolderIconToImage(FolderIcon fi) {
2977 final int width = fi.getMeasuredWidth();
2978 final int height = fi.getMeasuredHeight();
2979
2980 // Lazy load ImageView, Bitmap and Canvas
2981 if (mFolderIconImageView == null) {
2982 mFolderIconImageView = new ImageView(this);
2983 }
2984 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2985 mFolderIconBitmap.getHeight() != height) {
2986 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2987 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2988 }
2989
2990 DragLayer.LayoutParams lp;
2991 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2992 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2993 } else {
2994 lp = new DragLayer.LayoutParams(width, height);
2995 }
2996
Adam Cohen307fe232012-08-16 17:55:58 -07002997 // The layout from which the folder is being opened may be scaled, adjust the starting
2998 // view size by this scale factor.
2999 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003000 lp.customPosition = true;
3001 lp.x = mRectForFolderAnimation.left;
3002 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003003 lp.width = (int) (scale * width);
3004 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003005
3006 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3007 fi.draw(mFolderIconCanvas);
3008 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003009 if (fi.getFolder() != null) {
3010 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3011 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003012 }
Adam Cohen268c4752012-06-06 17:47:33 -07003013 // Just in case this image view is still in the drag layer from a previous animation,
3014 // we remove it and re-add it.
3015 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3016 mDragLayer.removeView(mFolderIconImageView);
3017 }
3018 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003019 if (fi.getFolder() != null) {
3020 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003021 }
Adam Cohen268c4752012-06-06 17:47:33 -07003022 }
3023
Adam Cohen37b2a492016-04-06 18:36:06 -07003024 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003025 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003026 FolderInfo info = (FolderInfo) fi.getTag();
3027 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3028 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003029 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3030 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003031 }
3032
Adam Cohen268c4752012-06-06 17:47:33 -07003033 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3034 copyFolderIconToImage(fi);
3035 fi.setVisibility(View.INVISIBLE);
3036
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003037 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3038 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003039 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003040 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3041 }
3042 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003043 oa.start();
3044 }
3045
Sunny Goyal935fca12015-10-13 10:19:01 -07003046 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003047 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003048 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003049
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // We remove and re-draw the FolderIcon in-case it has changed
3051 mDragLayer.removeView(mFolderIconImageView);
3052 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003053
3054 if (cl != null) {
3055 cl.clearFolderLeaveBehind();
3056 }
3057
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003058 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003059 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003060 oa.addListener(new AnimatorListenerAdapter() {
3061 @Override
3062 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003063 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003064 // Remove the ImageView copy of the FolderIcon and make the original visible.
3065 mDragLayer.removeView(mFolderIconImageView);
3066 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003067 }
3068 }
3069 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003070 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003071 if (!animate) {
3072 oa.end();
3073 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003074 }
3075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003077 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078 * is animated relative to the specified View. If the View is null, no animation
3079 * is played.
3080 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003081 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003083 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003084
Adam Cohenfb91f302012-06-11 15:45:18 -07003085 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003086 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3087 if (openFolder != null && openFolder != folder) {
3088 // Close any open folder before opening a folder.
3089 closeFolder();
3090 }
3091
Adam Cohena9cf38f2011-05-02 15:36:58 -07003092 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093
Adam Cohena9cf38f2011-05-02 15:36:58 -07003094 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095
Sunny Goyalf4066152015-04-15 09:42:19 -07003096 // While the folder is open, the position of the icon cannot change.
3097 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3098
Adam Cohen4554ee12011-08-03 16:13:21 -07003099 // Just verify that the folder hasn't already been added to the DragLayer.
3100 // There was a one-off crash where the folder had a parent already.
3101 if (folder.getParent() == null) {
3102 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003103 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003104 } else {
3105 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3106 folder.getParent() + ").");
3107 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003108 folder.animateOpen();
3109
3110 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003111
3112 // Notify the accessibility manager that this folder "window" has appeared and occluded
3113 // the workspace items
3114 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3115 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003116 }
3117
3118 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003119 closeFolder(true);
3120 }
3121
3122 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003123 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003125 if (folder.isEditingName()) {
3126 folder.dismissEditingName();
3127 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003128 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003129 }
3130 }
3131
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003133 animate &= !Utilities.isPowerSaverOn(this);
3134
Adam Cohen4554ee12011-08-03 16:13:21 -07003135 folder.getInfo().opened = false;
3136
3137 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3138 if (parent != null) {
3139 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003140 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003141 if (fi != null) {
3142 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3143 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003145 if (animate) {
3146 folder.animateClosed();
3147 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003148 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003149 }
Winson Chung83ca4802013-04-12 15:10:52 -07003150
Sunny Goyal935fca12015-10-13 10:19:01 -07003151 // Notify the accessibility manager that this folder "window" has disappeared and no
3152 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003153 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154 }
3155
Tony Wickham49c8d292016-07-01 11:44:34 -07003156 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003157 closeShortcutsContainer(true);
3158 }
3159
3160 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003161 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3162 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003163 if (animate) {
3164 deepShortcutsContainer.animateClose();
3165 } else {
3166 deepShortcutsContainer.close();
3167 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003168 }
3169 }
3170
3171 /**
3172 * @return The open shortcuts container, or null if there is none
3173 */
3174 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003175 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3176 // and will be one of the last views.
3177 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3178 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003179 if (child instanceof DeepShortcutsContainer
3180 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003181 return (DeepShortcutsContainer) child;
3182 }
3183 }
3184 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003185 }
3186
Tony Wickhamdadb3042016-02-24 11:07:00 -08003187 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003189 if (!isDraggingEnabled()) return false;
3190 if (isWorkspaceLocked()) return false;
3191 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003193 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3194 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003195 onLongClickAllAppsButton(v);
3196 return true;
3197 }
3198
Adam Cohen1697b792013-09-17 19:08:21 -07003199 if (v instanceof Workspace) {
3200 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003201 if (!mWorkspace.isTouchActive()) {
3202 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003203 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3204 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3205 return true;
3206 } else {
3207 return false;
3208 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003209 } else {
3210 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003211 }
Adam Cohen1697b792013-09-17 19:08:21 -07003212 }
3213
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003214 CellLayout.CellInfo longClickCellInfo = null;
3215 View itemUnderLongClick = null;
3216 if (v.getTag() instanceof ItemInfo) {
3217 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003218 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003219 itemUnderLongClick = longClickCellInfo.cell;
3220 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 }
3222
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 // The hotseat touch handling does not go through Workspace, and we always allow long press
3224 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003225 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003226 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003227 // User long pressed on empty space
3228 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3229 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003230 if (mWorkspace.isInOverviewMode()) {
3231 mWorkspace.startReordering(v);
3232 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003233 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003234 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003237 final boolean isAllAppsButton =
3238 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3239 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3240 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003241 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003243 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 }
3245 }
3246 }
3247 return true;
3248 }
3249
Winson Chung3d503fb2011-07-13 17:25:49 -07003250 boolean isHotseatLayout(View layout) {
3251 return mHotseat != null && layout != null &&
3252 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3253 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003254
Winson Chung3d503fb2011-07-13 17:25:49 -07003255 /**
3256 * Returns the CellLayout of the specified container at the specified screen.
3257 */
Sunny Goyal92820592015-03-02 11:31:35 -08003258 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3260 if (mHotseat != null) {
3261 return mHotseat.getLayout();
3262 } else {
3263 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003264 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003265 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003266 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003267 }
3268 }
3269
Winson Chungb745afb2015-03-02 11:51:23 -08003270 /**
3271 * For overridden classes.
3272 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003273 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003274 return isAppsViewVisible();
3275 }
3276
3277 public boolean isAppsViewVisible() {
3278 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3279 }
3280
3281 public boolean isWidgetsViewVisible() {
3282 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003283 }
3284
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003285 private void setWorkspaceBackground(int background) {
3286 switch (background) {
3287 case WORKSPACE_BACKGROUND_TRANSPARENT:
3288 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3289 break;
3290 case WORKSPACE_BACKGROUND_BLACK:
3291 getWindow().setBackgroundDrawable(null);
3292 break;
3293 default:
3294 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3295 }
Craig Mautner360310b2012-10-26 15:13:08 -07003296 }
3297
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003298 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003299 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3300 int curflags = getWindow().getAttributes().flags
3301 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3302 if (wpflags != curflags) {
3303 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3304 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003305 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003306 }
3307
Michael Jurkae326f182011-11-21 14:05:46 -08003308 @Override
3309 public void onTrimMemory(int level) {
3310 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003311 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3312 // The widget preview db can result in holding onto over
3313 // 3MB of memory for caching which isn't necessary.
3314 SQLiteDatabase.releaseMemory();
3315
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003316 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003318 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003319 if (mLauncherCallbacks != null) {
3320 mLauncherCallbacks.onTrimMemory(level);
3321 }
Michael Jurkae326f182011-11-21 14:05:46 -08003322 }
3323
Winson5c6bdbb2015-09-03 11:36:19 -07003324 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003325 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003326 }
3327
Winson5c6bdbb2015-09-03 11:36:19 -07003328 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003329 boolean changed = mState != State.WORKSPACE ||
3330 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003331 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003332 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003333 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003334 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003335
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 // Set focus to the AppsCustomize button
3337 if (mAllAppsButton != null) {
3338 mAllAppsButton.requestFocus();
3339 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003340 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003341
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003342 // Change the state *after* we've called all the transition code
3343 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003344
Winson Chung5fb63472011-02-02 17:03:37 -08003345 // Resume the auto-advance of widgets
3346 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003347 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003348
Winson Chung0f785722015-04-08 10:27:49 -07003349 if (changed) {
3350 // Send an accessibility event to announce the context change
3351 getWindow().getDecorView()
3352 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003353 }
Winson5c6bdbb2015-09-03 11:36:19 -07003354 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003355 }
3356
Winsone9f27272015-10-13 10:47:51 -07003357 /**
3358 * Shows the overview button.
3359 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003360 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003361 showOverviewMode(animated, false);
3362 }
3363
3364 /**
3365 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3366 * onto one of the overview panel buttons.
3367 */
3368 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3369 Runnable postAnimRunnable = null;
3370 if (requestButtonFocus) {
3371 postAnimRunnable = new Runnable() {
3372 @Override
3373 public void run() {
3374 // Hitting the menu button when in touch mode does not trigger touch mode to
3375 // be disabled, so if requested, force focus on one of the overview panel
3376 // buttons.
3377 mOverviewPanel.requestFocusFromTouch();
3378 }
3379 };
3380 }
Adam Cohened307df2013-10-02 09:37:31 -07003381 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003382 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003383 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003384 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003385 }
3386
Winson Chungb745afb2015-03-02 11:51:23 -08003387 /**
3388 * Shows the apps view.
3389 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003390 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003391 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003392 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003393 if (updatePredictedApps) {
3394 tryAndUpdatePredictedApps();
3395 }
Winson Chung76648c52015-07-10 14:33:23 -07003396 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003397 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003398
Winson Chungb745afb2015-03-02 11:51:23 -08003399 /**
3400 * Shows the widgets view.
3401 */
3402 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003403 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003404 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003406 }
Winson Chung76648c52015-07-10 14:33:23 -07003407 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003408
3409 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003410 @Override
3411 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003413 }
3414 });
Winson Chungb745afb2015-03-02 11:51:23 -08003415 }
3416
3417 /**
3418 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003419 *
3420 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003421 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003422 // TODO: calling method should use the return value so that when {@code false} is returned
3423 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003424 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003425 if (!(mState == State.WORKSPACE ||
3426 mState == State.APPS_SPRING_LOADED ||
3427 mState == State.WIDGETS_SPRING_LOADED ||
3428 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003429 return false;
3430 }
3431 if (toState != State.APPS && toState != State.WIDGETS) {
3432 return false;
3433 }
Winson Chungb745afb2015-03-02 11:51:23 -08003434
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003435 // This is a safe and supported transition to bypass spring_loaded mode.
3436 if (mExitSpringLoadedModeRunnable != null) {
3437 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3438 mExitSpringLoadedModeRunnable = null;
3439 }
3440
Winson Chungb745afb2015-03-02 11:51:23 -08003441 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003442 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3443 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003444 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003445 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003446 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003449 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003450
3451 // Pause the auto-advance of widgets until we are out of AllApps
3452 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003453 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003455 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456
3457 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003458 getWindow().getDecorView()
3459 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003460 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003461 }
3462
Winson Chungcd99cd32015-04-29 11:03:24 -07003463 /**
3464 * Updates the workspace and interaction state on state change, and return the animation to this
3465 * new state.
3466 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003467 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003468 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003469 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003470 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003471 updateInteraction(fromState, toState);
3472 return anim;
3473 }
3474
Hyunyoung Song3f471442015-04-08 19:01:34 -07003475 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003476 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003477 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003478 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003479 }
Winson Chungb745afb2015-03-02 11:51:23 -08003480
Winson Chung006ee262015-08-03 14:40:11 -07003481 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003482 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003483 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003484
3485 if (isAppsViewVisible()) {
3486 mState = State.APPS_SPRING_LOADED;
3487 } else if (isWidgetsViewVisible()) {
3488 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003489 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003490 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003491 } else {
3492 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003493 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003494 }
Adam Cohen7777d962011-08-18 18:58:38 -07003495
Hyunyoung Song3f471442015-04-08 19:01:34 -07003496 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003497 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003498 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003499
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003500 if (mExitSpringLoadedModeRunnable != null) {
3501 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3502 }
3503 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003504 @Override
3505 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003506 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003507 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3508 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003509 // Before we show workspace, hide all apps again because
3510 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3511 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003512 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003513 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003514 } else {
3515 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003516 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003517 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003518 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003519 };
3520 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003521 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003522
Tony Wickhame0c33232016-02-08 11:37:04 -08003523 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003524 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3525 || mState == State.WIDGETS_SPRING_LOADED;
3526 }
3527
Michael Jurkad3ef3062010-11-23 16:23:58 -08003528 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003529 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003530 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003531 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003532 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003533 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003534 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3535 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003536 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003537 }
3538
Winson Chung4ac30062015-05-08 17:34:17 -07003539 /**
3540 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3541 * resumed.
3542 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003543 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003544 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003545 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003546 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003547 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003548 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003549 }
3550 }
3551 }
3552
Michael Jurkab3e22d92011-10-31 15:58:33 -07003553 void lockAllApps() {
3554 // TODO
3555 }
3556
3557 void unlockAllApps() {
3558 // TODO
3559 }
3560
Winsonf768d932015-09-25 16:12:35 -07003561 public boolean launcherCallbacksProvidesSearch() {
3562 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3563 }
3564
Michael Jurkad7c28052012-04-27 15:43:36 -07003565 @Override
3566 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003567 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003568 final List<CharSequence> text = event.getText();
3569 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003570 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003571 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003572 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003573 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003574 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003575 } else if (mWorkspace != null) {
3576 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003577 } else {
3578 text.add(getString(R.string.all_apps_home_button_label));
3579 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003580 return result;
3581 }
3582
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003583 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003584 * If the activity is currently paused, signal that we need to run the passed Runnable
3585 * in onResume.
3586 *
3587 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003588 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3589 * wrong when we're not running, and if the activity comes back to what the configuration was
3590 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 *
3592 * Implementation of the method from LauncherModel.Callbacks.
3593 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003594 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003596 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003597 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003598 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003599 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003600 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003601 }
3602 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003603 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 return true;
3605 } else {
3606 return false;
3607 }
3608 }
3609
Michael Jurkac402cd92013-05-20 15:49:32 +02003610 private boolean waitUntilResume(Runnable run) {
3611 return waitUntilResume(run, false);
3612 }
3613
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003615 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003616 }
3617
Michael Jurka7607c2f2013-04-03 14:33:19 -07003618 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003619 * If the activity is currently paused, signal that we need to re-run the loader
3620 * in onResume.
3621 *
3622 * This needs to be called from incoming places where resources might have been loaded
3623 * while we are paused. That is becaues the Configuration might be wrong
3624 * when we're not running, and if it comes back to what it was when we
3625 * were paused, we are not restarted.
3626 *
3627 * Implementation of the method from LauncherModel.Callbacks.
3628 *
3629 * @return true if we are currently paused. The caller might be able to
3630 * skip some work in that case since we will come back again.
3631 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003632 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003633 public boolean setLoadOnResume() {
3634 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003635 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003636 mOnResumeNeedsLoad = true;
3637 return true;
3638 } else {
3639 return false;
3640 }
3641 }
3642
3643 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003646 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003648 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003649 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003650 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003651 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003654
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003656 * Clear any pending bind callbacks. This is called when is loader is planning to
3657 * perform a full rebind from scratch.
3658 */
3659 @Override
3660 public void clearPendingBinds() {
3661 mBindOnResumeCallbacks.clear();
3662 if (mPendingExecutor != null) {
3663 mPendingExecutor.markCompleted();
3664 mPendingExecutor = null;
3665 }
3666 }
3667
3668 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 * Refreshes the shortcuts shown on the workspace.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 */
3673 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003674 if (LauncherAppState.PROFILE_STARTUP) {
3675 Trace.beginSection("Starting page bind");
3676 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003677 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003678
Winson Chungd64d1762013-08-20 14:37:16 -07003679 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003680 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003681 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003682
Michael Jurka05bf644e2011-11-30 20:28:53 -08003683 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003684 if (mHotseat != null) {
3685 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003687 if (LauncherAppState.PROFILE_STARTUP) {
3688 Trace.endSection();
3689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690 }
3691
Adam Cohendcd297f2013-06-18 13:13:40 -07003692 @Override
3693 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003694 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003695 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3696 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003697 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3698 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3699 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003700 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3701 // If there are no screens, we need to have an empty screen
3702 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003703 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003704 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003705
3706 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003707 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003708 if (hasCustomContentToLeft()) {
3709 mWorkspace.createCustomContentContainer();
3710 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003711 }
Winsonc7d2e832016-07-28 12:24:55 -07003712
3713 // After we have added all the screens, if the wallpaper was locked to the default state,
3714 // then notify to indicate that it can be released and a proper wallpaper offset can be
3715 // computed before the next layout
3716 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003717 }
3718
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003719 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003720 int count = orderedScreenIds.size();
3721 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003722 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003723 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003724 // No need to bind the first screen, as its always bound.
3725 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3726 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003727 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 }
3729
Winson Chungd64d1762013-08-20 14:37:16 -07003730 public void bindAppsAdded(final ArrayList<Long> newScreens,
3731 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003732 final ArrayList<ItemInfo> addAnimated,
3733 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003736 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003737 }
3738 };
3739 if (waitUntilResume(r)) {
3740 return;
3741 }
3742
Winson Chungd64d1762013-08-20 14:37:16 -07003743 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003744 if (newScreens != null) {
3745 bindAddScreens(newScreens);
3746 }
Winson Chungd64d1762013-08-20 14:37:16 -07003747
3748 // We add the items without animation on non-visible pages, and with
3749 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003750 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003751 bindItems(addNotAnimated, 0,
3752 addNotAnimated.size(), false);
3753 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003754 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003755 bindItems(addAnimated, 0,
3756 addAnimated.size(), true);
3757 }
Winson Chungc58497e2013-09-03 17:48:37 -07003758
Winson Chung87412982013-10-03 18:34:14 -07003759 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003760 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003761
Winson Chungb745afb2015-03-02 11:51:23 -08003762 if (addedApps != null && mAppsView != null) {
3763 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003764 }
Winson Chungd64d1762013-08-20 14:37:16 -07003765 }
3766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003767 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 * Bind the items start-end from the list.
3769 *
3770 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003771 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003772 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003773 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3774 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003775 Runnable r = new Runnable() {
3776 public void run() {
3777 bindItems(shortcuts, start, end, forceAnimateIcons);
3778 }
3779 };
3780 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003781 return;
3782 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003783
Winson Chungf0c6ae02012-03-21 16:10:31 -07003784 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003785 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3786 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003787 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003788 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003789 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003790 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003792
3793 // Short circuit if we are loading dock items for a configuration which has no dock
3794 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3795 mHotseat == null) {
3796 continue;
3797 }
3798
Sunny Goyal639e9062015-08-19 19:17:06 -07003799 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 switch (item.itemType) {
3801 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3802 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003803 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003804 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003805 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003807 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003808 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003809 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003810 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003812 default:
3813 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003815
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003816 /*
3817 * Remove colliding items.
3818 */
3819 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3820 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3821 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3822 View v = cl.getChildAt(item.cellX, item.cellY);
3823 Object tag = v.getTag();
3824 String desc = "Collision while binding workspace item: " + item
3825 + ". Collides with " + tag;
3826 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3827 throw (new RuntimeException(desc));
3828 } else {
3829 Log.d(TAG, desc);
3830 LauncherModel.deleteItemFromDatabase(this, item);
3831 continue;
3832 }
3833 }
3834 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003835 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3836 item.cellY, 1, 1);
3837 if (animateIcons) {
3838 // Animate all the applications up now
3839 view.setAlpha(0f);
3840 view.setScaleX(0f);
3841 view.setScaleY(0f);
3842 bounceAnims.add(createNewAppBounceAnimation(view, i));
3843 newShortcutsScreenId = item.screenId;
3844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003845 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003846
Winson Chung997a9232013-07-24 15:33:46 -07003847 if (animateIcons) {
3848 // Animate to the correct page
3849 if (newShortcutsScreenId > -1) {
3850 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003851 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003852 final Runnable startBounceAnimRunnable = new Runnable() {
3853 public void run() {
3854 anim.playTogether(bounceAnims);
3855 anim.start();
3856 }
3857 };
Winson Chung997a9232013-07-24 15:33:46 -07003858 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003859 // We post the animation slightly delayed to prevent slowdowns
3860 // when we are loading right after we return to launcher.
3861 mWorkspace.postDelayed(new Runnable() {
3862 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003863 if (mWorkspace != null) {
3864 mWorkspace.snapToPage(newScreenIndex);
3865 mWorkspace.postDelayed(startBounceAnimRunnable,
3866 NEW_APPS_ANIMATION_DELAY);
3867 }
Winson Chung94d67682013-09-25 16:29:40 -07003868 }
3869 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003870 } else {
3871 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003872 }
3873 }
Winson Chung64359a52013-07-08 17:17:08 -07003874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003876 }
3877
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003878 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3879 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3880 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003881 view.updateAppWidget(null);
3882 view.setOnClickListener(this);
3883 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003884 mWorkspace.requestLayout();
3885 }
3886
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 /**
3888 * Add the views for a widget to the workspace.
3889 *
3890 * Implementation of the method from LauncherModel.Callbacks.
3891 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003892 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 Runnable r = new Runnable() {
3894 public void run() {
3895 bindAppWidget(item);
3896 }
3897 };
3898 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return;
3900 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003901
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 if (mIsSafeModeEnabled) {
3903 bindSafeModeWidget(item);
3904 return;
3905 }
3906
Daniel Sandler843e8602010-06-07 14:59:01 -04003907 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "bindAppWidget: " + item);
3910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003912 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003913
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003914 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3915 // If the provider is not ready, bind as a pending widget.
3916 appWidgetInfo = null;
3917 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3918 // The widget id is not valid. Try to find the widget based on the provider info.
3919 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3920 } else {
3921 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3922 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003923
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003924 // If the provider is ready, but the width is not yet restored, try to restore it.
3925 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3926 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003927 if (appWidgetInfo == null) {
3928 if (DEBUG_WIDGETS) {
3929 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3930 + " belongs to component " + item.providerName
3931 + ", as the povider is null");
3932 }
3933 LauncherModel.deleteItemFromDatabase(this, item);
3934 return;
3935 }
Sunny Goyalff572272014-07-23 13:58:07 -07003936
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003937 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003938 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003939 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3940 // Id has not been allocated yet. Allocate a new id.
3941 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3942 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003943
Sunny Goyald478c832016-04-01 12:04:16 -07003944 // Also try to bind the widget. If the bind fails, the user will be shown
3945 // a click to setup UI, which will ask for the bind permission.
3946 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3947 pendingInfo.spanX = item.spanX;
3948 pendingInfo.spanY = item.spanY;
3949 pendingInfo.minSpanX = item.minSpanX;
3950 pendingInfo.minSpanY = item.minSpanY;
3951 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3952 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3953 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003954
Sunny Goyald478c832016-04-01 12:04:16 -07003955 // Bind succeeded
3956 if (success) {
3957 // If the widget has a configure activity, it is still needs to set it up,
3958 // otherwise the widget is ready to go.
3959 item.restoreStatus = (appWidgetInfo.configure == null)
3960 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3961 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003962 }
Sunny Goyald478c832016-04-01 12:04:16 -07003963
3964 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003965 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003966 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003967 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003968 // The widget was marked as UI not ready, but there is no configure activity to
3969 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003970 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3971 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003972 }
Sunny Goyalff572272014-07-23 13:58:07 -07003973 }
3974
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003975 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003976 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003977 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3978 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003979 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003980
Sunny Goyal56c73602015-09-25 12:55:01 -07003981 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003982 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003983 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003984 deleteWidgetInfo(item);
3985 return;
3986 }
3987
Sunny Goyal233ee962015-08-03 13:05:01 -07003988 item.minSpanX = appWidgetInfo.minSpanX;
3989 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003990 addAppWidgetToWorkspace(
3991 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3992 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003994 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003995 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003996 view.updateAppWidget(null);
3997 view.setOnClickListener(this);
3998 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003999 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004000 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001
Daniel Sandler843e8602010-06-07 14:59:01 -04004002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4004 + (SystemClock.uptimeMillis()-start) + "ms");
4005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006 }
4007
Sunny Goyalff572272014-07-23 13:58:07 -07004008 /**
4009 * Restores a pending widget.
4010 *
4011 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004012 */
Sunny Goyald478c832016-04-01 12:04:16 -07004013 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07004014 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4015 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4016 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07004017 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07004018 }
4019
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004020 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07004021 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07004022
4023 mWorkspace.reinflateWidgetsIfNecessary();
4024 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004025 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004026 }
4027
Adam Cohen1462de32012-07-24 22:34:36 -07004028 public void onPageBoundSynchronously(int page) {
4029 mSynchronouslyBoundPages.add(page);
4030 }
4031
Sunny Goyal527c7d32015-08-28 15:19:36 -07004032 @Override
4033 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4034 if (mPendingExecutor != null) {
4035 mPendingExecutor.markCompleted();
4036 }
4037 mPendingExecutor = executor;
4038 executor.attachTo(this);
4039 }
4040
4041 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4042 if (mPendingExecutor == executor) {
4043 mPendingExecutor = null;
4044 }
4045 }
4046
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004047 @Override
4048 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4049 Runnable r = new Runnable() {
4050 public void run() {
4051 finishFirstPageBind(executor);
4052 }
4053 };
4054 if (waitUntilResume(r)) {
4055 return;
4056 }
4057
4058 Runnable onComplete = new Runnable() {
4059 @Override
4060 public void run() {
4061 if (executor != null) {
4062 executor.onLoadAnimationCompleted();
4063 }
4064 }
4065 };
4066 if (mDragLayer.getAlpha() < 1) {
4067 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4068 } else {
4069 onComplete.run();
4070 }
4071 }
4072
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 /**
4074 * Callback saying that there aren't any more items to bind.
4075 *
4076 * Implementation of the method from LauncherModel.Callbacks.
4077 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004078 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004079 Runnable r = new Runnable() {
4080 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004081 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004082 }
4083 };
4084 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 return;
4086 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004087 if (LauncherAppState.PROFILE_STARTUP) {
4088 Trace.beginSection("Page bind completed");
4089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 if (mSavedState != null) {
4091 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004092 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004094
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 mSavedState = null;
4096 }
4097
Adam Cohen1462de32012-07-24 22:34:36 -07004098 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004100 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004101
4102 // If we received the result of any pending adds while the loader was running (e.g. the
4103 // widget configuration forced an orientation change), process them now.
4104 if (sPendingAddItem != null) {
4105 final long screenId = completeAdd(sPendingAddItem);
4106
4107 // TODO: this moves the user to the page where the pending item was added. Ideally,
4108 // the screen would be guaranteed to exist after bind, and the page would be set through
4109 // the workspace restore process.
4110 mWorkspace.post(new Runnable() {
4111 @Override
4112 public void run() {
4113 mWorkspace.snapToScreenId(screenId);
4114 }
4115 });
4116 sPendingAddItem = null;
4117 }
4118
Sunny Goyal756adbc2015-04-16 15:20:51 -07004119 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004120
4121 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004122 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004123 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004124 if (LauncherAppState.PROFILE_STARTUP) {
4125 Trace.endSection();
4126 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004127 }
4128
Winson Chunga2413752012-04-03 14:22:34 -07004129 private boolean canRunNewAppsAnimation() {
4130 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004131 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004132 }
4133
Winson Chungc9168342013-06-26 14:54:55 -07004134 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004135 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004136 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4137 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004138 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004139 return bounceAnim;
4140 }
4141
Adam Cohen27772732013-11-11 14:00:56 +00004142 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004143 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004144 }
4145
Tony Wickham55616cd2015-09-23 14:55:17 -07004146 public int getSearchBarHeight() {
4147 if (mLauncherCallbacks != null) {
4148 return mLauncherCallbacks.getSearchBarHeight();
4149 }
4150 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4151 }
4152
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004153 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004154 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4155 * multiple calls to bind the same list.)
4156 */
4157 @Thunk ArrayList<AppInfo> mTmpAppsList;
4158 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4159 public void run() {
4160 bindAllApplications(mTmpAppsList);
4161 mTmpAppsList = null;
4162 }
4163 };
4164
4165 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004166 * Add the icons for all apps.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004170 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004171 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4172 mTmpAppsList = apps;
4173 return;
4174 }
4175
Winson Chungb745afb2015-03-02 11:51:23 -08004176 if (mAppsView != null) {
4177 mAppsView.setApps(apps);
4178 }
Adam Cohen9211d422014-10-07 18:14:53 -07004179 if (mLauncherCallbacks != null) {
4180 mLauncherCallbacks.bindAllApplications(apps);
4181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 }
4183
4184 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004185 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4186 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4187 */
4188 @Override
4189 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4190 mDeepShortcutMap = deepShortcutMapCopy;
4191 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4192 }
4193
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004194 public List<String> getShortcutIdsForItem(ItemInfo info) {
4195 if (!DeepShortcutManager.supportsShortcuts(info)) {
4196 return Collections.EMPTY_LIST;
4197 }
4198 ComponentName component = info.getTargetComponent();
4199 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4200 return ids == null ? Collections.EMPTY_LIST : ids;
4201 }
4202
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004203 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004204 * A package was updated.
4205 *
4206 * Implementation of the method from LauncherModel.Callbacks.
4207 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004208 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004209 Runnable r = new Runnable() {
4210 public void run() {
4211 bindAppsUpdated(apps);
4212 }
4213 };
4214 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004215 return;
4216 }
4217
Winson Chungb745afb2015-03-02 11:51:23 -08004218 if (mAppsView != null) {
4219 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004221 }
4222
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 @Override
4224 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4225 Runnable r = new Runnable() {
4226 public void run() {
4227 bindWidgetsRestored(widgets);
4228 }
4229 };
4230 if (waitUntilResume(r)) {
4231 return;
4232 }
4233 mWorkspace.widgetsRestored(widgets);
4234 }
4235
Joe Onorato9c1289c2009-08-17 11:03:03 -04004236 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004237 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004238 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004239 *
4240 * @param updated list of shortcuts which have changed.
4241 * @param removed list of shortcuts which were deleted in the background. This can happen when
4242 * an app gets removed from the system or some of its components are no longer
4243 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004244 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004245 @Override
4246 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4247 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004248 Runnable r = new Runnable() {
4249 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004250 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004251 }
4252 };
4253 if (waitUntilResume(r)) {
4254 return;
4255 }
4256
Sunny Goyal4390ace2014-10-13 11:33:11 -07004257 if (!updated.isEmpty()) {
4258 mWorkspace.updateShortcuts(updated);
4259 }
4260
4261 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004262 HashSet<ComponentName> removedComponents = new HashSet<>();
4263 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4264
Sunny Goyal4390ace2014-10-13 11:33:11 -07004265 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004266 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
4267 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
4268 } else {
4269 removedComponents.add(si.getTargetComponent());
4270 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004271 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004272
4273 if (!removedComponents.isEmpty()) {
4274 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4275 mWorkspace.removeItemsByMatcher(matcher);
4276 mDragController.onAppsRemoved(matcher);
4277 }
4278
4279 if (!removedDeepShortcuts.isEmpty()) {
4280 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4281 mWorkspace.removeItemsByMatcher(matcher);
4282 mDragController.onAppsRemoved(matcher);
4283 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004284 }
4285 }
4286
4287 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004288 * Update the state of a package, typically related to install state.
4289 *
4290 * Implementation of the method from LauncherModel.Callbacks.
4291 */
Sunny Goyale755d462014-07-22 13:48:29 -07004292 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004293 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4294 Runnable r = new Runnable() {
4295 public void run() {
4296 bindRestoreItemsChange(updates);
4297 }
4298 };
4299 if (waitUntilResume(r)) {
4300 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004301 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004302
Sunny Goyal756adbc2015-04-16 15:20:51 -07004303 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004304 }
4305
4306 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004307 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004308 * in addition to specific applications to remove, the reason being that
4309 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004310 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004311 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004312 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004313 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004314 public void bindWorkspaceComponentsRemoved(
4315 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4316 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004317 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004318 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004319 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004320 }
Winson Chungd64d1762013-08-20 14:37:16 -07004321 };
4322 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004323 return;
4324 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004325 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004326 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4327 mWorkspace.removeItemsByMatcher(matcher);
4328 mDragController.onAppsRemoved(matcher);
4329
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004330 }
4331 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004332 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4333 mWorkspace.removeItemsByMatcher(matcher);
4334 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004335 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004336 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004337
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004338 @Override
4339 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4340 Runnable r = new Runnable() {
4341 public void run() {
4342 bindAppInfosRemoved(appInfos);
4343 }
4344 };
4345 if (waitUntilResume(r)) {
4346 return;
Joe Onorato36115782010-06-17 13:28:48 -04004347 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004348
Winson Chungdf95eb12013-10-16 14:57:07 -07004349 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004350 if (mAppsView != null) {
4351 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353 }
Joe Onoratobe386092009-11-17 17:32:16 -08004354
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004355 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004356 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004357 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004358 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004359 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004360
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004361 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004362 public void bindWidgetsModel(WidgetsModel model) {
4363 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004364 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004365 return;
4366 }
4367
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004368 if (mWidgetsView != null && model != null) {
4369 mWidgetsView.addWidgets(model);
4370 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004371 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004372 }
4373
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004374 @Override
4375 public void notifyWidgetProvidersChanged() {
4376 if (mWorkspace.getState().shouldUpdateWidget) {
4377 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4378 }
4379 }
4380
Winson Chung400438b2011-01-16 17:53:48 -08004381 private int mapConfigurationOriActivityInfoOri(int configOri) {
4382 final Display d = getWindowManager().getDefaultDisplay();
4383 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4384 switch (d.getRotation()) {
4385 case Surface.ROTATION_0:
4386 case Surface.ROTATION_180:
4387 // We are currently in the same basic orientation as the natural orientation
4388 naturalOri = configOri;
4389 break;
4390 case Surface.ROTATION_90:
4391 case Surface.ROTATION_270:
4392 // We are currently in the other basic orientation to the natural orientation
4393 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4394 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4395 break;
4396 }
4397
4398 int[] oriMap = {
4399 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4400 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4401 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4402 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4403 };
4404 // Since the map starts at portrait, we need to offset if this device's natural orientation
4405 // is landscape.
4406 int indexOffset = 0;
4407 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4408 indexOffset = 1;
4409 }
4410 return oriMap[(d.getRotation() + indexOffset) % 4];
4411 }
Adam Cohen446e9402011-09-15 18:21:21 -07004412
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004413 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004414 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004415 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004416 if (Utilities.ATLEAST_JB_MR2) {
4417 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4418 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004419 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4420 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004421 }
Winson Chung4b919f82012-05-01 10:44:08 -07004422 }
4423 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004424
Winson Chung4b919f82012-05-01 10:44:08 -07004425 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004426 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004427 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004428 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004429 } else {
4430 mHandler.postDelayed(new Runnable() {
4431 public void run() {
4432 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4433 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004434 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004435 }
Winson Chung4b919f82012-05-01 10:44:08 -07004436 }
Winson Chung400438b2011-01-16 17:53:48 -08004437 }
4438
Adam Cohenea90f832014-05-21 15:03:34 -07004439 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004440 * To be overridden by subclasses to indicate that there is an activity to launch
4441 * before showing the standard launcher experience.
4442 */
4443 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004444 if (mLauncherCallbacks != null) {
4445 return mLauncherCallbacks.hasFirstRunActivity();
4446 }
Adam Cohen432609a2014-03-13 17:03:22 -07004447 return false;
4448 }
4449
4450 /**
4451 * To be overridden by subclasses to launch any first run activity
4452 */
4453 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.getFirstRunActivity();
4456 }
Adam Cohen432609a2014-03-13 17:03:22 -07004457 return null;
4458 }
4459
Winson Chunga6945242014-01-08 14:04:34 -08004460 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004461 return !ActivityManager.isRunningInTestHarness() &&
4462 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004463 }
4464
Adam Cohen432609a2014-03-13 17:03:22 -07004465 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004466 if (shouldRunFirstRunActivity() &&
4467 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004468 Intent firstRunIntent = getFirstRunActivity();
4469 if (firstRunIntent != null) {
4470 startActivity(firstRunIntent);
4471 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004472 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004473 }
4474 }
Adam Cohen432609a2014-03-13 17:03:22 -07004475 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004476 }
4477
4478 private void markFirstRunActivityShown() {
4479 SharedPreferences.Editor editor = mSharedPrefs.edit();
4480 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4481 editor.apply();
4482 }
4483
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004484 private void markAppsViewShown() {
4485 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4486 return;
4487 }
4488 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4489 }
4490
4491 private boolean shouldShowDiscoveryBounce() {
4492 if (mState != mState.WORKSPACE) {
4493 return false;
4494 }
4495 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4496 return true;
4497 }
4498 if (!mIsResumeFromActionScreenOff) {
4499 return false;
4500 }
4501 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4502 return false;
4503 }
4504 return true;
4505 }
4506
Adam Cohen432609a2014-03-13 17:03:22 -07004507 /**
4508 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4509 * screen that must be displayed and dismissed.
4510 */
4511 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004512 if (mLauncherCallbacks != null) {
4513 return mLauncherCallbacks.hasDismissableIntroScreen();
4514 }
Adam Cohen432609a2014-03-13 17:03:22 -07004515 return false;
4516 }
4517
4518 /**
4519 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4520 */
4521 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004522 if (mLauncherCallbacks != null) {
4523 return mLauncherCallbacks.getIntroScreen();
4524 }
Adam Cohen432609a2014-03-13 17:03:22 -07004525 return null;
4526 }
4527
4528 /**
4529 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4530 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4531 */
4532 private boolean shouldShowIntroScreen() {
4533 return hasDismissableIntroScreen() &&
4534 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4535 }
4536
4537 protected void showIntroScreen() {
4538 View introScreen = getIntroScreen();
4539 changeWallpaperVisiblity(false);
4540 if (introScreen != null) {
4541 mDragLayer.showOverlayView(introScreen);
4542 }
4543 }
4544
Winson Chung5ffd51d2015-06-25 11:36:50 -07004545 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004547 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004548 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004549 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004550 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004551 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4552 if (userHandle != null) {
4553 user = UserHandleCompat.fromUser(userHandle);
4554 }
4555 }
4556 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004557 }
4558
4559 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004560 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004561 if (user == null) {
4562 user = UserHandleCompat.myUserHandle();
4563 }
4564
4565 // Called from search suggestion, add the profile extra to the intent to ensure that we
4566 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004567 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004568 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004569 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 return null;
4571 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004572 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 }
4574
Winson Chung5ffd51d2015-06-25 11:36:50 -07004575 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004576 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4577 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004578 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004579 UserHandleCompat.myUserHandle());
4580 }
4581
Winson Chung5ffd51d2015-06-25 11:36:50 -07004582 protected void moveWorkspaceToDefaultScreen() {
4583 mWorkspace.moveToDefaultScreen(false);
4584 }
4585
Winson Chung80baf5a2010-08-09 16:03:15 -07004586 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004587 * Returns a FastBitmapDrawable with the icon, accurately sized.
4588 */
4589 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4590 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4591 d.setFilterBitmap(true);
4592 resizeIconDrawable(d);
4593 return d;
4594 }
4595
4596 /**
4597 * Resizes an icon drawable to the correct icon size.
4598 */
Winson5fbe0742015-09-30 15:33:00 -07004599 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004600 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004601 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004602 }
4603
4604 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004605 * Prints out out state for debugging.
4606 */
4607 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004608 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004609 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004610 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4611 Log.d(TAG, "mRestoring=" + mRestoring);
4612 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004613 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004614 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004615
Daniel Sandler325dc232013-06-05 22:57:57 -04004616 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004617 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004618
4619 @Override
4620 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4621 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004622 // Dump workspace
4623 writer.println(prefix + "Workspace Items");
4624 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4625 writer.println(prefix + " Homescreen " + i);
4626
4627 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4628 for (int j = 0; j < layout.getChildCount(); j++) {
4629 Object tag = layout.getChildAt(j).getTag();
4630 if (tag != null) {
4631 writer.println(prefix + " " + tag.toString());
4632 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004633 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004634 }
Sunny Goyala1365452015-10-01 15:46:24 -07004635
4636 writer.println(prefix + " Hotseat");
4637 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4638 for (int j = 0; j < layout.getChildCount(); j++) {
4639 Object tag = layout.getChildAt(j).getTag();
4640 if (tag != null) {
4641 writer.println(prefix + " " + tag.toString());
4642 }
4643 }
4644
Sunny Goyal713edfc2016-05-06 09:58:34 -07004645 try {
4646 FileLog.flushAll(writer);
4647 } catch (Exception e) {
4648 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004649 }
4650
Adam Cohen9211d422014-10-07 18:14:53 -07004651 if (mLauncherCallbacks != null) {
4652 mLauncherCallbacks.dump(prefix, fd, writer, args);
4653 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004654 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004655
Adam Cohen59400422014-03-05 18:07:04 -08004656 public static CustomAppWidget getCustomAppWidget(String name) {
4657 return sCustomAppWidgets.get(name);
4658 }
4659
4660 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4661 return sCustomAppWidgets;
4662 }
4663
Sunny Goyal29538332016-02-18 09:10:19 -08004664 public static List<View> getFolderContents(View icon) {
4665 if (icon instanceof FolderIcon) {
4666 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4667 } else {
4668 return Collections.EMPTY_LIST;
4669 }
4670 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004671
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004672 public static Launcher getLauncher(Context context) {
4673 if (context instanceof Launcher) {
4674 return (Launcher) context;
4675 }
4676 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4677 }
4678
Sunny Goyal745bad92016-05-02 10:54:12 -07004679 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4680
4681 @Override
4682 public void onSharedPreferenceChanged(
4683 SharedPreferences sharedPreferences, String key) {
4684 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4685 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4686 if (!waitUntilResume(this, true)) {
4687 run();
4688 }
4689 }
4690 }
4691
4692 @Override
4693 public void run() {
4694 setOrientation();
4695 }
4696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004697}