blob: 03b921bf342e278d546df39a4dd532d7baaffb23 [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;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700115import com.android.launcher3.pageindicators.PageIndicator;
116import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700119import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700120import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700121import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700122import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700125import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700127
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800132import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700134import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700135import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800136import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138/**
139 * Default launcher application.
140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100141public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700142 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
143 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700144 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700145 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700146 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700152 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
Dan Sandlerd5024042014-01-09 15:01:33 -0500154 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700159 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700166 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
167 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
168 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
169
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700170 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
171
Mathew Inwood876a8462013-06-14 14:12:41 +0100172 /**
173 * IntentStarter uses request codes starting with this. This must be greater than all activity
174 * request codes used internally.
175 */
176 protected static final int REQUEST_LAST = 100;
177
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700179 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800180 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Winson Chung2672ff92012-05-04 16:22:30 -0700182 // The Intent extra that defines whether to ignore the launch animation
183 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400184 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700190 // Type: Content Values / parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 private static final String QSB_WIDGET_ID = "qsb_widget_id";
201 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700229 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700238 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800239 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800240 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700241
Michael Jurka0280c3b2010-09-17 15:00:07 -0700242 private int[] mTmpAddItemCellCoordinates = new int[2];
243
Sunny Goyal316490e2015-06-02 09:38:28 -0700244 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800245 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700248 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249
Winson Chungc51db6a2011-10-05 11:44:49 -0700250 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700251 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700256 // Main container view and the model for the widget tray screen.
257 @Thunk WidgetsContainerView mWidgetsView;
258 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
Sunny Goyal594d76d2014-11-06 10:12:54 -0800260 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700279 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700283 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700286 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Sunny Goyal639e9062015-08-19 19:17:06 -0700289 private LauncherClings mClings;
290
Adam Cohen61f560d2013-09-30 15:58:20 -0700291 private View.OnTouchListener mHapticFeedbackTouchListener;
292
Adam Cohended9f8d2010-11-03 13:25:16 -0700293 // Related to the auto-advancing of widgets
294 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700295 private static final int ADVANCE_INTERVAL = 20000;
296 private static final int ADVANCE_STAGGER = 250;
297
298 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700299 private long mAutoAdvanceSentTime;
300 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700301 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700302
Winson Chung400438b2011-01-16 17:53:48 -0800303 // Determines how long to wait after a rotation before restoring the screen orientation to
304 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800306
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700308
Adam Cohen1462de32012-07-24 22:34:36 -0700309 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700310 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700311
Winson Chung46353de2012-02-16 14:05:10 -0800312 // We only want to get the SharedPreferences once since it does an FS stat each time we get
313 // it from the context.
314 private SharedPreferences mSharedPrefs;
315
Winson Chungf0c6ae02012-03-21 16:10:31 -0700316 // Holds the page that we need to animate to, and the icon views that we need to animate up
317 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700319 private Bitmap mFolderIconBitmap;
320 private Canvas mFolderIconCanvas;
321 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700323 private DeviceProfile mDeviceProfile;
324
Adam Cohen4b66bf32015-09-18 12:15:19 -0700325 private boolean mMoveToDefaultScreenFromNewIntent;
326
Winson Chung13eb5272015-05-11 16:30:13 -0700327 // This is set to the view that launched the activity that navigated the user away from
328 // launcher. Since there is no callback for when the activity has finished launching, enable
329 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800330 private BubbleTextView mWaitingForResume;
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
333 new HashMap<String, CustomAppWidget>();
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700336 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
337 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800338 }
339 }
340
Adam Cohen091440a2015-03-18 14:16:05 -0700341 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700342 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700343 if (mWorkspace != null) {
344 mWorkspace.buildPageHardwareLayers();
345 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 }
347 };
348
Adam Cohendb364c32014-05-20 14:23:40 -0700349 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352 int requestCode;
353 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700354 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int cellX;
357 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700358 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 }
360
Hyunyoung Songaa953652016-04-19 18:30:24 -0700361 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800362
Adam Cohenf9c184a2016-01-15 16:47:43 -0800363 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700364 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400365
366 @Thunk void setOrientation() {
367 if (mRotationEnabled) {
368 unlockScreenOrientation(true);
369 } else {
370 setRequestedOrientation(
371 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700372 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700374
Sunny Goyal745bad92016-05-02 10:54:12 -0700375 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 @Override
378 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700379 if (DEBUG_STRICT_MODE) {
380 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
381 .detectDiskReads()
382 .detectDiskWrites()
383 .detectNetwork() // or .detectAll() for all detectable problems
384 .penaltyLog()
385 .build());
386 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
387 .detectLeakedSqlLiteObjects()
388 .detectLeakedClosableObjects()
389 .penaltyLog()
390 .penaltyDeath()
391 .build());
392 }
393
Adam Cohen9211d422014-10-07 18:14:53 -0700394 if (mLauncherCallbacks != null) {
395 mLauncherCallbacks.preOnCreate();
396 }
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400399
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400400 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700401
Adam Cohen2e6da152015-05-06 11:42:25 -0700402 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700403 mDeviceProfile = getResources().getConfiguration().orientation
404 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700405 app.getInvariantDeviceProfile().landscapeProfile
406 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700407
Sunny Goyalf7258242015-10-19 16:59:07 -0700408 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700409 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800410 mModel = app.setLauncher(this);
411 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700412
Joe Onorato41a12d22009-10-31 18:30:00 -0400413 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700414 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700415
Sunny Goyalffe83f12014-08-14 17:39:34 -0700416 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700417
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
419 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
422 // this also ensures that any synchronous binding below doesn't re-trigger another
423 // LauncherModel load.
424 mPaused = false;
425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200427 android.os.Debug.startMethodTracing(
428 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700432
Tony Wickhameef44322015-10-20 13:24:36 -0700433 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
434 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700436 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700437 mExtractedColors = new ExtractedColors();
438 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439
Tony Wickhame2217252016-03-22 16:34:23 -0700440 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
441 .addAccessibilityStateChangeListener(this);
442
Joe Onorato7c312c12009-08-13 21:36:53 -0700443 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 mSavedState = savedInstanceState;
446 restoreState(mSavedState);
447
448 if (PROFILE_STARTUP) {
449 android.os.Debug.stopMethodTracing();
450 }
451
452 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700453 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 // If the user leaves launcher, then we should just load items asynchronously when
455 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700456 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 } else {
458 // We only load the page synchronously if the user rotates (or triggers a
459 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463
464 // For handling default keys
465 mDefaultKeySsb = new SpannableStringBuilder();
466 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800467
468 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
469 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800470
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800471 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700472 // In case we are on a device with locked rotation, we should look at preferences to check
473 // if the user has specifically allowed rotation.
474 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700475 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700476 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
477 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 }
479
480 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
481 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400482 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700483
Adam Cohen9211d422014-10-07 18:14:53 -0700484 if (mLauncherCallbacks != null) {
485 mLauncherCallbacks.onCreate(savedInstanceState);
486 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700487
488 if (shouldShowIntroScreen()) {
489 showIntroScreen();
490 } else {
491 showFirstRunActivity();
492 showFirstRunClings();
493 }
Adam Cohen9211d422014-10-07 18:14:53 -0700494 }
495
Sunny Goyal7779d622015-06-11 16:18:39 -0700496 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700497 public void onExtractedColorsChanged() {
498 loadExtractedColorsAndColorItems();
499 }
500
501 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700502 // TODO: do this in pre-N as well, once the extraction part is complete.
503 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700504 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700505 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickhamf549dab2016-05-16 09:54:06 -0700506 mPageIndicator.updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700507 }
508 }
509
Adam Cohen9211d422014-10-07 18:14:53 -0700510 private LauncherCallbacks mLauncherCallbacks;
511
512 public void onPostCreate(Bundle savedInstanceState) {
513 super.onPostCreate(savedInstanceState);
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onPostCreate(savedInstanceState);
516 }
517 }
518
Sunny Goyal32554d12015-12-03 15:31:25 -0800519 /**
520 * Call this after onCreate to set or clear overlay.
521 */
522 public void setLauncherOverlay(LauncherOverlay overlay) {
523 if (overlay != null) {
524 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
525 }
526 mWorkspace.setLauncherOverlay(overlay);
527 }
528
Adam Cohen9211d422014-10-07 18:14:53 -0700529 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
530 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700532 private boolean mWorkspaceImportanceStored = false;
533 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800535 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
537
538 @Override
539 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 return;
542 }
543 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700544 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspace != null) {
546 mWorkspaceImportanceForAccessibility =
547 mWorkspace.getImportantForAccessibility();
548 mWorkspace.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mWorkspaceImportanceStored = true;
551 }
552 if (mHotseat != null) {
553 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
554 mHotseat.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mHotseatImportanceStored = true;
557 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 }
559
560 @Override
561 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspaceImportanceStored && mWorkspace != null) {
563 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
564 }
565 if (mHotseatImportanceStored && mHotseat != null) {
566 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
567 }
568 mWorkspaceImportanceStored = false;
569 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571 });
Adam Cohen9211d422014-10-07 18:14:53 -0700572 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700573 }
574
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700576 public void onLauncherProviderChange() {
577 if (mLauncherCallbacks != null) {
578 mLauncherCallbacks.onLauncherProviderChange();
579 }
580 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581
Adam Cohen9211d422014-10-07 18:14:53 -0700582 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700583 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700584 if (mLauncherCallbacks != null) {
585 return mLauncherCallbacks.hasCustomContentToLeft();
586 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 return false;
588 }
589
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500591 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 * {@link #addToCustomContentPage}. This will only be invoked if
593 * {@link #hasCustomContentToLeft()} is {@code true}.
594 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.populateCustomContentContainer();
598 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 }
600
601 /**
602 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
603 * ensure the custom content page is added or removed if necessary.
604 */
605 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600606 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 // Not bound yet, wait for bindScreens to be called.
608 return;
609 }
610
611 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
612 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 mWorkspace.createCustomContentContainer();
614 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
616 mWorkspace.removeCustomContentPage();
617 }
618 }
619
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800620 /**
621 * Logger object is a singleton and does not have to be coupled with the foreground activity.
622 * Since most user event logging is done on the UI, the object is retrieved from the
623 * callback for convenience.
624 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700625 private UserEventDispatcher createUserEventDispatcher() {
626 return new UserEventDispatcher() {
627 @Override
628 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
629 if (!DEBUG_LOGGING) {
630 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800631 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 Log.d("UserEvent", String.format(Locale.US,
633 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
634 LoggerUtils.getActionStr(ev.action),
635 LoggerUtils.getTargetStr(ev.srcTarget[0]),
636 LoggerUtils.getTargetStr(ev.srcTarget[1]),
637 ev.elapsedContainerMillis,
638 ev.elapsedSessionMillis));
639 }
640 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
642
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 public UserEventDispatcher getUserEventDispatcher() {
644 if (mLauncherCallbacks != null) {
645 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
646 if (dispatcher != null) {
647 return dispatcher;
648 }
649 }
650
651 if (mUserEventDispatcher == null) {
652 mUserEventDispatcher = createUserEventDispatcher();
653 }
654 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800655 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100656
Hyunyoung Song3f471442015-04-08 19:01:34 -0700657 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
659 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700660 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 }
662
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000663 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700664 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
665 // This cast is safe as long as the id < 0x00FFFFFF
666 // Since we jail all the dynamically generated views, there should be no clashes
667 // with any other views.
668 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000669 }
670
671 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
673 * a configuration step, this allows the proper animations to run after other transitions.
674 */
Adam Cohendb364c32014-05-20 14:23:40 -0700675 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700676 long screenId = args.screenId;
677 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
678 // When the screen id represents an actual screen (as opposed to a rank) we make sure
679 // that the drop page actually exists.
680 screenId = ensurePendingDropLayoutExists(args.screenId);
681 }
Adam Cohendb364c32014-05-20 14:23:40 -0700682
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700685 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700689 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700691 case REQUEST_RECONFIGURE_APPWIDGET:
692 completeRestoreAppWidget(args.appWidgetId);
693 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700699 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800702 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700704 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700705 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700706 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
712 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
713 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
714 }
715 };
716
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700718 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
721 if (resultCode == RESULT_CANCELED) {
722 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800726 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700728
729 // When the user has granted permission to bind widgets, we should check to see if
730 // we can inflate the default search bar widget.
731 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 }
733 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
735 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700736 // User could have free-scrolled between pages before picking a wallpaper; make sure
737 // we move to the closest one now.
738 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700739 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740 }
741 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700745 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700746
Adam Cohendb364c32014-05-20 14:23:40 -0700747 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 // We have special handling for widgets
749 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800750 final int appWidgetId;
751 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
752 : -1;
753 if (widgetId < 0) {
754 appWidgetId = pendingAddWidgetId;
755 } else {
756 appWidgetId = widgetId;
757 }
758
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800760 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700761 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
762 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 result = RESULT_CANCELED;
764 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700765 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed(false, 0, null);
769 }
770 };
Adam Cohendb364c32014-05-20 14:23:40 -0700771 if (workspaceLocked) {
772 // No need to remove the empty screen if we're mid-binding, as the
773 // the bind will not add the empty screen.
774 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
775 } else {
776 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
777 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
778 }
Winson Chung5aaab772012-04-27 15:24:02 -0700779 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700780 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700781 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
782 // When the screen id represents an actual screen (as opposed to a rank)
783 // we make sure that the drop page actually exists.
784 mPendingAddInfo.screenId =
785 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
786 }
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
788
789 dropLayout.setDropPending(true);
790 final Runnable onComplete = new Runnable() {
791 @Override
792 public void run() {
793 completeTwoStageWidgetDrop(resultCode, appWidgetId);
794 dropLayout.setDropPending(false);
795 }
796 };
797 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
799 } else {
800 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
801 mPendingAddInfo);
802 sPendingAddItem = args;
803 }
Winson Chung5aaab772012-04-27 15:24:02 -0700804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 return;
806 }
807
Sunny Goyalff572272014-07-23 13:58:07 -0700808 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
809 if (resultCode == RESULT_OK) {
810 // Update the widget view.
811 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
812 pendingAddWidgetId, mPendingAddInfo);
813 if (workspaceLocked) {
814 sPendingAddItem = args;
815 } else {
816 completeAdd(args);
817 }
818 }
819 // Leave the widget in the pending state if the user canceled the configure.
820 return;
821 }
822
Sunny Goyalaad90582015-07-09 14:22:50 -0700823 if (requestCode == REQUEST_CREATE_SHORTCUT) {
824 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
825 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
826 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
827 mPendingAddInfo);
828 if (isWorkspaceLocked()) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
834 }
835 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800841
842 }
843
844 @Override
845 protected void onActivityResult(
846 final int requestCode, final int resultCode, final Intent data) {
847 handleActivityResult(requestCode, resultCode, data);
848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
850 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800851 }
852
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700854 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700856 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
857 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
858 View v = null;
859 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
860 if (layout != null) {
861 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
862 }
863 Intent intent = sPendingAddItem.intent;
864 sPendingAddItem = null;
865 if (grantResults.length > 0
866 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
867 startActivity(v, intent, null);
868 } else {
869 // TODO: Show a snack bar with link to settings
870 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
871 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
872 }
873 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
876 grantResults);
877 }
878 }
879
Adam Cohendb364c32014-05-20 14:23:40 -0700880 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
881 appWidgetId, ItemInfo info) {
882 PendingAddArguments args = new PendingAddArguments();
883 args.requestCode = requestCode;
884 args.intent = data;
885 args.container = info.container;
886 args.screenId = info.screenId;
887 args.cellX = info.cellX;
888 args.cellY = info.cellY;
889 args.appWidgetId = appWidgetId;
890 return args;
891 }
892
893 /**
894 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
895 *
896 * @param screenId the screen id to check
897 * @return the new screen, or screenId if it exists
898 */
899 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800900 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800912 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.preOnResume();
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700978 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700979
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700981 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700982 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800983 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700984 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700985 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700986 // view after launching an app, as they may be depending on the UI to be static to
987 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700988 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700989 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800990 } else if (mOnResumeState == State.WIDGETS) {
991 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 }
993 mOnResumeState = State.NONE;
994
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700995 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700996 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
997 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700998
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800999 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001001 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001002 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1008 // execute them here
1009 long startTimeCallbacks = 0;
1010 if (DEBUG_RESUME_TIME) {
1011 startTimeCallbacks = System.currentTimeMillis();
1012 }
1013
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1015 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001018 if (DEBUG_RESUME_TIME) {
1019 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1020 (System.currentTimeMillis() - startTimeCallbacks));
1021 }
Michael Jurka447bf842013-05-15 14:52:15 +02001022 }
Michael Jurka54554252013-08-01 12:52:23 +02001023 if (mOnResumeCallbacks.size() > 0) {
1024 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1025 mOnResumeCallbacks.get(i).run();
1026 }
1027 mOnResumeCallbacks.clear();
1028 }
Winson Chunge4e50662012-01-23 14:45:13 -08001029
1030 // Reset the pressed state of icons that were locked in the press state while activities
1031 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001032 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001033 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 mWaitingForResume.setStayPressed(false);
1035 }
Winson Chung82963d52013-10-09 11:20:57 -07001036
Adam Cohen06dff352012-06-01 17:17:08 -07001037 // It is possible that widgets can receive updates while launcher is not in the foreground.
1038 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1039 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1040 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001041 if (!isWorkspaceLoading()) {
1042 getWorkspace().reinflateWidgetsIfNecessary();
1043 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001044 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001045
Michael Jurka447bf842013-05-15 14:52:15 +02001046 if (DEBUG_RESUME_TIME) {
1047 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1048 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001049
Adam Cohen4b66bf32015-09-18 12:15:19 -07001050 // We want to suppress callbacks about CustomContent being shown if we have just received
1051 // onNewIntent while the user was present within launcher. In that case, we post a call
1052 // to move the user to the main screen (which will occur after onResume). We don't want to
1053 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1054 // suppress here.
1055 if (mWorkspace.getCustomContentCallbacks() != null
1056 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001058 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1060 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001061 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001062 }
1063 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001064 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001065 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001066 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001067
Sunny Goyal756adbc2015-04-16 15:20:51 -07001068 if (!isWorkspaceLoading()) {
1069 // Process any items that were added while Launcher was away.
1070 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1071 }
Adam Cohen9211d422014-10-07 18:14:53 -07001072
1073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onResume();
1075 }
Adam Cohen06dff352012-06-01 17:17:08 -07001076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001080 // Ensure that items added to Launcher are queued until Launcher returns
1081 InstallShortcutReceiver.enableInstallQueue();
1082
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001084 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001085 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001086 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 // We call onHide() aggressively. The custom content callbacks should be able to
1089 // debounce excess onHide calls.
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 mWorkspace.getCustomContentCallbacks().onHide();
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlay {
1115
1116 /**
1117 * Touch interaction leading to overscroll has begun
1118 */
1119 public void onScrollInteractionBegin();
1120
1121 /**
1122 * Touch interaction related to overscroll has ended
1123 */
1124 public void onScrollInteractionEnd();
1125
1126 /**
1127 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1128 * screen (or in the case of RTL, the rightmost screen).
1129 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001130 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131
1132 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001133 * Called when the launcher is ready to use the overlay
1134 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001136 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137 }
1138
Jun Mukai8af0cd82015-05-12 12:32:12 -07001139 public interface LauncherSearchCallbacks {
1140 /**
1141 * Called when the search overlay is shown.
1142 */
1143 public void onSearchOverlayOpened();
1144
1145 /**
1146 * Called when the search overlay is dismissed.
1147 */
1148 public void onSearchOverlayClosed();
1149 }
1150
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001152 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
Sunny Goyalc86df472016-02-25 09:19:38 -08001157 public void onScrollChanged(float progress) {
1158 if (mWorkspace != null) {
1159 mWorkspace.onOverlayScrollChanged(progress);
1160 }
1161 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001162 }
1163
Jorim Jaggid017f882014-01-14 17:08:48 -08001164 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001165 if (mLauncherCallbacks != null) {
1166 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001167 } else {
1168 // On devices with a locked orientation, we will at least have the allow rotation
1169 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001170 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001171 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 }
1173
Adam Cohen9211d422014-10-07 18:14:53 -07001174 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001175 CustomContentCallbacks callbacks, String description) {
1176 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001177 }
1178
Adam Cohenedb40762013-07-18 16:45:45 -07001179 // The custom content needs to offset its content to account for the QSB
1180 public int getTopOffsetForCustomContent() {
1181 return mWorkspace.getPaddingTop();
1182 }
1183
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001184 @Override
1185 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001187 if (mModel.isCurrentCallbacks(this)) {
1188 mModel.stopLoader();
1189 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001190 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1191
Romain Guy13c2e7b2010-03-10 19:45:00 -08001192 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001193 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001194
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001196 @Override
1197 public void onWindowFocusChanged(boolean hasFocus) {
1198 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001199 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001200
1201 if (mLauncherCallbacks != null) {
1202 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 private boolean acceptFilter() {
1207 final InputMethodManager inputManager = (InputMethodManager)
1208 getSystemService(Context.INPUT_METHOD_SERVICE);
1209 return !inputManager.isFullscreenMode();
1210 }
1211
1212 @Override
1213 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001214 final int uniChar = event.getUnicodeChar();
1215 final boolean handled = super.onKeyDown(keyCode, event);
1216 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1217 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1219 keyCode, event);
1220 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001222 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 // showSearchDialog()
1224 // If there are multiple keystrokes before the search dialog takes focus,
1225 // onSearchRequested() will be called for every keystroke,
1226 // but it is idempotent, so it's fine.
1227 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229 }
1230
Joe Onorato8a9625e2010-01-28 15:55:35 -08001231 // Eat the long press event so the keyboard doesn't come up.
1232 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1233 return true;
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 return handled;
1237 }
1238
Winson46163472015-09-22 17:31:56 -07001239 @Override
1240 public boolean onKeyUp(int keyCode, KeyEvent event) {
1241 if (keyCode == KeyEvent.KEYCODE_MENU) {
1242 // Ignore the menu key if we are currently dragging or are on the custom content screen
1243 if (!isOnCustomContent() && !mDragController.isDragging()) {
1244 // Close any open folders
1245 closeFolder();
1246
1247 // Stop resizing any widgets
1248 mWorkspace.exitWidgetResizeMode();
1249
1250 // Show the overview mode if we are on the workspace
1251 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1252 !mWorkspace.isSwitchingState()) {
1253 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001254 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001255 }
1256 }
1257 return true;
1258 }
1259 return super.onKeyUp(keyCode, event);
1260 }
1261
Karl Rosaen138a0412009-04-23 19:00:21 -07001262 private String getTypedText() {
1263 return mDefaultKeySsb.toString();
1264 }
1265
1266 private void clearTypedText() {
1267 mDefaultKeySsb.clear();
1268 mDefaultKeySsb.clearSpans();
1269 Selection.setSelection(mDefaultKeySsb, 0);
1270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001273 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1274 * State
1275 */
1276 private static State intToState(int stateOrdinal) {
1277 State state = State.WORKSPACE;
1278 final State[] stateValues = State.values();
1279 for (int i = 0; i < stateValues.length; i++) {
1280 if (stateValues[i].ordinal() == stateOrdinal) {
1281 state = stateValues[i];
1282 break;
1283 }
1284 }
1285 return state;
1286 }
1287
1288 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 * Restores the previous state, if it exists.
1290 *
1291 * @param savedState The previous state.
1292 */
1293 private void restoreState(Bundle savedState) {
1294 if (savedState == null) {
1295 return;
1296 }
1297
Michael Jurka883f55b2010-10-21 15:47:14 -07001298 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001299 if (state == State.APPS || state == State.WIDGETS) {
1300 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
1302
Adam Cohen21cd0022013-10-09 18:57:02 -07001303 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1304 PagedView.INVALID_RESTORE_PAGE);
1305 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001306 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Sunny Goyal756cd262015-08-20 12:33:21 -07001309 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1310 if (itemValues != null) {
1311 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001312 AppWidgetProviderInfo info = savedState.getParcelable(
1313 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001314 mPendingAddWidgetInfo = info == null ?
1315 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1316
Adam Cohen4637b5a2013-11-04 18:21:24 -08001317 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001318 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 mRestoring = true;
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 /**
1324 * Finds all the views we need and configure them properly.
1325 */
1326 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001327 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001328
Craig Mautner360310b2012-10-26 15:13:08 -07001329 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001330 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001331 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1332 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001333 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001334
Sunny Goyal784f9c32016-03-23 16:56:54 -07001335 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1336 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1337 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001338 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Winson Chung4c98d922011-05-31 16:50:48 -07001340 // Setup the drag layer
1341 mDragLayer.setup(this, dragController);
1342
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 // Setup the hotseat
1344 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1345 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001346 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 }
1348
Winsone9f27272015-10-13 10:47:51 -07001349 // Setup the overview panel
1350 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001351
Winson Chung4c98d922011-05-31 16:50:48 -07001352 // Setup the workspace
1353 mWorkspace.setHapticFeedbackEnabled(false);
1354 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001355 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001356 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001357
Tony Wickham34d2c912015-09-11 09:27:58 -07001358 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001359 mSearchDropTargetBar = (SearchDropTargetBar)
1360 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001361 // Get the app info bar
1362 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1363 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001364
Winson Chungef7f8742015-06-04 17:18:17 -07001365 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001366 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001367 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001368 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1369 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1370 } else {
1371 mAppsView.setSearchBarController(new DefaultAppSearchController());
1372 }
Craig Mautner360310b2012-10-26 15:13:08 -07001373
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001375 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001376 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001377 dragController.setMoveTarget(mWorkspace);
1378 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001379 if (mSearchDropTargetBar != null) {
1380 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001381 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001382 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001383 if (mAppInfoDropTargetBar != null) {
1384 mAppInfoDropTargetBar.setup(this, dragController);
1385 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001386
Sunny Goyal322d5562015-06-25 19:35:49 -07001387 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1388 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391
Winsone9f27272015-10-13 10:47:51 -07001392 private void setupOverviewPanel() {
1393 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1394
1395 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1396 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1397 @Override
1398 public boolean onLongClick(View v) {
1399 return v.performClick();
1400 }
1401 };
1402
1403 // Bind wallpaper button actions
1404 View wallpaperButton = findViewById(R.id.wallpaper_button);
1405 wallpaperButton.setOnClickListener(new OnClickListener() {
1406 @Override
1407 public void onClick(View view) {
1408 if (!mWorkspace.isSwitchingState()) {
1409 onClickWallpaperPicker(view);
1410 }
1411 }
1412 });
1413 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1414 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 // Bind widget button actions
1417 mWidgetsButton = findViewById(R.id.widget_button);
1418 mWidgetsButton.setOnClickListener(new OnClickListener() {
1419 @Override
1420 public void onClick(View view) {
1421 if (!mWorkspace.isSwitchingState()) {
1422 onClickAddWidgetButton(view);
1423 }
1424 }
1425 });
1426 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1427 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428
1429 // Bind settings actions
1430 View settingsButton = findViewById(R.id.settings_button);
1431 boolean hasSettings = hasSettings();
1432 if (hasSettings) {
1433 settingsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickSettingsButton(view);
1438 }
1439 }
1440 });
1441 settingsButton.setOnLongClickListener(performClickOnLongClick);
1442 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443 } else {
1444 settingsButton.setVisibility(View.GONE);
1445 }
1446
1447 mOverviewPanel.setAlpha(0f);
1448 }
1449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001451 * Sets the all apps button. This method is called from {@link Hotseat}.
1452 */
1453 public void setAllAppsButton(View allAppsButton) {
1454 mAllAppsButton = allAppsButton;
1455 }
1456
1457 public View getAllAppsButton() {
1458 return mAllAppsButton;
1459 }
1460
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001461 public View getWidgetsButton() {
1462 return mWidgetsButton;
1463 }
1464
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * Creates a view representing a shortcut.
1467 *
1468 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001470 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001471 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * @param parent The group the shortcut belongs to.
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from layoutResId.
1481 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001482 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001483 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 parent, false);
1485 favorite.applyFromShortcutInfo(info, mIconCache);
1486 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001488 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 return favorite;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Add a shortcut to the workspace.
1494 *
1495 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001500 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Sunny Goyal5c97f512015-05-19 16:03:28 -07001502 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001503 if (info == null) {
1504 return;
1505 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 final View view = createShortcut(info);
1507
Sunny Goyal5c97f512015-05-19 16:03:28 -07001508 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 // First we check if we already know the exact location where we want to add this item.
1510 if (cellX >= 0 && cellY >= 0) {
1511 cellXY[0] = cellX;
1512 cellXY[1] = cellY;
1513 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001514
1515 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001516 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 true, null,null)) {
1518 return;
1519 }
1520 DragObject dragObject = new DragObject();
1521 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1523 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 return;
1525 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 }
1529
1530 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 return;
1533 }
1534
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001535 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001539 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001544 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001546 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohen091440a2015-03-18 14:16:05 -07001548 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001549 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1550
1551 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001553 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohen59400422014-03-05 18:07:04 -08001556 if (appWidgetInfo.isCustomWidget) {
1557 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001558 }
1559
Adam Cohen59400422014-03-05 18:07:04 -08001560 LauncherAppWidgetInfo launcherInfo;
1561 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1562 launcherInfo.spanX = info.spanX;
1563 launcherInfo.spanY = info.spanY;
1564 launcherInfo.minSpanX = info.minSpanX;
1565 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001566 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001569 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (hostView == null) {
1573 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001576 hostView.setVisibility(View.VISIBLE);
1577 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001579 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 private void addAppWidgetToWorkspace(
1583 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setTag(item);
1586 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 hostView.setFocusable(true);
1589 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1593
1594 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 }
1597 }
1598
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1600 @Override
1601 public void onReceive(Context context, Intent intent) {
1602 final String action = intent.getAction();
1603 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1604 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001605 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001606 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001607
Winson Chungc100e8e2011-08-09 16:02:43 -07001608 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001609 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001610 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001611 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001612 if (!showWorkspace(false)) {
1613 // If we are already on the workspace, then manually reset all apps
1614 mAppsView.reset();
1615 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001616 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1618 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001619 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001620 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1621 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001622 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001623 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1624 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1625 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001626 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001627 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1628 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 }
1630 }
1631 };
1632
1633 @Override
1634 public void onAttachedToWindow() {
1635 super.onAttachedToWindow();
1636
1637 // Listen for broadcasts related to user-presence
1638 final IntentFilter filter = new IntentFilter();
1639 filter.addAction(Intent.ACTION_SCREEN_OFF);
1640 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001641 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001642 if (ENABLE_DEBUG_INTENTS) {
1643 filter.addAction(DebugIntents.DELETE_DATABASE);
1644 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1645 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001647 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001648 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001650
1651 if (mLauncherCallbacks != null) {
1652 mLauncherCallbacks.onAttachedToWindow();
1653 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 }
1655
1656 @Override
1657 public void onDetachedFromWindow() {
1658 super.onDetachedFromWindow();
1659 mVisible = false;
1660
Adam Cohend113e0c2010-11-11 10:48:05 -08001661 if (mAttached) {
1662 unregisterReceiver(mReceiver);
1663 mAttached = false;
1664 }
Winson Chungb745afb2015-03-02 11:51:23 -08001665 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001666
1667 if (mLauncherCallbacks != null) {
1668 mLauncherCallbacks.onDetachedFromWindow();
1669 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 }
1671
1672 public void onWindowVisibilityChanged(int visibility) {
1673 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001674 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 // The following code used to be in onResume, but it turns out onResume is called when
1676 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1677 // is a more appropriate event to handle
1678 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001679 if (!mWorkspaceLoading) {
1680 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 // We want to let Launcher draw itself at least once before we force it to build
1682 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001683 // apps is nice and speedy.
1684 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001685 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001687 if (mStarted) return;
1688 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001689 // We delay the layer building a bit in order to give
1690 // other message processing a time to run. In particular
1691 // this avoids a delay in hiding the IME if it was
1692 // currently shown, because doing that may involve
1693 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001694 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001695 final ViewTreeObserver.OnDrawListener listener = this;
1696 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001697 public void run() {
1698 if (mWorkspace != null &&
1699 mWorkspace.getViewTreeObserver() != null) {
1700 mWorkspace.getViewTreeObserver().
1701 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001702 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001703 }
1704 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001705 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001706 }
1707 });
1708 }
1709 clearTypedText();
1710 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 }
1712
Adam Cohen091440a2015-03-18 14:16:05 -07001713 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 mHandler.removeMessages(ADVANCE_MSG);
1715 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1716 mHandler.sendMessageDelayed(msg, delay);
1717 mAutoAdvanceSentTime = System.currentTimeMillis();
1718 }
1719
Adam Cohen091440a2015-03-18 14:16:05 -07001720 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1722 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1723 mAutoAdvanceRunning = autoAdvanceRunning;
1724 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001725 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 sendAdvanceMessage(delay);
1727 } else {
1728 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001729 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1731 }
1732 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001733 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 }
1735 }
1736 }
1737
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001738 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1739
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 if (msg.what == ADVANCE_MSG) {
1743 int i = 0;
1744 for (View key: mWidgetsToAdvance.keySet()) {
1745 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001746 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001748 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 public void run() {
1750 ((Advanceable) v).advance();
1751 }
1752 }, delay);
1753 }
1754 i++;
1755 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001756 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001758 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001761
Winsonc0b52fe2015-09-09 16:38:15 -07001762 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001763 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1765 if (v instanceof Advanceable) {
1766 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001767 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001768 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770 }
1771
Winsonc0b52fe2015-09-09 16:38:15 -07001772 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 if (mWidgetsToAdvance.containsKey(hostView)) {
1774 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001775 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001777 }
1778
Hyunyoung Song3f471442015-04-08 19:01:34 -07001779 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001780 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1781 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
1783
Winson Chunga6945242014-01-08 14:04:34 -08001784 public DragLayer getDragLayer() {
1785 return mDragLayer;
1786 }
1787
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001788 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001789 return mAppsView;
1790 }
1791
Hyunyoung Song3f471442015-04-08 19:01:34 -07001792 public WidgetsContainerView getWidgetsView() {
1793 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001794 }
1795
Winson Chunga6945242014-01-08 14:04:34 -08001796 public Workspace getWorkspace() {
1797 return mWorkspace;
1798 }
1799
1800 public Hotseat getHotseat() {
1801 return mHotseat;
1802 }
1803
Jorim Jaggid017f882014-01-14 17:08:48 -08001804 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001805 return mOverviewPanel;
1806 }
1807
Winson Chung006ee262015-08-03 14:40:11 -07001808 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001809 return mSearchDropTargetBar;
1810 }
1811
Tony Wickham34d2c912015-09-11 09:27:58 -07001812 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1813 return mAppInfoDropTargetBar;
1814 }
1815
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001816 public LauncherAppWidgetHost getAppWidgetHost() {
1817 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Winson Chunga9abd0e2010-10-27 17:18:37 -07001820 public LauncherModel getModel() {
1821 return mModel;
1822 }
1823
Adam Cohen79d90c52016-04-22 13:29:20 -07001824 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001825 return mSharedPrefs;
1826 }
1827
Adam Cohen2e6da152015-05-06 11:42:25 -07001828 public DeviceProfile getDeviceProfile() {
1829 return mDeviceProfile;
1830 }
1831
Bjorn Bringertc459e522013-06-07 19:36:01 +01001832 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001833 getWindow().closeAllPanels();
1834
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001835 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001836 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 }
1838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 @Override
1840 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001841 long startTime = 0;
1842 if (DEBUG_RESUME_TIME) {
1843 startTime = System.currentTimeMillis();
1844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 super.onNewIntent(intent);
1846
1847 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001848 Folder openFolder = mWorkspace.getOpenFolder();
1849 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1850 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1851 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1852 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1853 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001854 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001855 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001856
Adam Cohen6fecd412013-10-02 17:41:50 -07001857 if (mWorkspace == null) {
1858 // Can be cases where mWorkspace is null, this prevents a NPE
1859 return;
1860 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 // In all these cases, only animate if we're already on home
1862 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001863
Sunny Goyal935fca12015-10-13 10:19:01 -07001864 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001865 exitSpringLoadedDragMode();
1866
1867 // If we are already on home, then just animate back to the workspace,
1868 // otherwise, just wait until onResume to set the state back to Workspace
1869 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001870 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001871 } else {
1872 mOnResumeState = State.WORKSPACE;
1873 }
1874
1875 final View v = getWindow().peekDecorView();
1876 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001877 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001878 INPUT_METHOD_SERVICE);
1879 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1880 }
1881
Winson Chungb745afb2015-03-02 11:51:23 -08001882 // Reset the apps view
1883 if (!alreadyOnHome && mAppsView != null) {
1884 mAppsView.scrollToTop();
1885 }
1886
Hyunyoung Song3f471442015-04-08 19:01:34 -07001887 // Reset the widgets view
1888 if (!alreadyOnHome && mWidgetsView != null) {
1889 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001891
Adam Cohen9211d422014-10-07 18:14:53 -07001892 if (mLauncherCallbacks != null) {
1893 mLauncherCallbacks.onHomeIntent();
1894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
Adam Cohened307df2013-10-02 09:37:31 -07001896
Adam Cohen9211d422014-10-07 18:14:53 -07001897 if (mLauncherCallbacks != null) {
1898 mLauncherCallbacks.onNewIntent(intent);
1899 }
Winson15f8b172015-08-19 11:02:39 -07001900
1901 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1902 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1903 // animation.
1904 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001905 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1906 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001907 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1908 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001909
1910 // We use this flag to suppress noisy callbacks above custom content state
1911 // from onResume.
1912 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001913 mWorkspace.post(new Runnable() {
1914 @Override
1915 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001916 if (mWorkspace != null) {
1917 mWorkspace.moveToDefaultScreen(true);
1918 }
Winson15f8b172015-08-19 11:02:39 -07001919 }
1920 });
1921 }
1922 }
1923
1924 if (DEBUG_RESUME_TIME) {
1925 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1926 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001930 public void onRestoreInstanceState(Bundle state) {
1931 super.onRestoreInstanceState(state);
1932 for (int page: mSynchronouslyBoundPages) {
1933 mWorkspace.restoreInstanceStateForChild(page);
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
1937 @Override
1938 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001939 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001940 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1941 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001943 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
Michael Jurka883f55b2010-10-21 15:47:14 -07001945 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001946 // We close any open folder since it will not be re-opened, and we need to make sure
1947 // this state is reflected.
1948 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1949 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950
Adam Cohendcd297f2013-06-18 13:13:40 -07001951 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001953 ContentValues itemValues = new ContentValues();
1954 mPendingAddInfo.writeToValues(itemValues);
1955 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001956 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001957 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
Adam Cohen9211d422014-10-07 18:14:53 -07001960 if (mLauncherCallbacks != null) {
1961 mLauncherCallbacks.onSaveInstanceState(outState);
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
1965 @Override
1966 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001968
Winson Chunge7a03942011-08-05 15:05:12 -07001969 // Remove all pending runnables
1970 mHandler.removeMessages(ADVANCE_MSG);
1971 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001972 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001973
Winson Chungcd2b0142011-06-08 16:02:26 -07001974 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001975 // It's possible to receive onDestroy after a new Launcher activity has
1976 // been created. In this case, don't interfere with the new Launcher.
1977 if (mModel.isCurrentCallbacks(this)) {
1978 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001979 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001980 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001981
Sunny Goyal745bad92016-05-02 10:54:12 -07001982 if (mRotationPrefChangeHandler != null) {
1983 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1984 }
1985
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001987 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001989 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001991 mAppWidgetHost = null;
1992
1993 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994
1995 TextKeyListener.getInstance().release();
1996
Tony Wickhame2217252016-03-22 16:34:23 -07001997 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1998 .removeAccessibilityStateChangeListener(this);
1999
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002000 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002001
Michael Jurka2ecf9952012-06-18 12:52:28 -07002002 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002003
2004 if (mLauncherCallbacks != null) {
2005 mLauncherCallbacks.onDestroy();
2006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Adam Cohena9cf38f2011-05-02 15:36:58 -07002009 public DragController getDragController() {
2010 return mDragController;
2011 }
2012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 @Override
2014 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002015 onStartForResult(requestCode);
2016 super.startActivityForResult(intent, requestCode);
2017 }
2018
2019 @Override
2020 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2021 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2022 onStartForResult(requestCode);
2023 try {
2024 super.startIntentSenderForResult(intent, requestCode,
2025 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2026 } catch (IntentSender.SendIntentException e) {
2027 throw new ActivityNotFoundException();
2028 }
2029 }
2030
2031 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002032 if (requestCode >= 0) {
2033 setWaitingForResult(true);
2034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
Winson Chung70d72102011-08-12 11:24:35 -07002037 /**
2038 * Indicates that we want global search for this activity by setting the globalSearch
2039 * argument for {@link #startSearch} to true.
2040 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002042 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002044
Karl Rosaen138a0412009-04-23 19:00:21 -07002045 if (initialQuery == null) {
2046 // Use any text typed in the launcher as the initial query
2047 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 if (appSearchData == null) {
2050 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002051 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
Winson Chungadf0c182012-08-23 14:59:07 -07002053 Rect sourceBounds = new Rect();
2054 if (mSearchDropTargetBar != null) {
2055 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2056 }
Romain Guycbb89e42009-06-08 15:52:54 -07002057
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 if (clearTextImmediately) {
2061 clearTypedText();
2062 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002063
2064 // We need to show the workspace after starting the search
2065 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 }
2067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 /**
2069 * Start a text search.
2070 *
2071 * @return {@code true} if the search will start immediately, so any further keypresses
2072 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2073 * to buffer keypresses.
2074 */
2075 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002077 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2078 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2079 sourceBounds);
2080 }
2081
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002083 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002084 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 }
2086
2087 /**
2088 * Starts the global search activity. This code is a copied from SearchManager
2089 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002092 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2094 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2095 if (globalSearchActivity == null) {
2096 Log.w(TAG, "No global search activity found.");
2097 return;
2098 }
2099 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2100 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2101 intent.setComponent(globalSearchActivity);
2102 // Make sure that we have a Bundle to put source in
2103 if (appSearchData == null) {
2104 appSearchData = new Bundle();
2105 } else {
2106 appSearchData = new Bundle(appSearchData);
2107 }
Adam Cohen9211d422014-10-07 18:14:53 -07002108 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 if (!appSearchData.containsKey("source")) {
2110 appSearchData.putString("source", getPackageName());
2111 }
2112 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2113 if (!TextUtils.isEmpty(initialQuery)) {
2114 intent.putExtra(SearchManager.QUERY, initialQuery);
2115 }
2116 if (selectInitialQuery) {
2117 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2118 }
2119 intent.setSourceBounds(sourceBounds);
2120 try {
2121 startActivity(intent);
2122 } catch (ActivityNotFoundException ex) {
2123 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
2126
Yura4f93ec62014-02-04 14:15:21 +00002127 public boolean isOnCustomContent() {
2128 return mWorkspace.isOnOrMovingToCustomContent();
2129 }
2130
Winson Chung70d72102011-08-12 11:24:35 -07002131 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002132 public boolean onPrepareOptionsMenu(Menu menu) {
2133 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002134 if (mLauncherCallbacks != null) {
2135 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2136 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002137 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002140 @Override
2141 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002142 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002143 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002144 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002145 }
2146
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 public boolean isWorkspaceLocked() {
2148 return mWorkspaceLoading || mWaitingForResult;
2149 }
2150
Adam Cohen517a7f52014-03-01 12:12:59 -08002151 public boolean isWorkspaceLoading() {
2152 return mWorkspaceLoading;
2153 }
2154
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002155 private void setWorkspaceLoading(boolean value) {
2156 boolean isLocked = isWorkspaceLocked();
2157 mWorkspaceLoading = value;
2158 if (isLocked != isWorkspaceLocked()) {
2159 onWorkspaceLockedChanged();
2160 }
2161 }
2162
2163 private void setWaitingForResult(boolean value) {
2164 boolean isLocked = isWorkspaceLocked();
2165 mWaitingForResult = value;
2166 if (isLocked != isWorkspaceLocked()) {
2167 onWorkspaceLockedChanged();
2168 }
2169 }
2170
Adam Cohen9211d422014-10-07 18:14:53 -07002171 protected void onWorkspaceLockedChanged() {
2172 if (mLauncherCallbacks != null) {
2173 mLauncherCallbacks.onWorkspaceLockedChanged();
2174 }
2175 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002176
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2181 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002182 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002183 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002184
Tony Wickhama0628cc2015-10-14 15:23:04 -07002185 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002186 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 if (LOGD) {
2188 Log.d(TAG, "Adding widget from drop");
2189 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002190 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2191 }
2192
Sunny Goyale6b63a32015-01-16 16:14:29 -08002193 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002194 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2195 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002196 if (appWidgetInfo.configure != null) {
2197 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002198 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002199
Bjorn Bringert7984c942009-12-09 15:38:25 +00002200 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002201 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2202 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002205 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002206 Runnable onComplete = new Runnable() {
2207 @Override
2208 public void run() {
2209 // Exit spring loaded mode if necessary after adding the widget
2210 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2211 null);
2212 }
2213 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002215 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002216 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 }
2218 }
Romain Guycbb89e42009-06-08 15:52:54 -07002219
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002220 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002221 // Close any folders that may be open.
2222 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002223 mWorkspace.moveToCustomContentScreen(animate);
2224 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002225
2226 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2227 int[] cell, int spanX, int spanY) {
2228 switch (info.itemType) {
2229 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2230 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2231 int span[] = new int[2];
2232 span[0] = spanX;
2233 span[1] = spanY;
2234 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2235 container, screenId, cell, span);
2236 break;
2237 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2238 processShortcutFromDrop(info.componentName, container, screenId, cell);
2239 break;
2240 default:
2241 throw new IllegalStateException("Unknown item type: " + info.itemType);
2242 }
2243 }
2244
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 /**
2246 * Process a shortcut drop.
2247 *
2248 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002252 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2253 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257
2258 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.cellX = cell[0];
2260 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262
2263 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2264 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002265 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266 }
2267
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 /**
2269 * Process a widget drop.
2270 *
2271 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002275 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2276 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002280 mPendingAddInfo.minSpanX = info.minSpanX;
2281 mPendingAddInfo.minSpanY = info.minSpanY;
2282
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002284 mPendingAddInfo.cellX = cell[0];
2285 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002287 if (span != null) {
2288 mPendingAddInfo.spanX = span[0];
2289 mPendingAddInfo.spanY = span[1];
2290 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 AppWidgetHostView hostView = info.boundWidget;
2293 int appWidgetId;
2294 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002295 // In the case where we've prebound the widget, we remove it from the DragLayer
2296 if (LOGD) {
2297 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2298 }
2299 getDragLayer().removeView(hostView);
2300
Adam Cohened66b2b2012-01-23 17:28:51 -08002301 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002302 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002303
2304 // Clear the boundWidget so that it doesn't get destroyed.
2305 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002307 // In this case, we either need to start an activity to get permission to bind
2308 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002309 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002310 Bundle options = info.bindOptions;
2311
Sunny Goyalffe83f12014-08-14 17:39:34 -07002312 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2313 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002314 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002315 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002316 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002317 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2319 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002321 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2322 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002323 // TODO: we need to make sure that this accounts for the options bundle.
2324 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002325 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2326 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 }
2329
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002332 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 folderInfo.title = getText(R.string.folder_name);
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002336 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2337 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338
2339 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 FolderIcon newFolder =
2341 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002344 // Force measure the new folder icon
2345 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2346 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002347 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 }
Romain Guycbb89e42009-06-08 15:52:54 -07002349
Winsonc0b52fe2015-09-09 16:38:15 -07002350 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002351 * Unbinds the view for the specified item, and removes the item and all its children.
2352 *
2353 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002354 * @param itemInfo the {@link ItemInfo} for this view.
2355 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002356 */
Winson2949fb52015-09-24 09:56:11 -07002357 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002358 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002359 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002360 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2361 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002362 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002363 } else {
2364 mWorkspace.removeWorkspaceItem(v);
2365 }
Winsonc0b52fe2015-09-09 16:38:15 -07002366 if (deleteFromDb) {
2367 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2368 }
2369 } else if (itemInfo instanceof FolderInfo) {
2370 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002371 mWorkspace.removeWorkspaceItem(v);
2372 if (deleteFromDb) {
2373 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2374 }
2375 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2376 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002377 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002378 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002379 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002381 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002382
Winsonc0b52fe2015-09-09 16:38:15 -07002383 } else {
2384 return false;
2385 }
2386 return true;
2387 }
2388
2389 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002390 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002391 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002393 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002395 // Deleting an app widget ID is a void call but writes to disk before returning
2396 // to the caller...
2397 new AsyncTask<Void, Void, Void>() {
2398 public Void doInBackground(Void ... args) {
2399 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2400 return null;
2401 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002402 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002403 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002404 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002405 }
2406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 @Override
2408 public boolean dispatchKeyEvent(KeyEvent event) {
2409 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2410 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002412 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002413 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002414 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002415 dumpState();
2416 return true;
2417 }
2418 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002419 }
2420 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2421 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 return true;
2424 }
2425 }
2426
2427 return super.dispatchKeyEvent(event);
2428 }
2429
Joe Onorato88ec0992009-11-19 13:16:06 -08002430 @Override
2431 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002432 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2433 return;
2434 }
2435
Sunny Goyal45478022015-06-08 16:52:41 -07002436 if (mDragController.isDragging()) {
2437 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002438 return;
2439 }
2440
Winson Chungb745afb2015-03-02 11:51:23 -08002441 if (isAppsViewVisible()) {
2442 showWorkspace(true);
2443 } else if (isWidgetsViewVisible()) {
2444 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002445 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002446 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002447 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002448 Folder openFolder = mWorkspace.getOpenFolder();
2449 if (openFolder.isEditingName()) {
2450 openFolder.dismissEditingName();
2451 } else {
2452 closeFolder();
2453 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002454 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002455 mWorkspace.exitWidgetResizeMode();
2456
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002457 // Back button is a no-op here, but give at least some feedback for the button press
2458 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002459 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002460 }
2461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002463 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002464 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002465 @Override
2466 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002467 if (mAppWidgetHost != null) {
2468 mAppWidgetHost.startListening();
2469 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002470
2471 // Recreate the QSB, as the widget has been reset.
2472 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002473 }
2474
2475 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 * Launches the intent referred by the clicked shortcut.
2477 *
2478 * @param v The view representing the clicked shortcut.
2479 */
2480 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002481 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2482 // view has detached (it's possible for this to happen if the view is removed mid touch).
2483 if (v.getWindowToken() == null) {
2484 return;
2485 }
2486
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002487 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002488 return;
2489 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002490
Adam Cohen1697b792013-09-17 19:08:21 -07002491 if (v instanceof Workspace) {
2492 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002493 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
2495 return;
2496 }
2497
2498 if (v instanceof CellLayout) {
2499 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002500 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2501 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002502 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002503 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002504 }
2505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002507 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002508 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002510 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002511 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002513 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002515 } else if (tag instanceof AppInfo) {
2516 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002517 } else if (tag instanceof LauncherAppWidgetInfo) {
2518 if (v instanceof PendingAppWidgetHostView) {
2519 onClickPendingWidget((PendingAppWidgetHostView) v);
2520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 }
2522 }
2523
Sunny Goyal70660032015-05-14 00:07:08 -07002524 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002525 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002526 return false;
2527 }
2528
Michael Jurkaaf442092010-06-10 17:01:57 -07002529 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002530 * Event handler for the app widget view which has not fully restored.
2531 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002532 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002533 if (mIsSafeModeEnabled) {
2534 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2535 return;
2536 }
2537
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002538 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002539 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002540 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002541 LauncherAppWidgetProviderInfo appWidgetInfo =
2542 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002543 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002544 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002545 mPendingAddInfo.copyFrom(info);
2546 mPendingAddWidgetId = widgetId;
2547
Sunny Goyalffe83f12014-08-14 17:39:34 -07002548 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2549 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002550 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 } else if (info.installProgress < 0) {
2552 // The install has not been queued
2553 final String packageName = info.providerName.getPackageName();
2554 showBrokenAppInstallDialog(packageName,
2555 new DialogInterface.OnClickListener() {
2556 public void onClick(DialogInterface dialog, int id) {
2557 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2558 }
2559 });
2560 } else {
2561 // Download has started.
2562 final String packageName = info.providerName.getPackageName();
2563 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002564 }
2565 }
2566
2567 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002568 * Event handler for the "grid" button that appears on the home screen, which
2569 * enters all apps mode.
2570 *
2571 * @param v The view that was clicked.
2572 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002573 protected void onClickAllAppsButton(View v) {
2574 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002575 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002576 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002577 true /* updatePredictedApps */, false /* focusSearchBar */);
2578 }
2579 }
2580
2581 protected void onLongClickAllAppsButton(View v) {
2582 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2583 if (!isAppsViewVisible()) {
2584 showAppsView(true /* animated */, false /* resetListToTop */,
2585 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 }
2587 }
2588
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002589 private void showBrokenAppInstallDialog(final String packageName,
2590 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002591 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002592 .setTitle(R.string.abandoned_promises_title)
2593 .setMessage(R.string.abandoned_promise_explanation)
2594 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2595 .setNeutralButton(R.string.abandoned_clean_this,
2596 new DialogInterface.OnClickListener() {
2597 public void onClick(DialogInterface dialog, int id) {
2598 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2599 mWorkspace.removeAbandonedPromise(packageName, user);
2600 }
2601 })
2602 .create().show();
2603 return;
2604 }
2605
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 /**
2607 * Event handler for an app shortcut click.
2608 *
2609 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2610 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002611 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2613 Object tag = v.getTag();
2614 if (!(tag instanceof ShortcutInfo)) {
2615 throw new IllegalArgumentException("Input must be a Shortcut");
2616 }
2617
2618 // Open shortcut
2619 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002620
2621 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002622 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2623 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002624 // Launch activity anyway, framework will tell the user why the app is suspended.
2625 } else {
2626 int error = R.string.activity_not_available;
2627 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2628 error = R.string.safemode_shortcut_error;
2629 }
2630 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2631 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633 }
2634
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002636 if ((v instanceof BubbleTextView)
2637 && shortcut.isPromise()
2638 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002639 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002640 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 new DialogInterface.OnClickListener() {
2642 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002643 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002650 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 }
2652
Adam Cohen091440a2015-03-18 14:16:05 -07002653 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002655 final ShortcutInfo shortcut;
2656 final Intent intent;
2657 if (tag instanceof ShortcutInfo) {
2658 shortcut = (ShortcutInfo) tag;
2659 intent = shortcut.intent;
2660 int[] pos = new int[2];
2661 v.getLocationOnScreen(pos);
2662 intent.setSourceBounds(new Rect(pos[0], pos[1],
2663 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002664
Sunny Goyal508da152014-08-14 10:53:27 -07002665 } else if (tag instanceof AppInfo) {
2666 shortcut = null;
2667 intent = ((AppInfo) tag).intent;
2668 } else {
2669 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2670 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671
2672 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002673 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674
2675 if (success && v instanceof BubbleTextView) {
2676 mWaitingForResume = (BubbleTextView) v;
2677 mWaitingForResume.setStayPressed(true);
2678 }
2679 }
2680
2681 /**
2682 * Event handler for a folder icon click.
2683 *
2684 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2685 */
2686 protected void onClickFolderIcon(View v) {
2687 if (LOGD) Log.d(TAG, "onClickFolder");
2688 if (!(v instanceof FolderIcon)){
2689 throw new IllegalArgumentException("Input must be a FolderIcon");
2690 }
2691
2692 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002693 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002695 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 }
Michael Jurka5130e402011-10-13 04:55:35 -07002697 }
2698
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 /**
2700 * Event handler for the (Add) Widgets button that appears after a long press
2701 * on the home screen.
2702 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002703 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002705 if (mIsSafeModeEnabled) {
2706 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2707 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002708 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002709 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002710 }
2711
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 /**
2713 * Event handler for the wallpaper picker button that appears after a long press
2714 * on the home screen.
2715 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002716 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002717 if (!Utilities.isWallapaperAllowed(this)) {
2718 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2719 return;
2720 }
2721
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002722 String pickerPackage = getString(R.string.wallpaper_picker_package);
2723 if (TextUtils.isEmpty(pickerPackage)) {
2724 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2725 }
2726
Tony Wickham785f7a52015-08-31 17:28:32 -07002727 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2728 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002729 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2730 .setPackage(pickerPackage)
2731 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2732 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 }
2734
2735 /**
2736 * Event handler for a click on the settings button that appears after a long press
2737 * on the home screen.
2738 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002739 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002741 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2742 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 }
2744
Adam Cohen61f560d2013-09-30 15:58:20 -07002745 public View.OnTouchListener getHapticFeedbackTouchListener() {
2746 if (mHapticFeedbackTouchListener == null) {
2747 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002748 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002749 @Override
2750 public boolean onTouch(View v, MotionEvent event) {
2751 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2752 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2753 }
2754 return false;
2755 }
2756 };
2757 }
2758 return mHapticFeedbackTouchListener;
2759 }
2760
Tony Wickhame2217252016-03-22 16:34:23 -07002761 @Override
2762 public void onAccessibilityStateChanged(boolean enabled) {
2763 mDragLayer.onAccessibilityStateChanged(enabled);
2764 }
2765
Adam Cohen9211d422014-10-07 18:14:53 -07002766 public void onDragStarted(View view) {
2767 if (isOnCustomContent()) {
2768 // Custom content screen doesn't participate in drag and drop. If on custom
2769 // content screen, move to default.
2770 moveWorkspaceToDefaultScreen();
2771 }
Adam Cohen9211d422014-10-07 18:14:53 -07002772 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002773
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002774 /**
2775 * Called when the user stops interacting with the launcher.
2776 * This implies that the user is now on the homescreen and is not doing housekeeping.
2777 */
Adam Cohen9211d422014-10-07 18:14:53 -07002778 protected void onInteractionEnd() {
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onInteractionEnd();
2781 }
2782 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002783
2784 /**
2785 * Called when the user starts interacting with the launcher.
2786 * The possible interactions are:
2787 * - open all apps
2788 * - reorder an app shortcut, or a widget
2789 * - open the overview mode.
2790 * This is a good time to stop doing things that only make sense
2791 * when the user is on the homescreen and not doing housekeeping.
2792 */
Adam Cohen9211d422014-10-07 18:14:53 -07002793 protected void onInteractionBegin() {
2794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onInteractionBegin();
2796 }
2797 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002798
Winson Chungcd99cd32015-04-29 11:03:24 -07002799 /** Updates the interaction state. */
2800 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002801 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002803 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2804 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002805 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002806 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002807 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002808 onInteractionEnd();
2809 }
2810 }
2811
Winson Chung8f1eff72015-05-28 17:33:40 -07002812 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002813 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002815 // Only launch using the new animation if the shortcut has not opted out (this is a
2816 // private contract between launcher and may be ignored in the future).
2817 boolean useLaunchAnimation = (v != null) &&
2818 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002819 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2820 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002821
Kenny Guy1317e2d2014-05-08 18:52:50 +01002822 UserHandleCompat user = null;
2823 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2824 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2825 user = userManager.getUserForSerialNumber(serialNumber);
2826 }
2827
2828 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002829 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002830 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002831 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002832 int left = 0, top = 0;
2833 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2834 if (v instanceof TextView) {
2835 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002836 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2837 if (icon != null) {
2838 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002839 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002840 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002841 width = bounds.width();
2842 height = bounds.height();
2843 }
2844 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002845 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2846 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002847 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002848 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002849 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002850 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002851 // On L devices, we use the device default slide-up transition.
2852 // On L MR1 devices, we a custom version of the slide-up transition which
2853 // doesn't have the delay present in the device default.
2854 opts = ActivityOptions.makeCustomAnimation(this,
2855 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002856 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002857 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002858 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002859
2860 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2861 // Could be launching some bookkeeping activity
2862 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002863 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002864 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002865 launcherApps.startActivityForProfile(intent.getComponent(), user,
2866 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002867 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002868 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002870 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2871 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2872 // corresponding permission. Show the appropriate permission prompt if that
2873 // is the case.
2874 if (intent.getComponent() == null
2875 && Intent.ACTION_CALL.equals(intent.getAction())
2876 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2877 PackageManager.PERMISSION_GRANTED) {
2878 // TODO: Rename sPendingAddItem to a generic name.
2879 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2880 0, (ItemInfo) tag);
2881 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2882 REQUEST_PERMISSION_CALL_PHONE);
2883 return false;
2884 }
2885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002887 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002889 "or use the exported attribute for this activity. "
2890 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002892 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002894
Winson Chungbedf9232015-07-10 12:38:30 -07002895 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002896 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002897 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2898 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2899 return false;
2900 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002901 try {
2902 success = startActivity(v, intent, tag);
2903 } catch (ActivityNotFoundException e) {
2904 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2905 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2906 }
2907 return success;
2908 }
2909
Adam Cohen268c4752012-06-06 17:47:33 -07002910 /**
2911 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2912 * in the DragLayer in the exact absolute location of the original FolderIcon.
2913 */
2914 private void copyFolderIconToImage(FolderIcon fi) {
2915 final int width = fi.getMeasuredWidth();
2916 final int height = fi.getMeasuredHeight();
2917
2918 // Lazy load ImageView, Bitmap and Canvas
2919 if (mFolderIconImageView == null) {
2920 mFolderIconImageView = new ImageView(this);
2921 }
2922 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2923 mFolderIconBitmap.getHeight() != height) {
2924 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2925 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2926 }
2927
2928 DragLayer.LayoutParams lp;
2929 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2930 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2931 } else {
2932 lp = new DragLayer.LayoutParams(width, height);
2933 }
2934
Adam Cohen307fe232012-08-16 17:55:58 -07002935 // The layout from which the folder is being opened may be scaled, adjust the starting
2936 // view size by this scale factor.
2937 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002938 lp.customPosition = true;
2939 lp.x = mRectForFolderAnimation.left;
2940 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002941 lp.width = (int) (scale * width);
2942 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002943
2944 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2945 fi.draw(mFolderIconCanvas);
2946 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002947 if (fi.getFolder() != null) {
2948 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2949 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002950 }
Adam Cohen268c4752012-06-06 17:47:33 -07002951 // Just in case this image view is still in the drag layer from a previous animation,
2952 // we remove it and re-add it.
2953 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2954 mDragLayer.removeView(mFolderIconImageView);
2955 }
2956 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002957 if (fi.getFolder() != null) {
2958 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002959 }
Adam Cohen268c4752012-06-06 17:47:33 -07002960 }
2961
Adam Cohen37b2a492016-04-06 18:36:06 -07002962 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002963 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002964 FolderInfo info = (FolderInfo) fi.getTag();
2965 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2966 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002967 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2968 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002969 }
2970
Adam Cohen268c4752012-06-06 17:47:33 -07002971 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2972 copyFolderIconToImage(fi);
2973 fi.setVisibility(View.INVISIBLE);
2974
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002975 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2976 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002977 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002978 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2979 }
2980 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002981 oa.start();
2982 }
2983
Sunny Goyal935fca12015-10-13 10:19:01 -07002984 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002985 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002986 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002987
Adam Cohen268c4752012-06-06 17:47:33 -07002988 // We remove and re-draw the FolderIcon in-case it has changed
2989 mDragLayer.removeView(mFolderIconImageView);
2990 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002991
2992 if (cl != null) {
2993 cl.clearFolderLeaveBehind();
2994 }
2995
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002996 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002997 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002998 oa.addListener(new AnimatorListenerAdapter() {
2999 @Override
3000 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003001 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // Remove the ImageView copy of the FolderIcon and make the original visible.
3003 mDragLayer.removeView(mFolderIconImageView);
3004 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003005 }
3006 }
3007 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003008 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003009 if (!animate) {
3010 oa.end();
3011 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003012 }
3013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003015 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 * is animated relative to the specified View. If the View is null, no animation
3017 * is played.
3018 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003019 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003021 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003022
Adam Cohenfb91f302012-06-11 15:45:18 -07003023 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003024 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3025 if (openFolder != null && openFolder != folder) {
3026 // Close any open folder before opening a folder.
3027 closeFolder();
3028 }
3029
Adam Cohena9cf38f2011-05-02 15:36:58 -07003030 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031
Adam Cohena9cf38f2011-05-02 15:36:58 -07003032 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033
Sunny Goyalf4066152015-04-15 09:42:19 -07003034 // While the folder is open, the position of the icon cannot change.
3035 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3036
Adam Cohen4554ee12011-08-03 16:13:21 -07003037 // Just verify that the folder hasn't already been added to the DragLayer.
3038 // There was a one-off crash where the folder had a parent already.
3039 if (folder.getParent() == null) {
3040 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003041 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003042 } else {
3043 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3044 folder.getParent() + ").");
3045 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003046 folder.animateOpen();
3047
3048 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003049
3050 // Notify the accessibility manager that this folder "window" has appeared and occluded
3051 // the workspace items
3052 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3053 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003054 }
3055
3056 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003057 closeFolder(true);
3058 }
3059
3060 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003061 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003063 if (folder.isEditingName()) {
3064 folder.dismissEditingName();
3065 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003066 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003067 }
3068 }
3069
Sunny Goyal935fca12015-10-13 10:19:01 -07003070 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003071 animate &= !Utilities.isPowerSaverOn(this);
3072
Adam Cohen4554ee12011-08-03 16:13:21 -07003073 folder.getInfo().opened = false;
3074
3075 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3076 if (parent != null) {
3077 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003079 if (fi != null) {
3080 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3081 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003083 if (animate) {
3084 folder.animateClosed();
3085 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003086 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003087 }
Winson Chung83ca4802013-04-12 15:10:52 -07003088
Sunny Goyal935fca12015-10-13 10:19:01 -07003089 // Notify the accessibility manager that this folder "window" has disappeared and no
3090 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003091 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 }
3093
Tony Wickhamdadb3042016-02-24 11:07:00 -08003094 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003096 if (!isDraggingEnabled()) return false;
3097 if (isWorkspaceLocked()) return false;
3098 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Winson Chung76648c52015-07-10 14:33:23 -07003100 if (v == mAllAppsButton) {
3101 onLongClickAllAppsButton(v);
3102 return true;
3103 }
3104
Adam Cohen1697b792013-09-17 19:08:21 -07003105 if (v instanceof Workspace) {
3106 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003107 if (!mWorkspace.isTouchActive()) {
3108 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003109 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3110 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3111 return true;
3112 } else {
3113 return false;
3114 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003115 } else {
3116 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003117 }
Adam Cohen1697b792013-09-17 19:08:21 -07003118 }
3119
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003120 CellLayout.CellInfo longClickCellInfo = null;
3121 View itemUnderLongClick = null;
3122 if (v.getTag() instanceof ItemInfo) {
3123 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003124 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003125 itemUnderLongClick = longClickCellInfo.cell;
3126 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127 }
3128
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 // The hotseat touch handling does not go through Workspace, and we always allow long press
3130 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003131 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003132 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003133 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003134 // User long pressed on empty space
3135 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3136 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003137 if (mWorkspace.isInOverviewMode()) {
3138 mWorkspace.startReordering(v);
3139 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003140 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003143 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3144 mHotseat.getOrderInHotseat(
3145 longClickCellInfo.cellX,
3146 longClickCellInfo.cellY));
3147 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003149 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 }
3151 }
3152 }
3153 return true;
3154 }
3155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 boolean isHotseatLayout(View layout) {
3157 return mHotseat != null && layout != null &&
3158 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3159 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003160
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 /**
3162 * Returns the CellLayout of the specified container at the specified screen.
3163 */
Sunny Goyal92820592015-03-02 11:31:35 -08003164 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3166 if (mHotseat != null) {
3167 return mHotseat.getLayout();
3168 } else {
3169 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003170 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003172 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003173 }
3174 }
3175
Winson Chungb745afb2015-03-02 11:51:23 -08003176 /**
3177 * For overridden classes.
3178 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003179 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003180 return isAppsViewVisible();
3181 }
3182
3183 public boolean isAppsViewVisible() {
3184 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3185 }
3186
3187 public boolean isWidgetsViewVisible() {
3188 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003189 }
3190
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003191 private void setWorkspaceBackground(int background) {
3192 switch (background) {
3193 case WORKSPACE_BACKGROUND_TRANSPARENT:
3194 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3195 break;
3196 case WORKSPACE_BACKGROUND_BLACK:
3197 getWindow().setBackgroundDrawable(null);
3198 break;
3199 default:
3200 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3201 }
Craig Mautner360310b2012-10-26 15:13:08 -07003202 }
3203
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003204 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003205 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3206 int curflags = getWindow().getAttributes().flags
3207 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3208 if (wpflags != curflags) {
3209 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3210 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003211 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003212 }
3213
Michael Jurkae326f182011-11-21 14:05:46 -08003214 @Override
3215 public void onTrimMemory(int level) {
3216 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003217 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3218 // The widget preview db can result in holding onto over
3219 // 3MB of memory for caching which isn't necessary.
3220 SQLiteDatabase.releaseMemory();
3221
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003222 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003223 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003224 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003225 if (mLauncherCallbacks != null) {
3226 mLauncherCallbacks.onTrimMemory(level);
3227 }
Michael Jurkae326f182011-11-21 14:05:46 -08003228 }
3229
Winson5c6bdbb2015-09-03 11:36:19 -07003230 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003231 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003232 }
3233
Winson5c6bdbb2015-09-03 11:36:19 -07003234 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003235 boolean changed = mState != State.WORKSPACE ||
3236 mWorkspace.getState() != Workspace.State.NORMAL;
3237 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003238 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003239 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003240 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003241
Michael Jurkab3e22d92011-10-31 15:58:33 -07003242 // Set focus to the AppsCustomize button
3243 if (mAllAppsButton != null) {
3244 mAllAppsButton.requestFocus();
3245 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003246 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003247
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003248 // Change the state *after* we've called all the transition code
3249 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003250
Winson Chung5fb63472011-02-02 17:03:37 -08003251 // Resume the auto-advance of widgets
3252 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003253 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003254
Winson Chung0f785722015-04-08 10:27:49 -07003255 if (changed) {
3256 // Send an accessibility event to announce the context change
3257 getWindow().getDecorView()
3258 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003259 }
Winson5c6bdbb2015-09-03 11:36:19 -07003260 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003261 }
3262
Winsone9f27272015-10-13 10:47:51 -07003263 /**
3264 * Shows the overview button.
3265 */
Adam Cohened307df2013-10-02 09:37:31 -07003266 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003267 showOverviewMode(animated, false);
3268 }
3269
3270 /**
3271 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3272 * onto one of the overview panel buttons.
3273 */
3274 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3275 Runnable postAnimRunnable = null;
3276 if (requestButtonFocus) {
3277 postAnimRunnable = new Runnable() {
3278 @Override
3279 public void run() {
3280 // Hitting the menu button when in touch mode does not trigger touch mode to
3281 // be disabled, so if requested, force focus on one of the overview panel
3282 // buttons.
3283 mOverviewPanel.requestFocusFromTouch();
3284 }
3285 };
3286 }
Adam Cohened307df2013-10-02 09:37:31 -07003287 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003288 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003289 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003290 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003291 }
3292
Winson Chungb745afb2015-03-02 11:51:23 -08003293 /**
3294 * Shows the apps view.
3295 */
Winson Chung76648c52015-07-10 14:33:23 -07003296 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3297 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003298 if (resetListToTop) {
3299 mAppsView.scrollToTop();
3300 }
Winson Chung4ac30062015-05-08 17:34:17 -07003301 if (updatePredictedApps) {
3302 tryAndUpdatePredictedApps();
3303 }
Winson Chung76648c52015-07-10 14:33:23 -07003304 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003305 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306
Winson Chungb745afb2015-03-02 11:51:23 -08003307 /**
3308 * Shows the widgets view.
3309 */
3310 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003311 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003312 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003313 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003314 }
Winson Chung76648c52015-07-10 14:33:23 -07003315 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003316
3317 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003318 @Override
3319 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003321 }
3322 });
Winson Chungb745afb2015-03-02 11:51:23 -08003323 }
3324
3325 /**
3326 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003327 *
3328 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003329 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003330 // TODO: calling method should use the return value so that when {@code false} is returned
3331 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003332 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003333 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3334 mState != State.WIDGETS_SPRING_LOADED) {
3335 return false;
3336 }
3337 if (toState != State.APPS && toState != State.WIDGETS) {
3338 return false;
3339 }
Winson Chungb745afb2015-03-02 11:51:23 -08003340
3341 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003342 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3343 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003344 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003345 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003346 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003349 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350
3351 // Pause the auto-advance of widgets until we are out of AllApps
3352 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003353 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354 closeFolder();
3355
3356 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003357 getWindow().getDecorView()
3358 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003359 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360 }
3361
Winson Chungcd99cd32015-04-29 11:03:24 -07003362 /**
3363 * Updates the workspace and interaction state on state change, and return the animation to this
3364 * new state.
3365 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003366 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003367 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003368 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003369 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003370 updateInteraction(fromState, toState);
3371 return anim;
3372 }
3373
Jun Mukaif0033da2015-08-04 18:34:30 -07003374 public void onLauncherClingShown() {
3375 // When a launcher cling appears, it should cover the underlying layers, so their focus
3376 // should be blocked.
3377 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3378 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3379 }
3380 }
3381
3382 public void onLauncherClingDismissed() {
3383 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3384 }
3385
Hyunyoung Song3f471442015-04-08 19:01:34 -07003386 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003387 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003388 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003389 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003390 }
Winson Chungb745afb2015-03-02 11:51:23 -08003391
Winson Chung006ee262015-08-03 14:40:11 -07003392 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003393 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003394 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003395
3396 if (isAppsViewVisible()) {
3397 mState = State.APPS_SPRING_LOADED;
3398 } else if (isWidgetsViewVisible()) {
3399 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003400 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003401 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003402 } else {
3403 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003405 }
Adam Cohen7777d962011-08-18 18:58:38 -07003406
Hyunyoung Song3f471442015-04-08 19:01:34 -07003407 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003408 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003409 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003410
Winson Chunge7a03942011-08-05 15:05:12 -07003411 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 @Override
3413 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003414 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3416 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003417 // Before we show workspace, hide all apps again because
3418 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3419 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003421 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003422 } else {
3423 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003424 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003425 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003426 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003427 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428
Tony Wickhame0c33232016-02-08 11:37:04 -08003429 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003430 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3431 || mState == State.WIDGETS_SPRING_LOADED;
3432 }
3433
Michael Jurkad3ef3062010-11-23 16:23:58 -08003434 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003435 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003436 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003437 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003438 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003439 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003440 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3441 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003442 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003443 }
3444
Winson Chung4ac30062015-05-08 17:34:17 -07003445 /**
3446 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3447 * resumed.
3448 */
3449 private void tryAndUpdatePredictedApps() {
3450 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003451 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003452 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003453 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003454 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003455 }
3456 }
3457 }
3458
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459 void lockAllApps() {
3460 // TODO
3461 }
3462
3463 void unlockAllApps() {
3464 // TODO
3465 }
3466
Winsonf768d932015-09-25 16:12:35 -07003467 public boolean launcherCallbacksProvidesSearch() {
3468 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3469 }
3470
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003471 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003472 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003473 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003474 }
3475
Adam Cohen24ce0b32014-01-14 16:18:14 -08003476 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003477 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3478 if (searchProvider == null) {
3479 return null;
3480 }
3481
3482 Bundle opts = new Bundle();
3483 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003484 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003485
Tony Wickham3a3517f2015-10-06 11:27:04 -07003486 // Determine the min and max dimensions of the widget.
3487 LauncherAppState app = LauncherAppState.getInstance();
3488 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3489 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3490 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003491 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3492 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003493 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003494 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003495 int minWidth = maxWidth;
3496 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003497 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3498 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3499 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3500 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3501 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003502 }
3503 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3504 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3505 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3506 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003507 if (LOGD) {
3508 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3509 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3510 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003511
Tony Wickham775455c2015-10-16 09:49:32 -07003512 if (mLauncherCallbacks != null) {
3513 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3514 }
3515
Sunny Goyalf7258242015-10-19 16:59:07 -07003516 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003517 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003518 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003519 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003520 || (widgetInfo == null)
3521 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003522 // A valid widget is not already bound.
3523 if (widgetId > -1) {
3524 mAppWidgetHost.deleteAppWidgetId(widgetId);
3525 widgetId = -1;
3526 }
3527
3528 // Try to bind a new widget
3529 widgetId = mAppWidgetHost.allocateAppWidgetId();
3530
3531 if (!AppWidgetManagerCompat.getInstance(this)
3532 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3533 mAppWidgetHost.deleteAppWidgetId(widgetId);
3534 widgetId = -1;
3535 }
3536
Sunny Goyalf7258242015-10-19 16:59:07 -07003537 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003538 .putInt(QSB_WIDGET_ID, widgetId)
3539 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003540 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003541 }
3542
Sunny Goyal8d595092015-07-06 12:22:14 -07003543 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003544 if (widgetId != -1) {
3545 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003546 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003547 mQsb.updateAppWidgetOptions(opts);
3548 mQsb.setPadding(0, 0, 0, 0);
3549 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003550 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003551 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003552 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003553 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003554 }
3555
Sunny Goyal22235bc2015-04-03 09:23:43 -07003556 private void reinflateQSBIfNecessary() {
3557 if (mQsb instanceof LauncherAppWidgetHostView &&
3558 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3559 mSearchDropTargetBar.removeView(mQsb);
3560 mQsb = null;
3561 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3562 }
3563 }
3564
Michael Jurkad7c28052012-04-27 15:43:36 -07003565 @Override
3566 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003567 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003568 final List<CharSequence> text = event.getText();
3569 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003570 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003571 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003572 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003573 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003574 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003575 } else if (mWorkspace != null) {
3576 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003577 } else {
3578 text.add(getString(R.string.all_apps_home_button_label));
3579 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003580 return result;
3581 }
3582
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003583 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003584 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003585 */
Adam Cohen091440a2015-03-18 14:16:05 -07003586 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003587 @Override
3588 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003589 closeSystemDialogs();
3590 }
3591 }
3592
3593 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003594 * If the activity is currently paused, signal that we need to run the passed Runnable
3595 * in onResume.
3596 *
3597 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003598 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3599 * wrong when we're not running, and if the activity comes back to what the configuration was
3600 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 *
3602 * Implementation of the method from LauncherModel.Callbacks.
3603 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003604 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003606 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003607 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003608 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003609 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003610 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003611 }
3612 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003613 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003614 return true;
3615 } else {
3616 return false;
3617 }
3618 }
3619
Michael Jurkac402cd92013-05-20 15:49:32 +02003620 private boolean waitUntilResume(Runnable run) {
3621 return waitUntilResume(run, false);
3622 }
3623
Michael Jurka1e2f4652013-07-08 18:03:46 -07003624 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003625 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003626 }
3627
Michael Jurka7607c2f2013-04-03 14:33:19 -07003628 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003629 * If the activity is currently paused, signal that we need to re-run the loader
3630 * in onResume.
3631 *
3632 * This needs to be called from incoming places where resources might have been loaded
3633 * while we are paused. That is becaues the Configuration might be wrong
3634 * when we're not running, and if it comes back to what it was when we
3635 * were paused, we are not restarted.
3636 *
3637 * Implementation of the method from LauncherModel.Callbacks.
3638 *
3639 * @return true if we are currently paused. The caller might be able to
3640 * skip some work in that case since we will come back again.
3641 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003642 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003643 public boolean setLoadOnResume() {
3644 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003645 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003646 mOnResumeNeedsLoad = true;
3647 return true;
3648 } else {
3649 return false;
3650 }
3651 }
3652
3653 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003655 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003656 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003658 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003659 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003660 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003661 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003664
Joe Onorato9c1289c2009-08-17 11:03:03 -04003665 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003666 * Clear any pending bind callbacks. This is called when is loader is planning to
3667 * perform a full rebind from scratch.
3668 */
3669 @Override
3670 public void clearPendingBinds() {
3671 mBindOnResumeCallbacks.clear();
3672 if (mPendingExecutor != null) {
3673 mPendingExecutor.markCompleted();
3674 mPendingExecutor = null;
3675 }
3676 }
3677
3678 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 * Refreshes the shortcuts shown on the workspace.
3680 *
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 */
3683 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003684 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003685
Winson Chungd64d1762013-08-20 14:37:16 -07003686 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003687 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003688 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003689
Michael Jurka05bf644e2011-11-30 20:28:53 -08003690 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003691 if (mHotseat != null) {
3692 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003693 }
3694 }
3695
Adam Cohendcd297f2013-06-18 13:13:40 -07003696 @Override
3697 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003698 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003699
Adam Cohen5084cba2013-09-03 12:01:16 -07003700 // If there are no screens, we need to have an empty screen
3701 if (orderedScreenIds.size() == 0) {
3702 mWorkspace.addExtraEmptyScreen();
3703 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003704
3705 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003706 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003707 if (hasCustomContentToLeft()) {
3708 mWorkspace.createCustomContentContainer();
3709 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003710 }
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712
3713 @Override
3714 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003715 int count = orderedScreenIds.size();
3716 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003717 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003718 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003719 }
3720
Winson Chungd64d1762013-08-20 14:37:16 -07003721 public void bindAppsAdded(final ArrayList<Long> newScreens,
3722 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003723 final ArrayList<ItemInfo> addAnimated,
3724 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003725 Runnable r = new Runnable() {
3726 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003727 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003728 }
3729 };
3730 if (waitUntilResume(r)) {
3731 return;
3732 }
3733
Winson Chungd64d1762013-08-20 14:37:16 -07003734 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003735 if (newScreens != null) {
3736 bindAddScreens(newScreens);
3737 }
Winson Chungd64d1762013-08-20 14:37:16 -07003738
3739 // We add the items without animation on non-visible pages, and with
3740 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003741 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003742 bindItems(addNotAnimated, 0,
3743 addNotAnimated.size(), false);
3744 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003745 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003746 bindItems(addAnimated, 0,
3747 addAnimated.size(), true);
3748 }
Winson Chungc58497e2013-09-03 17:48:37 -07003749
Winson Chung87412982013-10-03 18:34:14 -07003750 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003751 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003752
Winson Chungb745afb2015-03-02 11:51:23 -08003753 if (addedApps != null && mAppsView != null) {
3754 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003755 }
Winson Chungd64d1762013-08-20 14:37:16 -07003756 }
3757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 * Bind the items start-end from the list.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003763 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003764 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3765 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 Runnable r = new Runnable() {
3767 public void run() {
3768 bindItems(shortcuts, start, end, forceAnimateIcons);
3769 }
3770 };
3771 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003772 return;
3773 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003774
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003776 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3777 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003778 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003779 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003780 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003781 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003783
3784 // Short circuit if we are loading dock items for a configuration which has no dock
3785 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3786 mHotseat == null) {
3787 continue;
3788 }
3789
Sunny Goyal639e9062015-08-19 19:17:06 -07003790 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 switch (item.itemType) {
3792 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3793 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003795 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003796
Winson Chung64359a52013-07-08 17:17:08 -07003797 /*
3798 * TODO: FIX collision case
3799 */
Winson Chungce376632013-07-11 16:12:41 -07003800 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3801 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3802 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003803 View v = cl.getChildAt(item.cellX, item.cellY);
3804 Object tag = v.getTag();
3805 String desc = "Collision while binding workspace item: " + item
3806 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003807 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003808 throw (new RuntimeException(desc));
3809 } else {
3810 Log.d(TAG, desc);
3811 }
Winson Chungce376632013-07-11 16:12:41 -07003812 }
Winson Chung64359a52013-07-08 17:17:08 -07003813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003815 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003816 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003817 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003818 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003820 default:
3821 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003823
3824 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3825 item.cellY, 1, 1);
3826 if (animateIcons) {
3827 // Animate all the applications up now
3828 view.setAlpha(0f);
3829 view.setScaleX(0f);
3830 view.setScaleY(0f);
3831 bounceAnims.add(createNewAppBounceAnimation(view, i));
3832 newShortcutsScreenId = item.screenId;
3833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003834 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835
Winson Chung997a9232013-07-24 15:33:46 -07003836 if (animateIcons) {
3837 // Animate to the correct page
3838 if (newShortcutsScreenId > -1) {
3839 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003840 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003841 final Runnable startBounceAnimRunnable = new Runnable() {
3842 public void run() {
3843 anim.playTogether(bounceAnims);
3844 anim.start();
3845 }
3846 };
Winson Chung997a9232013-07-24 15:33:46 -07003847 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003848 // We post the animation slightly delayed to prevent slowdowns
3849 // when we are loading right after we return to launcher.
3850 mWorkspace.postDelayed(new Runnable() {
3851 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003852 if (mWorkspace != null) {
3853 mWorkspace.snapToPage(newScreenIndex);
3854 mWorkspace.postDelayed(startBounceAnimRunnable,
3855 NEW_APPS_ANIMATION_DELAY);
3856 }
Winson Chung94d67682013-09-25 16:29:40 -07003857 }
3858 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003859 } else {
3860 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003861 }
3862 }
Winson Chung64359a52013-07-08 17:17:08 -07003863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003865 }
3866
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003867 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3868 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3869 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003870 view.updateAppWidget(null);
3871 view.setOnClickListener(this);
3872 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003873 mWorkspace.requestLayout();
3874 }
3875
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 /**
3877 * Add the views for a widget to the workspace.
3878 *
3879 * Implementation of the method from LauncherModel.Callbacks.
3880 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003881 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003882 Runnable r = new Runnable() {
3883 public void run() {
3884 bindAppWidget(item);
3885 }
3886 };
3887 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003888 return;
3889 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003890
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003891 if (mIsSafeModeEnabled) {
3892 bindSafeModeWidget(item);
3893 return;
3894 }
3895
Daniel Sandler843e8602010-06-07 14:59:01 -04003896 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3897 if (DEBUG_WIDGETS) {
3898 Log.d(TAG, "bindAppWidget: " + item);
3899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003901 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003902
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003903 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3904 // If the provider is not ready, bind as a pending widget.
3905 appWidgetInfo = null;
3906 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3907 // The widget id is not valid. Try to find the widget based on the provider info.
3908 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3909 } else {
3910 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3911 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003912
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003913 // If the provider is ready, but the width is not yet restored, try to restore it.
3914 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3915 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003916 if (appWidgetInfo == null) {
3917 if (DEBUG_WIDGETS) {
3918 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3919 + " belongs to component " + item.providerName
3920 + ", as the povider is null");
3921 }
3922 LauncherModel.deleteItemFromDatabase(this, item);
3923 return;
3924 }
Sunny Goyalff572272014-07-23 13:58:07 -07003925
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003926 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003927 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003928 // Note: This assumes that the id remap broadcast is received before this step.
3929 // If that is not the case, the id remap will be ignored and user may see the
3930 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003931 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003932 pendingInfo.spanX = item.spanX;
3933 pendingInfo.spanY = item.spanY;
3934 pendingInfo.minSpanX = item.minSpanX;
3935 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003936 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003937
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003938 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3939 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3940 newWidgetId, appWidgetInfo, options);
3941
3942 // TODO consider showing a permission dialog when the widget is clicked.
3943 if (!success) {
3944 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3945 if (DEBUG_WIDGETS) {
3946 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3947 + " belongs to component " + item.providerName
3948 + ", as the launcher is unable to bing a new widget id");
3949 }
3950 LauncherModel.deleteItemFromDatabase(this, item);
3951 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003952 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003953
3954 item.appWidgetId = newWidgetId;
3955
3956 // If the widget has a configure activity, it is still needs to set it up, otherwise
3957 // the widget is ready to go.
3958 item.restoreStatus = (appWidgetInfo.configure == null)
3959 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3960 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3961
3962 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003963 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003964 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003965 // The widget was marked as UI not ready, but there is no configure activity to
3966 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003967 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3968 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003969 }
Sunny Goyalff572272014-07-23 13:58:07 -07003970 }
3971
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003972 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003973 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003974 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3975 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003976 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003977
Sunny Goyal56c73602015-09-25 12:55:01 -07003978 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003979 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003980 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003981 deleteWidgetInfo(item);
3982 return;
3983 }
3984
Sunny Goyal233ee962015-08-03 13:05:01 -07003985 item.minSpanX = appWidgetInfo.minSpanX;
3986 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003987 addAppWidgetToWorkspace(
3988 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3989 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003991 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003992 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003993 view.updateAppWidget(null);
3994 view.setOnClickListener(this);
3995 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003996 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003997 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998
Daniel Sandler843e8602010-06-07 14:59:01 -04003999 if (DEBUG_WIDGETS) {
4000 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4001 + (SystemClock.uptimeMillis()-start) + "ms");
4002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 }
4004
Sunny Goyalff572272014-07-23 13:58:07 -07004005 /**
4006 * Restores a pending widget.
4007 *
4008 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004009 */
4010 private void completeRestoreAppWidget(final int appWidgetId) {
4011 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4012 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4013 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4014 return;
4015 }
4016
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004017 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004018 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4019
4020 mWorkspace.reinflateWidgetsIfNecessary();
4021 LauncherModel.updateItemInDatabase(this, info);
4022 }
4023
Adam Cohen1462de32012-07-24 22:34:36 -07004024 public void onPageBoundSynchronously(int page) {
4025 mSynchronouslyBoundPages.add(page);
4026 }
4027
Sunny Goyal527c7d32015-08-28 15:19:36 -07004028 @Override
4029 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4030 if (mPendingExecutor != null) {
4031 mPendingExecutor.markCompleted();
4032 }
4033 mPendingExecutor = executor;
4034 executor.attachTo(this);
4035 }
4036
4037 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4038 if (mPendingExecutor == executor) {
4039 mPendingExecutor = null;
4040 }
4041 }
4042
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043 /**
4044 * Callback saying that there aren't any more items to bind.
4045 *
4046 * Implementation of the method from LauncherModel.Callbacks.
4047 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004048 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004049 Runnable r = new Runnable() {
4050 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004051 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004052 }
4053 };
4054 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004055 return;
4056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057 if (mSavedState != null) {
4058 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004059 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004061
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 mSavedState = null;
4063 }
4064
Adam Cohen1462de32012-07-24 22:34:36 -07004065 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004067 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004068
4069 // If we received the result of any pending adds while the loader was running (e.g. the
4070 // widget configuration forced an orientation change), process them now.
4071 if (sPendingAddItem != null) {
4072 final long screenId = completeAdd(sPendingAddItem);
4073
4074 // TODO: this moves the user to the page where the pending item was added. Ideally,
4075 // the screen would be guaranteed to exist after bind, and the page would be set through
4076 // the workspace restore process.
4077 mWorkspace.post(new Runnable() {
4078 @Override
4079 public void run() {
4080 mWorkspace.snapToScreenId(screenId);
4081 }
4082 });
4083 sPendingAddItem = null;
4084 }
4085
Sunny Goyal756adbc2015-04-16 15:20:51 -07004086 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004087
4088 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004089 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004090 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004091 }
4092
Winson Chunga0b7e862013-09-05 16:03:15 -07004093 public boolean isAllAppsButtonRank(int rank) {
4094 if (mHotseat != null) {
4095 return mHotseat.isAllAppsButtonRank(rank);
4096 }
4097 return false;
4098 }
4099
Winson Chunga2413752012-04-03 14:22:34 -07004100 private boolean canRunNewAppsAnimation() {
4101 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004102 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4103 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004104 }
4105
Winson Chungc9168342013-06-26 14:54:55 -07004106 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004107 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004108 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4109 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004110 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004111 return bounceAnim;
4112 }
4113
Adam Cohen27772732013-11-11 14:00:56 +00004114 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004115 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004116 }
4117
Tony Wickham3a3517f2015-10-06 11:27:04 -07004118 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004119 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004120 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004121 }
4122
Tony Wickham55616cd2015-09-23 14:55:17 -07004123 public int getSearchBarHeight() {
4124 if (mLauncherCallbacks != null) {
4125 return mLauncherCallbacks.getSearchBarHeight();
4126 }
4127 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4128 }
4129
Winson Chung88fa7412015-08-03 14:25:28 -07004130 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004131 if (mSearchDropTargetBar == null) {
4132 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004133 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004134 if (mQsb != null) {
4135 mSearchDropTargetBar.removeView(mQsb);
4136 mQsb = null;
4137 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004138 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004139 }
4140
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004141 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004142 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4143 * multiple calls to bind the same list.)
4144 */
4145 @Thunk ArrayList<AppInfo> mTmpAppsList;
4146 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4147 public void run() {
4148 bindAllApplications(mTmpAppsList);
4149 mTmpAppsList = null;
4150 }
4151 };
4152
4153 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004154 * Add the icons for all apps.
4155 *
4156 * Implementation of the method from LauncherModel.Callbacks.
4157 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004158 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004159 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4160 mTmpAppsList = apps;
4161 return;
4162 }
4163
Winson Chungb745afb2015-03-02 11:51:23 -08004164 if (mAppsView != null) {
4165 mAppsView.setApps(apps);
4166 }
Adam Cohen9211d422014-10-07 18:14:53 -07004167 if (mLauncherCallbacks != null) {
4168 mLauncherCallbacks.bindAllApplications(apps);
4169 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004170 }
4171
4172 /**
4173 * A package was updated.
4174 *
4175 * Implementation of the method from LauncherModel.Callbacks.
4176 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004177 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004178 Runnable r = new Runnable() {
4179 public void run() {
4180 bindAppsUpdated(apps);
4181 }
4182 };
4183 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004184 return;
4185 }
4186
Winson Chungb745afb2015-03-02 11:51:23 -08004187 if (mAppsView != null) {
4188 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004190 }
4191
Sunny Goyal4390ace2014-10-13 11:33:11 -07004192 @Override
4193 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4194 Runnable r = new Runnable() {
4195 public void run() {
4196 bindWidgetsRestored(widgets);
4197 }
4198 };
4199 if (waitUntilResume(r)) {
4200 return;
4201 }
4202 mWorkspace.widgetsRestored(widgets);
4203 }
4204
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004206 * Some shortcuts were updated in the background.
4207 *
4208 * Implementation of the method from LauncherModel.Callbacks.
4209 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004210 @Override
4211 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4212 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004213 Runnable r = new Runnable() {
4214 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004215 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004216 }
4217 };
4218 if (waitUntilResume(r)) {
4219 return;
4220 }
4221
Sunny Goyal4390ace2014-10-13 11:33:11 -07004222 if (!updated.isEmpty()) {
4223 mWorkspace.updateShortcuts(updated);
4224 }
4225
4226 if (!removed.isEmpty()) {
4227 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4228 for (ShortcutInfo si : removed) {
4229 removedComponents.add(si.getTargetComponent());
4230 }
4231 mWorkspace.removeItemsByComponentName(removedComponents, user);
4232 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004233 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004234 }
4235 }
4236
4237 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004238 * Update the state of a package, typically related to install state.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Sunny Goyale755d462014-07-22 13:48:29 -07004242 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004243 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4244 Runnable r = new Runnable() {
4245 public void run() {
4246 bindRestoreItemsChange(updates);
4247 }
4248 };
4249 if (waitUntilResume(r)) {
4250 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004251 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004252
Sunny Goyal756adbc2015-04-16 15:20:51 -07004253 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004254 }
4255
4256 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004257 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004258 * in addition to specific applications to remove, the reason being that
4259 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004260 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004261 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004262 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004263 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004264 public void bindWorkspaceComponentsRemoved(
4265 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4266 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004267 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004268 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004269 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004270 }
Winson Chungd64d1762013-08-20 14:37:16 -07004271 };
4272 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004273 return;
4274 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004275 if (!packageNames.isEmpty()) {
4276 mWorkspace.removeItemsByPackageName(packageNames, user);
4277 }
4278 if (!components.isEmpty()) {
4279 mWorkspace.removeItemsByComponentName(components, user);
4280 }
4281 // Notify the drag controller
4282 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004283
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004284 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004285
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004286 @Override
4287 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4288 Runnable r = new Runnable() {
4289 public void run() {
4290 bindAppInfosRemoved(appInfos);
4291 }
4292 };
4293 if (waitUntilResume(r)) {
4294 return;
Joe Onorato36115782010-06-17 13:28:48 -04004295 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004296
Winson Chungdf95eb12013-10-16 14:57:07 -07004297 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004298 if (mAppsView != null) {
4299 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 }
Joe Onoratobe386092009-11-17 17:32:16 -08004302
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004303 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004304 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004305 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004306 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004307 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004308
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004309 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004310 public void bindWidgetsModel(WidgetsModel model) {
4311 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004312 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004313 return;
4314 }
4315
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004316 if (mWidgetsView != null && model != null) {
4317 mWidgetsView.addWidgets(model);
4318 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004319 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004320 }
4321
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004322 @Override
4323 public void notifyWidgetProvidersChanged() {
4324 if (mWorkspace.getState().shouldUpdateWidget) {
4325 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4326 }
4327 }
4328
Winson Chung400438b2011-01-16 17:53:48 -08004329 private int mapConfigurationOriActivityInfoOri(int configOri) {
4330 final Display d = getWindowManager().getDefaultDisplay();
4331 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4332 switch (d.getRotation()) {
4333 case Surface.ROTATION_0:
4334 case Surface.ROTATION_180:
4335 // We are currently in the same basic orientation as the natural orientation
4336 naturalOri = configOri;
4337 break;
4338 case Surface.ROTATION_90:
4339 case Surface.ROTATION_270:
4340 // We are currently in the other basic orientation to the natural orientation
4341 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4342 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4343 break;
4344 }
4345
4346 int[] oriMap = {
4347 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4348 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4349 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4350 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4351 };
4352 // Since the map starts at portrait, we need to offset if this device's natural orientation
4353 // is landscape.
4354 int indexOffset = 0;
4355 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4356 indexOffset = 1;
4357 }
4358 return oriMap[(d.getRotation() + indexOffset) % 4];
4359 }
Adam Cohen446e9402011-09-15 18:21:21 -07004360
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004361 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004362 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004363 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004364 if (Utilities.ATLEAST_JB_MR2) {
4365 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4366 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004367 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4368 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004369 }
Winson Chung4b919f82012-05-01 10:44:08 -07004370 }
4371 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004372
Winson Chung4b919f82012-05-01 10:44:08 -07004373 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004374 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004375 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004376 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004377 } else {
4378 mHandler.postDelayed(new Runnable() {
4379 public void run() {
4380 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4381 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004382 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004383 }
Winson Chung4b919f82012-05-01 10:44:08 -07004384 }
Winson Chung400438b2011-01-16 17:53:48 -08004385 }
4386
Winson Chunge43a1e72014-01-15 10:33:02 -08004387 protected boolean isLauncherPreinstalled() {
4388 PackageManager pm = getPackageManager();
4389 try {
4390 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4391 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4392 return true;
4393 } else {
4394 return false;
4395 }
4396 } catch (NameNotFoundException e) {
4397 e.printStackTrace();
4398 return false;
4399 }
4400 }
4401
Adam Cohenea90f832014-05-21 15:03:34 -07004402 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004403 * To be overridden by subclasses to indicate that there is an activity to launch
4404 * before showing the standard launcher experience.
4405 */
4406 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004407 if (mLauncherCallbacks != null) {
4408 return mLauncherCallbacks.hasFirstRunActivity();
4409 }
Adam Cohen432609a2014-03-13 17:03:22 -07004410 return false;
4411 }
4412
4413 /**
4414 * To be overridden by subclasses to launch any first run activity
4415 */
4416 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004417 if (mLauncherCallbacks != null) {
4418 return mLauncherCallbacks.getFirstRunActivity();
4419 }
Adam Cohen432609a2014-03-13 17:03:22 -07004420 return null;
4421 }
4422
Winson Chunga6945242014-01-08 14:04:34 -08004423 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004424 return !ActivityManager.isRunningInTestHarness() &&
4425 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004426 }
4427
Adam Cohen4a9423d2014-03-28 14:22:31 -07004428 protected boolean hasRunFirstRunActivity() {
4429 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4430 }
4431
Adam Cohen432609a2014-03-13 17:03:22 -07004432 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004433 if (shouldRunFirstRunActivity() &&
4434 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004435 Intent firstRunIntent = getFirstRunActivity();
4436 if (firstRunIntent != null) {
4437 startActivity(firstRunIntent);
4438 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004439 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004440 }
4441 }
Adam Cohen432609a2014-03-13 17:03:22 -07004442 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004443 }
4444
4445 private void markFirstRunActivityShown() {
4446 SharedPreferences.Editor editor = mSharedPrefs.edit();
4447 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4448 editor.apply();
4449 }
4450
Adam Cohen432609a2014-03-13 17:03:22 -07004451 /**
4452 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4453 * screen that must be displayed and dismissed.
4454 */
4455 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004456 if (mLauncherCallbacks != null) {
4457 return mLauncherCallbacks.hasDismissableIntroScreen();
4458 }
Adam Cohen432609a2014-03-13 17:03:22 -07004459 return false;
4460 }
4461
4462 /**
4463 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4464 */
4465 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004466 if (mLauncherCallbacks != null) {
4467 return mLauncherCallbacks.getIntroScreen();
4468 }
Adam Cohen432609a2014-03-13 17:03:22 -07004469 return null;
4470 }
4471
4472 /**
4473 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4474 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4475 */
4476 private boolean shouldShowIntroScreen() {
4477 return hasDismissableIntroScreen() &&
4478 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4479 }
4480
4481 protected void showIntroScreen() {
4482 View introScreen = getIntroScreen();
4483 changeWallpaperVisiblity(false);
4484 if (introScreen != null) {
4485 mDragLayer.showOverlayView(introScreen);
4486 }
4487 }
4488
4489 public void dismissIntroScreen() {
4490 markIntroScreenDismissed();
4491 if (showFirstRunActivity()) {
4492 // We delay hiding the intro view until the first run activity is showing. This
4493 // avoids a blip.
4494 mWorkspace.postDelayed(new Runnable() {
4495 @Override
4496 public void run() {
4497 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004498 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004499 }
4500 }, ACTIVITY_START_DELAY);
4501 } else {
4502 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004503 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004504 }
4505 changeWallpaperVisiblity(true);
4506 }
4507
4508 private void markIntroScreenDismissed() {
4509 SharedPreferences.Editor editor = mSharedPrefs.edit();
4510 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4511 editor.apply();
4512 }
4513
Adam Cohen091440a2015-03-18 14:16:05 -07004514 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004515 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4516 // on the device, then we always show the first run cling experience (or if there is no
4517 // launcher2). Otherwise, we prompt the user upon started for migration
4518 LauncherClings launcherClings = new LauncherClings(this);
4519 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004520 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004521 if (mModel.canMigrateFromOldLauncherDb(this)) {
4522 launcherClings.showMigrationCling();
4523 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004524 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004525 }
4526 }
4527 }
4528
Winson Chunga6945242014-01-08 14:04:34 -08004529 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004530 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4531 if (mHotseat != null) mHotseat.setAlpha(1f);
Tony Wickhamf549dab2016-05-16 09:54:06 -07004532 if (mPageIndicator != null) mPageIndicator.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004533 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4534 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004535 }
4536
4537 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004538 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4539 if (mHotseat != null) mHotseat.setAlpha(0f);
Tony Wickhamf549dab2016-05-16 09:54:06 -07004540 if (mPageIndicator != null) mPageIndicator.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004541 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4542 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004543 }
4544
Winson Chung5ffd51d2015-06-25 11:36:50 -07004545 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004547 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004548 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004549 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004550 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004551 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4552 if (userHandle != null) {
4553 user = UserHandleCompat.fromUser(userHandle);
4554 }
4555 }
4556 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004557 }
4558
4559 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004560 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004561 if (user == null) {
4562 user = UserHandleCompat.myUserHandle();
4563 }
4564
4565 // Called from search suggestion, add the profile extra to the intent to ensure that we
4566 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004567 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004568 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004569 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 return null;
4571 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004572 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 }
4574
Winson Chung5ffd51d2015-06-25 11:36:50 -07004575 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004576 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4577 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004578 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004579 UserHandleCompat.myUserHandle());
4580 }
4581
Winson Chung5ffd51d2015-06-25 11:36:50 -07004582 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004583 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4584 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004585 mWorkspace.onExternalDragStartedWithItem(dragView);
4586 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004587 }
4588
Winson Chung5ffd51d2015-06-25 11:36:50 -07004589 protected void moveWorkspaceToDefaultScreen() {
4590 mWorkspace.moveToDefaultScreen(false);
4591 }
4592
Winson Chung80baf5a2010-08-09 16:03:15 -07004593 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004594 * Returns a FastBitmapDrawable with the icon, accurately sized.
4595 */
4596 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4597 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4598 d.setFilterBitmap(true);
4599 resizeIconDrawable(d);
4600 return d;
4601 }
4602
4603 /**
4604 * Resizes an icon drawable to the correct icon size.
4605 */
Winson5fbe0742015-09-30 15:33:00 -07004606 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004607 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004608 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004609 }
4610
4611 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004612 * Prints out out state for debugging.
4613 */
4614 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004615 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004616 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004617 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4618 Log.d(TAG, "mRestoring=" + mRestoring);
4619 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004620 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004621 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004622
Daniel Sandler325dc232013-06-05 22:57:57 -04004623 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004624 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004625
4626 @Override
4627 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4628 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004629 // Dump workspace
4630 writer.println(prefix + "Workspace Items");
4631 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4632 writer.println(prefix + " Homescreen " + i);
4633
4634 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4635 for (int j = 0; j < layout.getChildCount(); j++) {
4636 Object tag = layout.getChildAt(j).getTag();
4637 if (tag != null) {
4638 writer.println(prefix + " " + tag.toString());
4639 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004640 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004641 }
Sunny Goyala1365452015-10-01 15:46:24 -07004642
4643 writer.println(prefix + " Hotseat");
4644 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4645 for (int j = 0; j < layout.getChildCount(); j++) {
4646 Object tag = layout.getChildAt(j).getTag();
4647 if (tag != null) {
4648 writer.println(prefix + " " + tag.toString());
4649 }
4650 }
4651
Sunny Goyal713edfc2016-05-06 09:58:34 -07004652 try {
4653 FileLog.flushAll(writer);
4654 } catch (Exception e) {
4655 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004656 }
4657
Adam Cohen9211d422014-10-07 18:14:53 -07004658 if (mLauncherCallbacks != null) {
4659 mLauncherCallbacks.dump(prefix, fd, writer, args);
4660 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004661 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004662
Adam Cohen59400422014-03-05 18:07:04 -08004663 public static CustomAppWidget getCustomAppWidget(String name) {
4664 return sCustomAppWidgets.get(name);
4665 }
4666
4667 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4668 return sCustomAppWidgets;
4669 }
4670
Sunny Goyal29538332016-02-18 09:10:19 -08004671 public static List<View> getFolderContents(View icon) {
4672 if (icon instanceof FolderIcon) {
4673 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4674 } else {
4675 return Collections.EMPTY_LIST;
4676 }
4677 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004678
4679 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4680
4681 @Override
4682 public void onSharedPreferenceChanged(
4683 SharedPreferences sharedPreferences, String key) {
4684 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4685 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4686 if (!waitUntilResume(this, true)) {
4687 run();
4688 }
4689 }
4690 }
4691
4692 @Override
4693 public void run() {
4694 setOrientation();
4695 }
4696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004697}
Michael Jurka2763be32011-02-24 11:19:57 -08004698
Dan Sandlerd5024042014-01-09 15:01:33 -05004699interface DebugIntents {
4700 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4701 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004702}