blob: 2a83299347c3d531d35c6822458922bf9652ff72 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080048import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070088import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800112import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700113import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700114import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700117import com.android.launcher3.logging.FileLog;
Sunny Goyal322d5562015-06-25 19:35:49 -0700118import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700120import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800133import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700139 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
140 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700141 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700149 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Sunny Goyal594d76d2014-11-06 10:12:54 -0800197 private static final String QSB_WIDGET_ID = "qsb_widget_id";
198 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
199
Winson Chunga6945242014-01-08 14:04:34 -0800200 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700203 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
204 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700214 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800226 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
230 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Sunny Goyalffe83f12014-08-14 17:39:34 -0700232 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700235 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800236 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800237 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Winson Chungc51db6a2011-10-05 11:44:49 -0700247 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700248 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700249
250 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700251 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Sunny Goyal594d76d2014-11-06 10:12:54 -0800257 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
261 // scroll issues (because the workspace may not have been measured yet) and extra work.
262 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
263 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
265 private SpannableStringBuilder mDefaultKeySsb = null;
266
Adam Cohen091440a2015-03-18 14:16:05 -0700267 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800269 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private boolean mRestoring;
271 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700272 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700275 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700276 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700280 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700283 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Sunny Goyal639e9062015-08-19 19:17:06 -0700286 private LauncherClings mClings;
287
Adam Cohen61f560d2013-09-30 15:58:20 -0700288 private View.OnTouchListener mHapticFeedbackTouchListener;
289
Adam Cohended9f8d2010-11-03 13:25:16 -0700290 // Related to the auto-advancing of widgets
291 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700292 private static final int ADVANCE_INTERVAL = 20000;
293 private static final int ADVANCE_STAGGER = 250;
294
295 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 private long mAutoAdvanceSentTime;
297 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700299
Winson Chung400438b2011-01-16 17:53:48 -0800300 // Determines how long to wait after a rotation before restoring the screen orientation to
301 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800303
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700307 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
312
Winson Chungf0c6ae02012-03-21 16:10:31 -0700313 // Holds the page that we need to animate to, and the icon views that we need to animate up
314 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700320 private DeviceProfile mDeviceProfile;
321
Adam Cohen4b66bf32015-09-18 12:15:19 -0700322 private boolean mMoveToDefaultScreenFromNewIntent;
323
Winson Chung13eb5272015-05-11 16:30:13 -0700324 // This is set to the view that launched the activity that navigated the user away from
325 // launcher. Since there is no callback for when the activity has finished launching, enable
326 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 private BubbleTextView mWaitingForResume;
328
Adam Cohen59400422014-03-05 18:07:04 -0800329 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
330 new HashMap<String, CustomAppWidget>();
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700333 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
334 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800335 }
336 }
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Adam Cohendb364c32014-05-20 14:23:40 -0700346 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800349 int requestCode;
350 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700351 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 int cellX;
354 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700355 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 }
357
Hyunyoung Songaa953652016-04-19 18:30:24 -0700358 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800359
Adam Cohenf9c184a2016-01-15 16:47:43 -0800360 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700361 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400362
363 @Thunk void setOrientation() {
364 if (mRotationEnabled) {
365 unlockScreenOrientation(true);
366 } else {
367 setRequestedOrientation(
368 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700369 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700371
Sunny Goyal745bad92016-05-02 10:54:12 -0700372 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700376 if (DEBUG_STRICT_MODE) {
377 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378 .detectDiskReads()
379 .detectDiskWrites()
380 .detectNetwork() // or .detectAll() for all detectable problems
381 .penaltyLog()
382 .build());
383 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384 .detectLeakedSqlLiteObjects()
385 .detectLeakedClosableObjects()
386 .penaltyLog()
387 .penaltyDeath()
388 .build());
389 }
390
Adam Cohen9211d422014-10-07 18:14:53 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.preOnCreate();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400397 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 mDeviceProfile = getResources().getConfiguration().orientation
401 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700402 app.getInvariantDeviceProfile().landscapeProfile
403 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyalf7258242015-10-19 16:59:07 -0700405 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700406 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700409
Joe Onorato41a12d22009-10-31 18:30:00 -0400410 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700411 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700412
Sunny Goyalffe83f12014-08-14 17:39:34 -0700413 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700414
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700429
Tony Wickhameef44322015-10-20 13:24:36 -0700430 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
431 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700433 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700434 mExtractedColors = new ExtractedColors();
435 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436
Tony Wickhame2217252016-03-22 16:34:23 -0700437 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
438 .addAccessibilityStateChangeListener(this);
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 mSavedState = savedInstanceState;
443 restoreState(mSavedState);
444
445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700450 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700453 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700457 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800468 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700469 // In case we are on a device with locked rotation, we should look at preferences to check
470 // if the user has specifically allowed rotation.
471 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700472 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700473 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
474 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 }
476
477 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
478 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400479 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onCreate(savedInstanceState);
483 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700484
485 if (shouldShowIntroScreen()) {
486 showIntroScreen();
487 } else {
488 showFirstRunActivity();
489 showFirstRunClings();
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
492
Sunny Goyal7779d622015-06-11 16:18:39 -0700493 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700494 public void onExtractedColorsChanged() {
495 loadExtractedColorsAndColorItems();
496 }
497
498 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700499 // TODO: do this in pre-N as well, once the extraction part is complete.
500 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700501 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700502 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham827cef22016-03-17 15:39:39 -0700503 }
504 }
505
Adam Cohen9211d422014-10-07 18:14:53 -0700506 private LauncherCallbacks mLauncherCallbacks;
507
508 public void onPostCreate(Bundle savedInstanceState) {
509 super.onPostCreate(savedInstanceState);
510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onPostCreate(savedInstanceState);
512 }
513 }
514
Sunny Goyal32554d12015-12-03 15:31:25 -0800515 /**
516 * Call this after onCreate to set or clear overlay.
517 */
518 public void setLauncherOverlay(LauncherOverlay overlay) {
519 if (overlay != null) {
520 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
521 }
522 mWorkspace.setLauncherOverlay(overlay);
523 }
524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
526 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700527 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700528 private boolean mWorkspaceImportanceStored = false;
529 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800531 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700532 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
533
534 @Override
535 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 return;
538 }
539 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700540 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700541 if (mWorkspace != null) {
542 mWorkspaceImportanceForAccessibility =
543 mWorkspace.getImportantForAccessibility();
544 mWorkspace.setImportantForAccessibility(
545 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
546 mWorkspaceImportanceStored = true;
547 }
548 if (mHotseat != null) {
549 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
550 mHotseat.setImportantForAccessibility(
551 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
552 mHotseatImportanceStored = true;
553 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 }
555
556 @Override
557 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspaceImportanceStored && mWorkspace != null) {
559 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
560 }
561 if (mHotseatImportanceStored && mHotseat != null) {
562 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
563 }
564 mWorkspaceImportanceStored = false;
565 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700566 }
567 });
Adam Cohen9211d422014-10-07 18:14:53 -0700568 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700569 }
570
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700571 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700572 public void onLauncherProviderChange() {
573 if (mLauncherCallbacks != null) {
574 mLauncherCallbacks.onLauncherProviderChange();
575 }
576 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700577
Adam Cohen9211d422014-10-07 18:14:53 -0700578 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700579 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700580 if (mLauncherCallbacks != null) {
581 return mLauncherCallbacks.hasCustomContentToLeft();
582 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700583 return false;
584 }
585
Dave Hawkeya8881582013-09-17 15:55:33 -0600586 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500587 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600588 * {@link #addToCustomContentPage}. This will only be invoked if
589 * {@link #hasCustomContentToLeft()} is {@code true}.
590 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500591 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700592 if (mLauncherCallbacks != null) {
593 mLauncherCallbacks.populateCustomContentContainer();
594 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 }
596
597 /**
598 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
599 * ensure the custom content page is added or removed if necessary.
600 */
601 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600602 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 // Not bound yet, wait for bindScreens to be called.
604 return;
605 }
606
607 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
608 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 mWorkspace.createCustomContentContainer();
610 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
612 mWorkspace.removeCustomContentPage();
613 }
614 }
615
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800616 /**
617 * Logger object is a singleton and does not have to be coupled with the foreground activity.
618 * Since most user event logging is done on the UI, the object is retrieved from the
619 * callback for convenience.
620 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700621 private UserEventDispatcher createUserEventDispatcher() {
622 return new UserEventDispatcher() {
623 @Override
624 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
625 if (!DEBUG_LOGGING) {
626 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800627 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700628 Log.d("UserEvent", String.format(Locale.US,
629 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
630 LoggerUtils.getActionStr(ev.action),
631 LoggerUtils.getTargetStr(ev.srcTarget[0]),
632 LoggerUtils.getTargetStr(ev.srcTarget[1]),
633 ev.elapsedContainerMillis,
634 ev.elapsedSessionMillis));
635 }
636 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800637 }
638
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 public UserEventDispatcher getUserEventDispatcher() {
640 if (mLauncherCallbacks != null) {
641 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
642 if (dispatcher != null) {
643 return dispatcher;
644 }
645 }
646
647 if (mUserEventDispatcher == null) {
648 mUserEventDispatcher = createUserEventDispatcher();
649 }
650 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800651 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100652
Hyunyoung Song3f471442015-04-08 19:01:34 -0700653 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700654 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
655 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700656 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700657 }
658
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000659 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700660 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
661 // This cast is safe as long as the id < 0x00FFFFFF
662 // Since we jail all the dynamically generated views, there should be no clashes
663 // with any other views.
664 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000665 }
666
667 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700668 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
669 * a configuration step, this allows the proper animations to run after other transitions.
670 */
Adam Cohendb364c32014-05-20 14:23:40 -0700671 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700672 long screenId = args.screenId;
673 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
674 // When the screen id represents an actual screen (as opposed to a rank) we make sure
675 // that the drop page actually exists.
676 screenId = ensurePendingDropLayoutExists(args.screenId);
677 }
Adam Cohendb364c32014-05-20 14:23:40 -0700678
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700681 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700683 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700685 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700686 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700687 case REQUEST_RECONFIGURE_APPWIDGET:
688 completeRestoreAppWidget(args.appWidgetId);
689 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 }
Michael Jurka27614d22012-04-02 06:40:22 -0700691 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
692 // if you turned the screen off and then back while in All Apps, Launcher would not
693 // return to the workspace. Clearing mAddInfo.container here fixes this issue
694 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700695 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 }
697
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800698 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700700 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700701 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700702 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800703 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700704
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 Runnable exitSpringLoaded = new Runnable() {
706 @Override
707 public void run() {
708 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
709 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
710 }
711 };
712
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700714 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700715 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
717 if (resultCode == RESULT_CANCELED) {
718 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700719 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700720 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800722 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700724
725 // When the user has granted permission to bind widgets, we should check to see if
726 // we can inflate the default search bar widget.
727 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
729 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200730 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
731 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700732 // User could have free-scrolled between pages before picking a wallpaper; make sure
733 // we move to the closest one now.
734 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700735 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736 }
737 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700741 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700742
Adam Cohendb364c32014-05-20 14:23:40 -0700743 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 // We have special handling for widgets
745 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 final int appWidgetId;
747 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
748 : -1;
749 if (widgetId < 0) {
750 appWidgetId = pendingAddWidgetId;
751 } else {
752 appWidgetId = widgetId;
753 }
754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800756 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700757 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
758 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 result = RESULT_CANCELED;
760 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed(false, 0, null);
765 }
766 };
Adam Cohendb364c32014-05-20 14:23:40 -0700767 if (workspaceLocked) {
768 // No need to remove the empty screen if we're mid-binding, as the
769 // the bind will not add the empty screen.
770 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
771 } else {
772 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
773 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
774 }
Winson Chung5aaab772012-04-27 15:24:02 -0700775 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700777 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
778 // When the screen id represents an actual screen (as opposed to a rank)
779 // we make sure that the drop page actually exists.
780 mPendingAddInfo.screenId =
781 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
782 }
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
784
785 dropLayout.setDropPending(true);
786 final Runnable onComplete = new Runnable() {
787 @Override
788 public void run() {
789 completeTwoStageWidgetDrop(resultCode, appWidgetId);
790 dropLayout.setDropPending(false);
791 }
792 };
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 } else {
796 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
797 mPendingAddInfo);
798 sPendingAddItem = args;
799 }
Winson Chung5aaab772012-04-27 15:24:02 -0700800 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 return;
802 }
803
Sunny Goyalff572272014-07-23 13:58:07 -0700804 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
805 if (resultCode == RESULT_OK) {
806 // Update the widget view.
807 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
808 pendingAddWidgetId, mPendingAddInfo);
809 if (workspaceLocked) {
810 sPendingAddItem = args;
811 } else {
812 completeAdd(args);
813 }
814 }
815 // Leave the widget in the pending state if the user canceled the configure.
816 return;
817 }
818
Sunny Goyalaad90582015-07-09 14:22:50 -0700819 if (requestCode == REQUEST_CREATE_SHORTCUT) {
820 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
821 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
822 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
823 mPendingAddInfo);
824 if (isWorkspaceLocked()) {
825 sPendingAddItem = args;
826 } else {
827 completeAdd(args);
828 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
830 }
831 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800837
838 }
839
840 @Override
841 protected void onActivityResult(
842 final int requestCode, final int resultCode, final Intent data) {
843 handleActivityResult(requestCode, resultCode, data);
844 if (mLauncherCallbacks != null) {
845 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
846 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 }
848
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700850 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600851 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700852 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
853 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
854 View v = null;
855 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
856 if (layout != null) {
857 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
858 }
859 Intent intent = sPendingAddItem.intent;
860 sPendingAddItem = null;
861 if (grantResults.length > 0
862 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
863 startActivity(v, intent, null);
864 } else {
865 // TODO: Show a snack bar with link to settings
866 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
867 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
868 }
869 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
872 grantResults);
873 }
874 }
875
Adam Cohendb364c32014-05-20 14:23:40 -0700876 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
877 appWidgetId, ItemInfo info) {
878 PendingAddArguments args = new PendingAddArguments();
879 args.requestCode = requestCode;
880 args.intent = data;
881 args.container = info.container;
882 args.screenId = info.screenId;
883 args.cellX = info.cellX;
884 args.cellY = info.cellY;
885 args.appWidgetId = appWidgetId;
886 return args;
887 }
888
889 /**
890 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
891 *
892 * @param screenId the screen id to check
893 * @return the new screen, or screenId if it exists
894 */
895 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800896 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700897 if (dropLayout == null) {
898 // it's possible that the add screen was removed because it was
899 // empty and a re-bind occurred
900 mWorkspace.addExtraEmptyScreen();
901 return mWorkspace.commitExtraEmptyScreen();
902 } else {
903 return screenId;
904 }
905 }
906
Adam Cohen091440a2015-03-18 14:16:05 -0700907 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800908 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 Runnable onCompleteRunnable = null;
910 int animationType = 0;
911
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700912 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 if (resultCode == RESULT_OK) {
914 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
915 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 mPendingAddWidgetInfo);
917 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 onCompleteRunnable = new Runnable() {
919 @Override
920 public void run() {
921 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700923 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
924 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 }
926 };
927 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800928 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 if (mDragLayer.getAnimatedView() != null) {
932 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
933 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
934 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 // The animated view may be null in the case of a rotation during widget configuration
937 onCompleteRunnable.run();
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 protected void onStop() {
943 super.onStop();
944 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onStop();
948 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700949 }
950
951 @Override
952 protected void onStart() {
953 super.onStart();
954 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700955
956 if (mLauncherCallbacks != null) {
957 mLauncherCallbacks.onStart();
958 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700959 }
960
961 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200963 long startTime = 0;
964 if (DEBUG_RESUME_TIME) {
965 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400966 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200967 }
Adam Cohen9211d422014-10-07 18:14:53 -0700968
969 if (mLauncherCallbacks != null) {
970 mLauncherCallbacks.preOnResume();
971 }
972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700974 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700975
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700977 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700978 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800979 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700980 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700981 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700982 // view after launching an app, as they may be depending on the UI to be static to
983 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700984 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700985 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800986 } else if (mOnResumeState == State.WIDGETS) {
987 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 }
989 mOnResumeState = State.NONE;
990
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700991 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700992 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
993 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700994
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800995 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700996 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700997 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700998 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001003 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1004 // execute them here
1005 long startTimeCallbacks = 0;
1006 if (DEBUG_RESUME_TIME) {
1007 startTimeCallbacks = System.currentTimeMillis();
1008 }
1009
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1011 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001012 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001014 if (DEBUG_RESUME_TIME) {
1015 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1016 (System.currentTimeMillis() - startTimeCallbacks));
1017 }
Michael Jurka447bf842013-05-15 14:52:15 +02001018 }
Michael Jurka54554252013-08-01 12:52:23 +02001019 if (mOnResumeCallbacks.size() > 0) {
1020 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1021 mOnResumeCallbacks.get(i).run();
1022 }
1023 mOnResumeCallbacks.clear();
1024 }
Winson Chunge4e50662012-01-23 14:45:13 -08001025
1026 // Reset the pressed state of icons that were locked in the press state while activities
1027 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001028 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001029 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001030 mWaitingForResume.setStayPressed(false);
1031 }
Winson Chung82963d52013-10-09 11:20:57 -07001032
Adam Cohen06dff352012-06-01 17:17:08 -07001033 // It is possible that widgets can receive updates while launcher is not in the foreground.
1034 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1035 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1036 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001037 if (!isWorkspaceLoading()) {
1038 getWorkspace().reinflateWidgetsIfNecessary();
1039 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001040 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001041
Michael Jurka447bf842013-05-15 14:52:15 +02001042 if (DEBUG_RESUME_TIME) {
1043 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1044 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001045
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 // We want to suppress callbacks about CustomContent being shown if we have just received
1047 // onNewIntent while the user was present within launcher. In that case, we post a call
1048 // to move the user to the main screen (which will occur after onResume). We don't want to
1049 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1050 // suppress here.
1051 if (mWorkspace.getCustomContentCallbacks() != null
1052 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001054 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1056 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001057 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 }
1059 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001060 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001061 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001062 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001063
Sunny Goyal756adbc2015-04-16 15:20:51 -07001064 if (!isWorkspaceLoading()) {
1065 // Process any items that were added while Launcher was away.
1066 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1067 }
Adam Cohen9211d422014-10-07 18:14:53 -07001068
1069 if (mLauncherCallbacks != null) {
1070 mLauncherCallbacks.onResume();
1071 }
Adam Cohen06dff352012-06-01 17:17:08 -07001072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001076 // Ensure that items added to Launcher are queued until Launcher returns
1077 InstallShortcutReceiver.enableInstallQueue();
1078
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001080 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001081 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001082 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001083
1084 // We call onHide() aggressively. The custom content callbacks should be able to
1085 // debounce excess onHide calls.
1086 if (mWorkspace.getCustomContentCallbacks() != null) {
1087 mWorkspace.getCustomContentCallbacks().onHide();
1088 }
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Adam Cohen9211d422014-10-07 18:14:53 -07001090 if (mLauncherCallbacks != null) {
1091 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
1094
1095 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001096 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1097 // by a onResume or by scrolling otherwise.
1098 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001099
1100 // Custom content is completely hidden
1101 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001102
1103 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1104 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001105
1106 // Indicates whether the user is allowed to scroll away from the custom content.
1107 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
1109
Adam Cohenc2d6e892014-10-16 09:49:24 -07001110 public interface LauncherOverlay {
1111
1112 /**
1113 * Touch interaction leading to overscroll has begun
1114 */
1115 public void onScrollInteractionBegin();
1116
1117 /**
1118 * Touch interaction related to overscroll has ended
1119 */
1120 public void onScrollInteractionEnd();
1121
1122 /**
1123 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1124 * screen (or in the case of RTL, the rightmost screen).
1125 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001126 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001127
1128 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001129 * Called when the launcher is ready to use the overlay
1130 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 }
1134
Jun Mukai8af0cd82015-05-12 12:32:12 -07001135 public interface LauncherSearchCallbacks {
1136 /**
1137 * Called when the search overlay is shown.
1138 */
1139 public void onSearchOverlayOpened();
1140
1141 /**
1142 * Called when the search overlay is dismissed.
1143 */
1144 public void onSearchOverlayClosed();
1145 }
1146
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001148 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 }
1150
1151 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1152
Sunny Goyalc86df472016-02-25 09:19:38 -08001153 public void onScrollChanged(float progress) {
1154 if (mWorkspace != null) {
1155 mWorkspace.onOverlayScrollChanged(progress);
1156 }
1157 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 }
1159
Jorim Jaggid017f882014-01-14 17:08:48 -08001160 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001161 if (mLauncherCallbacks != null) {
1162 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001163 } else {
1164 // On devices with a locked orientation, we will at least have the allow rotation
1165 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001166 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001167 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001168 }
1169
Adam Cohen9211d422014-10-07 18:14:53 -07001170 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001171 CustomContentCallbacks callbacks, String description) {
1172 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001173 }
1174
Adam Cohenedb40762013-07-18 16:45:45 -07001175 // The custom content needs to offset its content to account for the QSB
1176 public int getTopOffsetForCustomContent() {
1177 return mWorkspace.getPaddingTop();
1178 }
1179
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001180 @Override
1181 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001183 if (mModel.isCurrentCallbacks(this)) {
1184 mModel.stopLoader();
1185 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001186 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1187
Romain Guy13c2e7b2010-03-10 19:45:00 -08001188 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001189 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001190
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 @Override
1193 public void onWindowFocusChanged(boolean hasFocus) {
1194 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001195 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001196
1197 if (mLauncherCallbacks != null) {
1198 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1199 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001200 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 private boolean acceptFilter() {
1203 final InputMethodManager inputManager = (InputMethodManager)
1204 getSystemService(Context.INPUT_METHOD_SERVICE);
1205 return !inputManager.isFullscreenMode();
1206 }
1207
1208 @Override
1209 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001210 final int uniChar = event.getUnicodeChar();
1211 final boolean handled = super.onKeyDown(keyCode, event);
1212 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1213 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1215 keyCode, event);
1216 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001218 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 // showSearchDialog()
1220 // If there are multiple keystrokes before the search dialog takes focus,
1221 // onSearchRequested() will be called for every keystroke,
1222 // but it is idempotent, so it's fine.
1223 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225 }
1226
Joe Onorato8a9625e2010-01-28 15:55:35 -08001227 // Eat the long press event so the keyboard doesn't come up.
1228 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1229 return true;
1230 }
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return handled;
1233 }
1234
Winson46163472015-09-22 17:31:56 -07001235 @Override
1236 public boolean onKeyUp(int keyCode, KeyEvent event) {
1237 if (keyCode == KeyEvent.KEYCODE_MENU) {
1238 // Ignore the menu key if we are currently dragging or are on the custom content screen
1239 if (!isOnCustomContent() && !mDragController.isDragging()) {
1240 // Close any open folders
1241 closeFolder();
1242
1243 // Stop resizing any widgets
1244 mWorkspace.exitWidgetResizeMode();
1245
1246 // Show the overview mode if we are on the workspace
1247 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1248 !mWorkspace.isSwitchingState()) {
1249 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001250 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001251 }
1252 }
1253 return true;
1254 }
1255 return super.onKeyUp(keyCode, event);
1256 }
1257
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 private String getTypedText() {
1259 return mDefaultKeySsb.toString();
1260 }
1261
1262 private void clearTypedText() {
1263 mDefaultKeySsb.clear();
1264 mDefaultKeySsb.clearSpans();
1265 Selection.setSelection(mDefaultKeySsb, 0);
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001269 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1270 * State
1271 */
1272 private static State intToState(int stateOrdinal) {
1273 State state = State.WORKSPACE;
1274 final State[] stateValues = State.values();
1275 for (int i = 0; i < stateValues.length; i++) {
1276 if (stateValues[i].ordinal() == stateOrdinal) {
1277 state = stateValues[i];
1278 break;
1279 }
1280 }
1281 return state;
1282 }
1283
1284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 * Restores the previous state, if it exists.
1286 *
1287 * @param savedState The previous state.
1288 */
1289 private void restoreState(Bundle savedState) {
1290 if (savedState == null) {
1291 return;
1292 }
1293
Michael Jurka883f55b2010-10-21 15:47:14 -07001294 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001295 if (state == State.APPS || state == State.WIDGETS) {
1296 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001297 }
1298
Adam Cohen21cd0022013-10-09 18:57:02 -07001299 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1300 PagedView.INVALID_RESTORE_PAGE);
1301 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001302 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
Sunny Goyal756cd262015-08-20 12:33:21 -07001305 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1306 if (itemValues != null) {
1307 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001308 AppWidgetProviderInfo info = savedState.getParcelable(
1309 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001310 mPendingAddWidgetInfo = info == null ?
1311 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1312
Adam Cohen4637b5a2013-11-04 18:21:24 -08001313 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001314 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 mRestoring = true;
1316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
1319 /**
1320 * Finds all the views we need and configure them properly.
1321 */
1322 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001323 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001324
Craig Mautner360310b2012-10-26 15:13:08 -07001325 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001326 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001327 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1328 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001329 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001330
Sunny Goyal784f9c32016-03-23 16:56:54 -07001331 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1332 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1333 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001334 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
Winson Chung4c98d922011-05-31 16:50:48 -07001336 // Setup the drag layer
1337 mDragLayer.setup(this, dragController);
1338
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 // Setup the hotseat
1340 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1341 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001342 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 }
1344
Winsone9f27272015-10-13 10:47:51 -07001345 // Setup the overview panel
1346 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001347
Winson Chung4c98d922011-05-31 16:50:48 -07001348 // Setup the workspace
1349 mWorkspace.setHapticFeedbackEnabled(false);
1350 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001351 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001352 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001353
Tony Wickham34d2c912015-09-11 09:27:58 -07001354 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001355 mSearchDropTargetBar = (SearchDropTargetBar)
1356 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001357 // Get the app info bar
1358 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1359 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001360
Winson Chungef7f8742015-06-04 17:18:17 -07001361 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001362 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001363 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001364 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1365 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1366 } else {
1367 mAppsView.setSearchBarController(new DefaultAppSearchController());
1368 }
Craig Mautner360310b2012-10-26 15:13:08 -07001369
Winson Chung3d503fb2011-07-13 17:25:49 -07001370 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001371 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001372 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001373 dragController.setMoveTarget(mWorkspace);
1374 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001375 if (mSearchDropTargetBar != null) {
1376 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001377 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001378 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001379 if (mAppInfoDropTargetBar != null) {
1380 mAppInfoDropTargetBar.setup(this, dragController);
1381 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001382
Sunny Goyal322d5562015-06-25 19:35:49 -07001383 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1384 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
Winsone9f27272015-10-13 10:47:51 -07001388 private void setupOverviewPanel() {
1389 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1390
1391 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1392 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1393 @Override
1394 public boolean onLongClick(View v) {
1395 return v.performClick();
1396 }
1397 };
1398
1399 // Bind wallpaper button actions
1400 View wallpaperButton = findViewById(R.id.wallpaper_button);
1401 wallpaperButton.setOnClickListener(new OnClickListener() {
1402 @Override
1403 public void onClick(View view) {
1404 if (!mWorkspace.isSwitchingState()) {
1405 onClickWallpaperPicker(view);
1406 }
1407 }
1408 });
1409 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1410 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1411
1412 // Bind widget button actions
1413 mWidgetsButton = findViewById(R.id.widget_button);
1414 mWidgetsButton.setOnClickListener(new OnClickListener() {
1415 @Override
1416 public void onClick(View view) {
1417 if (!mWorkspace.isSwitchingState()) {
1418 onClickAddWidgetButton(view);
1419 }
1420 }
1421 });
1422 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1423 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1424
1425 // Bind settings actions
1426 View settingsButton = findViewById(R.id.settings_button);
1427 boolean hasSettings = hasSettings();
1428 if (hasSettings) {
1429 settingsButton.setOnClickListener(new OnClickListener() {
1430 @Override
1431 public void onClick(View view) {
1432 if (!mWorkspace.isSwitchingState()) {
1433 onClickSettingsButton(view);
1434 }
1435 }
1436 });
1437 settingsButton.setOnLongClickListener(performClickOnLongClick);
1438 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1439 } else {
1440 settingsButton.setVisibility(View.GONE);
1441 }
1442
1443 mOverviewPanel.setAlpha(0f);
1444 }
1445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001447 * Sets the all apps button. This method is called from {@link Hotseat}.
1448 */
1449 public void setAllAppsButton(View allAppsButton) {
1450 mAllAppsButton = allAppsButton;
1451 }
1452
1453 public View getAllAppsButton() {
1454 return mAllAppsButton;
1455 }
1456
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001457 public View getWidgetsButton() {
1458 return mWidgetsButton;
1459 }
1460
Anjali Koppal5ad44842014-03-10 20:34:39 -07001461 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 * Creates a view representing a shortcut.
1463 *
1464 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001466 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001467 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469
1470 /**
1471 * Creates a view representing a shortcut inflated from the specified resource.
1472 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * @param parent The group the shortcut belongs to.
1474 * @param info The data structure describing the shortcut.
1475 *
1476 * @return A View inflated from layoutResId.
1477 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001478 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001479 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001480 parent, false);
1481 favorite.applyFromShortcutInfo(info, mIconCache);
1482 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001484 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 return favorite;
1486 }
1487
1488 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 * Add a shortcut to the workspace.
1490 *
1491 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001493 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001494 int cellY) {
1495 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001496 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001497
Sunny Goyal5c97f512015-05-19 16:03:28 -07001498 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001499 if (info == null) {
1500 return;
1501 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001502 final View view = createShortcut(info);
1503
Sunny Goyal5c97f512015-05-19 16:03:28 -07001504 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 // First we check if we already know the exact location where we want to add this item.
1506 if (cellX >= 0 && cellY >= 0) {
1507 cellXY[0] = cellX;
1508 cellXY[1] = cellY;
1509 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001510
1511 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001512 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001513 true, null,null)) {
1514 return;
1515 }
1516 DragObject dragObject = new DragObject();
1517 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001518 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1519 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001520 return;
1521 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001522 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001523 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001524 }
1525
1526 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001527 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001528 return;
1529 }
1530
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001531 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532
1533 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001534 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537 }
1538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001540 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001542 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 */
Adam Cohen091440a2015-03-18 14:16:05 -07001544 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001545 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1546
1547 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001548 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001549 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 }
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Adam Cohen59400422014-03-05 18:07:04 -08001552 if (appWidgetInfo.isCustomWidget) {
1553 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001554 }
1555
Adam Cohen59400422014-03-05 18:07:04 -08001556 LauncherAppWidgetInfo launcherInfo;
1557 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1558 launcherInfo.spanX = info.spanX;
1559 launcherInfo.spanY = info.spanY;
1560 launcherInfo.minSpanX = info.minSpanX;
1561 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001562 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001565 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
1567 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 if (hostView == null) {
1569 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001570 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1571 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 } else {
1573 // The AppWidgetHostView has already been inflated and instantiated
1574 launcherInfo.hostView = hostView;
1575 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001576 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001577 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001579 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001582 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1583 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1584 item.hostView.setTag(item);
1585 item.onBindAppWidget(this);
1586
1587 item.hostView.setFocusable(true);
1588 item.hostView.setOnFocusChangeListener(mFocusHandler);
1589
1590 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1591 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1592
1593 if (!item.isCustomWidget()) {
1594 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1595 }
1596 }
1597
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1599 @Override
1600 public void onReceive(Context context, Intent intent) {
1601 final String action = intent.getAction();
1602 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1603 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001604 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001606
Winson Chungc100e8e2011-08-09 16:02:43 -07001607 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001608 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001609 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001610 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001611 if (!showWorkspace(false)) {
1612 // If we are already on the workspace, then manually reset all apps
1613 mAppsView.reset();
1614 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1617 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001618 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001619 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1620 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001621 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001622 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1623 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1624 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001625 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1627 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 }
1629 }
1630 };
1631
1632 @Override
1633 public void onAttachedToWindow() {
1634 super.onAttachedToWindow();
1635
1636 // Listen for broadcasts related to user-presence
1637 final IntentFilter filter = new IntentFilter();
1638 filter.addAction(Intent.ACTION_SCREEN_OFF);
1639 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001640 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 if (ENABLE_DEBUG_INTENTS) {
1642 filter.addAction(DebugIntents.DELETE_DATABASE);
1643 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1644 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001646 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001647 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001649
1650 if (mLauncherCallbacks != null) {
1651 mLauncherCallbacks.onAttachedToWindow();
1652 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 }
1654
1655 @Override
1656 public void onDetachedFromWindow() {
1657 super.onDetachedFromWindow();
1658 mVisible = false;
1659
Adam Cohend113e0c2010-11-11 10:48:05 -08001660 if (mAttached) {
1661 unregisterReceiver(mReceiver);
1662 mAttached = false;
1663 }
Winson Chungb745afb2015-03-02 11:51:23 -08001664 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001665
1666 if (mLauncherCallbacks != null) {
1667 mLauncherCallbacks.onDetachedFromWindow();
1668 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 }
1670
1671 public void onWindowVisibilityChanged(int visibility) {
1672 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001673 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 // The following code used to be in onResume, but it turns out onResume is called when
1675 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1676 // is a more appropriate event to handle
1677 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 if (!mWorkspaceLoading) {
1679 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001680 // We want to let Launcher draw itself at least once before we force it to build
1681 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 // apps is nice and speedy.
1683 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001685 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 if (mStarted) return;
1687 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001688 // We delay the layer building a bit in order to give
1689 // other message processing a time to run. In particular
1690 // this avoids a delay in hiding the IME if it was
1691 // currently shown, because doing that may involve
1692 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001693 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001694 final ViewTreeObserver.OnDrawListener listener = this;
1695 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001696 public void run() {
1697 if (mWorkspace != null &&
1698 mWorkspace.getViewTreeObserver() != null) {
1699 mWorkspace.getViewTreeObserver().
1700 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001702 }
1703 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001704 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001705 }
1706 });
1707 }
1708 clearTypedText();
1709 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 }
1711
Adam Cohen091440a2015-03-18 14:16:05 -07001712 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 mHandler.removeMessages(ADVANCE_MSG);
1714 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1715 mHandler.sendMessageDelayed(msg, delay);
1716 mAutoAdvanceSentTime = System.currentTimeMillis();
1717 }
1718
Adam Cohen091440a2015-03-18 14:16:05 -07001719 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1721 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1722 mAutoAdvanceRunning = autoAdvanceRunning;
1723 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001724 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 sendAdvanceMessage(delay);
1726 } else {
1727 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1730 }
1731 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001732 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734 }
1735 }
1736
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1738
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 if (msg.what == ADVANCE_MSG) {
1742 int i = 0;
1743 for (View key: mWidgetsToAdvance.keySet()) {
1744 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001745 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 public void run() {
1749 ((Advanceable) v).advance();
1750 }
1751 }, delay);
1752 }
1753 i++;
1754 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001755 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001760
Winsonc0b52fe2015-09-09 16:38:15 -07001761 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001762 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1764 if (v instanceof Advanceable) {
1765 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001766 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001767 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769 }
1770
Winsonc0b52fe2015-09-09 16:38:15 -07001771 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 if (mWidgetsToAdvance.containsKey(hostView)) {
1773 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001774 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 }
1777
Hyunyoung Song3f471442015-04-08 19:01:34 -07001778 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001779 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1780 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 }
1782
Winson Chunga6945242014-01-08 14:04:34 -08001783 public DragLayer getDragLayer() {
1784 return mDragLayer;
1785 }
1786
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001787 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001788 return mAppsView;
1789 }
1790
Hyunyoung Song3f471442015-04-08 19:01:34 -07001791 public WidgetsContainerView getWidgetsView() {
1792 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public Workspace getWorkspace() {
1796 return mWorkspace;
1797 }
1798
1799 public Hotseat getHotseat() {
1800 return mHotseat;
1801 }
1802
Jorim Jaggid017f882014-01-14 17:08:48 -08001803 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001804 return mOverviewPanel;
1805 }
1806
Winson Chung006ee262015-08-03 14:40:11 -07001807 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001808 return mSearchDropTargetBar;
1809 }
1810
Tony Wickham34d2c912015-09-11 09:27:58 -07001811 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1812 return mAppInfoDropTargetBar;
1813 }
1814
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001815 public LauncherAppWidgetHost getAppWidgetHost() {
1816 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Winson Chunga9abd0e2010-10-27 17:18:37 -07001819 public LauncherModel getModel() {
1820 return mModel;
1821 }
1822
Adam Cohen79d90c52016-04-22 13:29:20 -07001823 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001824 return mSharedPrefs;
1825 }
1826
Adam Cohen2e6da152015-05-06 11:42:25 -07001827 public DeviceProfile getDeviceProfile() {
1828 return mDeviceProfile;
1829 }
1830
Bjorn Bringertc459e522013-06-07 19:36:01 +01001831 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001832 getWindow().closeAllPanels();
1833
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001834 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001835 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001836 }
1837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 @Override
1839 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001840 long startTime = 0;
1841 if (DEBUG_RESUME_TIME) {
1842 startTime = System.currentTimeMillis();
1843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 super.onNewIntent(intent);
1845
1846 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001847 Folder openFolder = mWorkspace.getOpenFolder();
1848 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1849 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1850 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1851 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1852 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001853 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001854 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001855
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 if (mWorkspace == null) {
1857 // Can be cases where mWorkspace is null, this prevents a NPE
1858 return;
1859 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 // In all these cases, only animate if we're already on home
1861 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001862
Sunny Goyal935fca12015-10-13 10:19:01 -07001863 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001864 exitSpringLoadedDragMode();
1865
1866 // If we are already on home, then just animate back to the workspace,
1867 // otherwise, just wait until onResume to set the state back to Workspace
1868 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001869 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001870 } else {
1871 mOnResumeState = State.WORKSPACE;
1872 }
1873
1874 final View v = getWindow().peekDecorView();
1875 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001876 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 INPUT_METHOD_SERVICE);
1878 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1879 }
1880
Winson Chungb745afb2015-03-02 11:51:23 -08001881 // Reset the apps view
1882 if (!alreadyOnHome && mAppsView != null) {
1883 mAppsView.scrollToTop();
1884 }
1885
Hyunyoung Song3f471442015-04-08 19:01:34 -07001886 // Reset the widgets view
1887 if (!alreadyOnHome && mWidgetsView != null) {
1888 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001890
Adam Cohen9211d422014-10-07 18:14:53 -07001891 if (mLauncherCallbacks != null) {
1892 mLauncherCallbacks.onHomeIntent();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
Adam Cohened307df2013-10-02 09:37:31 -07001895
Adam Cohen9211d422014-10-07 18:14:53 -07001896 if (mLauncherCallbacks != null) {
1897 mLauncherCallbacks.onNewIntent(intent);
1898 }
Winson15f8b172015-08-19 11:02:39 -07001899
1900 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1901 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1902 // animation.
1903 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001904 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1905 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001906 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1907 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001908
1909 // We use this flag to suppress noisy callbacks above custom content state
1910 // from onResume.
1911 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001912 mWorkspace.post(new Runnable() {
1913 @Override
1914 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001915 if (mWorkspace != null) {
1916 mWorkspace.moveToDefaultScreen(true);
1917 }
Winson15f8b172015-08-19 11:02:39 -07001918 }
1919 });
1920 }
1921 }
1922
1923 if (DEBUG_RESUME_TIME) {
1924 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1925 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001926 }
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001929 public void onRestoreInstanceState(Bundle state) {
1930 super.onRestoreInstanceState(state);
1931 for (int page: mSynchronouslyBoundPages) {
1932 mWorkspace.restoreInstanceStateForChild(page);
1933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935
1936 @Override
1937 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001938 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001939 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1940 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001941 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001942 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943
Michael Jurka883f55b2010-10-21 15:47:14 -07001944 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001945 // We close any open folder since it will not be re-opened, and we need to make sure
1946 // this state is reflected.
1947 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1948 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001952 ContentValues itemValues = new ContentValues();
1953 mPendingAddInfo.writeToValues(itemValues);
1954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001955 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001956 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Hyunyoung Song3f471442015-04-08 19:01:34 -07001959 // Save the current widgets tray?
1960 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001961
1962 if (mLauncherCallbacks != null) {
1963 mLauncherCallbacks.onSaveInstanceState(outState);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966
1967 @Override
1968 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001970
Winson Chunge7a03942011-08-05 15:05:12 -07001971 // Remove all pending runnables
1972 mHandler.removeMessages(ADVANCE_MSG);
1973 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001974 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001975
Winson Chungcd2b0142011-06-08 16:02:26 -07001976 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001977 // It's possible to receive onDestroy after a new Launcher activity has
1978 // been created. In this case, don't interfere with the new Launcher.
1979 if (mModel.isCurrentCallbacks(this)) {
1980 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001981 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001982 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001983
Sunny Goyal745bad92016-05-02 10:54:12 -07001984 if (mRotationPrefChangeHandler != null) {
1985 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001991 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993 mAppWidgetHost = null;
1994
1995 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
1997 TextKeyListener.getInstance().release();
1998
Tony Wickhame2217252016-03-22 16:34:23 -07001999 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2000 .removeAccessibilityStateChangeListener(this);
2001
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002002 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002003
Michael Jurka2ecf9952012-06-18 12:52:28 -07002004 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002005
2006 if (mLauncherCallbacks != null) {
2007 mLauncherCallbacks.onDestroy();
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 public DragController getDragController() {
2012 return mDragController;
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
2016 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002017 onStartForResult(requestCode);
2018 super.startActivityForResult(intent, requestCode);
2019 }
2020
2021 @Override
2022 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2023 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2024 onStartForResult(requestCode);
2025 try {
2026 super.startIntentSenderForResult(intent, requestCode,
2027 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2028 } catch (IntentSender.SendIntentException e) {
2029 throw new ActivityNotFoundException();
2030 }
2031 }
2032
2033 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002034 if (requestCode >= 0) {
2035 setWaitingForResult(true);
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Winson Chung70d72102011-08-12 11:24:35 -07002039 /**
2040 * Indicates that we want global search for this activity by setting the globalSearch
2041 * argument for {@link #startSearch} to true.
2042 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002044 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002046
Karl Rosaen138a0412009-04-23 19:00:21 -07002047 if (initialQuery == null) {
2048 // Use any text typed in the launcher as the initial query
2049 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 if (appSearchData == null) {
2052 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002053 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
Winson Chungadf0c182012-08-23 14:59:07 -07002055 Rect sourceBounds = new Rect();
2056 if (mSearchDropTargetBar != null) {
2057 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2058 }
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 if (clearTextImmediately) {
2063 clearTypedText();
2064 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002065
2066 // We need to show the workspace after starting the search
2067 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 }
2069
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 /**
2071 * Start a text search.
2072 *
2073 * @return {@code true} if the search will start immediately, so any further keypresses
2074 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2075 * to buffer keypresses.
2076 */
2077 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002079 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2080 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2081 sourceBounds);
2082 }
2083
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 }
2088
2089 /**
2090 * Starts the global search activity. This code is a copied from SearchManager
2091 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002094 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2096 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2097 if (globalSearchActivity == null) {
2098 Log.w(TAG, "No global search activity found.");
2099 return;
2100 }
2101 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2103 intent.setComponent(globalSearchActivity);
2104 // Make sure that we have a Bundle to put source in
2105 if (appSearchData == null) {
2106 appSearchData = new Bundle();
2107 } else {
2108 appSearchData = new Bundle(appSearchData);
2109 }
Adam Cohen9211d422014-10-07 18:14:53 -07002110 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 if (!appSearchData.containsKey("source")) {
2112 appSearchData.putString("source", getPackageName());
2113 }
2114 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2115 if (!TextUtils.isEmpty(initialQuery)) {
2116 intent.putExtra(SearchManager.QUERY, initialQuery);
2117 }
2118 if (selectInitialQuery) {
2119 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2120 }
2121 intent.setSourceBounds(sourceBounds);
2122 try {
2123 startActivity(intent);
2124 } catch (ActivityNotFoundException ex) {
2125 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
2128
Yura4f93ec62014-02-04 14:15:21 +00002129 public boolean isOnCustomContent() {
2130 return mWorkspace.isOnOrMovingToCustomContent();
2131 }
2132
Winson Chung70d72102011-08-12 11:24:35 -07002133 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002134 public boolean onPrepareOptionsMenu(Menu menu) {
2135 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002136 if (mLauncherCallbacks != null) {
2137 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2138 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002139 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 @Override
2143 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002144 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 }
2148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 public boolean isWorkspaceLocked() {
2150 return mWorkspaceLoading || mWaitingForResult;
2151 }
2152
Adam Cohen517a7f52014-03-01 12:12:59 -08002153 public boolean isWorkspaceLoading() {
2154 return mWorkspaceLoading;
2155 }
2156
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002157 private void setWorkspaceLoading(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWorkspaceLoading = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 private void setWaitingForResult(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWaitingForResult = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
Adam Cohen9211d422014-10-07 18:14:53 -07002173 protected void onWorkspaceLockedChanged() {
2174 if (mLauncherCallbacks != null) {
2175 mLauncherCallbacks.onWorkspaceLockedChanged();
2176 }
2177 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2183 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002184 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002185 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002186
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002188 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002189 if (LOGD) {
2190 Log.d(TAG, "Adding widget from drop");
2191 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2193 }
2194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002196 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2197 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 if (appWidgetInfo.configure != null) {
2199 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002200 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002201
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002203 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2204 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 Runnable onComplete = new Runnable() {
2209 @Override
2210 public void run() {
2211 // Exit spring loaded mode if necessary after adding the widget
2212 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2213 null);
2214 }
2215 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002218 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002223 // Close any folders that may be open.
2224 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002225 mWorkspace.moveToCustomContentScreen(animate);
2226 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002227
2228 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2229 int[] cell, int spanX, int spanY) {
2230 switch (info.itemType) {
2231 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2232 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2233 int span[] = new int[2];
2234 span[0] = spanX;
2235 span[1] = spanY;
2236 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2237 container, screenId, cell, span);
2238 break;
2239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2240 processShortcutFromDrop(info.componentName, container, screenId, cell);
2241 break;
2242 default:
2243 throw new IllegalStateException("Unknown item type: " + info.itemType);
2244 }
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a shortcut drop.
2249 *
2250 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2255 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259
2260 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.cellX = cell[0];
2262 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264
2265 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2266 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002267 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 /**
2271 * Process a widget drop.
2272 *
2273 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002277 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2278 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = info.minSpanX;
2283 mPendingAddInfo.minSpanY = info.minSpanY;
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.cellX = cell[0];
2287 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002289 if (span != null) {
2290 mPendingAddInfo.spanX = span[0];
2291 mPendingAddInfo.spanY = span[1];
2292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 AppWidgetHostView hostView = info.boundWidget;
2295 int appWidgetId;
2296 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002297 // In the case where we've prebound the widget, we remove it from the DragLayer
2298 if (LOGD) {
2299 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2300 }
2301 getDragLayer().removeView(hostView);
2302
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002304 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002305
2306 // Clear the boundWidget so that it doesn't get destroyed.
2307 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002309 // In this case, we either need to start an activity to get permission to bind
2310 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002312 Bundle options = info.bindOptions;
2313
Sunny Goyalffe83f12014-08-14 17:39:34 -07002314 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2315 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002316 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002317 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002319 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002323 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2324 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002325 // TODO: we need to make sure that this accounts for the options bundle.
2326 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002327 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2328 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 }
2331
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002334 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 folderInfo.title = getText(R.string.folder_name);
2336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002338 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2339 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
2341 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 FolderIcon newFolder =
2343 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002346 // Force measure the new folder icon
2347 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2348 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002349 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
Winsonc0b52fe2015-09-09 16:38:15 -07002352 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * Unbinds the view for the specified item, and removes the item and all its children.
2354 *
2355 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002356 * @param itemInfo the {@link ItemInfo} for this view.
2357 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002358 */
Winson2949fb52015-09-24 09:56:11 -07002359 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002361 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002362 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2363 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002364 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002365 } else {
2366 mWorkspace.removeWorkspaceItem(v);
2367 }
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (deleteFromDb) {
2369 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2370 }
2371 } else if (itemInfo instanceof FolderInfo) {
2372 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002373 mWorkspace.removeWorkspaceItem(v);
2374 if (deleteFromDb) {
2375 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2376 }
2377 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2378 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002379 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 removeWidgetToAutoAdvance(widgetInfo.hostView);
2381 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002382 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002383 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002385
Winsonc0b52fe2015-09-09 16:38:15 -07002386 } else {
2387 return false;
2388 }
2389 return true;
2390 }
2391
2392 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002394 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002396 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002398 // Deleting an app widget ID is a void call but writes to disk before returning
2399 // to the caller...
2400 new AsyncTask<Void, Void, Void>() {
2401 public Void doInBackground(Void ... args) {
2402 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2403 return null;
2404 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002405 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002406 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002407 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002408 }
2409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 @Override
2411 public boolean dispatchKeyEvent(KeyEvent event) {
2412 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2413 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002415 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002416 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002417 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002418 dumpState();
2419 return true;
2420 }
2421 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 }
2423 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2424 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002425 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 return true;
2427 }
2428 }
2429
2430 return super.dispatchKeyEvent(event);
2431 }
2432
Joe Onorato88ec0992009-11-19 13:16:06 -08002433 @Override
2434 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002435 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2436 return;
2437 }
2438
Sunny Goyal45478022015-06-08 16:52:41 -07002439 if (mDragController.isDragging()) {
2440 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002441 return;
2442 }
2443
Winson Chungb745afb2015-03-02 11:51:23 -08002444 if (isAppsViewVisible()) {
2445 showWorkspace(true);
2446 } else if (isWidgetsViewVisible()) {
2447 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002448 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002449 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002450 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002451 Folder openFolder = mWorkspace.getOpenFolder();
2452 if (openFolder.isEditingName()) {
2453 openFolder.dismissEditingName();
2454 } else {
2455 closeFolder();
2456 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002457 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002458 mWorkspace.exitWidgetResizeMode();
2459
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 // Back button is a no-op here, but give at least some feedback for the button press
2461 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002462 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002463 }
2464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002466 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002467 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002468 @Override
2469 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002470 if (mAppWidgetHost != null) {
2471 mAppWidgetHost.startListening();
2472 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002473
2474 // Recreate the QSB, as the widget has been reset.
2475 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002476 }
2477
2478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 * Launches the intent referred by the clicked shortcut.
2480 *
2481 * @param v The view representing the clicked shortcut.
2482 */
2483 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002484 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2485 // view has detached (it's possible for this to happen if the view is removed mid touch).
2486 if (v.getWindowToken() == null) {
2487 return;
2488 }
2489
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002490 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002491 return;
2492 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002493
Adam Cohen1697b792013-09-17 19:08:21 -07002494 if (v instanceof Workspace) {
2495 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002496 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 }
2498 return;
2499 }
2500
2501 if (v instanceof CellLayout) {
2502 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002503 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2504 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002506 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002507 }
2508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002510 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002511 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002513 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002515 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002516 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002518 } else if (tag instanceof AppInfo) {
2519 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002520 } else if (tag instanceof LauncherAppWidgetInfo) {
2521 if (v instanceof PendingAppWidgetHostView) {
2522 onClickPendingWidget((PendingAppWidgetHostView) v);
2523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 }
2525 }
2526
Sunny Goyal70660032015-05-14 00:07:08 -07002527 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002528 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002529 return false;
2530 }
2531
Michael Jurkaaf442092010-06-10 17:01:57 -07002532 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002533 * Event handler for the app widget view which has not fully restored.
2534 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002535 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002536 if (mIsSafeModeEnabled) {
2537 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2538 return;
2539 }
2540
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002541 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002542 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002543 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002544 LauncherAppWidgetProviderInfo appWidgetInfo =
2545 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002546 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002547 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002548 mPendingAddInfo.copyFrom(info);
2549 mPendingAddWidgetId = widgetId;
2550
Sunny Goyalffe83f12014-08-14 17:39:34 -07002551 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2552 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002553 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 } else if (info.installProgress < 0) {
2555 // The install has not been queued
2556 final String packageName = info.providerName.getPackageName();
2557 showBrokenAppInstallDialog(packageName,
2558 new DialogInterface.OnClickListener() {
2559 public void onClick(DialogInterface dialog, int id) {
2560 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2561 }
2562 });
2563 } else {
2564 // Download has started.
2565 final String packageName = info.providerName.getPackageName();
2566 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 }
2568 }
2569
2570 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002571 * Event handler for the "grid" button that appears on the home screen, which
2572 * enters all apps mode.
2573 *
2574 * @param v The view that was clicked.
2575 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002576 protected void onClickAllAppsButton(View v) {
2577 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002578 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002579 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002580 true /* updatePredictedApps */, false /* focusSearchBar */);
2581 }
2582 }
2583
2584 protected void onLongClickAllAppsButton(View v) {
2585 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2586 if (!isAppsViewVisible()) {
2587 showAppsView(true /* animated */, false /* resetListToTop */,
2588 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 }
2590 }
2591
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002592 private void showBrokenAppInstallDialog(final String packageName,
2593 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002594 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 .setTitle(R.string.abandoned_promises_title)
2596 .setMessage(R.string.abandoned_promise_explanation)
2597 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2598 .setNeutralButton(R.string.abandoned_clean_this,
2599 new DialogInterface.OnClickListener() {
2600 public void onClick(DialogInterface dialog, int id) {
2601 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2602 mWorkspace.removeAbandonedPromise(packageName, user);
2603 }
2604 })
2605 .create().show();
2606 return;
2607 }
2608
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 /**
2610 * Event handler for an app shortcut click.
2611 *
2612 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2613 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002614 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2616 Object tag = v.getTag();
2617 if (!(tag instanceof ShortcutInfo)) {
2618 throw new IllegalArgumentException("Input must be a Shortcut");
2619 }
2620
2621 // Open shortcut
2622 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002623
2624 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002625 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2626 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002627 // Launch activity anyway, framework will tell the user why the app is suspended.
2628 } else {
2629 int error = R.string.activity_not_available;
2630 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2631 error = R.string.safemode_shortcut_error;
2632 }
2633 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2634 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002635 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002636 }
2637
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002639 if ((v instanceof BubbleTextView)
2640 && shortcut.isPromise()
2641 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002642 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002643 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 new DialogInterface.OnClickListener() {
2645 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002646 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 return;
2650 }
2651
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002653 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 }
2655
Adam Cohen091440a2015-03-18 14:16:05 -07002656 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002658 final ShortcutInfo shortcut;
2659 final Intent intent;
2660 if (tag instanceof ShortcutInfo) {
2661 shortcut = (ShortcutInfo) tag;
2662 intent = shortcut.intent;
2663 int[] pos = new int[2];
2664 v.getLocationOnScreen(pos);
2665 intent.setSourceBounds(new Rect(pos[0], pos[1],
2666 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002667
Sunny Goyal508da152014-08-14 10:53:27 -07002668 } else if (tag instanceof AppInfo) {
2669 shortcut = null;
2670 intent = ((AppInfo) tag).intent;
2671 } else {
2672 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2673 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674
2675 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002676 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677
2678 if (success && v instanceof BubbleTextView) {
2679 mWaitingForResume = (BubbleTextView) v;
2680 mWaitingForResume.setStayPressed(true);
2681 }
2682 }
2683
2684 /**
2685 * Event handler for a folder icon click.
2686 *
2687 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2688 */
2689 protected void onClickFolderIcon(View v) {
2690 if (LOGD) Log.d(TAG, "onClickFolder");
2691 if (!(v instanceof FolderIcon)){
2692 throw new IllegalArgumentException("Input must be a FolderIcon");
2693 }
2694
2695 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002696 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002698 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 }
Michael Jurka5130e402011-10-13 04:55:35 -07002700 }
2701
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002702 /**
2703 * Event handler for the (Add) Widgets button that appears after a long press
2704 * on the home screen.
2705 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002706 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002708 if (mIsSafeModeEnabled) {
2709 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2710 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002711 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002712 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 /**
2716 * Event handler for the wallpaper picker button that appears after a long press
2717 * on the home screen.
2718 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002719 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002720 if (!Utilities.isWallapaperAllowed(this)) {
2721 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2722 return;
2723 }
2724
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002726 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2727 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal30a71612016-04-12 17:57:41 -07002728 // TODO: Start the system wallpaper picker
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 }
2730
2731 /**
2732 * Event handler for a click on the settings button that appears after a long press
2733 * on the home screen.
2734 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002735 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002737 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2738 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 }
2740
Adam Cohen61f560d2013-09-30 15:58:20 -07002741 public View.OnTouchListener getHapticFeedbackTouchListener() {
2742 if (mHapticFeedbackTouchListener == null) {
2743 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002744 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002745 @Override
2746 public boolean onTouch(View v, MotionEvent event) {
2747 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2748 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2749 }
2750 return false;
2751 }
2752 };
2753 }
2754 return mHapticFeedbackTouchListener;
2755 }
2756
Tony Wickhame2217252016-03-22 16:34:23 -07002757 @Override
2758 public void onAccessibilityStateChanged(boolean enabled) {
2759 mDragLayer.onAccessibilityStateChanged(enabled);
2760 }
2761
Adam Cohen9211d422014-10-07 18:14:53 -07002762 public void onDragStarted(View view) {
2763 if (isOnCustomContent()) {
2764 // Custom content screen doesn't participate in drag and drop. If on custom
2765 // content screen, move to default.
2766 moveWorkspaceToDefaultScreen();
2767 }
Adam Cohen9211d422014-10-07 18:14:53 -07002768 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002769
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002770 /**
2771 * Called when the user stops interacting with the launcher.
2772 * This implies that the user is now on the homescreen and is not doing housekeeping.
2773 */
Adam Cohen9211d422014-10-07 18:14:53 -07002774 protected void onInteractionEnd() {
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onInteractionEnd();
2777 }
2778 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002779
2780 /**
2781 * Called when the user starts interacting with the launcher.
2782 * The possible interactions are:
2783 * - open all apps
2784 * - reorder an app shortcut, or a widget
2785 * - open the overview mode.
2786 * This is a good time to stop doing things that only make sense
2787 * when the user is on the homescreen and not doing housekeeping.
2788 */
Adam Cohen9211d422014-10-07 18:14:53 -07002789 protected void onInteractionBegin() {
2790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onInteractionBegin();
2792 }
2793 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002794
Winson Chungcd99cd32015-04-29 11:03:24 -07002795 /** Updates the interaction state. */
2796 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002797 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002799 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2800 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002801 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002803 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002804 onInteractionEnd();
2805 }
2806 }
2807
Winson Chung8f1eff72015-05-28 17:33:40 -07002808 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002809 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002811 // Only launch using the new animation if the shortcut has not opted out (this is a
2812 // private contract between launcher and may be ignored in the future).
2813 boolean useLaunchAnimation = (v != null) &&
2814 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002815 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2816 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002817
Kenny Guy1317e2d2014-05-08 18:52:50 +01002818 UserHandleCompat user = null;
2819 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2820 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2821 user = userManager.getUserForSerialNumber(serialNumber);
2822 }
2823
2824 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002825 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002826 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002827 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002828 int left = 0, top = 0;
2829 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2830 if (v instanceof TextView) {
2831 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002832 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2833 if (icon != null) {
2834 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002835 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002836 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002837 width = bounds.width();
2838 height = bounds.height();
2839 }
2840 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002841 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2842 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002843 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002844 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002846 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002847 // On L devices, we use the device default slide-up transition.
2848 // On L MR1 devices, we a custom version of the slide-up transition which
2849 // doesn't have the delay present in the device default.
2850 opts = ActivityOptions.makeCustomAnimation(this,
2851 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002852 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002853 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002854 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002855
2856 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2857 // Could be launching some bookkeeping activity
2858 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002859 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002860 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002861 launcherApps.startActivityForProfile(intent.getComponent(), user,
2862 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002863 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002864 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002866 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2867 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2868 // corresponding permission. Show the appropriate permission prompt if that
2869 // is the case.
2870 if (intent.getComponent() == null
2871 && Intent.ACTION_CALL.equals(intent.getAction())
2872 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2873 PackageManager.PERMISSION_GRANTED) {
2874 // TODO: Rename sPendingAddItem to a generic name.
2875 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2876 0, (ItemInfo) tag);
2877 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2878 REQUEST_PERMISSION_CALL_PHONE);
2879 return false;
2880 }
2881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002883 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002885 "or use the exported attribute for this activity. "
2886 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002888 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002890
Winson Chungbedf9232015-07-10 12:38:30 -07002891 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002892 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002893 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2894 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2895 return false;
2896 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002897 try {
2898 success = startActivity(v, intent, tag);
2899 } catch (ActivityNotFoundException e) {
2900 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2901 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2902 }
2903 return success;
2904 }
2905
Adam Cohen268c4752012-06-06 17:47:33 -07002906 /**
2907 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2908 * in the DragLayer in the exact absolute location of the original FolderIcon.
2909 */
2910 private void copyFolderIconToImage(FolderIcon fi) {
2911 final int width = fi.getMeasuredWidth();
2912 final int height = fi.getMeasuredHeight();
2913
2914 // Lazy load ImageView, Bitmap and Canvas
2915 if (mFolderIconImageView == null) {
2916 mFolderIconImageView = new ImageView(this);
2917 }
2918 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2919 mFolderIconBitmap.getHeight() != height) {
2920 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2921 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2922 }
2923
2924 DragLayer.LayoutParams lp;
2925 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2926 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2927 } else {
2928 lp = new DragLayer.LayoutParams(width, height);
2929 }
2930
Adam Cohen307fe232012-08-16 17:55:58 -07002931 // The layout from which the folder is being opened may be scaled, adjust the starting
2932 // view size by this scale factor.
2933 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002934 lp.customPosition = true;
2935 lp.x = mRectForFolderAnimation.left;
2936 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002937 lp.width = (int) (scale * width);
2938 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002939
2940 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2941 fi.draw(mFolderIconCanvas);
2942 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002943 if (fi.getFolder() != null) {
2944 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2945 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002946 }
Adam Cohen268c4752012-06-06 17:47:33 -07002947 // Just in case this image view is still in the drag layer from a previous animation,
2948 // we remove it and re-add it.
2949 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2950 mDragLayer.removeView(mFolderIconImageView);
2951 }
2952 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002953 if (fi.getFolder() != null) {
2954 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002955 }
Adam Cohen268c4752012-06-06 17:47:33 -07002956 }
2957
Adam Cohen37b2a492016-04-06 18:36:06 -07002958 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002959 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002960 FolderInfo info = (FolderInfo) fi.getTag();
2961 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2962 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002963 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2964 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002965 }
2966
Adam Cohen268c4752012-06-06 17:47:33 -07002967 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2968 copyFolderIconToImage(fi);
2969 fi.setVisibility(View.INVISIBLE);
2970
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002971 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2972 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002973 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002974 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2975 }
2976 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002977 oa.start();
2978 }
2979
Sunny Goyal935fca12015-10-13 10:19:01 -07002980 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002981 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002982 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002983
Adam Cohen268c4752012-06-06 17:47:33 -07002984 // We remove and re-draw the FolderIcon in-case it has changed
2985 mDragLayer.removeView(mFolderIconImageView);
2986 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002987
2988 if (cl != null) {
2989 cl.clearFolderLeaveBehind();
2990 }
2991
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002992 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002993 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 oa.addListener(new AnimatorListenerAdapter() {
2995 @Override
2996 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002997 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // Remove the ImageView copy of the FolderIcon and make the original visible.
2999 mDragLayer.removeView(mFolderIconImageView);
3000 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003001 }
3002 }
3003 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003004 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003005 if (!animate) {
3006 oa.end();
3007 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003008 }
3009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003011 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 * is animated relative to the specified View. If the View is null, no animation
3013 * is played.
3014 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003015 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003017 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003018
Adam Cohenfb91f302012-06-11 15:45:18 -07003019 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003020 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3021 if (openFolder != null && openFolder != folder) {
3022 // Close any open folder before opening a folder.
3023 closeFolder();
3024 }
3025
Adam Cohena9cf38f2011-05-02 15:36:58 -07003026 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027
Adam Cohena9cf38f2011-05-02 15:36:58 -07003028 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029
Sunny Goyalf4066152015-04-15 09:42:19 -07003030 // While the folder is open, the position of the icon cannot change.
3031 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3032
Adam Cohen4554ee12011-08-03 16:13:21 -07003033 // Just verify that the folder hasn't already been added to the DragLayer.
3034 // There was a one-off crash where the folder had a parent already.
3035 if (folder.getParent() == null) {
3036 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003037 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003038 } else {
3039 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3040 folder.getParent() + ").");
3041 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003042 folder.animateOpen();
3043
3044 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003045
3046 // Notify the accessibility manager that this folder "window" has appeared and occluded
3047 // the workspace items
3048 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3049 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003050 }
3051
3052 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003053 closeFolder(true);
3054 }
3055
3056 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003057 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003058 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003059 if (folder.isEditingName()) {
3060 folder.dismissEditingName();
3061 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003062 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003063 }
3064 }
3065
Sunny Goyal935fca12015-10-13 10:19:01 -07003066 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003067 animate &= !Utilities.isPowerSaverOn(this);
3068
Adam Cohen4554ee12011-08-03 16:13:21 -07003069 folder.getInfo().opened = false;
3070
3071 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3072 if (parent != null) {
3073 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003074 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003075 if (fi != null) {
3076 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3077 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003078 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003079 if (animate) {
3080 folder.animateClosed();
3081 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003082 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003083 }
Winson Chung83ca4802013-04-12 15:10:52 -07003084
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 // Notify the accessibility manager that this folder "window" has disappeared and no
3086 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003087 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 }
3089
Tony Wickhamdadb3042016-02-24 11:07:00 -08003090 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003092 if (!isDraggingEnabled()) return false;
3093 if (isWorkspaceLocked()) return false;
3094 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095
Winson Chung76648c52015-07-10 14:33:23 -07003096 if (v == mAllAppsButton) {
3097 onLongClickAllAppsButton(v);
3098 return true;
3099 }
3100
Adam Cohen1697b792013-09-17 19:08:21 -07003101 if (v instanceof Workspace) {
3102 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003103 if (!mWorkspace.isTouchActive()) {
3104 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003105 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3106 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3107 return true;
3108 } else {
3109 return false;
3110 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003111 } else {
3112 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003113 }
Adam Cohen1697b792013-09-17 19:08:21 -07003114 }
3115
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003116 CellLayout.CellInfo longClickCellInfo = null;
3117 View itemUnderLongClick = null;
3118 if (v.getTag() instanceof ItemInfo) {
3119 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003120 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003121 itemUnderLongClick = longClickCellInfo.cell;
3122 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 }
3124
Winson Chung3d503fb2011-07-13 17:25:49 -07003125 // The hotseat touch handling does not go through Workspace, and we always allow long press
3126 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003127 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003128 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003129 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003130 // User long pressed on empty space
3131 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3132 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003133 if (mWorkspace.isInOverviewMode()) {
3134 mWorkspace.startReordering(v);
3135 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003136 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003139 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3140 mHotseat.getOrderInHotseat(
3141 longClickCellInfo.cellX,
3142 longClickCellInfo.cellY));
3143 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003145 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 }
3147 }
3148 }
3149 return true;
3150 }
3151
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 boolean isHotseatLayout(View layout) {
3153 return mHotseat != null && layout != null &&
3154 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3155 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003156
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 /**
3158 * Returns the CellLayout of the specified container at the specified screen.
3159 */
Sunny Goyal92820592015-03-02 11:31:35 -08003160 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3162 if (mHotseat != null) {
3163 return mHotseat.getLayout();
3164 } else {
3165 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003166 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003167 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003168 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003169 }
3170 }
3171
Winson Chungb745afb2015-03-02 11:51:23 -08003172 /**
3173 * For overridden classes.
3174 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003175 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003176 return isAppsViewVisible();
3177 }
3178
3179 public boolean isAppsViewVisible() {
3180 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3181 }
3182
3183 public boolean isWidgetsViewVisible() {
3184 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003185 }
3186
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003187 private void setWorkspaceBackground(int background) {
3188 switch (background) {
3189 case WORKSPACE_BACKGROUND_TRANSPARENT:
3190 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3191 break;
3192 case WORKSPACE_BACKGROUND_BLACK:
3193 getWindow().setBackgroundDrawable(null);
3194 break;
3195 default:
3196 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3197 }
Craig Mautner360310b2012-10-26 15:13:08 -07003198 }
3199
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003200 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003201 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3202 int curflags = getWindow().getAttributes().flags
3203 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3204 if (wpflags != curflags) {
3205 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3206 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003207 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003208 }
3209
Michael Jurkae326f182011-11-21 14:05:46 -08003210 @Override
3211 public void onTrimMemory(int level) {
3212 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003213 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3214 // The widget preview db can result in holding onto over
3215 // 3MB of memory for caching which isn't necessary.
3216 SQLiteDatabase.releaseMemory();
3217
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003218 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003219 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003220 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003221 if (mLauncherCallbacks != null) {
3222 mLauncherCallbacks.onTrimMemory(level);
3223 }
Michael Jurkae326f182011-11-21 14:05:46 -08003224 }
3225
Winson5c6bdbb2015-09-03 11:36:19 -07003226 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003227 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003228 }
3229
Winson5c6bdbb2015-09-03 11:36:19 -07003230 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003231 boolean changed = mState != State.WORKSPACE ||
3232 mWorkspace.getState() != Workspace.State.NORMAL;
3233 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003234 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003235 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003236 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003237
Michael Jurkab3e22d92011-10-31 15:58:33 -07003238 // Set focus to the AppsCustomize button
3239 if (mAllAppsButton != null) {
3240 mAllAppsButton.requestFocus();
3241 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003242 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003243
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003244 // Change the state *after* we've called all the transition code
3245 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003246
Winson Chung5fb63472011-02-02 17:03:37 -08003247 // Resume the auto-advance of widgets
3248 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003249 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003250
Winson Chung0f785722015-04-08 10:27:49 -07003251 if (changed) {
3252 // Send an accessibility event to announce the context change
3253 getWindow().getDecorView()
3254 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003255 }
Winson5c6bdbb2015-09-03 11:36:19 -07003256 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003257 }
3258
Winsone9f27272015-10-13 10:47:51 -07003259 /**
3260 * Shows the overview button.
3261 */
Adam Cohened307df2013-10-02 09:37:31 -07003262 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003263 showOverviewMode(animated, false);
3264 }
3265
3266 /**
3267 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3268 * onto one of the overview panel buttons.
3269 */
3270 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3271 Runnable postAnimRunnable = null;
3272 if (requestButtonFocus) {
3273 postAnimRunnable = new Runnable() {
3274 @Override
3275 public void run() {
3276 // Hitting the menu button when in touch mode does not trigger touch mode to
3277 // be disabled, so if requested, force focus on one of the overview panel
3278 // buttons.
3279 mOverviewPanel.requestFocusFromTouch();
3280 }
3281 };
3282 }
Adam Cohened307df2013-10-02 09:37:31 -07003283 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003284 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003285 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003286 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003287 }
3288
Winson Chungb745afb2015-03-02 11:51:23 -08003289 /**
3290 * Shows the apps view.
3291 */
Winson Chung76648c52015-07-10 14:33:23 -07003292 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3293 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003294 if (resetListToTop) {
3295 mAppsView.scrollToTop();
3296 }
Winson Chung4ac30062015-05-08 17:34:17 -07003297 if (updatePredictedApps) {
3298 tryAndUpdatePredictedApps();
3299 }
Winson Chung76648c52015-07-10 14:33:23 -07003300 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003301 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003302
Winson Chungb745afb2015-03-02 11:51:23 -08003303 /**
3304 * Shows the widgets view.
3305 */
3306 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003307 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003308 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003309 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003310 }
Winson Chung76648c52015-07-10 14:33:23 -07003311 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003312
3313 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003314 @Override
3315 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003316 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003317 }
3318 });
Winson Chungb745afb2015-03-02 11:51:23 -08003319 }
3320
3321 /**
3322 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003323 *
3324 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003325 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003326 // TODO: calling method should use the return value so that when {@code false} is returned
3327 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003328 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003329 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3330 mState != State.WIDGETS_SPRING_LOADED) {
3331 return false;
3332 }
3333 if (toState != State.APPS && toState != State.WIDGETS) {
3334 return false;
3335 }
Winson Chungb745afb2015-03-02 11:51:23 -08003336
3337 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003338 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3339 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003340 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003341 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003342 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003345 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346
3347 // Pause the auto-advance of widgets until we are out of AllApps
3348 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003349 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350 closeFolder();
3351
3352 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003353 getWindow().getDecorView()
3354 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003355 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 }
3357
Winson Chungcd99cd32015-04-29 11:03:24 -07003358 /**
3359 * Updates the workspace and interaction state on state change, and return the animation to this
3360 * new state.
3361 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003362 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003363 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003364 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003365 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003366 updateInteraction(fromState, toState);
3367 return anim;
3368 }
3369
Jun Mukaif0033da2015-08-04 18:34:30 -07003370 public void onLauncherClingShown() {
3371 // When a launcher cling appears, it should cover the underlying layers, so their focus
3372 // should be blocked.
3373 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3374 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3375 }
3376 }
3377
3378 public void onLauncherClingDismissed() {
3379 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3380 }
3381
Hyunyoung Song3f471442015-04-08 19:01:34 -07003382 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003383 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003384 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003385 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003386 }
Winson Chungb745afb2015-03-02 11:51:23 -08003387
Winson Chung006ee262015-08-03 14:40:11 -07003388 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003389 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003390 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003391
3392 if (isAppsViewVisible()) {
3393 mState = State.APPS_SPRING_LOADED;
3394 } else if (isWidgetsViewVisible()) {
3395 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003396 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003397 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003398 } else {
3399 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003400 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003401 }
Adam Cohen7777d962011-08-18 18:58:38 -07003402
Hyunyoung Song3f471442015-04-08 19:01:34 -07003403 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003404 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003405 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003406
Winson Chunge7a03942011-08-05 15:05:12 -07003407 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003408 @Override
3409 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003410 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003411 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3412 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003413 // Before we show workspace, hide all apps again because
3414 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3415 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003417 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003418 } else {
3419 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003420 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003421 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003422 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003423 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424
Tony Wickhame0c33232016-02-08 11:37:04 -08003425 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003426 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3427 || mState == State.WIDGETS_SPRING_LOADED;
3428 }
3429
Michael Jurkad3ef3062010-11-23 16:23:58 -08003430 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003431 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003432 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003433 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003434 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003435 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003436 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3437 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003438 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003439 }
3440
Winson Chung4ac30062015-05-08 17:34:17 -07003441 /**
3442 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3443 * resumed.
3444 */
3445 private void tryAndUpdatePredictedApps() {
3446 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003447 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003448 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003449 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003450 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003451 }
3452 }
3453 }
3454
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455 void lockAllApps() {
3456 // TODO
3457 }
3458
3459 void unlockAllApps() {
3460 // TODO
3461 }
3462
Winsonf768d932015-09-25 16:12:35 -07003463 public boolean launcherCallbacksProvidesSearch() {
3464 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3465 }
3466
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003467 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003468 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003469 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003470 }
3471
Adam Cohen24ce0b32014-01-14 16:18:14 -08003472 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003473 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3474 if (searchProvider == null) {
3475 return null;
3476 }
3477
3478 Bundle opts = new Bundle();
3479 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003480 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003481
Tony Wickham3a3517f2015-10-06 11:27:04 -07003482 // Determine the min and max dimensions of the widget.
3483 LauncherAppState app = LauncherAppState.getInstance();
3484 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3485 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3486 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003487 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3488 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003489 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003490 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003491 int minWidth = maxWidth;
3492 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003493 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3494 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3495 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3496 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3497 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003498 }
3499 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3500 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3501 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3502 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003503 if (LOGD) {
3504 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3505 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3506 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003507
Tony Wickham775455c2015-10-16 09:49:32 -07003508 if (mLauncherCallbacks != null) {
3509 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3510 }
3511
Sunny Goyalf7258242015-10-19 16:59:07 -07003512 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003513 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003514 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003515 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003516 || (widgetInfo == null)
3517 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003518 // A valid widget is not already bound.
3519 if (widgetId > -1) {
3520 mAppWidgetHost.deleteAppWidgetId(widgetId);
3521 widgetId = -1;
3522 }
3523
3524 // Try to bind a new widget
3525 widgetId = mAppWidgetHost.allocateAppWidgetId();
3526
3527 if (!AppWidgetManagerCompat.getInstance(this)
3528 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3529 mAppWidgetHost.deleteAppWidgetId(widgetId);
3530 widgetId = -1;
3531 }
3532
Sunny Goyalf7258242015-10-19 16:59:07 -07003533 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003534 .putInt(QSB_WIDGET_ID, widgetId)
3535 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003536 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003537 }
3538
Sunny Goyal8d595092015-07-06 12:22:14 -07003539 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003540 if (widgetId != -1) {
3541 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003542 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003543 mQsb.updateAppWidgetOptions(opts);
3544 mQsb.setPadding(0, 0, 0, 0);
3545 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003546 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003547 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003548 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003549 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003550 }
3551
Sunny Goyal22235bc2015-04-03 09:23:43 -07003552 private void reinflateQSBIfNecessary() {
3553 if (mQsb instanceof LauncherAppWidgetHostView &&
3554 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3555 mSearchDropTargetBar.removeView(mQsb);
3556 mQsb = null;
3557 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3558 }
3559 }
3560
Michael Jurkad7c28052012-04-27 15:43:36 -07003561 @Override
3562 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003563 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003564 final List<CharSequence> text = event.getText();
3565 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003566 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003567 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003568 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003569 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003570 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003571 } else if (mWorkspace != null) {
3572 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003573 } else {
3574 text.add(getString(R.string.all_apps_home_button_label));
3575 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003576 return result;
3577 }
3578
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003579 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003580 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003581 */
Adam Cohen091440a2015-03-18 14:16:05 -07003582 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003583 @Override
3584 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003585 closeSystemDialogs();
3586 }
3587 }
3588
3589 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 * If the activity is currently paused, signal that we need to run the passed Runnable
3591 * in onResume.
3592 *
3593 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003594 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3595 * wrong when we're not running, and if the activity comes back to what the configuration was
3596 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003597 *
3598 * Implementation of the method from LauncherModel.Callbacks.
3599 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003600 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003602 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003603 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003604 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003605 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003606 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003607 }
3608 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003609 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003610 return true;
3611 } else {
3612 return false;
3613 }
3614 }
3615
Michael Jurkac402cd92013-05-20 15:49:32 +02003616 private boolean waitUntilResume(Runnable run) {
3617 return waitUntilResume(run, false);
3618 }
3619
Michael Jurka1e2f4652013-07-08 18:03:46 -07003620 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003621 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003622 }
3623
Michael Jurka7607c2f2013-04-03 14:33:19 -07003624 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003625 * If the activity is currently paused, signal that we need to re-run the loader
3626 * in onResume.
3627 *
3628 * This needs to be called from incoming places where resources might have been loaded
3629 * while we are paused. That is becaues the Configuration might be wrong
3630 * when we're not running, and if it comes back to what it was when we
3631 * were paused, we are not restarted.
3632 *
3633 * Implementation of the method from LauncherModel.Callbacks.
3634 *
3635 * @return true if we are currently paused. The caller might be able to
3636 * skip some work in that case since we will come back again.
3637 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003638 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003639 public boolean setLoadOnResume() {
3640 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003641 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003642 mOnResumeNeedsLoad = true;
3643 return true;
3644 } else {
3645 return false;
3646 }
3647 }
3648
3649 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003651 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003652 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003654 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003655 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003656 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003657 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003658 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003660
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003662 * Clear any pending bind callbacks. This is called when is loader is planning to
3663 * perform a full rebind from scratch.
3664 */
3665 @Override
3666 public void clearPendingBinds() {
3667 mBindOnResumeCallbacks.clear();
3668 if (mPendingExecutor != null) {
3669 mPendingExecutor.markCompleted();
3670 mPendingExecutor = null;
3671 }
3672 }
3673
3674 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 * Refreshes the shortcuts shown on the workspace.
3676 *
3677 * Implementation of the method from LauncherModel.Callbacks.
3678 */
3679 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003680 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003681
Winson Chungd64d1762013-08-20 14:37:16 -07003682 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003683 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003684 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003685
Michael Jurka05bf644e2011-11-30 20:28:53 -08003686 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003687 if (mHotseat != null) {
3688 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003689 }
3690 }
3691
Adam Cohendcd297f2013-06-18 13:13:40 -07003692 @Override
3693 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003694 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003695
Adam Cohen5084cba2013-09-03 12:01:16 -07003696 // If there are no screens, we need to have an empty screen
3697 if (orderedScreenIds.size() == 0) {
3698 mWorkspace.addExtraEmptyScreen();
3699 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003700
3701 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003702 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003703 if (hasCustomContentToLeft()) {
3704 mWorkspace.createCustomContentContainer();
3705 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003706 }
Winson Chung64359a52013-07-08 17:17:08 -07003707 }
3708
3709 @Override
3710 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003711 int count = orderedScreenIds.size();
3712 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003713 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003714 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003715 }
3716
Winson Chungd64d1762013-08-20 14:37:16 -07003717 public void bindAppsAdded(final ArrayList<Long> newScreens,
3718 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003719 final ArrayList<ItemInfo> addAnimated,
3720 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003721 Runnable r = new Runnable() {
3722 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003723 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003724 }
3725 };
3726 if (waitUntilResume(r)) {
3727 return;
3728 }
3729
Winson Chungd64d1762013-08-20 14:37:16 -07003730 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003731 if (newScreens != null) {
3732 bindAddScreens(newScreens);
3733 }
Winson Chungd64d1762013-08-20 14:37:16 -07003734
3735 // We add the items without animation on non-visible pages, and with
3736 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003737 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003738 bindItems(addNotAnimated, 0,
3739 addNotAnimated.size(), false);
3740 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003741 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003742 bindItems(addAnimated, 0,
3743 addAnimated.size(), true);
3744 }
Winson Chungc58497e2013-09-03 17:48:37 -07003745
Winson Chung87412982013-10-03 18:34:14 -07003746 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003747 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003748
Winson Chungb745afb2015-03-02 11:51:23 -08003749 if (addedApps != null && mAppsView != null) {
3750 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003751 }
Winson Chungd64d1762013-08-20 14:37:16 -07003752 }
3753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003755 * Bind the items start-end from the list.
3756 *
3757 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003759 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003760 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3761 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003762 Runnable r = new Runnable() {
3763 public void run() {
3764 bindItems(shortcuts, start, end, forceAnimateIcons);
3765 }
3766 };
3767 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003768 return;
3769 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003770
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003772 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3773 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003774 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003776 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003777 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003779
3780 // Short circuit if we are loading dock items for a configuration which has no dock
3781 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3782 mHotseat == null) {
3783 continue;
3784 }
3785
Sunny Goyal639e9062015-08-19 19:17:06 -07003786 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 switch (item.itemType) {
3788 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3789 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003790 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003791 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003792
Winson Chung64359a52013-07-08 17:17:08 -07003793 /*
3794 * TODO: FIX collision case
3795 */
Winson Chungce376632013-07-11 16:12:41 -07003796 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3797 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3798 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003799 View v = cl.getChildAt(item.cellX, item.cellY);
3800 Object tag = v.getTag();
3801 String desc = "Collision while binding workspace item: " + item
3802 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003803 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003804 throw (new RuntimeException(desc));
3805 } else {
3806 Log.d(TAG, desc);
3807 }
Winson Chungce376632013-07-11 16:12:41 -07003808 }
Winson Chung64359a52013-07-08 17:17:08 -07003809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003811 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003812 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003813 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003814 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003816 default:
3817 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003819
3820 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3821 item.cellY, 1, 1);
3822 if (animateIcons) {
3823 // Animate all the applications up now
3824 view.setAlpha(0f);
3825 view.setScaleX(0f);
3826 view.setScaleY(0f);
3827 bounceAnims.add(createNewAppBounceAnimation(view, i));
3828 newShortcutsScreenId = item.screenId;
3829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003830 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003831
Winson Chung997a9232013-07-24 15:33:46 -07003832 if (animateIcons) {
3833 // Animate to the correct page
3834 if (newShortcutsScreenId > -1) {
3835 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003836 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003837 final Runnable startBounceAnimRunnable = new Runnable() {
3838 public void run() {
3839 anim.playTogether(bounceAnims);
3840 anim.start();
3841 }
3842 };
Winson Chung997a9232013-07-24 15:33:46 -07003843 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003844 // We post the animation slightly delayed to prevent slowdowns
3845 // when we are loading right after we return to launcher.
3846 mWorkspace.postDelayed(new Runnable() {
3847 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003848 if (mWorkspace != null) {
3849 mWorkspace.snapToPage(newScreenIndex);
3850 mWorkspace.postDelayed(startBounceAnimRunnable,
3851 NEW_APPS_ANIMATION_DELAY);
3852 }
Winson Chung94d67682013-09-25 16:29:40 -07003853 }
3854 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003855 } else {
3856 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003857 }
3858 }
Winson Chung64359a52013-07-08 17:17:08 -07003859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003861 }
3862
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003863 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3864 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3865 view.updateIcon(mIconCache);
3866 item.hostView = view;
3867 item.hostView.updateAppWidget(null);
3868 item.hostView.setOnClickListener(this);
3869 addAppWidgetToWorkspace(item, null, false);
3870 mWorkspace.requestLayout();
3871 }
3872
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 /**
3874 * Add the views for a widget to the workspace.
3875 *
3876 * Implementation of the method from LauncherModel.Callbacks.
3877 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003878 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003879 Runnable r = new Runnable() {
3880 public void run() {
3881 bindAppWidget(item);
3882 }
3883 };
3884 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003885 return;
3886 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003887
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 if (mIsSafeModeEnabled) {
3889 bindSafeModeWidget(item);
3890 return;
3891 }
3892
Daniel Sandler843e8602010-06-07 14:59:01 -04003893 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3894 if (DEBUG_WIDGETS) {
3895 Log.d(TAG, "bindAppWidget: " + item);
3896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003898 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003899
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003900 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3901 // If the provider is not ready, bind as a pending widget.
3902 appWidgetInfo = null;
3903 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3904 // The widget id is not valid. Try to find the widget based on the provider info.
3905 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3906 } else {
3907 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3908 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003909
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003910 // If the provider is ready, but the width is not yet restored, try to restore it.
3911 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3912 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003913 if (appWidgetInfo == null) {
3914 if (DEBUG_WIDGETS) {
3915 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3916 + " belongs to component " + item.providerName
3917 + ", as the povider is null");
3918 }
3919 LauncherModel.deleteItemFromDatabase(this, item);
3920 return;
3921 }
Sunny Goyalff572272014-07-23 13:58:07 -07003922
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003923 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003924 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003925 // Note: This assumes that the id remap broadcast is received before this step.
3926 // If that is not the case, the id remap will be ignored and user may see the
3927 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003928 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003929 pendingInfo.spanX = item.spanX;
3930 pendingInfo.spanY = item.spanY;
3931 pendingInfo.minSpanX = item.minSpanX;
3932 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003933 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003934
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003935 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3936 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3937 newWidgetId, appWidgetInfo, options);
3938
3939 // TODO consider showing a permission dialog when the widget is clicked.
3940 if (!success) {
3941 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3942 if (DEBUG_WIDGETS) {
3943 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3944 + " belongs to component " + item.providerName
3945 + ", as the launcher is unable to bing a new widget id");
3946 }
3947 LauncherModel.deleteItemFromDatabase(this, item);
3948 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003949 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003950
3951 item.appWidgetId = newWidgetId;
3952
3953 // If the widget has a configure activity, it is still needs to set it up, otherwise
3954 // the widget is ready to go.
3955 item.restoreStatus = (appWidgetInfo.configure == null)
3956 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3957 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3958
3959 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003960 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003961 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003962 // The widget was marked as UI not ready, but there is no configure activity to
3963 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003964 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3965 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003966 }
Sunny Goyalff572272014-07-23 13:58:07 -07003967 }
3968
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003969 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003970 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003971 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3972 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003973 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003974
Sunny Goyal56c73602015-09-25 12:55:01 -07003975 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003976 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003977 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003978 deleteWidgetInfo(item);
3979 return;
3980 }
3981
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003982 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003983 item.minSpanX = appWidgetInfo.minSpanX;
3984 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003985 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003986 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003987 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3988 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003989 view.updateIcon(mIconCache);
3990 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003991 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003992 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003993 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003994 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003995 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Daniel Sandler843e8602010-06-07 14:59:01 -04003997 if (DEBUG_WIDGETS) {
3998 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3999 + (SystemClock.uptimeMillis()-start) + "ms");
4000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 }
4002
Sunny Goyalff572272014-07-23 13:58:07 -07004003 /**
4004 * Restores a pending widget.
4005 *
4006 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004007 */
4008 private void completeRestoreAppWidget(final int appWidgetId) {
4009 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4010 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4011 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4012 return;
4013 }
4014
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004015 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004016 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4017
4018 mWorkspace.reinflateWidgetsIfNecessary();
4019 LauncherModel.updateItemInDatabase(this, info);
4020 }
4021
Adam Cohen1462de32012-07-24 22:34:36 -07004022 public void onPageBoundSynchronously(int page) {
4023 mSynchronouslyBoundPages.add(page);
4024 }
4025
Sunny Goyal527c7d32015-08-28 15:19:36 -07004026 @Override
4027 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4028 if (mPendingExecutor != null) {
4029 mPendingExecutor.markCompleted();
4030 }
4031 mPendingExecutor = executor;
4032 executor.attachTo(this);
4033 }
4034
4035 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4036 if (mPendingExecutor == executor) {
4037 mPendingExecutor = null;
4038 }
4039 }
4040
Joe Onorato9c1289c2009-08-17 11:03:03 -04004041 /**
4042 * Callback saying that there aren't any more items to bind.
4043 *
4044 * Implementation of the method from LauncherModel.Callbacks.
4045 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004046 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004047 Runnable r = new Runnable() {
4048 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004049 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004050 }
4051 };
4052 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004053 return;
4054 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004055 if (mSavedState != null) {
4056 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004057 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004059
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 mSavedState = null;
4061 }
4062
Adam Cohen1462de32012-07-24 22:34:36 -07004063 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004065 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004066
4067 // If we received the result of any pending adds while the loader was running (e.g. the
4068 // widget configuration forced an orientation change), process them now.
4069 if (sPendingAddItem != null) {
4070 final long screenId = completeAdd(sPendingAddItem);
4071
4072 // TODO: this moves the user to the page where the pending item was added. Ideally,
4073 // the screen would be guaranteed to exist after bind, and the page would be set through
4074 // the workspace restore process.
4075 mWorkspace.post(new Runnable() {
4076 @Override
4077 public void run() {
4078 mWorkspace.snapToScreenId(screenId);
4079 }
4080 });
4081 sPendingAddItem = null;
4082 }
4083
Sunny Goyal756adbc2015-04-16 15:20:51 -07004084 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004085
4086 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004087 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004088 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004089 }
4090
Winson Chunga0b7e862013-09-05 16:03:15 -07004091 public boolean isAllAppsButtonRank(int rank) {
4092 if (mHotseat != null) {
4093 return mHotseat.isAllAppsButtonRank(rank);
4094 }
4095 return false;
4096 }
4097
Winson Chunga2413752012-04-03 14:22:34 -07004098 private boolean canRunNewAppsAnimation() {
4099 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004100 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4101 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004102 }
4103
Winson Chungc9168342013-06-26 14:54:55 -07004104 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004105 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004106 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4107 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004108 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004109 return bounceAnim;
4110 }
4111
Adam Cohen27772732013-11-11 14:00:56 +00004112 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004113 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004114 }
4115
Tony Wickham3a3517f2015-10-06 11:27:04 -07004116 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004117 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004118 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004119 }
4120
Tony Wickham55616cd2015-09-23 14:55:17 -07004121 public int getSearchBarHeight() {
4122 if (mLauncherCallbacks != null) {
4123 return mLauncherCallbacks.getSearchBarHeight();
4124 }
4125 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4126 }
4127
Winson Chung88fa7412015-08-03 14:25:28 -07004128 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004129 if (mSearchDropTargetBar == null) {
4130 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004131 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004132 if (mQsb != null) {
4133 mSearchDropTargetBar.removeView(mQsb);
4134 mQsb = null;
4135 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004136 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004137 }
4138
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004139 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004140 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4141 * multiple calls to bind the same list.)
4142 */
4143 @Thunk ArrayList<AppInfo> mTmpAppsList;
4144 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4145 public void run() {
4146 bindAllApplications(mTmpAppsList);
4147 mTmpAppsList = null;
4148 }
4149 };
4150
4151 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 * Add the icons for all apps.
4153 *
4154 * Implementation of the method from LauncherModel.Callbacks.
4155 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004156 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004157 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4158 mTmpAppsList = apps;
4159 return;
4160 }
4161
Winson Chungb745afb2015-03-02 11:51:23 -08004162 if (mAppsView != null) {
4163 mAppsView.setApps(apps);
4164 }
Adam Cohen9211d422014-10-07 18:14:53 -07004165 if (mLauncherCallbacks != null) {
4166 mLauncherCallbacks.bindAllApplications(apps);
4167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004168 }
4169
4170 /**
4171 * A package was updated.
4172 *
4173 * Implementation of the method from LauncherModel.Callbacks.
4174 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004175 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004176 Runnable r = new Runnable() {
4177 public void run() {
4178 bindAppsUpdated(apps);
4179 }
4180 };
4181 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004182 return;
4183 }
4184
Winson Chungb745afb2015-03-02 11:51:23 -08004185 if (mAppsView != null) {
4186 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 }
4189
Sunny Goyal4390ace2014-10-13 11:33:11 -07004190 @Override
4191 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4192 Runnable r = new Runnable() {
4193 public void run() {
4194 bindWidgetsRestored(widgets);
4195 }
4196 };
4197 if (waitUntilResume(r)) {
4198 return;
4199 }
4200 mWorkspace.widgetsRestored(widgets);
4201 }
4202
Joe Onorato9c1289c2009-08-17 11:03:03 -04004203 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004204 * Some shortcuts were updated in the background.
4205 *
4206 * Implementation of the method from LauncherModel.Callbacks.
4207 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004208 @Override
4209 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4210 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004211 Runnable r = new Runnable() {
4212 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004213 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004214 }
4215 };
4216 if (waitUntilResume(r)) {
4217 return;
4218 }
4219
Sunny Goyal4390ace2014-10-13 11:33:11 -07004220 if (!updated.isEmpty()) {
4221 mWorkspace.updateShortcuts(updated);
4222 }
4223
4224 if (!removed.isEmpty()) {
4225 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4226 for (ShortcutInfo si : removed) {
4227 removedComponents.add(si.getTargetComponent());
4228 }
4229 mWorkspace.removeItemsByComponentName(removedComponents, user);
4230 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004231 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004232 }
4233 }
4234
4235 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004236 * Update the state of a package, typically related to install state.
4237 *
4238 * Implementation of the method from LauncherModel.Callbacks.
4239 */
Sunny Goyale755d462014-07-22 13:48:29 -07004240 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004241 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4242 Runnable r = new Runnable() {
4243 public void run() {
4244 bindRestoreItemsChange(updates);
4245 }
4246 };
4247 if (waitUntilResume(r)) {
4248 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004249 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004250
Sunny Goyal756adbc2015-04-16 15:20:51 -07004251 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004252 }
4253
4254 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004255 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004256 * in addition to specific applications to remove, the reason being that
4257 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004258 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004259 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004261 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004262 public void bindWorkspaceComponentsRemoved(
4263 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4264 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004265 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004266 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004267 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004268 }
Winson Chungd64d1762013-08-20 14:37:16 -07004269 };
4270 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004271 return;
4272 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004273 if (!packageNames.isEmpty()) {
4274 mWorkspace.removeItemsByPackageName(packageNames, user);
4275 }
4276 if (!components.isEmpty()) {
4277 mWorkspace.removeItemsByComponentName(components, user);
4278 }
4279 // Notify the drag controller
4280 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004281
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004282 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004283
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004284 @Override
4285 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4286 Runnable r = new Runnable() {
4287 public void run() {
4288 bindAppInfosRemoved(appInfos);
4289 }
4290 };
4291 if (waitUntilResume(r)) {
4292 return;
Joe Onorato36115782010-06-17 13:28:48 -04004293 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004294
Winson Chungdf95eb12013-10-16 14:57:07 -07004295 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004296 if (mAppsView != null) {
4297 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004298 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004299 }
Joe Onoratobe386092009-11-17 17:32:16 -08004300
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004301 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004302 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004303 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004304 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004305 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004306
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004307 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004308 public void bindWidgetsModel(WidgetsModel model) {
4309 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004310 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004311 return;
4312 }
4313
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004314 if (mWidgetsView != null && model != null) {
4315 mWidgetsView.addWidgets(model);
4316 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004317 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004318 }
4319
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004320 @Override
4321 public void notifyWidgetProvidersChanged() {
4322 if (mWorkspace.getState().shouldUpdateWidget) {
4323 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4324 }
4325 }
4326
Winson Chung400438b2011-01-16 17:53:48 -08004327 private int mapConfigurationOriActivityInfoOri(int configOri) {
4328 final Display d = getWindowManager().getDefaultDisplay();
4329 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4330 switch (d.getRotation()) {
4331 case Surface.ROTATION_0:
4332 case Surface.ROTATION_180:
4333 // We are currently in the same basic orientation as the natural orientation
4334 naturalOri = configOri;
4335 break;
4336 case Surface.ROTATION_90:
4337 case Surface.ROTATION_270:
4338 // We are currently in the other basic orientation to the natural orientation
4339 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4340 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4341 break;
4342 }
4343
4344 int[] oriMap = {
4345 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4346 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4347 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4348 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4349 };
4350 // Since the map starts at portrait, we need to offset if this device's natural orientation
4351 // is landscape.
4352 int indexOffset = 0;
4353 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4354 indexOffset = 1;
4355 }
4356 return oriMap[(d.getRotation() + indexOffset) % 4];
4357 }
Adam Cohen446e9402011-09-15 18:21:21 -07004358
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004359 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004360 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004361 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004362 if (Utilities.ATLEAST_JB_MR2) {
4363 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4364 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004365 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4366 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004367 }
Winson Chung4b919f82012-05-01 10:44:08 -07004368 }
4369 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004370
Winson Chung4b919f82012-05-01 10:44:08 -07004371 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004372 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004373 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004374 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004375 } else {
4376 mHandler.postDelayed(new Runnable() {
4377 public void run() {
4378 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4379 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004380 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004381 }
Winson Chung4b919f82012-05-01 10:44:08 -07004382 }
Winson Chung400438b2011-01-16 17:53:48 -08004383 }
4384
Winson Chunge43a1e72014-01-15 10:33:02 -08004385 protected boolean isLauncherPreinstalled() {
4386 PackageManager pm = getPackageManager();
4387 try {
4388 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4389 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4390 return true;
4391 } else {
4392 return false;
4393 }
4394 } catch (NameNotFoundException e) {
4395 e.printStackTrace();
4396 return false;
4397 }
4398 }
4399
Adam Cohenea90f832014-05-21 15:03:34 -07004400 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004401 * To be overridden by subclasses to indicate that there is an activity to launch
4402 * before showing the standard launcher experience.
4403 */
4404 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004405 if (mLauncherCallbacks != null) {
4406 return mLauncherCallbacks.hasFirstRunActivity();
4407 }
Adam Cohen432609a2014-03-13 17:03:22 -07004408 return false;
4409 }
4410
4411 /**
4412 * To be overridden by subclasses to launch any first run activity
4413 */
4414 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004415 if (mLauncherCallbacks != null) {
4416 return mLauncherCallbacks.getFirstRunActivity();
4417 }
Adam Cohen432609a2014-03-13 17:03:22 -07004418 return null;
4419 }
4420
Winson Chunga6945242014-01-08 14:04:34 -08004421 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004422 return !ActivityManager.isRunningInTestHarness() &&
4423 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004424 }
4425
Adam Cohen4a9423d2014-03-28 14:22:31 -07004426 protected boolean hasRunFirstRunActivity() {
4427 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4428 }
4429
Adam Cohen432609a2014-03-13 17:03:22 -07004430 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004431 if (shouldRunFirstRunActivity() &&
4432 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004433 Intent firstRunIntent = getFirstRunActivity();
4434 if (firstRunIntent != null) {
4435 startActivity(firstRunIntent);
4436 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004437 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439 }
Adam Cohen432609a2014-03-13 17:03:22 -07004440 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004441 }
4442
4443 private void markFirstRunActivityShown() {
4444 SharedPreferences.Editor editor = mSharedPrefs.edit();
4445 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4446 editor.apply();
4447 }
4448
Adam Cohen432609a2014-03-13 17:03:22 -07004449 /**
4450 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4451 * screen that must be displayed and dismissed.
4452 */
4453 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.hasDismissableIntroScreen();
4456 }
Adam Cohen432609a2014-03-13 17:03:22 -07004457 return false;
4458 }
4459
4460 /**
4461 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4462 */
4463 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004464 if (mLauncherCallbacks != null) {
4465 return mLauncherCallbacks.getIntroScreen();
4466 }
Adam Cohen432609a2014-03-13 17:03:22 -07004467 return null;
4468 }
4469
4470 /**
4471 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4472 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4473 */
4474 private boolean shouldShowIntroScreen() {
4475 return hasDismissableIntroScreen() &&
4476 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4477 }
4478
4479 protected void showIntroScreen() {
4480 View introScreen = getIntroScreen();
4481 changeWallpaperVisiblity(false);
4482 if (introScreen != null) {
4483 mDragLayer.showOverlayView(introScreen);
4484 }
4485 }
4486
4487 public void dismissIntroScreen() {
4488 markIntroScreenDismissed();
4489 if (showFirstRunActivity()) {
4490 // We delay hiding the intro view until the first run activity is showing. This
4491 // avoids a blip.
4492 mWorkspace.postDelayed(new Runnable() {
4493 @Override
4494 public void run() {
4495 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004496 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004497 }
4498 }, ACTIVITY_START_DELAY);
4499 } else {
4500 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004501 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004502 }
4503 changeWallpaperVisiblity(true);
4504 }
4505
4506 private void markIntroScreenDismissed() {
4507 SharedPreferences.Editor editor = mSharedPrefs.edit();
4508 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4509 editor.apply();
4510 }
4511
Adam Cohen091440a2015-03-18 14:16:05 -07004512 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004513 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4514 // on the device, then we always show the first run cling experience (or if there is no
4515 // launcher2). Otherwise, we prompt the user upon started for migration
4516 LauncherClings launcherClings = new LauncherClings(this);
4517 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004518 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004519 if (mModel.canMigrateFromOldLauncherDb(this)) {
4520 launcherClings.showMigrationCling();
4521 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004522 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004523 }
4524 }
4525 }
4526
Winson Chunga6945242014-01-08 14:04:34 -08004527 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004528 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4529 if (mHotseat != null) mHotseat.setAlpha(1f);
4530 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004531 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4532 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004533 }
4534
4535 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004536 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4537 if (mHotseat != null) mHotseat.setAlpha(0f);
4538 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004539 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4540 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004541 }
4542
Winson Chung5ffd51d2015-06-25 11:36:50 -07004543 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004544 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004545 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004546 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004547 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004548 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004549 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4550 if (userHandle != null) {
4551 user = UserHandleCompat.fromUser(userHandle);
4552 }
4553 }
4554 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004555 }
4556
4557 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004558 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004559 if (user == null) {
4560 user = UserHandleCompat.myUserHandle();
4561 }
4562
4563 // Called from search suggestion, add the profile extra to the intent to ensure that we
4564 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004565 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004566 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004567 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004568 return null;
4569 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004570 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004571 }
4572
Winson Chung5ffd51d2015-06-25 11:36:50 -07004573 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004574 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4575 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004576 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004577 UserHandleCompat.myUserHandle());
4578 }
4579
Winson Chung5ffd51d2015-06-25 11:36:50 -07004580 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004581 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4582 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004583 mWorkspace.onExternalDragStartedWithItem(dragView);
4584 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004585 }
4586
Winson Chung5ffd51d2015-06-25 11:36:50 -07004587 protected void moveWorkspaceToDefaultScreen() {
4588 mWorkspace.moveToDefaultScreen(false);
4589 }
4590
Winson Chung80baf5a2010-08-09 16:03:15 -07004591 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004592 * Returns a FastBitmapDrawable with the icon, accurately sized.
4593 */
4594 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4595 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4596 d.setFilterBitmap(true);
4597 resizeIconDrawable(d);
4598 return d;
4599 }
4600
4601 /**
4602 * Resizes an icon drawable to the correct icon size.
4603 */
Winson5fbe0742015-09-30 15:33:00 -07004604 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004605 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004606 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004607 }
4608
4609 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004610 * Prints out out state for debugging.
4611 */
4612 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004613 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004614 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004615 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4616 Log.d(TAG, "mRestoring=" + mRestoring);
4617 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004618 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004619 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004620
Daniel Sandler325dc232013-06-05 22:57:57 -04004621 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004622 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004623
4624 @Override
4625 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4626 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004627 // Dump workspace
4628 writer.println(prefix + "Workspace Items");
4629 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4630 writer.println(prefix + " Homescreen " + i);
4631
4632 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4633 for (int j = 0; j < layout.getChildCount(); j++) {
4634 Object tag = layout.getChildAt(j).getTag();
4635 if (tag != null) {
4636 writer.println(prefix + " " + tag.toString());
4637 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004638 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004639 }
Sunny Goyala1365452015-10-01 15:46:24 -07004640
4641 writer.println(prefix + " Hotseat");
4642 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4643 for (int j = 0; j < layout.getChildCount(); j++) {
4644 Object tag = layout.getChildAt(j).getTag();
4645 if (tag != null) {
4646 writer.println(prefix + " " + tag.toString());
4647 }
4648 }
4649
Sunny Goyal713edfc2016-05-06 09:58:34 -07004650 try {
4651 FileLog.flushAll(writer);
4652 } catch (Exception e) {
4653 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004654 }
4655
Adam Cohen9211d422014-10-07 18:14:53 -07004656 if (mLauncherCallbacks != null) {
4657 mLauncherCallbacks.dump(prefix, fd, writer, args);
4658 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004659 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004660
Adam Cohen59400422014-03-05 18:07:04 -08004661 public static CustomAppWidget getCustomAppWidget(String name) {
4662 return sCustomAppWidgets.get(name);
4663 }
4664
4665 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4666 return sCustomAppWidgets;
4667 }
4668
Sunny Goyal29538332016-02-18 09:10:19 -08004669 public static List<View> getFolderContents(View icon) {
4670 if (icon instanceof FolderIcon) {
4671 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4672 } else {
4673 return Collections.EMPTY_LIST;
4674 }
4675 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004676
4677 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4678
4679 @Override
4680 public void onSharedPreferenceChanged(
4681 SharedPreferences sharedPreferences, String key) {
4682 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4683 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4684 if (!waitUntilResume(this, true)) {
4685 run();
4686 }
4687 }
4688 }
4689
4690 @Override
4691 public void run() {
4692 setOrientation();
4693 }
4694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004695}
Michael Jurka2763be32011-02-24 11:19:57 -08004696
Dan Sandlerd5024042014-01-09 15:01:33 -05004697interface DebugIntents {
4698 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4699 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004700}