blob: 7da31cd7c622f71dd053acdb385248a11b073bcb [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;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070055import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070087import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070096import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080097import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800103import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700108import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800111import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800112import com.android.launcher3.logging.UserEventLogger;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700113import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700116import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700117import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700123
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700130import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800134import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700140 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Sunny Goyal53405842016-04-14 12:30:40 -0700141 View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700150 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700188 // Type: Content Values / parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Sunny Goyal594d76d2014-11-06 10:12:54 -0800198 private static final String QSB_WIDGET_ID = "qsb_widget_id";
199 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
200
Winson Chunga6945242014-01-08 14:04:34 -0800201 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700226 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800227 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700230
231 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Sunny Goyalffe83f12014-08-14 17:39:34 -0700233 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700236 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800237 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700249 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700281 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700282 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700284 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Sunny Goyal639e9062015-08-19 19:17:06 -0700287 private LauncherClings mClings;
288
Adam Cohen61f560d2013-09-30 15:58:20 -0700289 private View.OnTouchListener mHapticFeedbackTouchListener;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700293 private static final int ADVANCE_INTERVAL = 20000;
294 private static final int ADVANCE_STAGGER = 250;
295
296 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Sunny Goyala1365452015-10-01 15:46:24 -0700310 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
311 private static final Date sDateStamp = new Date();
312 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700313 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700314
Winson Chung46353de2012-02-16 14:05:10 -0800315 // We only want to get the SharedPreferences once since it does an FS stat each time we get
316 // it from the context.
317 private SharedPreferences mSharedPrefs;
318
Winson Chungf0c6ae02012-03-21 16:10:31 -0700319 // Holds the page that we need to animate to, and the icon views that we need to animate up
320 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700322 private Bitmap mFolderIconBitmap;
323 private Canvas mFolderIconCanvas;
324 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700326 private DeviceProfile mDeviceProfile;
327
Adam Cohen4b66bf32015-09-18 12:15:19 -0700328 private boolean mMoveToDefaultScreenFromNewIntent;
329
Winson Chung13eb5272015-05-11 16:30:13 -0700330 // This is set to the view that launched the activity that navigated the user away from
331 // launcher. Since there is no callback for when the activity has finished launching, enable
332 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
336 new HashMap<String, CustomAppWidget>();
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700339 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
340 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800341 }
342 }
343
Adam Cohen091440a2015-03-18 14:16:05 -0700344 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700345 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700346 if (mWorkspace != null) {
347 mWorkspace.buildPageHardwareLayers();
348 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 }
350 };
351
Adam Cohendb364c32014-05-20 14:23:40 -0700352 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Adam Cohen091440a2015-03-18 14:16:05 -0700354 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 int requestCode;
356 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700357 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700358 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int cellX;
360 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700361 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 }
363
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800364 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800365
Adam Cohenf9c184a2016-01-15 16:47:43 -0800366 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400368
369 @Thunk void setOrientation() {
370 if (mRotationEnabled) {
371 unlockScreenOrientation(true);
372 } else {
373 setRequestedOrientation(
374 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700375 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400376 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377
Sunny Goyal7779d622015-06-11 16:18:39 -0700378 private Runnable mUpdateOrientationRunnable = new Runnable() {
379 public void run() {
380 setOrientation();
381 }
382 };
383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
400
Adam Cohen9211d422014-10-07 18:14:53 -0700401 if (mLauncherCallbacks != null) {
402 mLauncherCallbacks.preOnCreate();
403 }
404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400407 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700408
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700410 mDeviceProfile = getResources().getConfiguration().orientation
411 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700412 app.getInvariantDeviceProfile().landscapeProfile
413 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700414
Sunny Goyalf7258242015-10-19 16:59:07 -0700415 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700416 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800417 mModel = app.setLauncher(this);
418 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700419
Joe Onorato41a12d22009-10-31 18:30:00 -0400420 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700421 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800423 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400424
Sunny Goyalffe83f12014-08-14 17:39:34 -0700425 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700441
Tony Wickhameef44322015-10-20 13:24:36 -0700442 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
443 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700445 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700446 mExtractedColors = new ExtractedColors();
447 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Tony Wickhame2217252016-03-22 16:34:23 -0700449 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
450 .addAccessibilityStateChangeListener(this);
451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700485 }
486
487 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
488 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400489 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
493 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700494
495 if (shouldShowIntroScreen()) {
496 showIntroScreen();
497 } else {
498 showFirstRunActivity();
499 showFirstRunClings();
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
502
Sunny Goyal7779d622015-06-11 16:18:39 -0700503 @Override
504 public void onSettingsChanged(String settings, boolean value) {
505 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
506 mRotationEnabled = value;
507 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
508 mUpdateOrientationRunnable.run();
509 }
510 }
511 }
512
Tony Wickham827cef22016-03-17 15:39:39 -0700513 @Override
514 public void onExtractedColorsChanged() {
515 loadExtractedColorsAndColorItems();
516 }
517
518 private void loadExtractedColorsAndColorItems() {
519 if (mExtractedColors != null) {
520 mExtractedColors.load(this);
521 // TODO: pass mExtractedColors to interested items such as hotseat.
Tony Wickham827cef22016-03-17 15:39:39 -0700522 }
523 }
524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 private LauncherCallbacks mLauncherCallbacks;
526
527 public void onPostCreate(Bundle savedInstanceState) {
528 super.onPostCreate(savedInstanceState);
529 if (mLauncherCallbacks != null) {
530 mLauncherCallbacks.onPostCreate(savedInstanceState);
531 }
532 }
533
Sunny Goyal32554d12015-12-03 15:31:25 -0800534 /**
535 * Call this after onCreate to set or clear overlay.
536 */
537 public void setLauncherOverlay(LauncherOverlay overlay) {
538 if (overlay != null) {
539 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
540 }
541 mWorkspace.setLauncherOverlay(overlay);
542 }
543
Adam Cohen9211d422014-10-07 18:14:53 -0700544 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
545 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700547 private boolean mWorkspaceImportanceStored = false;
548 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800550 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700551 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
552
553 @Override
554 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 return;
557 }
558 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700559 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700560 if (mWorkspace != null) {
561 mWorkspaceImportanceForAccessibility =
562 mWorkspace.getImportantForAccessibility();
563 mWorkspace.setImportantForAccessibility(
564 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
565 mWorkspaceImportanceStored = true;
566 }
567 if (mHotseat != null) {
568 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
569 mHotseat.setImportantForAccessibility(
570 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
571 mHotseatImportanceStored = true;
572 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700573 }
574
575 @Override
576 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700577 if (mWorkspaceImportanceStored && mWorkspace != null) {
578 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
579 }
580 if (mHotseatImportanceStored && mHotseat != null) {
581 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
582 }
583 mWorkspaceImportanceStored = false;
584 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700585 }
586 });
Adam Cohen9211d422014-10-07 18:14:53 -0700587 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700588 }
589
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700590 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700591 public void onLauncherProviderChange() {
592 if (mLauncherCallbacks != null) {
593 mLauncherCallbacks.onLauncherProviderChange();
594 }
595 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700596
Adam Cohen9211d422014-10-07 18:14:53 -0700597 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700598 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700599 if (mLauncherCallbacks != null) {
600 return mLauncherCallbacks.hasCustomContentToLeft();
601 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700602 return false;
603 }
604
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500606 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 * {@link #addToCustomContentPage}. This will only be invoked if
608 * {@link #hasCustomContentToLeft()} is {@code true}.
609 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500610 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700611 if (mLauncherCallbacks != null) {
612 mLauncherCallbacks.populateCustomContentContainer();
613 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 }
615
616 /**
617 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
618 * ensure the custom content page is added or removed if necessary.
619 */
620 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600621 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 // Not bound yet, wait for bindScreens to be called.
623 return;
624 }
625
626 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
627 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500628 mWorkspace.createCustomContentContainer();
629 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600630 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
631 mWorkspace.removeCustomContentPage();
632 }
633 }
634
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800635 /**
636 * Logger object is a singleton and does not have to be coupled with the foreground activity.
637 * Since most user event logging is done on the UI, the object is retrieved from the
638 * callback for convenience.
639 */
640 private void initLogger() {
641 if (mLauncherCallbacks != null) {
642 mUserEventLogger = mLauncherCallbacks.getLogger();
643 }
644 if (mUserEventLogger == null) {
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700645 mUserEventLogger = new UserEventLogger(this) {
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800646 @Override
647 public void processEvent(LauncherLogProto.LauncherEvent ev) {
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700648 if (!DEBUG_LOGGING) {
649 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800650 }
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700651 Log.d("UserEvent", String.format(Locale.US,
652 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
653 LoggerUtils.getActionStr(ev.action),
654 LoggerUtils.getTargetStr(ev.srcTarget[0]),
655 LoggerUtils.getTargetStr(ev.srcTarget[1]),
656 ev.elapsedContainerMillis,
657 ev.elapsedSessionMillis));
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800658 }
659 };
660 }
661 }
662
663 public UserEventLogger getLogger() {
664 return mUserEventLogger;
665 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666
Hyunyoung Song3f471442015-04-08 19:01:34 -0700667 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700668 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
669 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700670 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700671 }
672
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700674 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
675 // This cast is safe as long as the id < 0x00FFFFFF
676 // Since we jail all the dynamically generated views, there should be no clashes
677 // with any other views.
678 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000679 }
680
681 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700682 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
683 * a configuration step, this allows the proper animations to run after other transitions.
684 */
Adam Cohendb364c32014-05-20 14:23:40 -0700685 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700686 long screenId = args.screenId;
687 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
688 // When the screen id represents an actual screen (as opposed to a rank) we make sure
689 // that the drop page actually exists.
690 screenId = ensurePendingDropLayoutExists(args.screenId);
691 }
Adam Cohendb364c32014-05-20 14:23:40 -0700692
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700695 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700696 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700700 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700701 case REQUEST_RECONFIGURE_APPWIDGET:
702 completeRestoreAppWidget(args.appWidgetId);
703 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
Michael Jurka27614d22012-04-02 06:40:22 -0700705 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
706 // if you turned the screen off and then back while in All Apps, Launcher would not
707 // return to the workspace. Clearing mAddInfo.container here fixes this issue
708 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700709 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800710 }
711
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800712 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700714 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700715 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700716 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800717 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700718
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 Runnable exitSpringLoaded = new Runnable() {
720 @Override
721 public void run() {
722 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
723 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
724 }
725 };
726
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700728 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
731 if (resultCode == RESULT_CANCELED) {
732 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700733 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800736 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700738
739 // When the user has granted permission to bind widgets, we should check to see if
740 // we can inflate the default search bar widget.
741 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
743 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200744 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
745 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700746 // User could have free-scrolled between pages before picking a wallpaper; make sure
747 // we move to the closest one now.
748 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700749 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200750 }
751 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200753
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700755 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700756
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 // We have special handling for widgets
759 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800760 final int appWidgetId;
761 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
762 : -1;
763 if (widgetId < 0) {
764 appWidgetId = pendingAddWidgetId;
765 } else {
766 appWidgetId = widgetId;
767 }
768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800770 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700771 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
772 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 result = RESULT_CANCELED;
774 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700775 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 @Override
777 public void run() {
778 exitSpringLoadedDragModeDelayed(false, 0, null);
779 }
780 };
Adam Cohendb364c32014-05-20 14:23:40 -0700781 if (workspaceLocked) {
782 // No need to remove the empty screen if we're mid-binding, as the
783 // the bind will not add the empty screen.
784 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
785 } else {
786 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
788 }
Winson Chung5aaab772012-04-27 15:24:02 -0700789 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700790 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700791 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
792 // When the screen id represents an actual screen (as opposed to a rank)
793 // we make sure that the drop page actually exists.
794 mPendingAddInfo.screenId =
795 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
796 }
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
798
799 dropLayout.setDropPending(true);
800 final Runnable onComplete = new Runnable() {
801 @Override
802 public void run() {
803 completeTwoStageWidgetDrop(resultCode, appWidgetId);
804 dropLayout.setDropPending(false);
805 }
806 };
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
809 } else {
810 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
811 mPendingAddInfo);
812 sPendingAddItem = args;
813 }
Winson Chung5aaab772012-04-27 15:24:02 -0700814 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 return;
816 }
817
Sunny Goyalff572272014-07-23 13:58:07 -0700818 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
819 if (resultCode == RESULT_OK) {
820 // Update the widget view.
821 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
822 pendingAddWidgetId, mPendingAddInfo);
823 if (workspaceLocked) {
824 sPendingAddItem = args;
825 } else {
826 completeAdd(args);
827 }
828 }
829 // Leave the widget in the pending state if the user canceled the configure.
830 return;
831 }
832
Sunny Goyalaad90582015-07-09 14:22:50 -0700833 if (requestCode == REQUEST_CREATE_SHORTCUT) {
834 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
835 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
836 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
837 mPendingAddInfo);
838 if (isWorkspaceLocked()) {
839 sPendingAddItem = args;
840 } else {
841 completeAdd(args);
842 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
843 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
844 }
845 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700846 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800851
852 }
853
854 @Override
855 protected void onActivityResult(
856 final int requestCode, final int resultCode, final Intent data) {
857 handleActivityResult(requestCode, resultCode, data);
858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 }
862
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600863 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700864 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600865 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
867 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
868 View v = null;
869 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
870 if (layout != null) {
871 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
872 }
873 Intent intent = sPendingAddItem.intent;
874 sPendingAddItem = null;
875 if (grantResults.length > 0
876 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
877 startActivity(v, intent, null);
878 } else {
879 // TODO: Show a snack bar with link to settings
880 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
881 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
882 }
883 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
886 grantResults);
887 }
888 }
889
Adam Cohendb364c32014-05-20 14:23:40 -0700890 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
891 appWidgetId, ItemInfo info) {
892 PendingAddArguments args = new PendingAddArguments();
893 args.requestCode = requestCode;
894 args.intent = data;
895 args.container = info.container;
896 args.screenId = info.screenId;
897 args.cellX = info.cellX;
898 args.cellY = info.cellY;
899 args.appWidgetId = appWidgetId;
900 return args;
901 }
902
903 /**
904 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
905 *
906 * @param screenId the screen id to check
907 * @return the new screen, or screenId if it exists
908 */
909 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800910 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700911 if (dropLayout == null) {
912 // it's possible that the add screen was removed because it was
913 // empty and a re-bind occurred
914 mWorkspace.addExtraEmptyScreen();
915 return mWorkspace.commitExtraEmptyScreen();
916 } else {
917 return screenId;
918 }
919 }
920
Adam Cohen091440a2015-03-18 14:16:05 -0700921 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800922 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
925
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 mPendingAddWidgetInfo);
931 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 onCompleteRunnable = new Runnable() {
933 @Override
934 public void run() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
940 };
941 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 protected void onStop() {
957 super.onStop();
958 FirstFrameAnimatorHelper.setIsVisible(false);
Sunny Goyal23ac9cb2016-04-07 12:14:37 -0700959 if (Utilities.isNycOrAbove()) {
960 mAppWidgetHost.stopListening();
961 }
Adam Cohen9211d422014-10-07 18:14:53 -0700962
963 if (mLauncherCallbacks != null) {
964 mLauncherCallbacks.onStop();
965 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700966 }
967
968 @Override
969 protected void onStart() {
970 super.onStart();
971 FirstFrameAnimatorHelper.setIsVisible(true);
Sunny Goyal23ac9cb2016-04-07 12:14:37 -0700972 if (Utilities.isNycOrAbove()) {
973 mAppWidgetHost.startListening();
974 }
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStart();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200983 long startTime = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400986 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.preOnResume();
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800994 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700995
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700997 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001000 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001001 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001002 // view after launching an app, as they may be depending on the UI to be static to
1003 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001004 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001005 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.WIDGETS) {
1007 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 }
1009 mOnResumeState = State.NONE;
1010
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001011 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001012 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1013 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001014
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001015 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001017 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001018 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001023 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1024 // execute them here
1025 long startTimeCallbacks = 0;
1026 if (DEBUG_RESUME_TIME) {
1027 startTimeCallbacks = System.currentTimeMillis();
1028 }
1029
Michael Jurka1e2f4652013-07-08 18:03:46 -07001030 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1031 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1036 (System.currentTimeMillis() - startTimeCallbacks));
1037 }
Michael Jurka447bf842013-05-15 14:52:15 +02001038 }
Michael Jurka54554252013-08-01 12:52:23 +02001039 if (mOnResumeCallbacks.size() > 0) {
1040 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1041 mOnResumeCallbacks.get(i).run();
1042 }
1043 mOnResumeCallbacks.clear();
1044 }
Winson Chunge4e50662012-01-23 14:45:13 -08001045
1046 // Reset the pressed state of icons that were locked in the press state while activities
1047 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001049 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 mWaitingForResume.setStayPressed(false);
1051 }
Winson Chung82963d52013-10-09 11:20:57 -07001052
Adam Cohen06dff352012-06-01 17:17:08 -07001053 // It is possible that widgets can receive updates while launcher is not in the foreground.
1054 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1055 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1056 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001057 if (!isWorkspaceLoading()) {
1058 getWorkspace().reinflateWidgetsIfNecessary();
1059 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001060 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001061
Michael Jurka447bf842013-05-15 14:52:15 +02001062 if (DEBUG_RESUME_TIME) {
1063 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1064 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001065
Adam Cohen4b66bf32015-09-18 12:15:19 -07001066 // We want to suppress callbacks about CustomContent being shown if we have just received
1067 // onNewIntent while the user was present within launcher. In that case, we post a call
1068 // to move the user to the main screen (which will occur after onResume). We don't want to
1069 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1070 // suppress here.
1071 if (mWorkspace.getCustomContentCallbacks() != null
1072 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001074 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001075 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1076 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001077 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001078 }
1079 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001080 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001081 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001082 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001083
Sunny Goyal756adbc2015-04-16 15:20:51 -07001084 if (!isWorkspaceLoading()) {
1085 // Process any items that were added while Launcher was away.
1086 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1087 }
Adam Cohen9211d422014-10-07 18:14:53 -07001088
1089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onResume();
1091 }
Adam Cohen06dff352012-06-01 17:17:08 -07001092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001096 // Ensure that items added to Launcher are queued until Launcher returns
1097 InstallShortcutReceiver.enableInstallQueue();
1098
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001100 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001101 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001102 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001103
1104 // We call onHide() aggressively. The custom content callbacks should be able to
1105 // debounce excess onHide calls.
1106 if (mWorkspace.getCustomContentCallbacks() != null) {
1107 mWorkspace.getCustomContentCallbacks().onHide();
1108 }
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Adam Cohen9211d422014-10-07 18:14:53 -07001110 if (mLauncherCallbacks != null) {
1111 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
1114
1115 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001116 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1117 // by a onResume or by scrolling otherwise.
1118 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001119
1120 // Custom content is completely hidden
1121 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001122
1123 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1124 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001125
1126 // Indicates whether the user is allowed to scroll away from the custom content.
1127 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001128 }
1129
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 public interface LauncherOverlay {
1131
1132 /**
1133 * Touch interaction leading to overscroll has begun
1134 */
1135 public void onScrollInteractionBegin();
1136
1137 /**
1138 * Touch interaction related to overscroll has ended
1139 */
1140 public void onScrollInteractionEnd();
1141
1142 /**
1143 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1144 * screen (or in the case of RTL, the rightmost screen).
1145 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001146 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147
1148 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001149 * Called when the launcher is ready to use the overlay
1150 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001152 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 }
1154
Jun Mukai8af0cd82015-05-12 12:32:12 -07001155 public interface LauncherSearchCallbacks {
1156 /**
1157 * Called when the search overlay is shown.
1158 */
1159 public void onSearchOverlayOpened();
1160
1161 /**
1162 * Called when the search overlay is dismissed.
1163 */
1164 public void onSearchOverlayClosed();
1165 }
1166
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001168 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 }
1170
1171 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1172
Sunny Goyalc86df472016-02-25 09:19:38 -08001173 public void onScrollChanged(float progress) {
1174 if (mWorkspace != null) {
1175 mWorkspace.onOverlayScrollChanged(progress);
1176 }
1177 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001178 }
1179
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001181 if (mLauncherCallbacks != null) {
1182 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001183 } else {
1184 // On devices with a locked orientation, we will at least have the allow rotation
1185 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001186 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001187 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 }
1189
Adam Cohen9211d422014-10-07 18:14:53 -07001190 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001191 CustomContentCallbacks callbacks, String description) {
1192 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001193 }
1194
Adam Cohenedb40762013-07-18 16:45:45 -07001195 // The custom content needs to offset its content to account for the QSB
1196 public int getTopOffsetForCustomContent() {
1197 return mWorkspace.getPaddingTop();
1198 }
1199
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 @Override
1201 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 if (mModel.isCurrentCallbacks(this)) {
1204 mModel.stopLoader();
1205 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001206 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1207
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 @Override
1213 public void onWindowFocusChanged(boolean hasFocus) {
1214 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001215 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001216
1217 if (mLauncherCallbacks != null) {
1218 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 private boolean acceptFilter() {
1223 final InputMethodManager inputManager = (InputMethodManager)
1224 getSystemService(Context.INPUT_METHOD_SERVICE);
1225 return !inputManager.isFullscreenMode();
1226 }
1227
1228 @Override
1229 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001230 final int uniChar = event.getUnicodeChar();
1231 final boolean handled = super.onKeyDown(keyCode, event);
1232 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1233 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1235 keyCode, event);
1236 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001238 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // showSearchDialog()
1240 // If there are multiple keystrokes before the search dialog takes focus,
1241 // onSearchRequested() will be called for every keystroke,
1242 // but it is idempotent, so it's fine.
1243 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Joe Onorato8a9625e2010-01-28 15:55:35 -08001247 // Eat the long press event so the keyboard doesn't come up.
1248 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1249 return true;
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return handled;
1253 }
1254
Winson46163472015-09-22 17:31:56 -07001255 @Override
1256 public boolean onKeyUp(int keyCode, KeyEvent event) {
1257 if (keyCode == KeyEvent.KEYCODE_MENU) {
1258 // Ignore the menu key if we are currently dragging or are on the custom content screen
1259 if (!isOnCustomContent() && !mDragController.isDragging()) {
1260 // Close any open folders
1261 closeFolder();
1262
1263 // Stop resizing any widgets
1264 mWorkspace.exitWidgetResizeMode();
1265
1266 // Show the overview mode if we are on the workspace
1267 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1268 !mWorkspace.isSwitchingState()) {
1269 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001270 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001271 }
1272 }
1273 return true;
1274 }
1275 return super.onKeyUp(keyCode, event);
1276 }
1277
Karl Rosaen138a0412009-04-23 19:00:21 -07001278 private String getTypedText() {
1279 return mDefaultKeySsb.toString();
1280 }
1281
1282 private void clearTypedText() {
1283 mDefaultKeySsb.clear();
1284 mDefaultKeySsb.clearSpans();
1285 Selection.setSelection(mDefaultKeySsb, 0);
1286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001289 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1290 * State
1291 */
1292 private static State intToState(int stateOrdinal) {
1293 State state = State.WORKSPACE;
1294 final State[] stateValues = State.values();
1295 for (int i = 0; i < stateValues.length; i++) {
1296 if (stateValues[i].ordinal() == stateOrdinal) {
1297 state = stateValues[i];
1298 break;
1299 }
1300 }
1301 return state;
1302 }
1303
1304 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 * Restores the previous state, if it exists.
1306 *
1307 * @param savedState The previous state.
1308 */
1309 private void restoreState(Bundle savedState) {
1310 if (savedState == null) {
1311 return;
1312 }
1313
Michael Jurka883f55b2010-10-21 15:47:14 -07001314 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001315 if (state == State.APPS || state == State.WIDGETS) {
1316 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001317 }
1318
Adam Cohen21cd0022013-10-09 18:57:02 -07001319 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1320 PagedView.INVALID_RESTORE_PAGE);
1321 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001322 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
Sunny Goyal756cd262015-08-20 12:33:21 -07001325 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1326 if (itemValues != null) {
1327 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001328 AppWidgetProviderInfo info = savedState.getParcelable(
1329 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001330 mPendingAddWidgetInfo = info == null ?
1331 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1332
Adam Cohen4637b5a2013-11-04 18:21:24 -08001333 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001334 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 mRestoring = true;
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 /**
1340 * Finds all the views we need and configure them properly.
1341 */
1342 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001343 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001344
Craig Mautner360310b2012-10-26 15:13:08 -07001345 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001346 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001347 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001349 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001350
Sunny Goyal784f9c32016-03-23 16:56:54 -07001351 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1352 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1353 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Winson Chung4c98d922011-05-31 16:50:48 -07001356 // Setup the drag layer
1357 mDragLayer.setup(this, dragController);
1358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 // Setup the hotseat
1360 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1361 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001362 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 }
1364
Winsone9f27272015-10-13 10:47:51 -07001365 // Setup the overview panel
1366 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001367
Winson Chung4c98d922011-05-31 16:50:48 -07001368 // Setup the workspace
1369 mWorkspace.setHapticFeedbackEnabled(false);
1370 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001371 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001372 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001373
Tony Wickham34d2c912015-09-11 09:27:58 -07001374 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001375 mSearchDropTargetBar = (SearchDropTargetBar)
1376 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001377 // Get the app info bar
1378 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1379 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001380
Winson Chungef7f8742015-06-04 17:18:17 -07001381 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001382 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001383 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001384 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1385 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1386 } else {
1387 mAppsView.setSearchBarController(new DefaultAppSearchController());
1388 }
Craig Mautner360310b2012-10-26 15:13:08 -07001389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001391 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001392 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001393 dragController.setMoveTarget(mWorkspace);
1394 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001395 if (mSearchDropTargetBar != null) {
1396 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001397 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001398 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001399 if (mAppInfoDropTargetBar != null) {
1400 mAppInfoDropTargetBar.setup(this, dragController);
1401 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001402
Sunny Goyal322d5562015-06-25 19:35:49 -07001403 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1404 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407
Winsone9f27272015-10-13 10:47:51 -07001408 private void setupOverviewPanel() {
1409 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1410
1411 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1412 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1413 @Override
1414 public boolean onLongClick(View v) {
1415 return v.performClick();
1416 }
1417 };
1418
1419 // Bind wallpaper button actions
1420 View wallpaperButton = findViewById(R.id.wallpaper_button);
1421 wallpaperButton.setOnClickListener(new OnClickListener() {
1422 @Override
1423 public void onClick(View view) {
1424 if (!mWorkspace.isSwitchingState()) {
1425 onClickWallpaperPicker(view);
1426 }
1427 }
1428 });
1429 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1430 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1431
1432 // Bind widget button actions
1433 mWidgetsButton = findViewById(R.id.widget_button);
1434 mWidgetsButton.setOnClickListener(new OnClickListener() {
1435 @Override
1436 public void onClick(View view) {
1437 if (!mWorkspace.isSwitchingState()) {
1438 onClickAddWidgetButton(view);
1439 }
1440 }
1441 });
1442 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1443 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1444
1445 // Bind settings actions
1446 View settingsButton = findViewById(R.id.settings_button);
1447 boolean hasSettings = hasSettings();
1448 if (hasSettings) {
1449 settingsButton.setOnClickListener(new OnClickListener() {
1450 @Override
1451 public void onClick(View view) {
1452 if (!mWorkspace.isSwitchingState()) {
1453 onClickSettingsButton(view);
1454 }
1455 }
1456 });
1457 settingsButton.setOnLongClickListener(performClickOnLongClick);
1458 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1459 } else {
1460 settingsButton.setVisibility(View.GONE);
1461 }
1462
1463 mOverviewPanel.setAlpha(0f);
1464 }
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 * Sets the all apps button. This method is called from {@link Hotseat}.
1468 */
1469 public void setAllAppsButton(View allAppsButton) {
1470 mAllAppsButton = allAppsButton;
1471 }
1472
1473 public View getAllAppsButton() {
1474 return mAllAppsButton;
1475 }
1476
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001477 public View getWidgetsButton() {
1478 return mWidgetsButton;
1479 }
1480
Anjali Koppal5ad44842014-03-10 20:34:39 -07001481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 * Creates a view representing a shortcut.
1483 *
1484 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001487 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * @param parent The group the shortcut belongs to.
1494 * @param info The data structure describing the shortcut.
1495 *
1496 * @return A View inflated from layoutResId.
1497 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001498 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001499 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001500 parent, false);
1501 favorite.applyFromShortcutInfo(info, mIconCache);
1502 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001504 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return favorite;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Add a shortcut to the workspace.
1510 *
1511 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Sunny Goyal5c97f512015-05-19 16:03:28 -07001518 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001519 if (info == null) {
1520 return;
1521 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 final View view = createShortcut(info);
1523
Sunny Goyal5c97f512015-05-19 16:03:28 -07001524 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 // First we check if we already know the exact location where we want to add this item.
1526 if (cellX >= 0 && cellY >= 0) {
1527 cellXY[0] = cellX;
1528 cellXY[1] = cellY;
1529 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001530
1531 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001532 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 true, null,null)) {
1534 return;
1535 }
1536 DragObject dragObject = new DragObject();
1537 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001538 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1539 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001540 return;
1541 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 }
1545
1546 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001547 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 return;
1549 }
1550
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001551 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001555 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557 }
1558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001560 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001562 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 */
Adam Cohen091440a2015-03-18 14:16:05 -07001564 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001565 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1566
1567 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001569 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Adam Cohen59400422014-03-05 18:07:04 -08001572 if (appWidgetInfo.isCustomWidget) {
1573 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001574 }
1575
Adam Cohen59400422014-03-05 18:07:04 -08001576 LauncherAppWidgetInfo launcherInfo;
1577 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1578 launcherInfo.spanX = info.spanX;
1579 launcherInfo.spanY = info.spanY;
1580 launcherInfo.minSpanX = info.minSpanX;
1581 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001582 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001585 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586
1587 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 if (hostView == null) {
1589 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001590 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1591 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 } else {
1593 // The AppWidgetHostView has already been inflated and instantiated
1594 launcherInfo.hostView = hostView;
1595 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001596 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001599 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
Romain Guycbb89e42009-06-08 15:52:54 -07001601
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001602 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1603 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1604 item.hostView.setTag(item);
1605 item.onBindAppWidget(this);
1606
1607 item.hostView.setFocusable(true);
1608 item.hostView.setOnFocusChangeListener(mFocusHandler);
1609
1610 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1611 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1612
1613 if (!item.isCustomWidget()) {
1614 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1615 }
1616 }
1617
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1619 @Override
1620 public void onReceive(Context context, Intent intent) {
1621 final String action = intent.getAction();
1622 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1623 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001624 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001625 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001626
Winson Chungc100e8e2011-08-09 16:02:43 -07001627 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001628 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001629 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001630 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001631 if (!showWorkspace(false)) {
1632 // If we are already on the workspace, then manually reset all apps
1633 mAppsView.reset();
1634 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001636 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1637 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001638 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001639 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1640 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001641 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001642 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1643 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1644 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001645 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001646 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1647 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 }
1649 }
1650 };
1651
1652 @Override
1653 public void onAttachedToWindow() {
1654 super.onAttachedToWindow();
1655
1656 // Listen for broadcasts related to user-presence
1657 final IntentFilter filter = new IntentFilter();
1658 filter.addAction(Intent.ACTION_SCREEN_OFF);
1659 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001660 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001661 if (ENABLE_DEBUG_INTENTS) {
1662 filter.addAction(DebugIntents.DELETE_DATABASE);
1663 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001666 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001667 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001669
1670 if (mLauncherCallbacks != null) {
1671 mLauncherCallbacks.onAttachedToWindow();
1672 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674
1675 @Override
1676 public void onDetachedFromWindow() {
1677 super.onDetachedFromWindow();
1678 mVisible = false;
1679
Adam Cohend113e0c2010-11-11 10:48:05 -08001680 if (mAttached) {
1681 unregisterReceiver(mReceiver);
1682 mAttached = false;
1683 }
Winson Chungb745afb2015-03-02 11:51:23 -08001684 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001685
1686 if (mLauncherCallbacks != null) {
1687 mLauncherCallbacks.onDetachedFromWindow();
1688 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 }
1690
1691 public void onWindowVisibilityChanged(int visibility) {
1692 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001693 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001694 // The following code used to be in onResume, but it turns out onResume is called when
1695 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1696 // is a more appropriate event to handle
1697 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001698 if (!mWorkspaceLoading) {
1699 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001700 // We want to let Launcher draw itself at least once before we force it to build
1701 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 // apps is nice and speedy.
1703 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001705 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001706 if (mStarted) return;
1707 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001708 // We delay the layer building a bit in order to give
1709 // other message processing a time to run. In particular
1710 // this avoids a delay in hiding the IME if it was
1711 // currently shown, because doing that may involve
1712 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001713 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001714 final ViewTreeObserver.OnDrawListener listener = this;
1715 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001716 public void run() {
1717 if (mWorkspace != null &&
1718 mWorkspace.getViewTreeObserver() != null) {
1719 mWorkspace.getViewTreeObserver().
1720 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001721 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001722 }
1723 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001724 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001725 }
1726 });
1727 }
1728 clearTypedText();
1729 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
1731
Adam Cohen091440a2015-03-18 14:16:05 -07001732 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 mHandler.removeMessages(ADVANCE_MSG);
1734 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1735 mHandler.sendMessageDelayed(msg, delay);
1736 mAutoAdvanceSentTime = System.currentTimeMillis();
1737 }
1738
Adam Cohen091440a2015-03-18 14:16:05 -07001739 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1741 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1742 mAutoAdvanceRunning = autoAdvanceRunning;
1743 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001744 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 sendAdvanceMessage(delay);
1746 } else {
1747 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001748 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1750 }
1751 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001752 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754 }
1755 }
1756
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1758
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 if (msg.what == ADVANCE_MSG) {
1762 int i = 0;
1763 for (View key: mWidgetsToAdvance.keySet()) {
1764 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001765 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001767 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 public void run() {
1769 ((Advanceable) v).advance();
1770 }
1771 }, delay);
1772 }
1773 i++;
1774 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001775 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001777 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001779 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001780
Winsonc0b52fe2015-09-09 16:38:15 -07001781 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001782 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1784 if (v instanceof Advanceable) {
1785 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001786 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001787 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
1789 }
1790
Winsonc0b52fe2015-09-09 16:38:15 -07001791 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 if (mWidgetsToAdvance.containsKey(hostView)) {
1793 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001794 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 }
1797
Hyunyoung Song3f471442015-04-08 19:01:34 -07001798 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001799 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1800 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 }
1802
Winson Chunga6945242014-01-08 14:04:34 -08001803 public DragLayer getDragLayer() {
1804 return mDragLayer;
1805 }
1806
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001807 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001808 return mAppsView;
1809 }
1810
Hyunyoung Song3f471442015-04-08 19:01:34 -07001811 public WidgetsContainerView getWidgetsView() {
1812 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001813 }
1814
Winson Chunga6945242014-01-08 14:04:34 -08001815 public Workspace getWorkspace() {
1816 return mWorkspace;
1817 }
1818
1819 public Hotseat getHotseat() {
1820 return mHotseat;
1821 }
1822
Jorim Jaggid017f882014-01-14 17:08:48 -08001823 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001824 return mOverviewPanel;
1825 }
1826
Winson Chung006ee262015-08-03 14:40:11 -07001827 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001828 return mSearchDropTargetBar;
1829 }
1830
Tony Wickham34d2c912015-09-11 09:27:58 -07001831 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1832 return mAppInfoDropTargetBar;
1833 }
1834
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001835 public LauncherAppWidgetHost getAppWidgetHost() {
1836 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Winson Chunga9abd0e2010-10-27 17:18:37 -07001839 public LauncherModel getModel() {
1840 return mModel;
1841 }
1842
Winson Chunga6945242014-01-08 14:04:34 -08001843 protected SharedPreferences getSharedPrefs() {
1844 return mSharedPrefs;
1845 }
1846
Adam Cohen2e6da152015-05-06 11:42:25 -07001847 public DeviceProfile getDeviceProfile() {
1848 return mDeviceProfile;
1849 }
1850
Bjorn Bringertc459e522013-06-07 19:36:01 +01001851 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 getWindow().closeAllPanels();
1853
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001854 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001855 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 }
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 @Override
1859 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001860 long startTime = 0;
1861 if (DEBUG_RESUME_TIME) {
1862 startTime = System.currentTimeMillis();
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 super.onNewIntent(intent);
1865
1866 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001867 Folder openFolder = mWorkspace.getOpenFolder();
1868 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1869 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1870 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1871 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1872 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001873 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001874 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001875
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 if (mWorkspace == null) {
1877 // Can be cases where mWorkspace is null, this prevents a NPE
1878 return;
1879 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 // In all these cases, only animate if we're already on home
1881 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001882
Sunny Goyal935fca12015-10-13 10:19:01 -07001883 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 exitSpringLoadedDragMode();
1885
1886 // If we are already on home, then just animate back to the workspace,
1887 // otherwise, just wait until onResume to set the state back to Workspace
1888 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001889 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 } else {
1891 mOnResumeState = State.WORKSPACE;
1892 }
1893
1894 final View v = getWindow().peekDecorView();
1895 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001896 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 INPUT_METHOD_SERVICE);
1898 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1899 }
1900
Winson Chungb745afb2015-03-02 11:51:23 -08001901 // Reset the apps view
1902 if (!alreadyOnHome && mAppsView != null) {
1903 mAppsView.scrollToTop();
1904 }
1905
Hyunyoung Song3f471442015-04-08 19:01:34 -07001906 // Reset the widgets view
1907 if (!alreadyOnHome && mWidgetsView != null) {
1908 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001909 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001910
Adam Cohen9211d422014-10-07 18:14:53 -07001911 if (mLauncherCallbacks != null) {
1912 mLauncherCallbacks.onHomeIntent();
1913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Adam Cohened307df2013-10-02 09:37:31 -07001915
Adam Cohen9211d422014-10-07 18:14:53 -07001916 if (mLauncherCallbacks != null) {
1917 mLauncherCallbacks.onNewIntent(intent);
1918 }
Winson15f8b172015-08-19 11:02:39 -07001919
1920 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1921 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1922 // animation.
1923 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001924 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1925 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001926 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1927 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001928
1929 // We use this flag to suppress noisy callbacks above custom content state
1930 // from onResume.
1931 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001932 mWorkspace.post(new Runnable() {
1933 @Override
1934 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001935 if (mWorkspace != null) {
1936 mWorkspace.moveToDefaultScreen(true);
1937 }
Winson15f8b172015-08-19 11:02:39 -07001938 }
1939 });
1940 }
1941 }
1942
1943 if (DEBUG_RESUME_TIME) {
1944 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1945 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001949 public void onRestoreInstanceState(Bundle state) {
1950 super.onRestoreInstanceState(state);
1951 for (int page: mSynchronouslyBoundPages) {
1952 mWorkspace.restoreInstanceStateForChild(page);
1953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
1956 @Override
1957 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001958 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001959 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1960 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001961 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001962 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Michael Jurka883f55b2010-10-21 15:47:14 -07001964 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001965 // We close any open folder since it will not be re-opened, and we need to make sure
1966 // this state is reflected.
1967 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1968 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001971 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001972 ContentValues itemValues = new ContentValues();
1973 mPendingAddInfo.writeToValues(itemValues);
1974 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001975 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001976 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
Hyunyoung Song3f471442015-04-08 19:01:34 -07001979 // Save the current widgets tray?
1980 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001981
1982 if (mLauncherCallbacks != null) {
1983 mLauncherCallbacks.onSaveInstanceState(outState);
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
1987 @Override
1988 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001990
Winson Chunge7a03942011-08-05 15:05:12 -07001991 // Remove all pending runnables
1992 mHandler.removeMessages(ADVANCE_MSG);
1993 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001994 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001995
Winson Chungcd2b0142011-06-08 16:02:26 -07001996 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001997 // It's possible to receive onDestroy after a new Launcher activity has
1998 // been created. In this case, don't interfere with the new Launcher.
1999 if (mModel.isCurrentCallbacks(this)) {
2000 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08002001 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07002002 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002005 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002007 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009 mAppWidgetHost = null;
2010
2011 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012
2013 TextKeyListener.getInstance().release();
2014
Tony Wickhame2217252016-03-22 16:34:23 -07002015 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2016 .removeAccessibilityStateChangeListener(this);
2017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002018 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002019
Michael Jurka2ecf9952012-06-18 12:52:28 -07002020 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002021
2022 if (mLauncherCallbacks != null) {
2023 mLauncherCallbacks.onDestroy();
2024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
2026
Adam Cohena9cf38f2011-05-02 15:36:58 -07002027 public DragController getDragController() {
2028 return mDragController;
2029 }
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 @Override
2032 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002033 onStartForResult(requestCode);
2034 super.startActivityForResult(intent, requestCode);
2035 }
2036
2037 @Override
2038 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2039 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2040 onStartForResult(requestCode);
2041 try {
2042 super.startIntentSenderForResult(intent, requestCode,
2043 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2044 } catch (IntentSender.SendIntentException e) {
2045 throw new ActivityNotFoundException();
2046 }
2047 }
2048
2049 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002050 if (requestCode >= 0) {
2051 setWaitingForResult(true);
2052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
2054
Winson Chung70d72102011-08-12 11:24:35 -07002055 /**
2056 * Indicates that we want global search for this activity by setting the globalSearch
2057 * argument for {@link #startSearch} to true.
2058 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002060 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002062
Karl Rosaen138a0412009-04-23 19:00:21 -07002063 if (initialQuery == null) {
2064 // Use any text typed in the launcher as the initial query
2065 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 if (appSearchData == null) {
2068 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002069 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
Winson Chungadf0c182012-08-23 14:59:07 -07002071 Rect sourceBounds = new Rect();
2072 if (mSearchDropTargetBar != null) {
2073 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2074 }
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 if (clearTextImmediately) {
2079 clearTypedText();
2080 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002081
2082 // We need to show the workspace after starting the search
2083 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 }
2085
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 /**
2087 * Start a text search.
2088 *
2089 * @return {@code true} if the search will start immediately, so any further keypresses
2090 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2091 * to buffer keypresses.
2092 */
2093 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002094 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002095 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2096 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2097 sourceBounds);
2098 }
2099
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002101 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002102 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 }
2104
2105 /**
2106 * Starts the global search activity. This code is a copied from SearchManager
2107 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002108 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002110 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2112 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2113 if (globalSearchActivity == null) {
2114 Log.w(TAG, "No global search activity found.");
2115 return;
2116 }
2117 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2118 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2119 intent.setComponent(globalSearchActivity);
2120 // Make sure that we have a Bundle to put source in
2121 if (appSearchData == null) {
2122 appSearchData = new Bundle();
2123 } else {
2124 appSearchData = new Bundle(appSearchData);
2125 }
Adam Cohen9211d422014-10-07 18:14:53 -07002126 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002127 if (!appSearchData.containsKey("source")) {
2128 appSearchData.putString("source", getPackageName());
2129 }
2130 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2131 if (!TextUtils.isEmpty(initialQuery)) {
2132 intent.putExtra(SearchManager.QUERY, initialQuery);
2133 }
2134 if (selectInitialQuery) {
2135 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2136 }
2137 intent.setSourceBounds(sourceBounds);
2138 try {
2139 startActivity(intent);
2140 } catch (ActivityNotFoundException ex) {
2141 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 }
2144
Yura4f93ec62014-02-04 14:15:21 +00002145 public boolean isOnCustomContent() {
2146 return mWorkspace.isOnOrMovingToCustomContent();
2147 }
2148
Winson Chung70d72102011-08-12 11:24:35 -07002149 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002150 public boolean onPrepareOptionsMenu(Menu menu) {
2151 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002152 if (mLauncherCallbacks != null) {
2153 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2154 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002155 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002158 @Override
2159 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002160 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002161 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002162 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public boolean isWorkspaceLocked() {
2166 return mWorkspaceLoading || mWaitingForResult;
2167 }
2168
Adam Cohen517a7f52014-03-01 12:12:59 -08002169 public boolean isWorkspaceLoading() {
2170 return mWorkspaceLoading;
2171 }
2172
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173 private void setWorkspaceLoading(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWorkspaceLoading = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
2181 private void setWaitingForResult(boolean value) {
2182 boolean isLocked = isWorkspaceLocked();
2183 mWaitingForResult = value;
2184 if (isLocked != isWorkspaceLocked()) {
2185 onWorkspaceLockedChanged();
2186 }
2187 }
2188
Adam Cohen9211d422014-10-07 18:14:53 -07002189 protected void onWorkspaceLockedChanged() {
2190 if (mLauncherCallbacks != null) {
2191 mLauncherCallbacks.onWorkspaceLockedChanged();
2192 }
2193 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002194
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002196 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002197 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2199 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002200 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002201 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002202
Tony Wickhama0628cc2015-10-14 15:23:04 -07002203 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002204 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002205 if (LOGD) {
2206 Log.d(TAG, "Adding widget from drop");
2207 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2209 }
2210
Sunny Goyale6b63a32015-01-16 16:14:29 -08002211 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002212 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2213 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 if (appWidgetInfo.configure != null) {
2215 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002216 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002217
Bjorn Bringert7984c942009-12-09 15:38:25 +00002218 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002219 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2220 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002223 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002224 Runnable onComplete = new Runnable() {
2225 @Override
2226 public void run() {
2227 // Exit spring loaded mode if necessary after adding the widget
2228 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2229 null);
2230 }
2231 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002233 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002234 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002238 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002239 // Close any folders that may be open.
2240 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002241 mWorkspace.moveToCustomContentScreen(animate);
2242 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002243
2244 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2245 int[] cell, int spanX, int spanY) {
2246 switch (info.itemType) {
2247 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2248 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2249 int span[] = new int[2];
2250 span[0] = spanX;
2251 span[1] = spanY;
2252 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2253 container, screenId, cell, span);
2254 break;
2255 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2256 processShortcutFromDrop(info.componentName, container, screenId, cell);
2257 break;
2258 default:
2259 throw new IllegalStateException("Unknown item type: " + info.itemType);
2260 }
2261 }
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 /**
2264 * Process a shortcut drop.
2265 *
2266 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002270 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2271 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275
2276 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.cellX = cell[0];
2278 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280
2281 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2282 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002283 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 }
2285
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 /**
2287 * Process a widget drop.
2288 *
2289 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002293 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2294 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002298 mPendingAddInfo.minSpanX = info.minSpanX;
2299 mPendingAddInfo.minSpanY = info.minSpanY;
2300
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 mPendingAddInfo.cellX = cell[0];
2303 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002305 if (span != null) {
2306 mPendingAddInfo.spanX = span[0];
2307 mPendingAddInfo.spanY = span[1];
2308 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 AppWidgetHostView hostView = info.boundWidget;
2311 int appWidgetId;
2312 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002313 // In the case where we've prebound the widget, we remove it from the DragLayer
2314 if (LOGD) {
2315 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2316 }
2317 getDragLayer().removeView(hostView);
2318
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002320 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002321
2322 // Clear the boundWidget so that it doesn't get destroyed.
2323 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002325 // In this case, we either need to start an activity to get permission to bind
2326 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002328 Bundle options = info.bindOptions;
2329
Sunny Goyalffe83f12014-08-14 17:39:34 -07002330 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2331 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002332 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002333 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002334 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002335 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002336 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2337 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2338 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002339 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2340 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002341 // TODO: we need to make sure that this accounts for the options bundle.
2342 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002343 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2344 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002345 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
2347
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002350 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 folderInfo.title = getText(R.string.folder_name);
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002354 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2355 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
2357 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 FolderIcon newFolder =
2359 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002362 // Force measure the new folder icon
2363 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2364 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002365 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Winsonc0b52fe2015-09-09 16:38:15 -07002368 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002369 * Unbinds the view for the specified item, and removes the item and all its children.
2370 *
2371 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002372 * @param itemInfo the {@link ItemInfo} for this view.
2373 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002374 */
Winson2949fb52015-09-24 09:56:11 -07002375 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002377 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002378 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2379 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002380 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002381 } else {
2382 mWorkspace.removeWorkspaceItem(v);
2383 }
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
2385 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2386 }
2387 } else if (itemInfo instanceof FolderInfo) {
2388 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002389 mWorkspace.removeWorkspaceItem(v);
2390 if (deleteFromDb) {
2391 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2392 }
2393 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2394 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002395 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 removeWidgetToAutoAdvance(widgetInfo.hostView);
2397 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002398 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002401
Winsonc0b52fe2015-09-09 16:38:15 -07002402 } else {
2403 return false;
2404 }
2405 return true;
2406 }
2407
2408 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002410 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002411 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002412 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002413 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002414 // Deleting an app widget ID is a void call but writes to disk before returning
2415 // to the caller...
2416 new AsyncTask<Void, Void, Void>() {
2417 public Void doInBackground(Void ... args) {
2418 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2419 return null;
2420 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002421 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002422 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002423 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002424 }
2425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 @Override
2427 public boolean dispatchKeyEvent(KeyEvent event) {
2428 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2429 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002431 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002432 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002433 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002434 dumpState();
2435 return true;
2436 }
2437 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002438 }
2439 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2440 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002441 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 return true;
2443 }
2444 }
2445
2446 return super.dispatchKeyEvent(event);
2447 }
2448
Joe Onorato88ec0992009-11-19 13:16:06 -08002449 @Override
2450 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002451 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2452 return;
2453 }
2454
Sunny Goyal45478022015-06-08 16:52:41 -07002455 if (mDragController.isDragging()) {
2456 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002457 return;
2458 }
2459
Winson Chungb745afb2015-03-02 11:51:23 -08002460 if (isAppsViewVisible()) {
2461 showWorkspace(true);
2462 } else if (isWidgetsViewVisible()) {
2463 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002464 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002465 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002466 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002467 Folder openFolder = mWorkspace.getOpenFolder();
2468 if (openFolder.isEditingName()) {
2469 openFolder.dismissEditingName();
2470 } else {
2471 closeFolder();
2472 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002473 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002474 mWorkspace.exitWidgetResizeMode();
2475
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 // Back button is a no-op here, but give at least some feedback for the button press
2477 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002478 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002479 }
2480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002482 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002483 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002484 @Override
2485 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002486 if (mAppWidgetHost != null) {
2487 mAppWidgetHost.startListening();
2488 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002489
2490 // Recreate the QSB, as the widget has been reset.
2491 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002492 }
2493
2494 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 * Launches the intent referred by the clicked shortcut.
2496 *
2497 * @param v The view representing the clicked shortcut.
2498 */
2499 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002500 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2501 // view has detached (it's possible for this to happen if the view is removed mid touch).
2502 if (v.getWindowToken() == null) {
2503 return;
2504 }
2505
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002506 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002507 return;
2508 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002509
Adam Cohen1697b792013-09-17 19:08:21 -07002510 if (v instanceof Workspace) {
2511 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002512 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514 return;
2515 }
2516
2517 if (v instanceof CellLayout) {
2518 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002519 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2520 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002521 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002522 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002526 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002531 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002532 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002534 } else if (tag instanceof AppInfo) {
2535 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002536 } else if (tag instanceof LauncherAppWidgetInfo) {
2537 if (v instanceof PendingAppWidgetHostView) {
2538 onClickPendingWidget((PendingAppWidgetHostView) v);
2539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 }
2541 }
2542
Sunny Goyal70660032015-05-14 00:07:08 -07002543 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002544 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002545 return false;
2546 }
2547
Michael Jurkaaf442092010-06-10 17:01:57 -07002548 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002549 * Event handler for the app widget view which has not fully restored.
2550 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002552 if (mIsSafeModeEnabled) {
2553 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2554 return;
2555 }
2556
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002557 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002558 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002559 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002560 LauncherAppWidgetProviderInfo appWidgetInfo =
2561 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002562 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002563 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002564 mPendingAddInfo.copyFrom(info);
2565 mPendingAddWidgetId = widgetId;
2566
Sunny Goyalffe83f12014-08-14 17:39:34 -07002567 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2568 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002569 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002570 } else if (info.installProgress < 0) {
2571 // The install has not been queued
2572 final String packageName = info.providerName.getPackageName();
2573 showBrokenAppInstallDialog(packageName,
2574 new DialogInterface.OnClickListener() {
2575 public void onClick(DialogInterface dialog, int id) {
2576 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2577 }
2578 });
2579 } else {
2580 // Download has started.
2581 final String packageName = info.providerName.getPackageName();
2582 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002583 }
2584 }
2585
2586 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002587 * Event handler for the "grid" button that appears on the home screen, which
2588 * enters all apps mode.
2589 *
2590 * @param v The view that was clicked.
2591 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 protected void onClickAllAppsButton(View v) {
2593 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002594 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002595 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002596 true /* updatePredictedApps */, false /* focusSearchBar */);
2597 }
2598 }
2599
2600 protected void onLongClickAllAppsButton(View v) {
2601 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2602 if (!isAppsViewVisible()) {
2603 showAppsView(true /* animated */, false /* resetListToTop */,
2604 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 }
2606 }
2607
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 private void showBrokenAppInstallDialog(final String packageName,
2609 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002610 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 .setTitle(R.string.abandoned_promises_title)
2612 .setMessage(R.string.abandoned_promise_explanation)
2613 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2614 .setNeutralButton(R.string.abandoned_clean_this,
2615 new DialogInterface.OnClickListener() {
2616 public void onClick(DialogInterface dialog, int id) {
2617 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2618 mWorkspace.removeAbandonedPromise(packageName, user);
2619 }
2620 })
2621 .create().show();
2622 return;
2623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 /**
2626 * Event handler for an app shortcut click.
2627 *
2628 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2629 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2632 Object tag = v.getTag();
2633 if (!(tag instanceof ShortcutInfo)) {
2634 throw new IllegalArgumentException("Input must be a Shortcut");
2635 }
2636
2637 // Open shortcut
2638 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002639
2640 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002641 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2642 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002643 // Launch activity anyway, framework will tell the user why the app is suspended.
2644 } else {
2645 int error = R.string.activity_not_available;
2646 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2647 error = R.string.safemode_shortcut_error;
2648 }
2649 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2650 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002652 }
2653
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002655 if ((v instanceof BubbleTextView)
2656 && shortcut.isPromise()
2657 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002658 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002659 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 new DialogInterface.OnClickListener() {
2661 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002669 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 }
2671
Adam Cohen091440a2015-03-18 14:16:05 -07002672 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002674 final ShortcutInfo shortcut;
2675 final Intent intent;
2676 if (tag instanceof ShortcutInfo) {
2677 shortcut = (ShortcutInfo) tag;
2678 intent = shortcut.intent;
2679 int[] pos = new int[2];
2680 v.getLocationOnScreen(pos);
2681 intent.setSourceBounds(new Rect(pos[0], pos[1],
2682 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002683
Sunny Goyal508da152014-08-14 10:53:27 -07002684 } else if (tag instanceof AppInfo) {
2685 shortcut = null;
2686 intent = ((AppInfo) tag).intent;
2687 } else {
2688 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2689 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690
2691 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songddec1c72016-04-12 18:32:04 -07002692 mUserEventLogger.logLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002693
2694 if (success && v instanceof BubbleTextView) {
2695 mWaitingForResume = (BubbleTextView) v;
2696 mWaitingForResume.setStayPressed(true);
2697 }
2698 }
2699
2700 /**
2701 * Event handler for a folder icon click.
2702 *
2703 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2704 */
2705 protected void onClickFolderIcon(View v) {
2706 if (LOGD) Log.d(TAG, "onClickFolder");
2707 if (!(v instanceof FolderIcon)){
2708 throw new IllegalArgumentException("Input must be a FolderIcon");
2709 }
2710
2711 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002712 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002714 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 }
Michael Jurka5130e402011-10-13 04:55:35 -07002716 }
2717
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002718 /**
2719 * Event handler for the (Add) Widgets button that appears after a long press
2720 * on the home screen.
2721 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002722 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002724 if (mIsSafeModeEnabled) {
2725 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2726 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002727 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002728 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002729 }
2730
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 /**
2732 * Event handler for the wallpaper picker button that appears after a long press
2733 * on the home screen.
2734 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002735 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002737 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2738 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal30a71612016-04-12 17:57:41 -07002739 // TODO: Start the system wallpaper picker
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 }
2741
2742 /**
2743 * Event handler for a click on the settings button that appears after a long press
2744 * on the home screen.
2745 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002746 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002747 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002750 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002751 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002752 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002753 }
2754
Adam Cohen61f560d2013-09-30 15:58:20 -07002755 public View.OnTouchListener getHapticFeedbackTouchListener() {
2756 if (mHapticFeedbackTouchListener == null) {
2757 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002758 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002759 @Override
2760 public boolean onTouch(View v, MotionEvent event) {
2761 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2762 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2763 }
2764 return false;
2765 }
2766 };
2767 }
2768 return mHapticFeedbackTouchListener;
2769 }
2770
Tony Wickhame2217252016-03-22 16:34:23 -07002771 @Override
2772 public void onAccessibilityStateChanged(boolean enabled) {
2773 mDragLayer.onAccessibilityStateChanged(enabled);
2774 }
2775
Adam Cohen9211d422014-10-07 18:14:53 -07002776 public void onDragStarted(View view) {
2777 if (isOnCustomContent()) {
2778 // Custom content screen doesn't participate in drag and drop. If on custom
2779 // content screen, move to default.
2780 moveWorkspaceToDefaultScreen();
2781 }
Adam Cohen9211d422014-10-07 18:14:53 -07002782 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002783
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784 /**
2785 * Called when the user stops interacting with the launcher.
2786 * This implies that the user is now on the homescreen and is not doing housekeeping.
2787 */
Adam Cohen9211d422014-10-07 18:14:53 -07002788 protected void onInteractionEnd() {
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onInteractionEnd();
2791 }
2792 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002793
2794 /**
2795 * Called when the user starts interacting with the launcher.
2796 * The possible interactions are:
2797 * - open all apps
2798 * - reorder an app shortcut, or a widget
2799 * - open the overview mode.
2800 * This is a good time to stop doing things that only make sense
2801 * when the user is on the homescreen and not doing housekeeping.
2802 */
Adam Cohen9211d422014-10-07 18:14:53 -07002803 protected void onInteractionBegin() {
2804 if (mLauncherCallbacks != null) {
2805 mLauncherCallbacks.onInteractionBegin();
2806 }
2807 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002808
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 /** Updates the interaction state. */
2810 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002811 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002812 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002813 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2814 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002815 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002816 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002817 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002818 onInteractionEnd();
2819 }
2820 }
2821
Winson Chung8f1eff72015-05-28 17:33:40 -07002822 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002823 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002825 // Only launch using the new animation if the shortcut has not opted out (this is a
2826 // private contract between launcher and may be ignored in the future).
2827 boolean useLaunchAnimation = (v != null) &&
2828 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002829 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2830 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002831
Kenny Guy1317e2d2014-05-08 18:52:50 +01002832 UserHandleCompat user = null;
2833 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2834 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2835 user = userManager.getUserForSerialNumber(serialNumber);
2836 }
2837
2838 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002839 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002840 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002841 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002842 int left = 0, top = 0;
2843 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2844 if (v instanceof TextView) {
2845 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002846 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2847 if (icon != null) {
2848 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002849 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002850 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002851 width = bounds.width();
2852 height = bounds.height();
2853 }
2854 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002855 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2856 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002857 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002858 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002859 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002860 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002861 // On L devices, we use the device default slide-up transition.
2862 // On L MR1 devices, we a custom version of the slide-up transition which
2863 // doesn't have the delay present in the device default.
2864 opts = ActivityOptions.makeCustomAnimation(this,
2865 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002866 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002867 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002868 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002869
2870 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2871 // Could be launching some bookkeeping activity
2872 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002873 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002874 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002875 launcherApps.startActivityForProfile(intent.getComponent(), user,
2876 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002877 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002878 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002880 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2881 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2882 // corresponding permission. Show the appropriate permission prompt if that
2883 // is the case.
2884 if (intent.getComponent() == null
2885 && Intent.ACTION_CALL.equals(intent.getAction())
2886 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2887 PackageManager.PERMISSION_GRANTED) {
2888 // TODO: Rename sPendingAddItem to a generic name.
2889 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2890 0, (ItemInfo) tag);
2891 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2892 REQUEST_PERMISSION_CALL_PHONE);
2893 return false;
2894 }
2895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002897 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002899 "or use the exported attribute for this activity. "
2900 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002902 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002904
Winson Chungbedf9232015-07-10 12:38:30 -07002905 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002906 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002907 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2908 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2909 return false;
2910 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002911 try {
2912 success = startActivity(v, intent, tag);
2913 } catch (ActivityNotFoundException e) {
2914 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2915 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2916 }
2917 return success;
2918 }
2919
Adam Cohen268c4752012-06-06 17:47:33 -07002920 /**
2921 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2922 * in the DragLayer in the exact absolute location of the original FolderIcon.
2923 */
2924 private void copyFolderIconToImage(FolderIcon fi) {
2925 final int width = fi.getMeasuredWidth();
2926 final int height = fi.getMeasuredHeight();
2927
2928 // Lazy load ImageView, Bitmap and Canvas
2929 if (mFolderIconImageView == null) {
2930 mFolderIconImageView = new ImageView(this);
2931 }
2932 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2933 mFolderIconBitmap.getHeight() != height) {
2934 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2935 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2936 }
2937
2938 DragLayer.LayoutParams lp;
2939 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2940 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2941 } else {
2942 lp = new DragLayer.LayoutParams(width, height);
2943 }
2944
Adam Cohen307fe232012-08-16 17:55:58 -07002945 // The layout from which the folder is being opened may be scaled, adjust the starting
2946 // view size by this scale factor.
2947 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002948 lp.customPosition = true;
2949 lp.x = mRectForFolderAnimation.left;
2950 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002951 lp.width = (int) (scale * width);
2952 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002953
2954 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2955 fi.draw(mFolderIconCanvas);
2956 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002957 if (fi.getFolder() != null) {
2958 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2959 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002960 }
Adam Cohen268c4752012-06-06 17:47:33 -07002961 // Just in case this image view is still in the drag layer from a previous animation,
2962 // we remove it and re-add it.
2963 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2964 mDragLayer.removeView(mFolderIconImageView);
2965 }
2966 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002967 if (fi.getFolder() != null) {
2968 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002969 }
Adam Cohen268c4752012-06-06 17:47:33 -07002970 }
2971
Adam Cohen37b2a492016-04-06 18:36:06 -07002972 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002973 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002974 FolderInfo info = (FolderInfo) fi.getTag();
2975 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2976 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002977 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2978 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002979 }
2980
Adam Cohen268c4752012-06-06 17:47:33 -07002981 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2982 copyFolderIconToImage(fi);
2983 fi.setVisibility(View.INVISIBLE);
2984
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002985 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2986 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002987 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002988 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2989 }
2990 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002991 oa.start();
2992 }
2993
Sunny Goyal935fca12015-10-13 10:19:01 -07002994 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002995 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002996 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002997
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // We remove and re-draw the FolderIcon in-case it has changed
2999 mDragLayer.removeView(mFolderIconImageView);
3000 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003001
3002 if (cl != null) {
3003 cl.clearFolderLeaveBehind();
3004 }
3005
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003006 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003007 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003008 oa.addListener(new AnimatorListenerAdapter() {
3009 @Override
3010 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003011 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003012 // Remove the ImageView copy of the FolderIcon and make the original visible.
3013 mDragLayer.removeView(mFolderIconImageView);
3014 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003015 }
3016 }
3017 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003018 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003019 if (!animate) {
3020 oa.end();
3021 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003022 }
3023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003025 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 * is animated relative to the specified View. If the View is null, no animation
3027 * is played.
3028 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003029 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003031 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003032
Adam Cohenfb91f302012-06-11 15:45:18 -07003033 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003034 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3035 if (openFolder != null && openFolder != folder) {
3036 // Close any open folder before opening a folder.
3037 closeFolder();
3038 }
3039
Adam Cohena9cf38f2011-05-02 15:36:58 -07003040 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041
Adam Cohena9cf38f2011-05-02 15:36:58 -07003042 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043
Sunny Goyalf4066152015-04-15 09:42:19 -07003044 // While the folder is open, the position of the icon cannot change.
3045 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3046
Adam Cohen4554ee12011-08-03 16:13:21 -07003047 // Just verify that the folder hasn't already been added to the DragLayer.
3048 // There was a one-off crash where the folder had a parent already.
3049 if (folder.getParent() == null) {
3050 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003051 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003052 } else {
3053 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3054 folder.getParent() + ").");
3055 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003056 folder.animateOpen();
3057
3058 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003059
3060 // Notify the accessibility manager that this folder "window" has appeared and occluded
3061 // the workspace items
3062 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3063 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003064 }
3065
3066 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003067 closeFolder(true);
3068 }
3069
3070 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003071 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003072 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003073 if (folder.isEditingName()) {
3074 folder.dismissEditingName();
3075 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003076 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003077 }
3078 }
3079
Sunny Goyal935fca12015-10-13 10:19:01 -07003080 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003081 animate &= !Utilities.isPowerSaverOn(this);
3082
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 folder.getInfo().opened = false;
3084
3085 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3086 if (parent != null) {
3087 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003088 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003089 if (fi != null) {
3090 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3091 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003093 if (animate) {
3094 folder.animateClosed();
3095 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003096 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003097 }
Winson Chung83ca4802013-04-12 15:10:52 -07003098
Sunny Goyal935fca12015-10-13 10:19:01 -07003099 // Notify the accessibility manager that this folder "window" has disappeared and no
3100 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003101 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 }
3103
Tony Wickhamdadb3042016-02-24 11:07:00 -08003104 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003106 if (!isDraggingEnabled()) return false;
3107 if (isWorkspaceLocked()) return false;
3108 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Winson Chung76648c52015-07-10 14:33:23 -07003110 if (v == mAllAppsButton) {
3111 onLongClickAllAppsButton(v);
3112 return true;
3113 }
3114
Adam Cohen1697b792013-09-17 19:08:21 -07003115 if (v instanceof Workspace) {
3116 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003117 if (!mWorkspace.isTouchActive()) {
3118 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003119 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3120 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3121 return true;
3122 } else {
3123 return false;
3124 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003125 } else {
3126 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003127 }
Adam Cohen1697b792013-09-17 19:08:21 -07003128 }
3129
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003130 CellLayout.CellInfo longClickCellInfo = null;
3131 View itemUnderLongClick = null;
3132 if (v.getTag() instanceof ItemInfo) {
3133 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003134 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003135 itemUnderLongClick = longClickCellInfo.cell;
3136 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138
Winson Chung3d503fb2011-07-13 17:25:49 -07003139 // The hotseat touch handling does not go through Workspace, and we always allow long press
3140 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003141 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003142 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003143 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003144 // User long pressed on empty space
3145 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3146 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003147 if (mWorkspace.isInOverviewMode()) {
3148 mWorkspace.startReordering(v);
3149 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003150 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003153 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3154 mHotseat.getOrderInHotseat(
3155 longClickCellInfo.cellX,
3156 longClickCellInfo.cellY));
3157 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003159 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160 }
3161 }
3162 }
3163 return true;
3164 }
3165
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 boolean isHotseatLayout(View layout) {
3167 return mHotseat != null && layout != null &&
3168 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3169 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003170
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 /**
3172 * Returns the CellLayout of the specified container at the specified screen.
3173 */
Sunny Goyal92820592015-03-02 11:31:35 -08003174 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3176 if (mHotseat != null) {
3177 return mHotseat.getLayout();
3178 } else {
3179 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003180 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003182 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003183 }
3184 }
3185
Winson Chungb745afb2015-03-02 11:51:23 -08003186 /**
3187 * For overridden classes.
3188 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003189 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003190 return isAppsViewVisible();
3191 }
3192
3193 public boolean isAppsViewVisible() {
3194 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3195 }
3196
3197 public boolean isWidgetsViewVisible() {
3198 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003199 }
3200
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003201 private void setWorkspaceBackground(int background) {
3202 switch (background) {
3203 case WORKSPACE_BACKGROUND_TRANSPARENT:
3204 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3205 break;
3206 case WORKSPACE_BACKGROUND_BLACK:
3207 getWindow().setBackgroundDrawable(null);
3208 break;
3209 default:
3210 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3211 }
Craig Mautner360310b2012-10-26 15:13:08 -07003212 }
3213
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003214 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003215 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3216 int curflags = getWindow().getAttributes().flags
3217 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3218 if (wpflags != curflags) {
3219 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3220 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003221 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003222 }
3223
Michael Jurkae326f182011-11-21 14:05:46 -08003224 @Override
3225 public void onTrimMemory(int level) {
3226 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003227 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3228 // The widget preview db can result in holding onto over
3229 // 3MB of memory for caching which isn't necessary.
3230 SQLiteDatabase.releaseMemory();
3231
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003232 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003233 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003234 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003235 if (mLauncherCallbacks != null) {
3236 mLauncherCallbacks.onTrimMemory(level);
3237 }
Michael Jurkae326f182011-11-21 14:05:46 -08003238 }
3239
Winson5c6bdbb2015-09-03 11:36:19 -07003240 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003241 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003242 }
3243
Winson5c6bdbb2015-09-03 11:36:19 -07003244 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003245 boolean changed = mState != State.WORKSPACE ||
3246 mWorkspace.getState() != Workspace.State.NORMAL;
3247 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003248 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003249 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003250 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003251
Michael Jurkab3e22d92011-10-31 15:58:33 -07003252 // Set focus to the AppsCustomize button
3253 if (mAllAppsButton != null) {
3254 mAllAppsButton.requestFocus();
3255 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003256 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003257
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003258 // Change the state *after* we've called all the transition code
3259 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003260
Winson Chung5fb63472011-02-02 17:03:37 -08003261 // Resume the auto-advance of widgets
3262 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003263 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003264
Winson Chung0f785722015-04-08 10:27:49 -07003265 if (changed) {
3266 // Send an accessibility event to announce the context change
3267 getWindow().getDecorView()
3268 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003269 }
Winson5c6bdbb2015-09-03 11:36:19 -07003270 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003271 }
3272
Winsone9f27272015-10-13 10:47:51 -07003273 /**
3274 * Shows the overview button.
3275 */
Adam Cohened307df2013-10-02 09:37:31 -07003276 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003277 showOverviewMode(animated, false);
3278 }
3279
3280 /**
3281 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3282 * onto one of the overview panel buttons.
3283 */
3284 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3285 Runnable postAnimRunnable = null;
3286 if (requestButtonFocus) {
3287 postAnimRunnable = new Runnable() {
3288 @Override
3289 public void run() {
3290 // Hitting the menu button when in touch mode does not trigger touch mode to
3291 // be disabled, so if requested, force focus on one of the overview panel
3292 // buttons.
3293 mOverviewPanel.requestFocusFromTouch();
3294 }
3295 };
3296 }
Adam Cohened307df2013-10-02 09:37:31 -07003297 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003298 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003299 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003300 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003301 }
3302
Winson Chungb745afb2015-03-02 11:51:23 -08003303 /**
3304 * Shows the apps view.
3305 */
Winson Chung76648c52015-07-10 14:33:23 -07003306 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3307 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003308 if (resetListToTop) {
3309 mAppsView.scrollToTop();
3310 }
Winson Chung4ac30062015-05-08 17:34:17 -07003311 if (updatePredictedApps) {
3312 tryAndUpdatePredictedApps();
3313 }
Winson Chung76648c52015-07-10 14:33:23 -07003314 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003315 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003316
Winson Chungb745afb2015-03-02 11:51:23 -08003317 /**
3318 * Shows the widgets view.
3319 */
3320 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003321 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003322 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003323 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003324 }
Winson Chung76648c52015-07-10 14:33:23 -07003325 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003326
3327 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003328 @Override
3329 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003330 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003331 }
3332 });
Winson Chungb745afb2015-03-02 11:51:23 -08003333 }
3334
3335 /**
3336 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003337 *
3338 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003339 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003340 // TODO: calling method should use the return value so that when {@code false} is returned
3341 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003342 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003343 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3344 mState != State.WIDGETS_SPRING_LOADED) {
3345 return false;
3346 }
3347 if (toState != State.APPS && toState != State.WIDGETS) {
3348 return false;
3349 }
Winson Chungb745afb2015-03-02 11:51:23 -08003350
3351 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003352 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3353 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003354 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003355 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003356 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003359 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360
3361 // Pause the auto-advance of widgets until we are out of AllApps
3362 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003363 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 closeFolder();
3365
3366 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003367 getWindow().getDecorView()
3368 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003369 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 }
3371
Winson Chungcd99cd32015-04-29 11:03:24 -07003372 /**
3373 * Updates the workspace and interaction state on state change, and return the animation to this
3374 * new state.
3375 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003376 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003377 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003378 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003379 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003380 updateInteraction(fromState, toState);
3381 return anim;
3382 }
3383
Jun Mukaif0033da2015-08-04 18:34:30 -07003384 public void onLauncherClingShown() {
3385 // When a launcher cling appears, it should cover the underlying layers, so their focus
3386 // should be blocked.
3387 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3388 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3389 }
3390 }
3391
3392 public void onLauncherClingDismissed() {
3393 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3394 }
3395
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003397 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003398 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003399 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003400 }
Winson Chungb745afb2015-03-02 11:51:23 -08003401
Winson Chung006ee262015-08-03 14:40:11 -07003402 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003403 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003404 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003405
3406 if (isAppsViewVisible()) {
3407 mState = State.APPS_SPRING_LOADED;
3408 } else if (isWidgetsViewVisible()) {
3409 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003410 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003411 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003412 } else {
3413 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003414 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003415 }
Adam Cohen7777d962011-08-18 18:58:38 -07003416
Hyunyoung Song3f471442015-04-08 19:01:34 -07003417 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003418 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003419 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003420
Winson Chunge7a03942011-08-05 15:05:12 -07003421 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003422 @Override
3423 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003424 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003425 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3426 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003427 // Before we show workspace, hide all apps again because
3428 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3429 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003430 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003431 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003432 } else {
3433 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003434 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003435 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003436 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003437 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003438
Tony Wickhame0c33232016-02-08 11:37:04 -08003439 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003440 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3441 || mState == State.WIDGETS_SPRING_LOADED;
3442 }
3443
Michael Jurkad3ef3062010-11-23 16:23:58 -08003444 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003445 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003446 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003447 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003448 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003449 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003450 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3451 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003452 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003453 }
3454
Winson Chung4ac30062015-05-08 17:34:17 -07003455 /**
3456 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3457 * resumed.
3458 */
3459 private void tryAndUpdatePredictedApps() {
3460 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003461 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003462 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003463 mAppsView.setPredictedApps(apps);
Hyunyoung Songddec1c72016-04-12 18:32:04 -07003464 mUserEventLogger.setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003465 }
3466 }
3467 }
3468
Michael Jurkab3e22d92011-10-31 15:58:33 -07003469 void lockAllApps() {
3470 // TODO
3471 }
3472
3473 void unlockAllApps() {
3474 // TODO
3475 }
3476
Winsonf768d932015-09-25 16:12:35 -07003477 public boolean launcherCallbacksProvidesSearch() {
3478 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3479 }
3480
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003481 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003482 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003483 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003484 }
3485
Adam Cohen24ce0b32014-01-14 16:18:14 -08003486 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003487 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3488 if (searchProvider == null) {
3489 return null;
3490 }
3491
3492 Bundle opts = new Bundle();
3493 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003494 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003495
Tony Wickham3a3517f2015-10-06 11:27:04 -07003496 // Determine the min and max dimensions of the widget.
3497 LauncherAppState app = LauncherAppState.getInstance();
3498 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3499 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3500 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003501 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3502 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003503 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003504 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003505 int minWidth = maxWidth;
3506 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003507 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3508 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3509 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3510 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3511 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003512 }
3513 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3514 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3515 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3516 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003517 if (LOGD) {
3518 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3519 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3520 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003521
Tony Wickham775455c2015-10-16 09:49:32 -07003522 if (mLauncherCallbacks != null) {
3523 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3524 }
3525
Sunny Goyalf7258242015-10-19 16:59:07 -07003526 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003527 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003528 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003529 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003530 || (widgetInfo == null)
3531 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003532 // A valid widget is not already bound.
3533 if (widgetId > -1) {
3534 mAppWidgetHost.deleteAppWidgetId(widgetId);
3535 widgetId = -1;
3536 }
3537
3538 // Try to bind a new widget
3539 widgetId = mAppWidgetHost.allocateAppWidgetId();
3540
3541 if (!AppWidgetManagerCompat.getInstance(this)
3542 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3543 mAppWidgetHost.deleteAppWidgetId(widgetId);
3544 widgetId = -1;
3545 }
3546
Sunny Goyalf7258242015-10-19 16:59:07 -07003547 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 .putInt(QSB_WIDGET_ID, widgetId)
3549 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003550 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003551 }
3552
Sunny Goyal8d595092015-07-06 12:22:14 -07003553 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003554 if (widgetId != -1) {
3555 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003556 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003557 mQsb.updateAppWidgetOptions(opts);
3558 mQsb.setPadding(0, 0, 0, 0);
3559 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003560 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003561 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003562 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003563 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003564 }
3565
Sunny Goyal22235bc2015-04-03 09:23:43 -07003566 private void reinflateQSBIfNecessary() {
3567 if (mQsb instanceof LauncherAppWidgetHostView &&
3568 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3569 mSearchDropTargetBar.removeView(mQsb);
3570 mQsb = null;
3571 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3572 }
3573 }
3574
Michael Jurkad7c28052012-04-27 15:43:36 -07003575 @Override
3576 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003577 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003578 final List<CharSequence> text = event.getText();
3579 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003580 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003581 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003582 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003583 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003584 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003585 } else if (mWorkspace != null) {
3586 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003587 } else {
3588 text.add(getString(R.string.all_apps_home_button_label));
3589 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003590 return result;
3591 }
3592
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003593 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003594 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003595 */
Adam Cohen091440a2015-03-18 14:16:05 -07003596 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003597 @Override
3598 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003599 closeSystemDialogs();
3600 }
3601 }
3602
3603 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 * If the activity is currently paused, signal that we need to run the passed Runnable
3605 * in onResume.
3606 *
3607 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003608 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3609 * wrong when we're not running, and if the activity comes back to what the configuration was
3610 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 *
3612 * Implementation of the method from LauncherModel.Callbacks.
3613 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003614 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003615 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003616 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003617 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003618 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003619 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003620 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003621 }
3622 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003623 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003624 return true;
3625 } else {
3626 return false;
3627 }
3628 }
3629
Michael Jurkac402cd92013-05-20 15:49:32 +02003630 private boolean waitUntilResume(Runnable run) {
3631 return waitUntilResume(run, false);
3632 }
3633
Michael Jurka1e2f4652013-07-08 18:03:46 -07003634 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003635 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003636 }
3637
Michael Jurka7607c2f2013-04-03 14:33:19 -07003638 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003639 * If the activity is currently paused, signal that we need to re-run the loader
3640 * in onResume.
3641 *
3642 * This needs to be called from incoming places where resources might have been loaded
3643 * while we are paused. That is becaues the Configuration might be wrong
3644 * when we're not running, and if it comes back to what it was when we
3645 * were paused, we are not restarted.
3646 *
3647 * Implementation of the method from LauncherModel.Callbacks.
3648 *
3649 * @return true if we are currently paused. The caller might be able to
3650 * skip some work in that case since we will come back again.
3651 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003652 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003653 public boolean setLoadOnResume() {
3654 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003655 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003656 mOnResumeNeedsLoad = true;
3657 return true;
3658 } else {
3659 return false;
3660 }
3661 }
3662
3663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003665 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003666 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003668 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003669 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003670 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003671 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003672 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003674
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003676 * Clear any pending bind callbacks. This is called when is loader is planning to
3677 * perform a full rebind from scratch.
3678 */
3679 @Override
3680 public void clearPendingBinds() {
3681 mBindOnResumeCallbacks.clear();
3682 if (mPendingExecutor != null) {
3683 mPendingExecutor.markCompleted();
3684 mPendingExecutor = null;
3685 }
3686 }
3687
3688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 * Refreshes the shortcuts shown on the workspace.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
3692 */
3693 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003694 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003695
Winson Chungd64d1762013-08-20 14:37:16 -07003696 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003697 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003698 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003699
Michael Jurka05bf644e2011-11-30 20:28:53 -08003700 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003701 if (mHotseat != null) {
3702 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003703 }
3704 }
3705
Adam Cohendcd297f2013-06-18 13:13:40 -07003706 @Override
3707 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003708 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003709
Adam Cohen5084cba2013-09-03 12:01:16 -07003710 // If there are no screens, we need to have an empty screen
3711 if (orderedScreenIds.size() == 0) {
3712 mWorkspace.addExtraEmptyScreen();
3713 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003714
3715 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003716 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003717 if (hasCustomContentToLeft()) {
3718 mWorkspace.createCustomContentContainer();
3719 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003720 }
Winson Chung64359a52013-07-08 17:17:08 -07003721 }
3722
3723 @Override
3724 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003725 int count = orderedScreenIds.size();
3726 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003727 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003729 }
3730
Winson Chungd64d1762013-08-20 14:37:16 -07003731 public void bindAppsAdded(final ArrayList<Long> newScreens,
3732 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003733 final ArrayList<ItemInfo> addAnimated,
3734 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003735 Runnable r = new Runnable() {
3736 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003737 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003738 }
3739 };
3740 if (waitUntilResume(r)) {
3741 return;
3742 }
3743
Winson Chungd64d1762013-08-20 14:37:16 -07003744 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003745 if (newScreens != null) {
3746 bindAddScreens(newScreens);
3747 }
Winson Chungd64d1762013-08-20 14:37:16 -07003748
3749 // We add the items without animation on non-visible pages, and with
3750 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003751 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003752 bindItems(addNotAnimated, 0,
3753 addNotAnimated.size(), false);
3754 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003755 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003756 bindItems(addAnimated, 0,
3757 addAnimated.size(), true);
3758 }
Winson Chungc58497e2013-09-03 17:48:37 -07003759
Winson Chung87412982013-10-03 18:34:14 -07003760 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003761 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003762
Winson Chungb745afb2015-03-02 11:51:23 -08003763 if (addedApps != null && mAppsView != null) {
3764 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003765 }
Winson Chungd64d1762013-08-20 14:37:16 -07003766 }
3767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003768 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 * Bind the items start-end from the list.
3770 *
3771 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003772 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003773 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003774 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3775 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003776 Runnable r = new Runnable() {
3777 public void run() {
3778 bindItems(shortcuts, start, end, forceAnimateIcons);
3779 }
3780 };
3781 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003782 return;
3783 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003784
Winson Chungf0c6ae02012-03-21 16:10:31 -07003785 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003786 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3787 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003788 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003789 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003790 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003793
3794 // Short circuit if we are loading dock items for a configuration which has no dock
3795 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3796 mHotseat == null) {
3797 continue;
3798 }
3799
Sunny Goyal639e9062015-08-19 19:17:06 -07003800 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 switch (item.itemType) {
3802 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3803 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003804 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003805 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003806
Winson Chung64359a52013-07-08 17:17:08 -07003807 /*
3808 * TODO: FIX collision case
3809 */
Winson Chungce376632013-07-11 16:12:41 -07003810 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3811 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3812 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003813 View v = cl.getChildAt(item.cellX, item.cellY);
3814 Object tag = v.getTag();
3815 String desc = "Collision while binding workspace item: " + item
3816 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003817 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003818 throw (new RuntimeException(desc));
3819 } else {
3820 Log.d(TAG, desc);
3821 }
Winson Chungce376632013-07-11 16:12:41 -07003822 }
Winson Chung64359a52013-07-08 17:17:08 -07003823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003825 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003826 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003827 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003828 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003830 default:
3831 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003833
3834 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3835 item.cellY, 1, 1);
3836 if (animateIcons) {
3837 // Animate all the applications up now
3838 view.setAlpha(0f);
3839 view.setScaleX(0f);
3840 view.setScaleY(0f);
3841 bounceAnims.add(createNewAppBounceAnimation(view, i));
3842 newShortcutsScreenId = item.screenId;
3843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003844 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003845
Winson Chung997a9232013-07-24 15:33:46 -07003846 if (animateIcons) {
3847 // Animate to the correct page
3848 if (newShortcutsScreenId > -1) {
3849 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003850 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003851 final Runnable startBounceAnimRunnable = new Runnable() {
3852 public void run() {
3853 anim.playTogether(bounceAnims);
3854 anim.start();
3855 }
3856 };
Winson Chung997a9232013-07-24 15:33:46 -07003857 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003858 // We post the animation slightly delayed to prevent slowdowns
3859 // when we are loading right after we return to launcher.
3860 mWorkspace.postDelayed(new Runnable() {
3861 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003862 if (mWorkspace != null) {
3863 mWorkspace.snapToPage(newScreenIndex);
3864 mWorkspace.postDelayed(startBounceAnimRunnable,
3865 NEW_APPS_ANIMATION_DELAY);
3866 }
Winson Chung94d67682013-09-25 16:29:40 -07003867 }
3868 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003869 } else {
3870 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003871 }
3872 }
Winson Chung64359a52013-07-08 17:17:08 -07003873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875 }
3876
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003877 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3878 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3879 view.updateIcon(mIconCache);
3880 item.hostView = view;
3881 item.hostView.updateAppWidget(null);
3882 item.hostView.setOnClickListener(this);
3883 addAppWidgetToWorkspace(item, null, false);
3884 mWorkspace.requestLayout();
3885 }
3886
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 /**
3888 * Add the views for a widget to the workspace.
3889 *
3890 * Implementation of the method from LauncherModel.Callbacks.
3891 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003892 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 Runnable r = new Runnable() {
3894 public void run() {
3895 bindAppWidget(item);
3896 }
3897 };
3898 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return;
3900 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003901
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 if (mIsSafeModeEnabled) {
3903 bindSafeModeWidget(item);
3904 return;
3905 }
3906
Daniel Sandler843e8602010-06-07 14:59:01 -04003907 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "bindAppWidget: " + item);
3910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003912 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003913
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003914 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3915 // If the provider is not ready, bind as a pending widget.
3916 appWidgetInfo = null;
3917 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3918 // The widget id is not valid. Try to find the widget based on the provider info.
3919 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3920 } else {
3921 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3922 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003923
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003924 // If the provider is ready, but the width is not yet restored, try to restore it.
3925 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3926 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003927 if (appWidgetInfo == null) {
3928 if (DEBUG_WIDGETS) {
3929 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3930 + " belongs to component " + item.providerName
3931 + ", as the povider is null");
3932 }
3933 LauncherModel.deleteItemFromDatabase(this, item);
3934 return;
3935 }
Sunny Goyalff572272014-07-23 13:58:07 -07003936
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003937 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003938 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003939 // Note: This assumes that the id remap broadcast is received before this step.
3940 // If that is not the case, the id remap will be ignored and user may see the
3941 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003942 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003943 pendingInfo.spanX = item.spanX;
3944 pendingInfo.spanY = item.spanY;
3945 pendingInfo.minSpanX = item.minSpanX;
3946 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003947 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003948
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003949 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3950 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3951 newWidgetId, appWidgetInfo, options);
3952
3953 // TODO consider showing a permission dialog when the widget is clicked.
3954 if (!success) {
3955 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3958 + " belongs to component " + item.providerName
3959 + ", as the launcher is unable to bing a new widget id");
3960 }
3961 LauncherModel.deleteItemFromDatabase(this, item);
3962 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003963 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003964
3965 item.appWidgetId = newWidgetId;
3966
3967 // If the widget has a configure activity, it is still needs to set it up, otherwise
3968 // the widget is ready to go.
3969 item.restoreStatus = (appWidgetInfo.configure == null)
3970 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3971 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3972
3973 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003974 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003975 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003976 // The widget was marked as UI not ready, but there is no configure activity to
3977 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003978 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3979 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003980 }
Sunny Goyalff572272014-07-23 13:58:07 -07003981 }
3982
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003983 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003984 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003985 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3986 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003988
Sunny Goyal56c73602015-09-25 12:55:01 -07003989 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003990 if (appWidgetInfo == null) {
3991 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003992 deleteWidgetInfo(item);
3993 return;
3994 }
3995
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003996 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003997 item.minSpanX = appWidgetInfo.minSpanX;
3998 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003999 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004000 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004001 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4002 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004003 view.updateIcon(mIconCache);
4004 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004005 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004006 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004007 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004008 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004009 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010
Daniel Sandler843e8602010-06-07 14:59:01 -04004011 if (DEBUG_WIDGETS) {
4012 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4013 + (SystemClock.uptimeMillis()-start) + "ms");
4014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 }
4016
Sunny Goyalff572272014-07-23 13:58:07 -07004017 /**
4018 * Restores a pending widget.
4019 *
4020 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004021 */
4022 private void completeRestoreAppWidget(final int appWidgetId) {
4023 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4024 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4025 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4026 return;
4027 }
4028
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004029 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004030 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4031
4032 mWorkspace.reinflateWidgetsIfNecessary();
4033 LauncherModel.updateItemInDatabase(this, info);
4034 }
4035
Adam Cohen1462de32012-07-24 22:34:36 -07004036 public void onPageBoundSynchronously(int page) {
4037 mSynchronouslyBoundPages.add(page);
4038 }
4039
Sunny Goyal527c7d32015-08-28 15:19:36 -07004040 @Override
4041 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4042 if (mPendingExecutor != null) {
4043 mPendingExecutor.markCompleted();
4044 }
4045 mPendingExecutor = executor;
4046 executor.attachTo(this);
4047 }
4048
4049 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4050 if (mPendingExecutor == executor) {
4051 mPendingExecutor = null;
4052 }
4053 }
4054
Joe Onorato9c1289c2009-08-17 11:03:03 -04004055 /**
4056 * Callback saying that there aren't any more items to bind.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004060 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004061 Runnable r = new Runnable() {
4062 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004063 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004064 }
4065 };
4066 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 return;
4068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 if (mSavedState != null) {
4070 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004071 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004073
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 mSavedState = null;
4075 }
4076
Adam Cohen1462de32012-07-24 22:34:36 -07004077 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004079 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004080
4081 // If we received the result of any pending adds while the loader was running (e.g. the
4082 // widget configuration forced an orientation change), process them now.
4083 if (sPendingAddItem != null) {
4084 final long screenId = completeAdd(sPendingAddItem);
4085
4086 // TODO: this moves the user to the page where the pending item was added. Ideally,
4087 // the screen would be guaranteed to exist after bind, and the page would be set through
4088 // the workspace restore process.
4089 mWorkspace.post(new Runnable() {
4090 @Override
4091 public void run() {
4092 mWorkspace.snapToScreenId(screenId);
4093 }
4094 });
4095 sPendingAddItem = null;
4096 }
4097
Sunny Goyal756adbc2015-04-16 15:20:51 -07004098 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004099
4100 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004101 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004102 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004103 }
4104
Winson Chunga0b7e862013-09-05 16:03:15 -07004105 public boolean isAllAppsButtonRank(int rank) {
4106 if (mHotseat != null) {
4107 return mHotseat.isAllAppsButtonRank(rank);
4108 }
4109 return false;
4110 }
4111
Winson Chunga2413752012-04-03 14:22:34 -07004112 private boolean canRunNewAppsAnimation() {
4113 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004114 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4115 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004116 }
4117
Winson Chungc9168342013-06-26 14:54:55 -07004118 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004119 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004120 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4121 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004122 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004123 return bounceAnim;
4124 }
4125
Adam Cohen27772732013-11-11 14:00:56 +00004126 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004127 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004128 }
4129
Tony Wickham3a3517f2015-10-06 11:27:04 -07004130 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004131 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004132 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004133 }
4134
Tony Wickham55616cd2015-09-23 14:55:17 -07004135 public int getSearchBarHeight() {
4136 if (mLauncherCallbacks != null) {
4137 return mLauncherCallbacks.getSearchBarHeight();
4138 }
4139 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4140 }
4141
Winson Chung88fa7412015-08-03 14:25:28 -07004142 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004143 if (mSearchDropTargetBar == null) {
4144 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004145 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004146 if (mQsb != null) {
4147 mSearchDropTargetBar.removeView(mQsb);
4148 mQsb = null;
4149 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004150 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004151 }
4152
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004153 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004154 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4155 * multiple calls to bind the same list.)
4156 */
4157 @Thunk ArrayList<AppInfo> mTmpAppsList;
4158 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4159 public void run() {
4160 bindAllApplications(mTmpAppsList);
4161 mTmpAppsList = null;
4162 }
4163 };
4164
4165 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004166 * Add the icons for all apps.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004170 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004171 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4172 mTmpAppsList = apps;
4173 return;
4174 }
4175
Winson Chungb745afb2015-03-02 11:51:23 -08004176 if (mAppsView != null) {
4177 mAppsView.setApps(apps);
4178 }
Adam Cohen9211d422014-10-07 18:14:53 -07004179 if (mLauncherCallbacks != null) {
4180 mLauncherCallbacks.bindAllApplications(apps);
4181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 }
4183
4184 /**
4185 * A package was updated.
4186 *
4187 * Implementation of the method from LauncherModel.Callbacks.
4188 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004189 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004190 Runnable r = new Runnable() {
4191 public void run() {
4192 bindAppsUpdated(apps);
4193 }
4194 };
4195 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004196 return;
4197 }
4198
Winson Chungb745afb2015-03-02 11:51:23 -08004199 if (mAppsView != null) {
4200 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
4203
Sunny Goyal4390ace2014-10-13 11:33:11 -07004204 @Override
4205 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4206 Runnable r = new Runnable() {
4207 public void run() {
4208 bindWidgetsRestored(widgets);
4209 }
4210 };
4211 if (waitUntilResume(r)) {
4212 return;
4213 }
4214 mWorkspace.widgetsRestored(widgets);
4215 }
4216
Joe Onorato9c1289c2009-08-17 11:03:03 -04004217 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004218 * Some shortcuts were updated in the background.
4219 *
4220 * Implementation of the method from LauncherModel.Callbacks.
4221 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004222 @Override
4223 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4224 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004225 Runnable r = new Runnable() {
4226 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004227 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004228 }
4229 };
4230 if (waitUntilResume(r)) {
4231 return;
4232 }
4233
Sunny Goyal4390ace2014-10-13 11:33:11 -07004234 if (!updated.isEmpty()) {
4235 mWorkspace.updateShortcuts(updated);
4236 }
4237
4238 if (!removed.isEmpty()) {
4239 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4240 for (ShortcutInfo si : removed) {
4241 removedComponents.add(si.getTargetComponent());
4242 }
4243 mWorkspace.removeItemsByComponentName(removedComponents, user);
4244 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004245 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004246 }
4247 }
4248
4249 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004250 * Update the state of a package, typically related to install state.
4251 *
4252 * Implementation of the method from LauncherModel.Callbacks.
4253 */
Sunny Goyale755d462014-07-22 13:48:29 -07004254 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004255 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4256 Runnable r = new Runnable() {
4257 public void run() {
4258 bindRestoreItemsChange(updates);
4259 }
4260 };
4261 if (waitUntilResume(r)) {
4262 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004263 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004264
Sunny Goyal756adbc2015-04-16 15:20:51 -07004265 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004266 }
4267
4268 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004269 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004270 * in addition to specific applications to remove, the reason being that
4271 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004272 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004273 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004275 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004276 public void bindWorkspaceComponentsRemoved(
4277 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4278 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004279 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004280 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004281 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004282 }
Winson Chungd64d1762013-08-20 14:37:16 -07004283 };
4284 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004285 return;
4286 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004287 if (!packageNames.isEmpty()) {
4288 mWorkspace.removeItemsByPackageName(packageNames, user);
4289 }
4290 if (!components.isEmpty()) {
4291 mWorkspace.removeItemsByComponentName(components, user);
4292 }
4293 // Notify the drag controller
4294 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004295
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004296 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004297
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004298 @Override
4299 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindAppInfosRemoved(appInfos);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
4306 return;
Joe Onorato36115782010-06-17 13:28:48 -04004307 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004308
Winson Chungdf95eb12013-10-16 14:57:07 -07004309 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004310 if (mAppsView != null) {
4311 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004313 }
Joe Onoratobe386092009-11-17 17:32:16 -08004314
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004315 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004316 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004317 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004318 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004319 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004320
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004321 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004322 public void bindWidgetsModel(WidgetsModel model) {
4323 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004324 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004325 return;
4326 }
4327
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004328 if (mWidgetsView != null && model != null) {
4329 mWidgetsView.addWidgets(model);
4330 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004331 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004332 }
4333
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004334 @Override
4335 public void notifyWidgetProvidersChanged() {
4336 if (mWorkspace.getState().shouldUpdateWidget) {
4337 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4338 }
4339 }
4340
Winson Chung400438b2011-01-16 17:53:48 -08004341 private int mapConfigurationOriActivityInfoOri(int configOri) {
4342 final Display d = getWindowManager().getDefaultDisplay();
4343 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4344 switch (d.getRotation()) {
4345 case Surface.ROTATION_0:
4346 case Surface.ROTATION_180:
4347 // We are currently in the same basic orientation as the natural orientation
4348 naturalOri = configOri;
4349 break;
4350 case Surface.ROTATION_90:
4351 case Surface.ROTATION_270:
4352 // We are currently in the other basic orientation to the natural orientation
4353 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4354 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4355 break;
4356 }
4357
4358 int[] oriMap = {
4359 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4360 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4361 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4362 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4363 };
4364 // Since the map starts at portrait, we need to offset if this device's natural orientation
4365 // is landscape.
4366 int indexOffset = 0;
4367 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4368 indexOffset = 1;
4369 }
4370 return oriMap[(d.getRotation() + indexOffset) % 4];
4371 }
Adam Cohen446e9402011-09-15 18:21:21 -07004372
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004373 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004374 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004375 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004376 if (Utilities.ATLEAST_JB_MR2) {
4377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4378 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004379 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4380 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004381 }
Winson Chung4b919f82012-05-01 10:44:08 -07004382 }
4383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004384
Winson Chung4b919f82012-05-01 10:44:08 -07004385 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004386 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004387 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004388 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004389 } else {
4390 mHandler.postDelayed(new Runnable() {
4391 public void run() {
4392 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4393 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004394 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004395 }
Winson Chung4b919f82012-05-01 10:44:08 -07004396 }
Winson Chung400438b2011-01-16 17:53:48 -08004397 }
4398
Winson Chunge43a1e72014-01-15 10:33:02 -08004399 protected boolean isLauncherPreinstalled() {
4400 PackageManager pm = getPackageManager();
4401 try {
4402 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4403 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4404 return true;
4405 } else {
4406 return false;
4407 }
4408 } catch (NameNotFoundException e) {
4409 e.printStackTrace();
4410 return false;
4411 }
4412 }
4413
Adam Cohenea90f832014-05-21 15:03:34 -07004414 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004415 * To be overridden by subclasses to indicate that there is an activity to launch
4416 * before showing the standard launcher experience.
4417 */
4418 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004419 if (mLauncherCallbacks != null) {
4420 return mLauncherCallbacks.hasFirstRunActivity();
4421 }
Adam Cohen432609a2014-03-13 17:03:22 -07004422 return false;
4423 }
4424
4425 /**
4426 * To be overridden by subclasses to launch any first run activity
4427 */
4428 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004429 if (mLauncherCallbacks != null) {
4430 return mLauncherCallbacks.getFirstRunActivity();
4431 }
Adam Cohen432609a2014-03-13 17:03:22 -07004432 return null;
4433 }
4434
Winson Chunga6945242014-01-08 14:04:34 -08004435 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004436 return !ActivityManager.isRunningInTestHarness() &&
4437 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439
Adam Cohen4a9423d2014-03-28 14:22:31 -07004440 protected boolean hasRunFirstRunActivity() {
4441 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4442 }
4443
Adam Cohen432609a2014-03-13 17:03:22 -07004444 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 if (shouldRunFirstRunActivity() &&
4446 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004447 Intent firstRunIntent = getFirstRunActivity();
4448 if (firstRunIntent != null) {
4449 startActivity(firstRunIntent);
4450 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004452 }
4453 }
Adam Cohen432609a2014-03-13 17:03:22 -07004454 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004455 }
4456
4457 private void markFirstRunActivityShown() {
4458 SharedPreferences.Editor editor = mSharedPrefs.edit();
4459 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4460 editor.apply();
4461 }
4462
Adam Cohen432609a2014-03-13 17:03:22 -07004463 /**
4464 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4465 * screen that must be displayed and dismissed.
4466 */
4467 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.hasDismissableIntroScreen();
4470 }
Adam Cohen432609a2014-03-13 17:03:22 -07004471 return false;
4472 }
4473
4474 /**
4475 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4476 */
4477 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.getIntroScreen();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return null;
4482 }
4483
4484 /**
4485 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4486 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4487 */
4488 private boolean shouldShowIntroScreen() {
4489 return hasDismissableIntroScreen() &&
4490 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4491 }
4492
4493 protected void showIntroScreen() {
4494 View introScreen = getIntroScreen();
4495 changeWallpaperVisiblity(false);
4496 if (introScreen != null) {
4497 mDragLayer.showOverlayView(introScreen);
4498 }
4499 }
4500
4501 public void dismissIntroScreen() {
4502 markIntroScreenDismissed();
4503 if (showFirstRunActivity()) {
4504 // We delay hiding the intro view until the first run activity is showing. This
4505 // avoids a blip.
4506 mWorkspace.postDelayed(new Runnable() {
4507 @Override
4508 public void run() {
4509 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004510 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512 }, ACTIVITY_START_DELAY);
4513 } else {
4514 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004515 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004516 }
4517 changeWallpaperVisiblity(true);
4518 }
4519
4520 private void markIntroScreenDismissed() {
4521 SharedPreferences.Editor editor = mSharedPrefs.edit();
4522 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4523 editor.apply();
4524 }
4525
Adam Cohen091440a2015-03-18 14:16:05 -07004526 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004527 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4528 // on the device, then we always show the first run cling experience (or if there is no
4529 // launcher2). Otherwise, we prompt the user upon started for migration
4530 LauncherClings launcherClings = new LauncherClings(this);
4531 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004532 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004533 if (mModel.canMigrateFromOldLauncherDb(this)) {
4534 launcherClings.showMigrationCling();
4535 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004536 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004537 }
4538 }
4539 }
4540
Winson Chunga6945242014-01-08 14:04:34 -08004541 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004542 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4543 if (mHotseat != null) mHotseat.setAlpha(1f);
4544 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004545 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4546 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004547 }
4548
4549 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004550 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4551 if (mHotseat != null) mHotseat.setAlpha(0f);
4552 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004553 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4554 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004555 }
4556
Winson Chung5ffd51d2015-06-25 11:36:50 -07004557 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004558 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004559 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004560 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004561 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004562 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004563 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4564 if (userHandle != null) {
4565 user = UserHandleCompat.fromUser(userHandle);
4566 }
4567 }
4568 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004569 }
4570
4571 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004572 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004573 if (user == null) {
4574 user = UserHandleCompat.myUserHandle();
4575 }
4576
4577 // Called from search suggestion, add the profile extra to the intent to ensure that we
4578 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004579 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004580 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004581 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004582 return null;
4583 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004584 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004585 }
4586
Winson Chung5ffd51d2015-06-25 11:36:50 -07004587 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004588 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4589 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004590 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004591 UserHandleCompat.myUserHandle());
4592 }
4593
Winson Chung5ffd51d2015-06-25 11:36:50 -07004594 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004595 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4596 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004597 mWorkspace.onExternalDragStartedWithItem(dragView);
4598 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004599 }
4600
Winson Chung5ffd51d2015-06-25 11:36:50 -07004601 protected void moveWorkspaceToDefaultScreen() {
4602 mWorkspace.moveToDefaultScreen(false);
4603 }
4604
Winson Chung80baf5a2010-08-09 16:03:15 -07004605 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004606 * Returns a FastBitmapDrawable with the icon, accurately sized.
4607 */
4608 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4609 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4610 d.setFilterBitmap(true);
4611 resizeIconDrawable(d);
4612 return d;
4613 }
4614
4615 /**
4616 * Resizes an icon drawable to the correct icon size.
4617 */
Winson5fbe0742015-09-30 15:33:00 -07004618 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004619 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004620 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004621 }
4622
4623 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004624 * Prints out out state for debugging.
4625 */
4626 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004627 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004628 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004629 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4630 Log.d(TAG, "mRestoring=" + mRestoring);
4631 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004632 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004633 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004634
Daniel Sandler325dc232013-06-05 22:57:57 -04004635 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004636 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004637
4638 @Override
4639 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4640 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004641 // Dump workspace
4642 writer.println(prefix + "Workspace Items");
4643 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4644 writer.println(prefix + " Homescreen " + i);
4645
4646 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4647 for (int j = 0; j < layout.getChildCount(); j++) {
4648 Object tag = layout.getChildAt(j).getTag();
4649 if (tag != null) {
4650 writer.println(prefix + " " + tag.toString());
4651 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004652 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004653 }
Sunny Goyala1365452015-10-01 15:46:24 -07004654
4655 writer.println(prefix + " Hotseat");
4656 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4657 for (int j = 0; j < layout.getChildCount(); j++) {
4658 Object tag = layout.getChildAt(j).getTag();
4659 if (tag != null) {
4660 writer.println(prefix + " " + tag.toString());
4661 }
4662 }
4663
4664 synchronized (sDumpLogs) {
4665 writer.println();
4666 writer.println(prefix + "Debug logs");
4667 for (String log : sDumpLogs) {
4668 writer.println(prefix + " " + log);
4669 }
4670 }
4671
Adam Cohen9211d422014-10-07 18:14:53 -07004672 if (mLauncherCallbacks != null) {
4673 mLauncherCallbacks.dump(prefix, fd, writer, args);
4674 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004675 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004676
Sunny Goyala1365452015-10-01 15:46:24 -07004677 public static void addDumpLog(String tag, String log) {
4678 Log.d(tag, log);
4679 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004680 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004681 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004682 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004683 }
4684
Adam Cohen59400422014-03-05 18:07:04 -08004685 public static CustomAppWidget getCustomAppWidget(String name) {
4686 return sCustomAppWidgets.get(name);
4687 }
4688
4689 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4690 return sCustomAppWidgets;
4691 }
4692
Sunny Goyal29538332016-02-18 09:10:19 -08004693 public static List<View> getFolderContents(View icon) {
4694 if (icon instanceof FolderIcon) {
4695 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4696 } else {
4697 return Collections.EMPTY_LIST;
4698 }
4699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004700}
Michael Jurka2763be32011-02-24 11:19:57 -08004701
Dan Sandlerd5024042014-01-09 15:01:33 -05004702interface DebugIntents {
4703 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4704 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004705}