blob: 7311cce03159ce89b8723e088b67eb7e42ec5c5d [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;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
95import com.android.launcher3.PagedView.PageSwitchListener;
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,
Winson Chung006ee262015-08-03 14:40:11 -0700141 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
Dan Sandlerd5024042014-01-09 15:01:33 -0500150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
175 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700186 // Type: Content Values / parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohen432609a2014-03-13 17:03:22 -0700193 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Adam Cohen3ed316a2014-07-23 18:21:20 -0700196 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
197 static final String ACTION_FIRST_LOAD_COMPLETE =
198 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
199
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 private static final String QSB_WIDGET_ID = "qsb_widget_id";
201 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800229 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700238 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800239 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800240 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700241
Michael Jurka0280c3b2010-09-17 15:00:07 -0700242 private int[] mTmpAddItemCellCoordinates = new int[2];
243
Sunny Goyal316490e2015-06-02 09:38:28 -0700244 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800245 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700248 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249
Winson Chungc51db6a2011-10-05 11:44:49 -0700250 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700251 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700256 // Main container view and the model for the widget tray screen.
257 @Thunk WidgetsContainerView mWidgetsView;
258 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
Sunny Goyal594d76d2014-11-06 10:12:54 -0800260 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700279 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700283 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700286 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Sunny Goyal639e9062015-08-19 19:17:06 -0700289 private LauncherClings mClings;
290
Sunny Goyale2df0622015-04-24 11:27:00 -0700291 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700313
Sunny Goyala1365452015-10-01 15:46:24 -0700314 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
315 private static final Date sDateStamp = new Date();
316 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700317 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800369 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800370
Adam Cohenf9c184a2016-01-15 16:47:43 -0800371 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal7779d622015-06-11 16:18:39 -0700383 private Runnable mUpdateOrientationRunnable = new Runnable() {
384 public void run() {
385 setOrientation();
386 }
387 };
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700415 mDeviceProfile = getResources().getConfiguration().orientation
416 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700417 app.getInvariantDeviceProfile().landscapeProfile
418 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700419
Sunny Goyalf7258242015-10-19 16:59:07 -0700420 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800429 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
Tony Wickhameef44322015-10-20 13:24:36 -0700448 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
449 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700451 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700452 mExtractedColors = new ExtractedColors();
453 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454
Joe Onorato7c312c12009-08-13 21:36:53 -0700455 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mSavedState = savedInstanceState;
458 restoreState(mSavedState);
459
460 if (PROFILE_STARTUP) {
461 android.os.Debug.stopMethodTracing();
462 }
463
464 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700465 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 // If the user leaves launcher, then we should just load items asynchronously when
467 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 } else {
470 // We only load the page synchronously if the user rotates (or triggers a
471 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700472 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
481 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800483 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 // In case we are on a device with locked rotation, we should look at preferences to check
485 // if the user has specifically allowed rotation.
486 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700487 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700488 }
489
490 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
491 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400492 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700493
Adam Cohen9211d422014-10-07 18:14:53 -0700494 if (mLauncherCallbacks != null) {
495 mLauncherCallbacks.onCreate(savedInstanceState);
496 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700497
498 if (shouldShowIntroScreen()) {
499 showIntroScreen();
500 } else {
501 showFirstRunActivity();
502 showFirstRunClings();
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
505
Sunny Goyal7779d622015-06-11 16:18:39 -0700506 @Override
507 public void onSettingsChanged(String settings, boolean value) {
508 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
509 mRotationEnabled = value;
510 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
511 mUpdateOrientationRunnable.run();
512 }
513 }
514 }
515
Tony Wickham827cef22016-03-17 15:39:39 -0700516 @Override
517 public void onExtractedColorsChanged() {
518 loadExtractedColorsAndColorItems();
519 }
520
521 private void loadExtractedColorsAndColorItems() {
522 if (mExtractedColors != null) {
523 mExtractedColors.load(this);
524 // TODO: pass mExtractedColors to interested items such as hotseat.
Tony Wickham827cef22016-03-17 15:39:39 -0700525 }
526 }
527
Adam Cohen9211d422014-10-07 18:14:53 -0700528 private LauncherCallbacks mLauncherCallbacks;
529
530 public void onPostCreate(Bundle savedInstanceState) {
531 super.onPostCreate(savedInstanceState);
532 if (mLauncherCallbacks != null) {
533 mLauncherCallbacks.onPostCreate(savedInstanceState);
534 }
535 }
536
Sunny Goyal32554d12015-12-03 15:31:25 -0800537 /**
538 * Call this after onCreate to set or clear overlay.
539 */
540 public void setLauncherOverlay(LauncherOverlay overlay) {
541 if (overlay != null) {
542 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
543 }
544 mWorkspace.setLauncherOverlay(overlay);
545 }
546
Adam Cohen9211d422014-10-07 18:14:53 -0700547 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
548 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 private boolean mWorkspaceImportanceStored = false;
551 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800553 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
555
556 @Override
557 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700559 return;
560 }
561 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700562 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700563 if (mWorkspace != null) {
564 mWorkspaceImportanceForAccessibility =
565 mWorkspace.getImportantForAccessibility();
566 mWorkspace.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mWorkspaceImportanceStored = true;
569 }
570 if (mHotseat != null) {
571 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
572 mHotseat.setImportantForAccessibility(
573 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
574 mHotseatImportanceStored = true;
575 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577
578 @Override
579 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700580 if (mWorkspaceImportanceStored && mWorkspace != null) {
581 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
582 }
583 if (mHotseatImportanceStored && mHotseat != null) {
584 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
585 }
586 mWorkspaceImportanceStored = false;
587 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700588 }
589 });
Adam Cohen9211d422014-10-07 18:14:53 -0700590 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700591 }
592
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700594 public void onLauncherProviderChange() {
595 if (mLauncherCallbacks != null) {
596 mLauncherCallbacks.onLauncherProviderChange();
597 }
598 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700599
Adam Cohen9211d422014-10-07 18:14:53 -0700600 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 return mLauncherCallbacks.hasCustomContentToLeft();
604 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700605 return false;
606 }
607
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 * {@link #addToCustomContentPage}. This will only be invoked if
611 * {@link #hasCustomContentToLeft()} is {@code true}.
612 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700614 if (mLauncherCallbacks != null) {
615 mLauncherCallbacks.populateCustomContentContainer();
616 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 }
618
619 /**
620 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
621 * ensure the custom content page is added or removed if necessary.
622 */
623 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600624 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600625 // Not bound yet, wait for bindScreens to be called.
626 return;
627 }
628
629 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
630 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500631 mWorkspace.createCustomContentContainer();
632 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600633 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
634 mWorkspace.removeCustomContentPage();
635 }
636 }
637
Anton Hanssona2f665f2013-09-26 12:18:32 +0100638 public Stats getStats() {
639 return mStats;
640 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641
642 /**
643 * Logger object is a singleton and does not have to be coupled with the foreground activity.
644 * Since most user event logging is done on the UI, the object is retrieved from the
645 * callback for convenience.
646 */
647 private void initLogger() {
648 if (mLauncherCallbacks != null) {
649 mUserEventLogger = mLauncherCallbacks.getLogger();
650 }
651 if (mUserEventLogger == null) {
652 mUserEventLogger = new UserEventLogger() {
653 @Override
654 public void processEvent(LauncherLogProto.LauncherEvent ev) {
655 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
656 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
657 LoggerUtils.getActionStr(ev.action),
658 LoggerUtils.getTargetStr(ev.srcTarget),
659 ev.elapsedContainerMillis,
660 ev.elapsedSessionMillis));
661 }
662 }
663 };
664 }
665 }
666
667 public UserEventLogger getLogger() {
668 return mUserEventLogger;
669 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100670
Hyunyoung Song3f471442015-04-08 19:01:34 -0700671 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700672 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
673 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700674 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700675 }
676
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000677 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700678 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
679 // This cast is safe as long as the id < 0x00FFFFFF
680 // Since we jail all the dynamically generated views, there should be no clashes
681 // with any other views.
682 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000683 }
684
685 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700686 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
687 * a configuration step, this allows the proper animations to run after other transitions.
688 */
Adam Cohendb364c32014-05-20 14:23:40 -0700689 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700690 long screenId = args.screenId;
691 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
692 // When the screen id represents an actual screen (as opposed to a rank) we make sure
693 // that the drop page actually exists.
694 screenId = ensurePendingDropLayoutExists(args.screenId);
695 }
Adam Cohendb364c32014-05-20 14:23:40 -0700696
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700700 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700701 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700703 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700704 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700705 case REQUEST_RECONFIGURE_APPWIDGET:
706 completeRestoreAppWidget(args.appWidgetId);
707 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 }
Michael Jurka27614d22012-04-02 06:40:22 -0700709 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
710 // if you turned the screen off and then back while in All Apps, Launcher would not
711 // return to the workspace. Clearing mAddInfo.container here fixes this issue
712 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700713 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
715
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700718 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700719 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700720 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800721 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700732 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
735 if (resultCode == RESULT_CANCELED) {
736 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800740 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700741 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700742
743 // When the user has granted permission to bind widgets, we should check to see if
744 // we can inflate the default search bar widget.
745 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 }
747 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200748 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
749 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700750 // User could have free-scrolled between pages before picking a wallpaper; make sure
751 // we move to the closest one now.
752 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700753 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 }
755 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700759 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700760
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 // We have special handling for widgets
763 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 final int appWidgetId;
765 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
766 : -1;
767 if (widgetId < 0) {
768 appWidgetId = pendingAddWidgetId;
769 } else {
770 appWidgetId = widgetId;
771 }
772
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800774 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700775 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
776 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 result = RESULT_CANCELED;
778 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 @Override
781 public void run() {
782 exitSpringLoadedDragModeDelayed(false, 0, null);
783 }
784 };
Adam Cohendb364c32014-05-20 14:23:40 -0700785 if (workspaceLocked) {
786 // No need to remove the empty screen if we're mid-binding, as the
787 // the bind will not add the empty screen.
788 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
789 } else {
790 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
792 }
Winson Chung5aaab772012-04-27 15:24:02 -0700793 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700794 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700795 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
796 // When the screen id represents an actual screen (as opposed to a rank)
797 // we make sure that the drop page actually exists.
798 mPendingAddInfo.screenId =
799 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
800 }
Adam Cohendb364c32014-05-20 14:23:40 -0700801 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
802
803 dropLayout.setDropPending(true);
804 final Runnable onComplete = new Runnable() {
805 @Override
806 public void run() {
807 completeTwoStageWidgetDrop(resultCode, appWidgetId);
808 dropLayout.setDropPending(false);
809 }
810 };
811 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 } else {
814 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
815 mPendingAddInfo);
816 sPendingAddItem = args;
817 }
Winson Chung5aaab772012-04-27 15:24:02 -0700818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 return;
820 }
821
Sunny Goyalff572272014-07-23 13:58:07 -0700822 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
823 if (resultCode == RESULT_OK) {
824 // Update the widget view.
825 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
826 pendingAddWidgetId, mPendingAddInfo);
827 if (workspaceLocked) {
828 sPendingAddItem = args;
829 } else {
830 completeAdd(args);
831 }
832 }
833 // Leave the widget in the pending state if the user canceled the configure.
834 return;
835 }
836
Sunny Goyalaad90582015-07-09 14:22:50 -0700837 if (requestCode == REQUEST_CREATE_SHORTCUT) {
838 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
839 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
840 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
841 mPendingAddInfo);
842 if (isWorkspaceLocked()) {
843 sPendingAddItem = args;
844 } else {
845 completeAdd(args);
846 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 }
849 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700850 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
851 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800855
856 }
857
858 @Override
859 protected void onActivityResult(
860 final int requestCode, final int resultCode, final Intent data) {
861 handleActivityResult(requestCode, resultCode, data);
862 if (mLauncherCallbacks != null) {
863 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 }
866
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600867 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700868 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600869 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700870 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
871 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
872 View v = null;
873 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
874 if (layout != null) {
875 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
876 }
877 Intent intent = sPendingAddItem.intent;
878 sPendingAddItem = null;
879 if (grantResults.length > 0
880 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
881 startActivity(v, intent, null);
882 } else {
883 // TODO: Show a snack bar with link to settings
884 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
885 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
886 }
887 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600888 if (mLauncherCallbacks != null) {
889 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
890 grantResults);
891 }
892 }
893
Adam Cohendb364c32014-05-20 14:23:40 -0700894 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
895 appWidgetId, ItemInfo info) {
896 PendingAddArguments args = new PendingAddArguments();
897 args.requestCode = requestCode;
898 args.intent = data;
899 args.container = info.container;
900 args.screenId = info.screenId;
901 args.cellX = info.cellX;
902 args.cellY = info.cellY;
903 args.appWidgetId = appWidgetId;
904 return args;
905 }
906
907 /**
908 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
909 *
910 * @param screenId the screen id to check
911 * @return the new screen, or screenId if it exists
912 */
913 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800914 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700915 if (dropLayout == null) {
916 // it's possible that the add screen was removed because it was
917 // empty and a re-bind occurred
918 mWorkspace.addExtraEmptyScreen();
919 return mWorkspace.commitExtraEmptyScreen();
920 } else {
921 return screenId;
922 }
923 }
924
Adam Cohen091440a2015-03-18 14:16:05 -0700925 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800926 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 Runnable onCompleteRunnable = null;
928 int animationType = 0;
929
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 if (resultCode == RESULT_OK) {
932 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
933 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 mPendingAddWidgetInfo);
935 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 onCompleteRunnable = new Runnable() {
937 @Override
938 public void run() {
939 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700941 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
942 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 }
944 };
945 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800946 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700949 if (mDragLayer.getAnimatedView() != null) {
950 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
951 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
952 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700953 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 // The animated view may be null in the case of a rotation during widget configuration
955 onCompleteRunnable.run();
956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
959 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 protected void onStop() {
961 super.onStop();
962 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700963
964 if (mLauncherCallbacks != null) {
965 mLauncherCallbacks.onStop();
966 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 }
968
969 @Override
970 protected void onStart() {
971 super.onStart();
972 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onStart();
976 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700977 }
978
979 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200981 long startTime = 0;
982 if (DEBUG_RESUME_TIME) {
983 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400984 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200985 }
Adam Cohen9211d422014-10-07 18:14:53 -0700986
987 if (mLauncherCallbacks != null) {
988 mLauncherCallbacks.preOnResume();
989 }
990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800992 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700993
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700995 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800997 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700998 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700999 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001000 // view after launching an app, as they may be depending on the UI to be static to
1001 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001002 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001003 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001004 } else if (mOnResumeState == State.WIDGETS) {
1005 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001006 }
1007 mOnResumeState = State.NONE;
1008
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001009 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001010 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1011 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001012
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001013 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001014 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001015 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001016 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001020 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001021 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1022 // execute them here
1023 long startTimeCallbacks = 0;
1024 if (DEBUG_RESUME_TIME) {
1025 startTimeCallbacks = System.currentTimeMillis();
1026 }
1027
Michael Jurka1e2f4652013-07-08 18:03:46 -07001028 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1029 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001030 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 if (DEBUG_RESUME_TIME) {
1033 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1034 (System.currentTimeMillis() - startTimeCallbacks));
1035 }
Michael Jurka447bf842013-05-15 14:52:15 +02001036 }
Michael Jurka54554252013-08-01 12:52:23 +02001037 if (mOnResumeCallbacks.size() > 0) {
1038 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1039 mOnResumeCallbacks.get(i).run();
1040 }
1041 mOnResumeCallbacks.clear();
1042 }
Winson Chunge4e50662012-01-23 14:45:13 -08001043
1044 // Reset the pressed state of icons that were locked in the press state while activities
1045 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001046 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001047 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 mWaitingForResume.setStayPressed(false);
1049 }
Winson Chung82963d52013-10-09 11:20:57 -07001050
Adam Cohen06dff352012-06-01 17:17:08 -07001051 // It is possible that widgets can receive updates while launcher is not in the foreground.
1052 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1053 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1054 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001055 if (!isWorkspaceLoading()) {
1056 getWorkspace().reinflateWidgetsIfNecessary();
1057 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001058 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001059
Michael Jurka447bf842013-05-15 14:52:15 +02001060 if (DEBUG_RESUME_TIME) {
1061 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1062 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001063
Adam Cohen4b66bf32015-09-18 12:15:19 -07001064 // We want to suppress callbacks about CustomContent being shown if we have just received
1065 // onNewIntent while the user was present within launcher. In that case, we post a call
1066 // to move the user to the main screen (which will occur after onResume). We don't want to
1067 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1068 // suppress here.
1069 if (mWorkspace.getCustomContentCallbacks() != null
1070 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001071 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001072 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1074 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001075 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 }
1077 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001078 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001079 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001080 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001081
Sunny Goyal756adbc2015-04-16 15:20:51 -07001082 if (!isWorkspaceLoading()) {
1083 // Process any items that were added while Launcher was away.
1084 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1085 }
Adam Cohen9211d422014-10-07 18:14:53 -07001086
1087 if (mLauncherCallbacks != null) {
1088 mLauncherCallbacks.onResume();
1089 }
Adam Cohen06dff352012-06-01 17:17:08 -07001090 }
1091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001094 // Ensure that items added to Launcher are queued until Launcher returns
1095 InstallShortcutReceiver.enableInstallQueue();
1096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001098 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001099 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001100 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001101
1102 // We call onHide() aggressively. The custom content callbacks should be able to
1103 // debounce excess onHide calls.
1104 if (mWorkspace.getCustomContentCallbacks() != null) {
1105 mWorkspace.getCustomContentCallbacks().onHide();
1106 }
Romain Guycbb89e42009-06-08 15:52:54 -07001107
Adam Cohen9211d422014-10-07 18:14:53 -07001108 if (mLauncherCallbacks != null) {
1109 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001110 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
1113 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001114 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1115 // by a onResume or by scrolling otherwise.
1116 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001117
1118 // Custom content is completely hidden
1119 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001120
1121 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1122 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001123
1124 // Indicates whether the user is allowed to scroll away from the custom content.
1125 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001126 }
1127
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 public interface LauncherOverlay {
1129
1130 /**
1131 * Touch interaction leading to overscroll has begun
1132 */
1133 public void onScrollInteractionBegin();
1134
1135 /**
1136 * Touch interaction related to overscroll has ended
1137 */
1138 public void onScrollInteractionEnd();
1139
1140 /**
1141 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1142 * screen (or in the case of RTL, the rightmost screen).
1143 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001144 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145
1146 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001147 * Called when the launcher is ready to use the overlay
1148 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001150 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 }
1152
Jun Mukai8af0cd82015-05-12 12:32:12 -07001153 public interface LauncherSearchCallbacks {
1154 /**
1155 * Called when the search overlay is shown.
1156 */
1157 public void onSearchOverlayOpened();
1158
1159 /**
1160 * Called when the search overlay is dismissed.
1161 */
1162 public void onSearchOverlayClosed();
1163 }
1164
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001166 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 }
1168
1169 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1170
Sunny Goyalc86df472016-02-25 09:19:38 -08001171 public void onScrollChanged(float progress) {
1172 if (mWorkspace != null) {
1173 mWorkspace.onOverlayScrollChanged(progress);
1174 }
1175 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001176 }
1177
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001179 if (mLauncherCallbacks != null) {
1180 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001181 } else {
1182 // On devices with a locked orientation, we will at least have the allow rotation
1183 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001184 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001185 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001186 }
1187
Adam Cohen9211d422014-10-07 18:14:53 -07001188 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001189 CustomContentCallbacks callbacks, String description) {
1190 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001191 }
1192
Adam Cohenedb40762013-07-18 16:45:45 -07001193 // The custom content needs to offset its content to account for the QSB
1194 public int getTopOffsetForCustomContent() {
1195 return mWorkspace.getPaddingTop();
1196 }
1197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001198 @Override
1199 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001201 if (mModel.isCurrentCallbacks(this)) {
1202 mModel.stopLoader();
1203 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001204 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1205
Romain Guy13c2e7b2010-03-10 19:45:00 -08001206 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001207 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 @Override
1211 public void onWindowFocusChanged(boolean hasFocus) {
1212 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001213 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001214
1215 if (mLauncherCallbacks != null) {
1216 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1217 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 private boolean acceptFilter() {
1221 final InputMethodManager inputManager = (InputMethodManager)
1222 getSystemService(Context.INPUT_METHOD_SERVICE);
1223 return !inputManager.isFullscreenMode();
1224 }
1225
1226 @Override
1227 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001228 final int uniChar = event.getUnicodeChar();
1229 final boolean handled = super.onKeyDown(keyCode, event);
1230 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1231 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1233 keyCode, event);
1234 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001236 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // showSearchDialog()
1238 // If there are multiple keystrokes before the search dialog takes focus,
1239 // onSearchRequested() will be called for every keystroke,
1240 // but it is idempotent, so it's fine.
1241 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
1244
Joe Onorato8a9625e2010-01-28 15:55:35 -08001245 // Eat the long press event so the keyboard doesn't come up.
1246 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1247 return true;
1248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 return handled;
1251 }
1252
Winson46163472015-09-22 17:31:56 -07001253 @Override
1254 public boolean onKeyUp(int keyCode, KeyEvent event) {
1255 if (keyCode == KeyEvent.KEYCODE_MENU) {
1256 // Ignore the menu key if we are currently dragging or are on the custom content screen
1257 if (!isOnCustomContent() && !mDragController.isDragging()) {
1258 // Close any open folders
1259 closeFolder();
1260
1261 // Stop resizing any widgets
1262 mWorkspace.exitWidgetResizeMode();
1263
1264 // Show the overview mode if we are on the workspace
1265 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1266 !mWorkspace.isSwitchingState()) {
1267 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001268 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001269 }
1270 }
1271 return true;
1272 }
1273 return super.onKeyUp(keyCode, event);
1274 }
1275
Karl Rosaen138a0412009-04-23 19:00:21 -07001276 private String getTypedText() {
1277 return mDefaultKeySsb.toString();
1278 }
1279
1280 private void clearTypedText() {
1281 mDefaultKeySsb.clear();
1282 mDefaultKeySsb.clearSpans();
1283 Selection.setSelection(mDefaultKeySsb, 0);
1284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001287 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1288 * State
1289 */
1290 private static State intToState(int stateOrdinal) {
1291 State state = State.WORKSPACE;
1292 final State[] stateValues = State.values();
1293 for (int i = 0; i < stateValues.length; i++) {
1294 if (stateValues[i].ordinal() == stateOrdinal) {
1295 state = stateValues[i];
1296 break;
1297 }
1298 }
1299 return state;
1300 }
1301
1302 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 * Restores the previous state, if it exists.
1304 *
1305 * @param savedState The previous state.
1306 */
1307 private void restoreState(Bundle savedState) {
1308 if (savedState == null) {
1309 return;
1310 }
1311
Michael Jurka883f55b2010-10-21 15:47:14 -07001312 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001313 if (state == State.APPS || state == State.WIDGETS) {
1314 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001315 }
1316
Adam Cohen21cd0022013-10-09 18:57:02 -07001317 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1318 PagedView.INVALID_RESTORE_PAGE);
1319 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001320 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
Sunny Goyal756cd262015-08-20 12:33:21 -07001323 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1324 if (itemValues != null) {
1325 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001326 AppWidgetProviderInfo info = savedState.getParcelable(
1327 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001328 mPendingAddWidgetInfo = info == null ?
1329 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1330
Adam Cohen4637b5a2013-11-04 18:21:24 -08001331 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001332 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 mRestoring = true;
1334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
1337 /**
1338 * Finds all the views we need and configure them properly.
1339 */
1340 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001341 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001342
Craig Mautner360310b2012-10-26 15:13:08 -07001343 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001344 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001345 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1346 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001347 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001348 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001349
Sunny Goyal784f9c32016-03-23 16:56:54 -07001350 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1351 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1352 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
Winson Chung4c98d922011-05-31 16:50:48 -07001355 // Setup the drag layer
1356 mDragLayer.setup(this, dragController);
1357
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 // Setup the hotseat
1359 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1360 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001361 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 }
1363
Winsone9f27272015-10-13 10:47:51 -07001364 // Setup the overview panel
1365 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366
Winson Chung4c98d922011-05-31 16:50:48 -07001367 // Setup the workspace
1368 mWorkspace.setHapticFeedbackEnabled(false);
1369 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001370 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001371 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001372
Tony Wickham34d2c912015-09-11 09:27:58 -07001373 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001374 mSearchDropTargetBar = (SearchDropTargetBar)
1375 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001376 // Get the app info bar
1377 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1378 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001379
Winson Chungef7f8742015-06-04 17:18:17 -07001380 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001381 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001382 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001383 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1384 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1385 } else {
1386 mAppsView.setSearchBarController(new DefaultAppSearchController());
1387 }
Craig Mautner360310b2012-10-26 15:13:08 -07001388
Winson Chung3d503fb2011-07-13 17:25:49 -07001389 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001390 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001391 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001392 dragController.setMoveTarget(mWorkspace);
1393 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001394 if (mSearchDropTargetBar != null) {
1395 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001396 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001397 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001398 if (mAppInfoDropTargetBar != null) {
1399 mAppInfoDropTargetBar.setup(this, dragController);
1400 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001401
Sunny Goyal322d5562015-06-25 19:35:49 -07001402 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1403 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
Winsone9f27272015-10-13 10:47:51 -07001407 private void setupOverviewPanel() {
1408 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1409
1410 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1411 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1412 @Override
1413 public boolean onLongClick(View v) {
1414 return v.performClick();
1415 }
1416 };
1417
1418 // Bind wallpaper button actions
1419 View wallpaperButton = findViewById(R.id.wallpaper_button);
1420 wallpaperButton.setOnClickListener(new OnClickListener() {
1421 @Override
1422 public void onClick(View view) {
1423 if (!mWorkspace.isSwitchingState()) {
1424 onClickWallpaperPicker(view);
1425 }
1426 }
1427 });
1428 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1429 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430
1431 // Bind widget button actions
1432 mWidgetsButton = findViewById(R.id.widget_button);
1433 mWidgetsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickAddWidgetButton(view);
1438 }
1439 }
1440 });
1441 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1442 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443
1444 // Bind settings actions
1445 View settingsButton = findViewById(R.id.settings_button);
1446 boolean hasSettings = hasSettings();
1447 if (hasSettings) {
1448 settingsButton.setOnClickListener(new OnClickListener() {
1449 @Override
1450 public void onClick(View view) {
1451 if (!mWorkspace.isSwitchingState()) {
1452 onClickSettingsButton(view);
1453 }
1454 }
1455 });
1456 settingsButton.setOnLongClickListener(performClickOnLongClick);
1457 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1458 } else {
1459 settingsButton.setVisibility(View.GONE);
1460 }
1461
1462 mOverviewPanel.setAlpha(0f);
1463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001466 * Sets the all apps button. This method is called from {@link Hotseat}.
1467 */
1468 public void setAllAppsButton(View allAppsButton) {
1469 mAllAppsButton = allAppsButton;
1470 }
1471
1472 public View getAllAppsButton() {
1473 return mAllAppsButton;
1474 }
1475
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001476 public View getWidgetsButton() {
1477 return mWidgetsButton;
1478 }
1479
Anjali Koppal5ad44842014-03-10 20:34:39 -07001480 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 * Creates a view representing a shortcut.
1482 *
1483 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001486 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * @param parent The group the shortcut belongs to.
1493 * @param info The data structure describing the shortcut.
1494 *
1495 * @return A View inflated from layoutResId.
1496 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001497 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001498 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 parent, false);
1500 favorite.applyFromShortcutInfo(info, mIconCache);
1501 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001518 if (info == null) {
1519 return;
1520 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 final View view = createShortcut(info);
1522
Sunny Goyal5c97f512015-05-19 16:03:28 -07001523 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 }
1544
1545 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 return;
1548 }
1549
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001561 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 */
Adam Cohen091440a2015-03-18 14:16:05 -07001563 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001564 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1565
1566 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001568 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001589 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1590 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001591 } else {
1592 // The AppWidgetHostView has already been inflated and instantiated
1593 launcherInfo.hostView = hostView;
1594 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001595 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001598 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Romain Guycbb89e42009-06-08 15:52:54 -07001600
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1602 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1603 item.hostView.setTag(item);
1604 item.onBindAppWidget(this);
1605
1606 item.hostView.setFocusable(true);
1607 item.hostView.setOnFocusChangeListener(mFocusHandler);
1608
1609 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1610 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1611
1612 if (!item.isCustomWidget()) {
1613 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1614 }
1615 }
1616
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1618 @Override
1619 public void onReceive(Context context, Intent intent) {
1620 final String action = intent.getAction();
1621 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1622 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001623 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001624 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001625
Winson Chungc100e8e2011-08-09 16:02:43 -07001626 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001627 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001628 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001629 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001630 if (!showWorkspace(false)) {
1631 // If we are already on the workspace, then manually reset all apps
1632 mAppsView.reset();
1633 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1636 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001637 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001638 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1639 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001640 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1642 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1643 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001644 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001645 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1646 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 }
1648 }
1649 };
1650
1651 @Override
1652 public void onAttachedToWindow() {
1653 super.onAttachedToWindow();
1654
1655 // Listen for broadcasts related to user-presence
1656 final IntentFilter filter = new IntentFilter();
1657 filter.addAction(Intent.ACTION_SCREEN_OFF);
1658 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001659 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001660 if (ENABLE_DEBUG_INTENTS) {
1661 filter.addAction(DebugIntents.DELETE_DATABASE);
1662 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001665 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001666 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001668
1669 if (mLauncherCallbacks != null) {
1670 mLauncherCallbacks.onAttachedToWindow();
1671 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 }
1673
1674 @Override
1675 public void onDetachedFromWindow() {
1676 super.onDetachedFromWindow();
1677 mVisible = false;
1678
Adam Cohend113e0c2010-11-11 10:48:05 -08001679 if (mAttached) {
1680 unregisterReceiver(mReceiver);
1681 mAttached = false;
1682 }
Winson Chungb745afb2015-03-02 11:51:23 -08001683 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001684
1685 if (mLauncherCallbacks != null) {
1686 mLauncherCallbacks.onDetachedFromWindow();
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 }
1689
1690 public void onWindowVisibilityChanged(int visibility) {
1691 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001692 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 // The following code used to be in onResume, but it turns out onResume is called when
1694 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1695 // is a more appropriate event to handle
1696 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001697 if (!mWorkspaceLoading) {
1698 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001699 // We want to let Launcher draw itself at least once before we force it to build
1700 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 // apps is nice and speedy.
1702 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001703 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001704 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 if (mStarted) return;
1706 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001707 // We delay the layer building a bit in order to give
1708 // other message processing a time to run. In particular
1709 // this avoids a delay in hiding the IME if it was
1710 // currently shown, because doing that may involve
1711 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001712 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 final ViewTreeObserver.OnDrawListener listener = this;
1714 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001715 public void run() {
1716 if (mWorkspace != null &&
1717 mWorkspace.getViewTreeObserver() != null) {
1718 mWorkspace.getViewTreeObserver().
1719 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001720 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001721 }
1722 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001723 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001724 }
1725 });
1726 }
1727 clearTypedText();
1728 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730
Adam Cohen091440a2015-03-18 14:16:05 -07001731 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 mHandler.removeMessages(ADVANCE_MSG);
1733 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1734 mHandler.sendMessageDelayed(msg, delay);
1735 mAutoAdvanceSentTime = System.currentTimeMillis();
1736 }
1737
Adam Cohen091440a2015-03-18 14:16:05 -07001738 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1740 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1741 mAutoAdvanceRunning = autoAdvanceRunning;
1742 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001743 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 sendAdvanceMessage(delay);
1745 } else {
1746 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001747 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1749 }
1750 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001751 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
1753 }
1754 }
1755
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1757
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 if (msg.what == ADVANCE_MSG) {
1761 int i = 0;
1762 for (View key: mWidgetsToAdvance.keySet()) {
1763 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001764 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001766 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 public void run() {
1768 ((Advanceable) v).advance();
1769 }
1770 }, delay);
1771 }
1772 i++;
1773 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001774 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001776 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001778 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001779
Winsonc0b52fe2015-09-09 16:38:15 -07001780 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001781 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1783 if (v instanceof Advanceable) {
1784 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001785 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001786 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
1788 }
1789
Winsonc0b52fe2015-09-09 16:38:15 -07001790 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 if (mWidgetsToAdvance.containsKey(hostView)) {
1792 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001793 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001795 }
1796
Hyunyoung Song3f471442015-04-08 19:01:34 -07001797 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001798 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1799 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 }
1801
Winson Chunga6945242014-01-08 14:04:34 -08001802 public DragLayer getDragLayer() {
1803 return mDragLayer;
1804 }
1805
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001806 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001807 return mAppsView;
1808 }
1809
Hyunyoung Song3f471442015-04-08 19:01:34 -07001810 public WidgetsContainerView getWidgetsView() {
1811 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001812 }
1813
Winson Chunga6945242014-01-08 14:04:34 -08001814 public Workspace getWorkspace() {
1815 return mWorkspace;
1816 }
1817
1818 public Hotseat getHotseat() {
1819 return mHotseat;
1820 }
1821
Jorim Jaggid017f882014-01-14 17:08:48 -08001822 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001823 return mOverviewPanel;
1824 }
1825
Winson Chung006ee262015-08-03 14:40:11 -07001826 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001827 return mSearchDropTargetBar;
1828 }
1829
Tony Wickham34d2c912015-09-11 09:27:58 -07001830 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1831 return mAppInfoDropTargetBar;
1832 }
1833
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001834 public LauncherAppWidgetHost getAppWidgetHost() {
1835 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
Romain Guycbb89e42009-06-08 15:52:54 -07001837
Winson Chunga9abd0e2010-10-27 17:18:37 -07001838 public LauncherModel getModel() {
1839 return mModel;
1840 }
1841
Winson Chunga6945242014-01-08 14:04:34 -08001842 protected SharedPreferences getSharedPrefs() {
1843 return mSharedPrefs;
1844 }
1845
Adam Cohen2e6da152015-05-06 11:42:25 -07001846 public DeviceProfile getDeviceProfile() {
1847 return mDeviceProfile;
1848 }
1849
Bjorn Bringertc459e522013-06-07 19:36:01 +01001850 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001851 getWindow().closeAllPanels();
1852
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001853 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001854 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001855 }
1856
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 @Override
1858 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001859 long startTime = 0;
1860 if (DEBUG_RESUME_TIME) {
1861 startTime = System.currentTimeMillis();
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 super.onNewIntent(intent);
1864
1865 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001866 Folder openFolder = mWorkspace.getOpenFolder();
1867 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1868 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1869 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1870 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1871 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001872 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001873 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001874
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 if (mWorkspace == null) {
1876 // Can be cases where mWorkspace is null, this prevents a NPE
1877 return;
1878 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001879 // In all these cases, only animate if we're already on home
1880 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001881
Sunny Goyal935fca12015-10-13 10:19:01 -07001882 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 exitSpringLoadedDragMode();
1884
1885 // If we are already on home, then just animate back to the workspace,
1886 // otherwise, just wait until onResume to set the state back to Workspace
1887 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001888 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 } else {
1890 mOnResumeState = State.WORKSPACE;
1891 }
1892
1893 final View v = getWindow().peekDecorView();
1894 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001895 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001896 INPUT_METHOD_SERVICE);
1897 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1898 }
1899
Winson Chungb745afb2015-03-02 11:51:23 -08001900 // Reset the apps view
1901 if (!alreadyOnHome && mAppsView != null) {
1902 mAppsView.scrollToTop();
1903 }
1904
Hyunyoung Song3f471442015-04-08 19:01:34 -07001905 // Reset the widgets view
1906 if (!alreadyOnHome && mWidgetsView != null) {
1907 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001908 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001909
Adam Cohen9211d422014-10-07 18:14:53 -07001910 if (mLauncherCallbacks != null) {
1911 mLauncherCallbacks.onHomeIntent();
1912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
Adam Cohened307df2013-10-02 09:37:31 -07001914
Adam Cohen9211d422014-10-07 18:14:53 -07001915 if (mLauncherCallbacks != null) {
1916 mLauncherCallbacks.onNewIntent(intent);
1917 }
Winson15f8b172015-08-19 11:02:39 -07001918
1919 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1920 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1921 // animation.
1922 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001923 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1924 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001925 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1926 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001927
1928 // We use this flag to suppress noisy callbacks above custom content state
1929 // from onResume.
1930 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001931 mWorkspace.post(new Runnable() {
1932 @Override
1933 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001934 if (mWorkspace != null) {
1935 mWorkspace.moveToDefaultScreen(true);
1936 }
Winson15f8b172015-08-19 11:02:39 -07001937 }
1938 });
1939 }
1940 }
1941
1942 if (DEBUG_RESUME_TIME) {
1943 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1944 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001945 }
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001948 public void onRestoreInstanceState(Bundle state) {
1949 super.onRestoreInstanceState(state);
1950 for (int page: mSynchronouslyBoundPages) {
1951 mWorkspace.restoreInstanceStateForChild(page);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
1955 @Override
1956 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001957 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001958 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1959 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001960 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001961 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Michael Jurka883f55b2010-10-21 15:47:14 -07001963 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001964 // We close any open folder since it will not be re-opened, and we need to make sure
1965 // this state is reflected.
1966 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1967 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001971 ContentValues itemValues = new ContentValues();
1972 mPendingAddInfo.writeToValues(itemValues);
1973 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001974 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001975 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
Hyunyoung Song3f471442015-04-08 19:01:34 -07001978 // Save the current widgets tray?
1979 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001980
1981 if (mLauncherCallbacks != null) {
1982 mLauncherCallbacks.onSaveInstanceState(outState);
1983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985
1986 @Override
1987 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001989
Winson Chunge7a03942011-08-05 15:05:12 -07001990 // Remove all pending runnables
1991 mHandler.removeMessages(ADVANCE_MSG);
1992 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001993 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001994
Winson Chungcd2b0142011-06-08 16:02:26 -07001995 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001996 // It's possible to receive onDestroy after a new Launcher activity has
1997 // been created. In this case, don't interfere with the new Launcher.
1998 if (mModel.isCurrentCallbacks(this)) {
1999 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08002000 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07002001 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002004 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002006 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002008 mAppWidgetHost = null;
2009
2010 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
2012 TextKeyListener.getInstance().release();
2013
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002014 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015
Michael Jurka2ecf9952012-06-18 12:52:28 -07002016 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002017
2018 if (mLauncherCallbacks != null) {
2019 mLauncherCallbacks.onDestroy();
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Adam Cohena9cf38f2011-05-02 15:36:58 -07002023 public DragController getDragController() {
2024 return mDragController;
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
2028 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002029 onStartForResult(requestCode);
2030 super.startActivityForResult(intent, requestCode);
2031 }
2032
2033 @Override
2034 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2035 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2036 onStartForResult(requestCode);
2037 try {
2038 super.startIntentSenderForResult(intent, requestCode,
2039 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2040 } catch (IntentSender.SendIntentException e) {
2041 throw new ActivityNotFoundException();
2042 }
2043 }
2044
2045 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002046 if (requestCode >= 0) {
2047 setWaitingForResult(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 /**
2052 * Indicates that we want global search for this activity by setting the globalSearch
2053 * argument for {@link #startSearch} to true.
2054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002056 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 if (initialQuery == null) {
2060 // Use any text typed in the launcher as the initial query
2061 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 if (appSearchData == null) {
2064 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002065 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Winson Chungadf0c182012-08-23 14:59:07 -07002067 Rect sourceBounds = new Rect();
2068 if (mSearchDropTargetBar != null) {
2069 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2070 }
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 if (clearTextImmediately) {
2075 clearTypedText();
2076 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002077
2078 // We need to show the workspace after starting the search
2079 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 }
2081
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 /**
2083 * Start a text search.
2084 *
2085 * @return {@code true} if the search will start immediately, so any further keypresses
2086 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2087 * to buffer keypresses.
2088 */
2089 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002091 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2092 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2093 sourceBounds);
2094 }
2095
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002098 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 }
2100
2101 /**
2102 * Starts the global search activity. This code is a copied from SearchManager
2103 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002104 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002106 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2108 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2109 if (globalSearchActivity == null) {
2110 Log.w(TAG, "No global search activity found.");
2111 return;
2112 }
2113 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2114 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2115 intent.setComponent(globalSearchActivity);
2116 // Make sure that we have a Bundle to put source in
2117 if (appSearchData == null) {
2118 appSearchData = new Bundle();
2119 } else {
2120 appSearchData = new Bundle(appSearchData);
2121 }
Adam Cohen9211d422014-10-07 18:14:53 -07002122 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002123 if (!appSearchData.containsKey("source")) {
2124 appSearchData.putString("source", getPackageName());
2125 }
2126 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2127 if (!TextUtils.isEmpty(initialQuery)) {
2128 intent.putExtra(SearchManager.QUERY, initialQuery);
2129 }
2130 if (selectInitialQuery) {
2131 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2132 }
2133 intent.setSourceBounds(sourceBounds);
2134 try {
2135 startActivity(intent);
2136 } catch (ActivityNotFoundException ex) {
2137 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
2140
Yura4f93ec62014-02-04 14:15:21 +00002141 public boolean isOnCustomContent() {
2142 return mWorkspace.isOnOrMovingToCustomContent();
2143 }
2144
Winson Chung70d72102011-08-12 11:24:35 -07002145 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002146 public boolean onPrepareOptionsMenu(Menu menu) {
2147 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002148 if (mLauncherCallbacks != null) {
2149 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2150 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002151 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002154 @Override
2155 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002156 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002157 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002158 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002159 }
2160
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 public boolean isWorkspaceLocked() {
2162 return mWorkspaceLoading || mWaitingForResult;
2163 }
2164
Adam Cohen517a7f52014-03-01 12:12:59 -08002165 public boolean isWorkspaceLoading() {
2166 return mWorkspaceLoading;
2167 }
2168
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002169 private void setWorkspaceLoading(boolean value) {
2170 boolean isLocked = isWorkspaceLocked();
2171 mWorkspaceLoading = value;
2172 if (isLocked != isWorkspaceLocked()) {
2173 onWorkspaceLockedChanged();
2174 }
2175 }
2176
2177 private void setWaitingForResult(boolean value) {
2178 boolean isLocked = isWorkspaceLocked();
2179 mWaitingForResult = value;
2180 if (isLocked != isWorkspaceLocked()) {
2181 onWorkspaceLockedChanged();
2182 }
2183 }
2184
Adam Cohen9211d422014-10-07 18:14:53 -07002185 protected void onWorkspaceLockedChanged() {
2186 if (mLauncherCallbacks != null) {
2187 mLauncherCallbacks.onWorkspaceLockedChanged();
2188 }
2189 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002190
Michael Jurka0280c3b2010-09-17 15:00:07 -07002191 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002194 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2195 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002196 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002198
Tony Wickhama0628cc2015-10-14 15:23:04 -07002199 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002200 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002201 if (LOGD) {
2202 Log.d(TAG, "Adding widget from drop");
2203 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002204 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2205 }
2206
Sunny Goyale6b63a32015-01-16 16:14:29 -08002207 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002208 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2209 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002210 if (appWidgetInfo.configure != null) {
2211 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002212 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002213
Bjorn Bringert7984c942009-12-09 15:38:25 +00002214 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002215 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2216 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002219 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002220 Runnable onComplete = new Runnable() {
2221 @Override
2222 public void run() {
2223 // Exit spring loaded mode if necessary after adding the widget
2224 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2225 null);
2226 }
2227 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002229 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002230 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
2232 }
Romain Guycbb89e42009-06-08 15:52:54 -07002233
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002234 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002235 // Close any folders that may be open.
2236 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002237 mWorkspace.moveToCustomContentScreen(animate);
2238 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002239
2240 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2241 int[] cell, int spanX, int spanY) {
2242 switch (info.itemType) {
2243 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2244 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2245 int span[] = new int[2];
2246 span[0] = spanX;
2247 span[1] = spanY;
2248 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2249 container, screenId, cell, span);
2250 break;
2251 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2252 processShortcutFromDrop(info.componentName, container, screenId, cell);
2253 break;
2254 default:
2255 throw new IllegalStateException("Unknown item type: " + info.itemType);
2256 }
2257 }
2258
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 /**
2260 * Process a shortcut drop.
2261 *
2262 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002266 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2267 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271
2272 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.cellX = cell[0];
2274 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276
2277 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2278 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002279 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280 }
2281
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 /**
2283 * Process a widget drop.
2284 *
2285 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002289 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2290 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002294 mPendingAddInfo.minSpanX = info.minSpanX;
2295 mPendingAddInfo.minSpanY = info.minSpanY;
2296
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mPendingAddInfo.cellX = cell[0];
2299 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002301 if (span != null) {
2302 mPendingAddInfo.spanX = span[0];
2303 mPendingAddInfo.spanY = span[1];
2304 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 AppWidgetHostView hostView = info.boundWidget;
2307 int appWidgetId;
2308 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002309 // In the case where we've prebound the widget, we remove it from the DragLayer
2310 if (LOGD) {
2311 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2312 }
2313 getDragLayer().removeView(hostView);
2314
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002316 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002317
2318 // Clear the boundWidget so that it doesn't get destroyed.
2319 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002320 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002321 // In this case, we either need to start an activity to get permission to bind
2322 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002324 Bundle options = info.bindOptions;
2325
Sunny Goyalffe83f12014-08-14 17:39:34 -07002326 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2327 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002328 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002329 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002330 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002331 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002332 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2333 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2334 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2336 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002337 // TODO: we need to make sure that this accounts for the options bundle.
2338 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002339 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2340 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002341 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 }
2343
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002346 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 folderInfo.title = getText(R.string.folder_name);
2348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002350 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2351 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002352 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353
2354 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 FolderIcon newFolder =
2356 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002359 // Force measure the new folder icon
2360 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2361 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002362 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 }
Romain Guycbb89e42009-06-08 15:52:54 -07002364
Winsonc0b52fe2015-09-09 16:38:15 -07002365 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002366 * Unbinds the view for the specified item, and removes the item and all its children.
2367 *
2368 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002369 * @param itemInfo the {@link ItemInfo} for this view.
2370 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002371 */
Winson2949fb52015-09-24 09:56:11 -07002372 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002373 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002374 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002375 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2376 if (folderInfo != null) {
2377 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002378 } else {
2379 mWorkspace.removeWorkspaceItem(v);
2380 }
Winsonc0b52fe2015-09-09 16:38:15 -07002381 if (deleteFromDb) {
2382 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2383 }
2384 } else if (itemInfo instanceof FolderInfo) {
2385 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2386 unbindFolder(folderInfo);
2387 mWorkspace.removeWorkspaceItem(v);
2388 if (deleteFromDb) {
2389 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2390 }
2391 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2392 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002393 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 removeWidgetToAutoAdvance(widgetInfo.hostView);
2395 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002396 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002398 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002399
Winsonc0b52fe2015-09-09 16:38:15 -07002400 } else {
2401 return false;
2402 }
2403 return true;
2404 }
2405
2406 /**
2407 * Unbinds any launcher references to the folder.
2408 */
2409 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002410 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002411 }
2412
Winsonc0b52fe2015-09-09 16:38:15 -07002413 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002414 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002415 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002416 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002417 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002418 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002419 // Deleting an app widget ID is a void call but writes to disk before returning
2420 // to the caller...
2421 new AsyncTask<Void, Void, Void>() {
2422 public Void doInBackground(Void ... args) {
2423 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2424 return null;
2425 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002426 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002427 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002428 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002429 }
2430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 @Override
2432 public boolean dispatchKeyEvent(KeyEvent event) {
2433 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2434 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002436 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002437 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002438 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002439 dumpState();
2440 return true;
2441 }
2442 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002443 }
2444 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2445 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 return true;
2448 }
2449 }
2450
2451 return super.dispatchKeyEvent(event);
2452 }
2453
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 @Override
2455 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002456 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2457 return;
2458 }
2459
Sunny Goyal45478022015-06-08 16:52:41 -07002460 if (mDragController.isDragging()) {
2461 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002462 return;
2463 }
2464
Winson Chungb745afb2015-03-02 11:51:23 -08002465 if (isAppsViewVisible()) {
2466 showWorkspace(true);
2467 } else if (isWidgetsViewVisible()) {
2468 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002469 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002470 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002471 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002472 Folder openFolder = mWorkspace.getOpenFolder();
2473 if (openFolder.isEditingName()) {
2474 openFolder.dismissEditingName();
2475 } else {
2476 closeFolder();
2477 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002478 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002479 mWorkspace.exitWidgetResizeMode();
2480
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002481 // Back button is a no-op here, but give at least some feedback for the button press
2482 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002483 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002484 }
2485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002487 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002488 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002489 @Override
2490 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002491 if (mAppWidgetHost != null) {
2492 mAppWidgetHost.startListening();
2493 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002494
2495 // Recreate the QSB, as the widget has been reset.
2496 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002497 }
2498
2499 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 * Launches the intent referred by the clicked shortcut.
2501 *
2502 * @param v The view representing the clicked shortcut.
2503 */
2504 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002505 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2506 // view has detached (it's possible for this to happen if the view is removed mid touch).
2507 if (v.getWindowToken() == null) {
2508 return;
2509 }
2510
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002511 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002512 return;
2513 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002514
Adam Cohen1697b792013-09-17 19:08:21 -07002515 if (v instanceof Workspace) {
2516 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002517 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002518 }
2519 return;
2520 }
2521
2522 if (v instanceof CellLayout) {
2523 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002524 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2525 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002527 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002528 }
2529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002531 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002532 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002534 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002536 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002537 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002539 } else if (tag instanceof AppInfo) {
2540 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002541 } else if (tag instanceof LauncherAppWidgetInfo) {
2542 if (v instanceof PendingAppWidgetHostView) {
2543 onClickPendingWidget((PendingAppWidgetHostView) v);
2544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 }
2546 }
2547
Sunny Goyal70660032015-05-14 00:07:08 -07002548 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002549 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002550 return false;
2551 }
2552
Michael Jurkaaf442092010-06-10 17:01:57 -07002553 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002554 * Event handler for the app widget view which has not fully restored.
2555 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002556 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002557 if (mIsSafeModeEnabled) {
2558 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2559 return;
2560 }
2561
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002563 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002564 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002565 LauncherAppWidgetProviderInfo appWidgetInfo =
2566 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002568 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002569 mPendingAddInfo.copyFrom(info);
2570 mPendingAddWidgetId = widgetId;
2571
Sunny Goyalffe83f12014-08-14 17:39:34 -07002572 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2573 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 } else if (info.installProgress < 0) {
2576 // The install has not been queued
2577 final String packageName = info.providerName.getPackageName();
2578 showBrokenAppInstallDialog(packageName,
2579 new DialogInterface.OnClickListener() {
2580 public void onClick(DialogInterface dialog, int id) {
2581 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2582 }
2583 });
2584 } else {
2585 // Download has started.
2586 final String packageName = info.providerName.getPackageName();
2587 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 }
2589 }
2590
2591 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002592 * Event handler for the "grid" button that appears on the home screen, which
2593 * enters all apps mode.
2594 *
2595 * @param v The view that was clicked.
2596 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 protected void onClickAllAppsButton(View v) {
2598 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002599 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002600 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002601 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002602
2603 if (mLauncherCallbacks != null) {
2604 mLauncherCallbacks.onClickAllAppsButton(v);
2605 }
Winson Chung76648c52015-07-10 14:33:23 -07002606 }
2607 }
2608
2609 protected void onLongClickAllAppsButton(View v) {
2610 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2611 if (!isAppsViewVisible()) {
2612 showAppsView(true /* animated */, false /* resetListToTop */,
2613 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 }
2615 }
2616
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002617 private void showBrokenAppInstallDialog(final String packageName,
2618 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002619 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002620 .setTitle(R.string.abandoned_promises_title)
2621 .setMessage(R.string.abandoned_promise_explanation)
2622 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2623 .setNeutralButton(R.string.abandoned_clean_this,
2624 new DialogInterface.OnClickListener() {
2625 public void onClick(DialogInterface dialog, int id) {
2626 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2627 mWorkspace.removeAbandonedPromise(packageName, user);
2628 }
2629 })
2630 .create().show();
2631 return;
2632 }
2633
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 /**
2635 * Event handler for an app shortcut click.
2636 *
2637 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2638 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2641 Object tag = v.getTag();
2642 if (!(tag instanceof ShortcutInfo)) {
2643 throw new IllegalArgumentException("Input must be a Shortcut");
2644 }
2645
2646 // Open shortcut
2647 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002648
2649 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002650 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2651 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002652 // Launch activity anyway, framework will tell the user why the app is suspended.
2653 } else {
2654 int error = R.string.activity_not_available;
2655 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2656 error = R.string.safemode_shortcut_error;
2657 }
2658 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2659 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002660 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002661 }
2662
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002664 if ((v instanceof BubbleTextView)
2665 && shortcut.isPromise()
2666 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002667 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002668 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 new DialogInterface.OnClickListener() {
2670 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002671 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 return;
2675 }
2676
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002678 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002679
2680 if (mLauncherCallbacks != null) {
2681 mLauncherCallbacks.onClickAppShortcut(v);
2682 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 }
2684
Adam Cohen091440a2015-03-18 14:16:05 -07002685 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002687 final ShortcutInfo shortcut;
2688 final Intent intent;
2689 if (tag instanceof ShortcutInfo) {
2690 shortcut = (ShortcutInfo) tag;
2691 intent = shortcut.intent;
2692 int[] pos = new int[2];
2693 v.getLocationOnScreen(pos);
2694 intent.setSourceBounds(new Rect(pos[0], pos[1],
2695 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002696
Sunny Goyal508da152014-08-14 10:53:27 -07002697 } else if (tag instanceof AppInfo) {
2698 shortcut = null;
2699 intent = ((AppInfo) tag).intent;
2700 } else {
2701 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2702 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703
2704 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002705 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706
2707 if (success && v instanceof BubbleTextView) {
2708 mWaitingForResume = (BubbleTextView) v;
2709 mWaitingForResume.setStayPressed(true);
2710 }
2711 }
2712
2713 /**
2714 * Event handler for a folder icon click.
2715 *
2716 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2717 */
2718 protected void onClickFolderIcon(View v) {
2719 if (LOGD) Log.d(TAG, "onClickFolder");
2720 if (!(v instanceof FolderIcon)){
2721 throw new IllegalArgumentException("Input must be a FolderIcon");
2722 }
2723
2724 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002725 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002727 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002728 }
Adam Cohen9211d422014-10-07 18:14:53 -07002729
2730 if (mLauncherCallbacks != null) {
2731 mLauncherCallbacks.onClickFolderIcon(v);
2732 }
Michael Jurka5130e402011-10-13 04:55:35 -07002733 }
2734
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002735 /**
2736 * Event handler for the (Add) Widgets button that appears after a long press
2737 * on the home screen.
2738 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002739 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002741 if (mIsSafeModeEnabled) {
2742 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2743 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002744 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002745 if (mLauncherCallbacks != null) {
2746 mLauncherCallbacks.onClickAddWidgetButton(view);
2747 }
Adam Cohen9211d422014-10-07 18:14:53 -07002748 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002749 }
2750
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002751 /**
2752 * Event handler for the wallpaper picker button that appears after a long press
2753 * on the home screen.
2754 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002755 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002756 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002757 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2758 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2759 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal6f866092016-03-17 17:04:15 -07002760 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002761 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002762
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickWallpaperPicker(v);
2765 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 }
2767
2768 /**
2769 * Event handler for a click on the settings button that appears after a long press
2770 * on the home screen.
2771 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002772 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002774 if (mLauncherCallbacks != null) {
2775 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002776 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002777 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002778 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 }
2780
Adam Cohen61f560d2013-09-30 15:58:20 -07002781 public View.OnTouchListener getHapticFeedbackTouchListener() {
2782 if (mHapticFeedbackTouchListener == null) {
2783 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002784 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002785 @Override
2786 public boolean onTouch(View v, MotionEvent event) {
2787 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2788 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2789 }
2790 return false;
2791 }
2792 };
2793 }
2794 return mHapticFeedbackTouchListener;
2795 }
2796
Adam Cohen9211d422014-10-07 18:14:53 -07002797 public void onDragStarted(View view) {
2798 if (isOnCustomContent()) {
2799 // Custom content screen doesn't participate in drag and drop. If on custom
2800 // content screen, move to default.
2801 moveWorkspaceToDefaultScreen();
2802 }
2803
2804 if (mLauncherCallbacks != null) {
2805 mLauncherCallbacks.onDragStarted(view);
2806 }
2807 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002808
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002809 /**
2810 * Called when the user stops interacting with the launcher.
2811 * This implies that the user is now on the homescreen and is not doing housekeeping.
2812 */
Adam Cohen9211d422014-10-07 18:14:53 -07002813 protected void onInteractionEnd() {
2814 if (mLauncherCallbacks != null) {
2815 mLauncherCallbacks.onInteractionEnd();
2816 }
2817 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002818
2819 /**
2820 * Called when the user starts interacting with the launcher.
2821 * The possible interactions are:
2822 * - open all apps
2823 * - reorder an app shortcut, or a widget
2824 * - open the overview mode.
2825 * This is a good time to stop doing things that only make sense
2826 * when the user is on the homescreen and not doing housekeeping.
2827 */
Adam Cohen9211d422014-10-07 18:14:53 -07002828 protected void onInteractionBegin() {
2829 if (mLauncherCallbacks != null) {
2830 mLauncherCallbacks.onInteractionBegin();
2831 }
2832 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002833
Winson Chungcd99cd32015-04-29 11:03:24 -07002834 /** Updates the interaction state. */
2835 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002836 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002838 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2839 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002840 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002841 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002842 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002843 onInteractionEnd();
2844 }
2845 }
2846
Winson Chung8f1eff72015-05-28 17:33:40 -07002847 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002848 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002850 // Only launch using the new animation if the shortcut has not opted out (this is a
2851 // private contract between launcher and may be ignored in the future).
2852 boolean useLaunchAnimation = (v != null) &&
2853 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002854 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2855 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002856
Kenny Guy1317e2d2014-05-08 18:52:50 +01002857 UserHandleCompat user = null;
2858 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2859 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2860 user = userManager.getUserForSerialNumber(serialNumber);
2861 }
2862
2863 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002864 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002865 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002866 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002867 int left = 0, top = 0;
2868 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2869 if (v instanceof TextView) {
2870 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002871 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2872 if (icon != null) {
2873 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002874 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002875 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002876 width = bounds.width();
2877 height = bounds.height();
2878 }
2879 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002880 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2881 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002882 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002883 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002884 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002885 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002886 // On L devices, we use the device default slide-up transition.
2887 // On L MR1 devices, we a custom version of the slide-up transition which
2888 // doesn't have the delay present in the device default.
2889 opts = ActivityOptions.makeCustomAnimation(this,
2890 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002891 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002892 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002893 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002894
2895 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2896 // Could be launching some bookkeeping activity
2897 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002898 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002899 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002900 launcherApps.startActivityForProfile(intent.getComponent(), user,
2901 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002902 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002903 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002905 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2906 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2907 // corresponding permission. Show the appropriate permission prompt if that
2908 // is the case.
2909 if (intent.getComponent() == null
2910 && Intent.ACTION_CALL.equals(intent.getAction())
2911 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2912 PackageManager.PERMISSION_GRANTED) {
2913 // TODO: Rename sPendingAddItem to a generic name.
2914 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2915 0, (ItemInfo) tag);
2916 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2917 REQUEST_PERMISSION_CALL_PHONE);
2918 return false;
2919 }
2920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002921 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002922 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002924 "or use the exported attribute for this activity. "
2925 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002927 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002929
Winson Chungbedf9232015-07-10 12:38:30 -07002930 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002931 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002932 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2933 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2934 return false;
2935 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002936 try {
2937 success = startActivity(v, intent, tag);
2938 } catch (ActivityNotFoundException e) {
2939 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2940 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2941 }
2942 return success;
2943 }
2944
Adam Cohen268c4752012-06-06 17:47:33 -07002945 /**
2946 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2947 * in the DragLayer in the exact absolute location of the original FolderIcon.
2948 */
2949 private void copyFolderIconToImage(FolderIcon fi) {
2950 final int width = fi.getMeasuredWidth();
2951 final int height = fi.getMeasuredHeight();
2952
2953 // Lazy load ImageView, Bitmap and Canvas
2954 if (mFolderIconImageView == null) {
2955 mFolderIconImageView = new ImageView(this);
2956 }
2957 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2958 mFolderIconBitmap.getHeight() != height) {
2959 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2960 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2961 }
2962
2963 DragLayer.LayoutParams lp;
2964 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2965 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2966 } else {
2967 lp = new DragLayer.LayoutParams(width, height);
2968 }
2969
Adam Cohen307fe232012-08-16 17:55:58 -07002970 // The layout from which the folder is being opened may be scaled, adjust the starting
2971 // view size by this scale factor.
2972 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002973 lp.customPosition = true;
2974 lp.x = mRectForFolderAnimation.left;
2975 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002976 lp.width = (int) (scale * width);
2977 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002978
2979 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2980 fi.draw(mFolderIconCanvas);
2981 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002982 if (fi.getFolder() != null) {
2983 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2984 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002985 }
Adam Cohen268c4752012-06-06 17:47:33 -07002986 // Just in case this image view is still in the drag layer from a previous animation,
2987 // we remove it and re-add it.
2988 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2989 mDragLayer.removeView(mFolderIconImageView);
2990 }
2991 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002992 if (fi.getFolder() != null) {
2993 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002994 }
Adam Cohen268c4752012-06-06 17:47:33 -07002995 }
2996
Sunny Goyal08442b82015-10-21 17:15:08 -07002997 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002998 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002999 FolderInfo info = (FolderInfo) fi.getTag();
3000 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3001 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003002 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3003 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003004 }
3005
Adam Cohen268c4752012-06-06 17:47:33 -07003006 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3007 copyFolderIconToImage(fi);
3008 fi.setVisibility(View.INVISIBLE);
3009
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003010 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3011 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003012 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003013 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3014 }
3015 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003017 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003018 oa.end();
3019 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003020 }
3021
Sunny Goyal935fca12015-10-13 10:19:01 -07003022 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003023 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003024 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003025
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // We remove and re-draw the FolderIcon in-case it has changed
3027 mDragLayer.removeView(mFolderIconImageView);
3028 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003029
3030 if (cl != null) {
3031 cl.clearFolderLeaveBehind();
3032 }
3033
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003034 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003035 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003036 oa.addListener(new AnimatorListenerAdapter() {
3037 @Override
3038 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003039 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003040 // Remove the ImageView copy of the FolderIcon and make the original visible.
3041 mDragLayer.removeView(mFolderIconImageView);
3042 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 }
3044 }
3045 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003047 if (!animate) {
3048 oa.end();
3049 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 }
3051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003053 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 * is animated relative to the specified View. If the View is null, no animation
3055 * is played.
3056 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003057 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003059 public void openFolder(FolderIcon folderIcon, boolean animate) {
3060 animate &= !Utilities.isPowerSaverOn(this);
3061
Adam Cohenfb91f302012-06-11 15:45:18 -07003062 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003063 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3064 if (openFolder != null && openFolder != folder) {
3065 // Close any open folder before opening a folder.
3066 closeFolder();
3067 }
3068
Adam Cohena9cf38f2011-05-02 15:36:58 -07003069 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070
Adam Cohena9cf38f2011-05-02 15:36:58 -07003071 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072
Sunny Goyalf4066152015-04-15 09:42:19 -07003073 // While the folder is open, the position of the icon cannot change.
3074 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3075
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 // Just verify that the folder hasn't already been added to the DragLayer.
3077 // There was a one-off crash where the folder had a parent already.
3078 if (folder.getParent() == null) {
3079 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003080 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 } else {
3082 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3083 folder.getParent() + ").");
3084 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003085 if (animate) {
3086 folder.animateOpen();
3087 } else {
3088 folder.open();
3089 }
3090 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003091
3092 // Notify the accessibility manager that this folder "window" has appeared and occluded
3093 // the workspace items
3094 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3095 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 }
3097
3098 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003099 closeFolder(true);
3100 }
3101
3102 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003103 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003104 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003105 if (folder.isEditingName()) {
3106 folder.dismissEditingName();
3107 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003108 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 }
3110 }
3111
Sunny Goyal935fca12015-10-13 10:19:01 -07003112 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003113 animate &= !Utilities.isPowerSaverOn(this);
3114
Adam Cohen4554ee12011-08-03 16:13:21 -07003115 folder.getInfo().opened = false;
3116
3117 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3118 if (parent != null) {
3119 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003120 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003121 if (fi != null) {
3122 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3123 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003125 if (animate) {
3126 folder.animateClosed();
3127 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003128 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003129 }
Winson Chung83ca4802013-04-12 15:10:52 -07003130
Sunny Goyal935fca12015-10-13 10:19:01 -07003131 // Notify the accessibility manager that this folder "window" has disappeared and no
3132 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003133 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 }
3135
Tony Wickhamdadb3042016-02-24 11:07:00 -08003136 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003138 if (!isDraggingEnabled()) return false;
3139 if (isWorkspaceLocked()) return false;
3140 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141
Winson Chung76648c52015-07-10 14:33:23 -07003142 if (v == mAllAppsButton) {
3143 onLongClickAllAppsButton(v);
3144 return true;
3145 }
3146
Adam Cohen1697b792013-09-17 19:08:21 -07003147 if (v instanceof Workspace) {
3148 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003149 if (!mWorkspace.isTouchActive()) {
3150 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003151 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3152 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3153 return true;
3154 } else {
3155 return false;
3156 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003157 } else {
3158 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003159 }
Adam Cohen1697b792013-09-17 19:08:21 -07003160 }
3161
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003162 CellLayout.CellInfo longClickCellInfo = null;
3163 View itemUnderLongClick = null;
3164 if (v.getTag() instanceof ItemInfo) {
3165 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003166 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003167 itemUnderLongClick = longClickCellInfo.cell;
3168 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
3170
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 // The hotseat touch handling does not go through Workspace, and we always allow long press
3172 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003173 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003174 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003175 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003176 // User long pressed on empty space
3177 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3178 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003179 if (mWorkspace.isInOverviewMode()) {
3180 mWorkspace.startReordering(v);
3181 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003182 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003185 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3186 mHotseat.getOrderInHotseat(
3187 longClickCellInfo.cellX,
3188 longClickCellInfo.cellY));
3189 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003191 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
3193 }
3194 }
3195 return true;
3196 }
3197
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 boolean isHotseatLayout(View layout) {
3199 return mHotseat != null && layout != null &&
3200 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3201 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 /**
3204 * Returns the CellLayout of the specified container at the specified screen.
3205 */
Sunny Goyal92820592015-03-02 11:31:35 -08003206 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3208 if (mHotseat != null) {
3209 return mHotseat.getLayout();
3210 } else {
3211 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003212 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003213 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003214 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003215 }
3216 }
3217
Winson Chungb745afb2015-03-02 11:51:23 -08003218 /**
3219 * For overridden classes.
3220 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003221 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003222 return isAppsViewVisible();
3223 }
3224
3225 public boolean isAppsViewVisible() {
3226 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3227 }
3228
3229 public boolean isWidgetsViewVisible() {
3230 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003231 }
3232
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003233 private void setWorkspaceBackground(int background) {
3234 switch (background) {
3235 case WORKSPACE_BACKGROUND_TRANSPARENT:
3236 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3237 break;
3238 case WORKSPACE_BACKGROUND_BLACK:
3239 getWindow().setBackgroundDrawable(null);
3240 break;
3241 default:
3242 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3243 }
Craig Mautner360310b2012-10-26 15:13:08 -07003244 }
3245
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003246 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003247 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3248 int curflags = getWindow().getAttributes().flags
3249 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3250 if (wpflags != curflags) {
3251 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3252 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003253 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003254 }
3255
Michael Jurkae326f182011-11-21 14:05:46 -08003256 @Override
3257 public void onTrimMemory(int level) {
3258 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003259 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3260 // The widget preview db can result in holding onto over
3261 // 3MB of memory for caching which isn't necessary.
3262 SQLiteDatabase.releaseMemory();
3263
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003264 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003265 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003266 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003267 if (mLauncherCallbacks != null) {
3268 mLauncherCallbacks.onTrimMemory(level);
3269 }
Michael Jurkae326f182011-11-21 14:05:46 -08003270 }
3271
Winson5c6bdbb2015-09-03 11:36:19 -07003272 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003273 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003274 }
3275
Winson5c6bdbb2015-09-03 11:36:19 -07003276 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003277 boolean changed = mState != State.WORKSPACE ||
3278 mWorkspace.getState() != Workspace.State.NORMAL;
3279 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003280 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003281 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003282 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003283
Michael Jurkab3e22d92011-10-31 15:58:33 -07003284 // Set focus to the AppsCustomize button
3285 if (mAllAppsButton != null) {
3286 mAllAppsButton.requestFocus();
3287 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003288 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003289
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003290 // Change the state *after* we've called all the transition code
3291 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003292
Winson Chung5fb63472011-02-02 17:03:37 -08003293 // Resume the auto-advance of widgets
3294 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003295 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003296
Winson Chung0f785722015-04-08 10:27:49 -07003297 if (changed) {
3298 // Send an accessibility event to announce the context change
3299 getWindow().getDecorView()
3300 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003301 }
Winson5c6bdbb2015-09-03 11:36:19 -07003302 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003303 }
3304
Winsone9f27272015-10-13 10:47:51 -07003305 /**
3306 * Shows the overview button.
3307 */
Adam Cohened307df2013-10-02 09:37:31 -07003308 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003309 showOverviewMode(animated, false);
3310 }
3311
3312 /**
3313 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3314 * onto one of the overview panel buttons.
3315 */
3316 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3317 Runnable postAnimRunnable = null;
3318 if (requestButtonFocus) {
3319 postAnimRunnable = new Runnable() {
3320 @Override
3321 public void run() {
3322 // Hitting the menu button when in touch mode does not trigger touch mode to
3323 // be disabled, so if requested, force focus on one of the overview panel
3324 // buttons.
3325 mOverviewPanel.requestFocusFromTouch();
3326 }
3327 };
3328 }
Adam Cohened307df2013-10-02 09:37:31 -07003329 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003330 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003331 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003332 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003333 }
3334
Winson Chungb745afb2015-03-02 11:51:23 -08003335 /**
3336 * Shows the apps view.
3337 */
Winson Chung76648c52015-07-10 14:33:23 -07003338 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3339 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003340 if (resetListToTop) {
3341 mAppsView.scrollToTop();
3342 }
Winson Chung4ac30062015-05-08 17:34:17 -07003343 if (updatePredictedApps) {
3344 tryAndUpdatePredictedApps();
3345 }
Winson Chung76648c52015-07-10 14:33:23 -07003346 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348
Winson Chungb745afb2015-03-02 11:51:23 -08003349 /**
3350 * Shows the widgets view.
3351 */
3352 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003353 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003354 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003355 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003356 }
Winson Chung76648c52015-07-10 14:33:23 -07003357 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003358
3359 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003360 @Override
3361 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003362 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003363 }
3364 });
Winson Chungb745afb2015-03-02 11:51:23 -08003365 }
3366
3367 /**
3368 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003369 *
3370 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003371 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003372 // TODO: calling method should use the return value so that when {@code false} is returned
3373 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003374 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003375 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3376 mState != State.WIDGETS_SPRING_LOADED) {
3377 return false;
3378 }
3379 if (toState != State.APPS && toState != State.WIDGETS) {
3380 return false;
3381 }
Winson Chungb745afb2015-03-02 11:51:23 -08003382
3383 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003384 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3385 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003386 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003387 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003388 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003391 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392
3393 // Pause the auto-advance of widgets until we are out of AllApps
3394 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003395 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003396 closeFolder();
3397
3398 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003399 getWindow().getDecorView()
3400 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003401 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402 }
3403
Winson Chungcd99cd32015-04-29 11:03:24 -07003404 /**
3405 * Updates the workspace and interaction state on state change, and return the animation to this
3406 * new state.
3407 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003408 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003409 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003410 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003411 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003412 updateInteraction(fromState, toState);
3413 return anim;
3414 }
3415
Jun Mukaif0033da2015-08-04 18:34:30 -07003416 public void onLauncherClingShown() {
3417 // When a launcher cling appears, it should cover the underlying layers, so their focus
3418 // should be blocked.
3419 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3420 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3421 }
3422 }
3423
3424 public void onLauncherClingDismissed() {
3425 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3426 }
3427
Hyunyoung Song3f471442015-04-08 19:01:34 -07003428 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003429 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003430 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003431 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003432 }
Winson Chungb745afb2015-03-02 11:51:23 -08003433
Winson Chung006ee262015-08-03 14:40:11 -07003434 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003435 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003436 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003437
3438 if (isAppsViewVisible()) {
3439 mState = State.APPS_SPRING_LOADED;
3440 } else if (isWidgetsViewVisible()) {
3441 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003442 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003443 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003444 } else {
3445 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003446 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003447 }
Adam Cohen7777d962011-08-18 18:58:38 -07003448
Hyunyoung Song3f471442015-04-08 19:01:34 -07003449 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003450 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003451 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003452
Winson Chunge7a03942011-08-05 15:05:12 -07003453 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003454 @Override
3455 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003456 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003457 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3458 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003459 // Before we show workspace, hide all apps again because
3460 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3461 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003462 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003463 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003464 } else {
3465 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003466 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003467 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003468 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003469 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470
Tony Wickhame0c33232016-02-08 11:37:04 -08003471 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003472 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3473 || mState == State.WIDGETS_SPRING_LOADED;
3474 }
3475
Michael Jurkad3ef3062010-11-23 16:23:58 -08003476 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003477 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003478 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003479 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003480 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003481 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003482 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3483 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003484 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003485 }
3486
Winson Chung4ac30062015-05-08 17:34:17 -07003487 /**
3488 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3489 * resumed.
3490 */
3491 private void tryAndUpdatePredictedApps() {
3492 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003493 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003494 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003495 mAppsView.setPredictedApps(apps);
3496 }
3497 }
3498 }
3499
Michael Jurkab3e22d92011-10-31 15:58:33 -07003500 void lockAllApps() {
3501 // TODO
3502 }
3503
3504 void unlockAllApps() {
3505 // TODO
3506 }
3507
Winsonf768d932015-09-25 16:12:35 -07003508 public boolean launcherCallbacksProvidesSearch() {
3509 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3510 }
3511
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003512 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003513 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003514 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003515 }
3516
Adam Cohen24ce0b32014-01-14 16:18:14 -08003517 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003518 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3519 if (searchProvider == null) {
3520 return null;
3521 }
3522
3523 Bundle opts = new Bundle();
3524 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003525 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003526
Tony Wickham3a3517f2015-10-06 11:27:04 -07003527 // Determine the min and max dimensions of the widget.
3528 LauncherAppState app = LauncherAppState.getInstance();
3529 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3530 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3531 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003532 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3533 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003534 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003535 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003536 int minWidth = maxWidth;
3537 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003538 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3539 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3540 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3541 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3542 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003543 }
3544 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3545 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3546 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3547 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003548 if (LOGD) {
3549 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3550 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3551 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003552
Tony Wickham775455c2015-10-16 09:49:32 -07003553 if (mLauncherCallbacks != null) {
3554 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3555 }
3556
Sunny Goyalf7258242015-10-19 16:59:07 -07003557 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003558 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003559 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003560 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003561 || (widgetInfo == null)
3562 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003563 // A valid widget is not already bound.
3564 if (widgetId > -1) {
3565 mAppWidgetHost.deleteAppWidgetId(widgetId);
3566 widgetId = -1;
3567 }
3568
3569 // Try to bind a new widget
3570 widgetId = mAppWidgetHost.allocateAppWidgetId();
3571
3572 if (!AppWidgetManagerCompat.getInstance(this)
3573 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3574 mAppWidgetHost.deleteAppWidgetId(widgetId);
3575 widgetId = -1;
3576 }
3577
Sunny Goyalf7258242015-10-19 16:59:07 -07003578 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003579 .putInt(QSB_WIDGET_ID, widgetId)
3580 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003581 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 }
3583
Sunny Goyal8d595092015-07-06 12:22:14 -07003584 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003585 if (widgetId != -1) {
3586 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003587 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003588 mQsb.updateAppWidgetOptions(opts);
3589 mQsb.setPadding(0, 0, 0, 0);
3590 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003591 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003592 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003593 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003594 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003595 }
3596
Sunny Goyal22235bc2015-04-03 09:23:43 -07003597 private void reinflateQSBIfNecessary() {
3598 if (mQsb instanceof LauncherAppWidgetHostView &&
3599 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3600 mSearchDropTargetBar.removeView(mQsb);
3601 mQsb = null;
3602 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3603 }
3604 }
3605
Michael Jurkad7c28052012-04-27 15:43:36 -07003606 @Override
3607 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003608 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003609 final List<CharSequence> text = event.getText();
3610 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003611 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003612 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003613 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003614 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003615 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003616 } else if (mWorkspace != null) {
3617 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003618 } else {
3619 text.add(getString(R.string.all_apps_home_button_label));
3620 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003621 return result;
3622 }
3623
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003624 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003625 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003626 */
Adam Cohen091440a2015-03-18 14:16:05 -07003627 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003628 @Override
3629 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003630 closeSystemDialogs();
3631 }
3632 }
3633
3634 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003635 * If the activity is currently paused, signal that we need to run the passed Runnable
3636 * in onResume.
3637 *
3638 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003639 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3640 * wrong when we're not running, and if the activity comes back to what the configuration was
3641 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 *
3643 * Implementation of the method from LauncherModel.Callbacks.
3644 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003645 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003646 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003647 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003649 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003650 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003651 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003652 }
3653 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003654 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 return true;
3656 } else {
3657 return false;
3658 }
3659 }
3660
Michael Jurkac402cd92013-05-20 15:49:32 +02003661 private boolean waitUntilResume(Runnable run) {
3662 return waitUntilResume(run, false);
3663 }
3664
Michael Jurka1e2f4652013-07-08 18:03:46 -07003665 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003666 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003667 }
3668
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003670 * If the activity is currently paused, signal that we need to re-run the loader
3671 * in onResume.
3672 *
3673 * This needs to be called from incoming places where resources might have been loaded
3674 * while we are paused. That is becaues the Configuration might be wrong
3675 * when we're not running, and if it comes back to what it was when we
3676 * were paused, we are not restarted.
3677 *
3678 * Implementation of the method from LauncherModel.Callbacks.
3679 *
3680 * @return true if we are currently paused. The caller might be able to
3681 * skip some work in that case since we will come back again.
3682 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003683 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003684 public boolean setLoadOnResume() {
3685 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003686 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003687 mOnResumeNeedsLoad = true;
3688 return true;
3689 } else {
3690 return false;
3691 }
3692 }
3693
3694 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003697 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003699 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003700 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003701 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003702 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003705
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003707 * Clear any pending bind callbacks. This is called when is loader is planning to
3708 * perform a full rebind from scratch.
3709 */
3710 @Override
3711 public void clearPendingBinds() {
3712 mBindOnResumeCallbacks.clear();
3713 if (mPendingExecutor != null) {
3714 mPendingExecutor.markCompleted();
3715 mPendingExecutor = null;
3716 }
3717 }
3718
3719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 * Refreshes the shortcuts shown on the workspace.
3721 *
3722 * Implementation of the method from LauncherModel.Callbacks.
3723 */
3724 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003725 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003726
Winson Chungd64d1762013-08-20 14:37:16 -07003727 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003728 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003729 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003730
Michael Jurka05bf644e2011-11-30 20:28:53 -08003731 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003732 if (mHotseat != null) {
3733 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003734 }
3735 }
3736
Adam Cohendcd297f2013-06-18 13:13:40 -07003737 @Override
3738 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003739 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003740
Adam Cohen5084cba2013-09-03 12:01:16 -07003741 // If there are no screens, we need to have an empty screen
3742 if (orderedScreenIds.size() == 0) {
3743 mWorkspace.addExtraEmptyScreen();
3744 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003745
3746 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003747 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003748 if (hasCustomContentToLeft()) {
3749 mWorkspace.createCustomContentContainer();
3750 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003751 }
Winson Chung64359a52013-07-08 17:17:08 -07003752 }
3753
3754 @Override
3755 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003756 int count = orderedScreenIds.size();
3757 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003758 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003759 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003760 }
3761
Winson Chungd64d1762013-08-20 14:37:16 -07003762 public void bindAppsAdded(final ArrayList<Long> newScreens,
3763 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003764 final ArrayList<ItemInfo> addAnimated,
3765 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 Runnable r = new Runnable() {
3767 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003768 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003769 }
3770 };
3771 if (waitUntilResume(r)) {
3772 return;
3773 }
3774
Winson Chungd64d1762013-08-20 14:37:16 -07003775 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003776 if (newScreens != null) {
3777 bindAddScreens(newScreens);
3778 }
Winson Chungd64d1762013-08-20 14:37:16 -07003779
3780 // We add the items without animation on non-visible pages, and with
3781 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003782 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003783 bindItems(addNotAnimated, 0,
3784 addNotAnimated.size(), false);
3785 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003786 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003787 bindItems(addAnimated, 0,
3788 addAnimated.size(), true);
3789 }
Winson Chungc58497e2013-09-03 17:48:37 -07003790
Winson Chung87412982013-10-03 18:34:14 -07003791 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003792 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003793
Winson Chungb745afb2015-03-02 11:51:23 -08003794 if (addedApps != null && mAppsView != null) {
3795 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003796 }
Winson Chungd64d1762013-08-20 14:37:16 -07003797 }
3798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 * Bind the items start-end from the list.
3801 *
3802 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003803 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003804 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003805 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3806 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 Runnable r = new Runnable() {
3808 public void run() {
3809 bindItems(shortcuts, start, end, forceAnimateIcons);
3810 }
3811 };
3812 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003813 return;
3814 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003815
Winson Chungf0c6ae02012-03-21 16:10:31 -07003816 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003817 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3818 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003819 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003821 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003822 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003824
3825 // Short circuit if we are loading dock items for a configuration which has no dock
3826 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3827 mHotseat == null) {
3828 continue;
3829 }
3830
Sunny Goyal639e9062015-08-19 19:17:06 -07003831 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 switch (item.itemType) {
3833 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3834 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003836 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003837
Winson Chung64359a52013-07-08 17:17:08 -07003838 /*
3839 * TODO: FIX collision case
3840 */
Winson Chungce376632013-07-11 16:12:41 -07003841 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3842 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3843 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003844 View v = cl.getChildAt(item.cellX, item.cellY);
3845 Object tag = v.getTag();
3846 String desc = "Collision while binding workspace item: " + item
3847 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003848 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003849 throw (new RuntimeException(desc));
3850 } else {
3851 Log.d(TAG, desc);
3852 }
Winson Chungce376632013-07-11 16:12:41 -07003853 }
Winson Chung64359a52013-07-08 17:17:08 -07003854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003856 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003857 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003858 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003859 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003861 default:
3862 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003863 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003864
3865 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3866 item.cellY, 1, 1);
3867 if (animateIcons) {
3868 // Animate all the applications up now
3869 view.setAlpha(0f);
3870 view.setScaleX(0f);
3871 view.setScaleY(0f);
3872 bounceAnims.add(createNewAppBounceAnimation(view, i));
3873 newShortcutsScreenId = item.screenId;
3874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003876
Winson Chung997a9232013-07-24 15:33:46 -07003877 if (animateIcons) {
3878 // Animate to the correct page
3879 if (newShortcutsScreenId > -1) {
3880 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003881 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003882 final Runnable startBounceAnimRunnable = new Runnable() {
3883 public void run() {
3884 anim.playTogether(bounceAnims);
3885 anim.start();
3886 }
3887 };
Winson Chung997a9232013-07-24 15:33:46 -07003888 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003889 // We post the animation slightly delayed to prevent slowdowns
3890 // when we are loading right after we return to launcher.
3891 mWorkspace.postDelayed(new Runnable() {
3892 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003893 if (mWorkspace != null) {
3894 mWorkspace.snapToPage(newScreenIndex);
3895 mWorkspace.postDelayed(startBounceAnimRunnable,
3896 NEW_APPS_ANIMATION_DELAY);
3897 }
Winson Chung94d67682013-09-25 16:29:40 -07003898 }
3899 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003900 } else {
3901 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003902 }
3903 }
Winson Chung64359a52013-07-08 17:17:08 -07003904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003906 }
3907
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 /**
3909 * Implementation of the method from LauncherModel.Callbacks.
3910 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003911 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003912 Runnable r = new Runnable() {
3913 public void run() {
3914 bindFolders(folders);
3915 }
3916 };
3917 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003918 return;
3919 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003920 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003921 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003923 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3924 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3925 view.updateIcon(mIconCache);
3926 item.hostView = view;
3927 item.hostView.updateAppWidget(null);
3928 item.hostView.setOnClickListener(this);
3929 addAppWidgetToWorkspace(item, null, false);
3930 mWorkspace.requestLayout();
3931 }
3932
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 /**
3934 * Add the views for a widget to the workspace.
3935 *
3936 * Implementation of the method from LauncherModel.Callbacks.
3937 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003938 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003939 Runnable r = new Runnable() {
3940 public void run() {
3941 bindAppWidget(item);
3942 }
3943 };
3944 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003945 return;
3946 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003947
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003948 if (mIsSafeModeEnabled) {
3949 bindSafeModeWidget(item);
3950 return;
3951 }
3952
Daniel Sandler843e8602010-06-07 14:59:01 -04003953 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3954 if (DEBUG_WIDGETS) {
3955 Log.d(TAG, "bindAppWidget: " + item);
3956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003958 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003959
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003960 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3961 // If the provider is not ready, bind as a pending widget.
3962 appWidgetInfo = null;
3963 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3964 // The widget id is not valid. Try to find the widget based on the provider info.
3965 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3966 } else {
3967 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3968 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003969
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003970 // If the provider is ready, but the width is not yet restored, try to restore it.
3971 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3972 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003973 if (appWidgetInfo == null) {
3974 if (DEBUG_WIDGETS) {
3975 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3976 + " belongs to component " + item.providerName
3977 + ", as the povider is null");
3978 }
3979 LauncherModel.deleteItemFromDatabase(this, item);
3980 return;
3981 }
Sunny Goyalff572272014-07-23 13:58:07 -07003982
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003983 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003984 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003985 // Note: This assumes that the id remap broadcast is received before this step.
3986 // If that is not the case, the id remap will be ignored and user may see the
3987 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003988 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003989 pendingInfo.spanX = item.spanX;
3990 pendingInfo.spanY = item.spanY;
3991 pendingInfo.minSpanX = item.minSpanX;
3992 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003993 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003994
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003995 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3996 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3997 newWidgetId, appWidgetInfo, options);
3998
3999 // TODO consider showing a permission dialog when the widget is clicked.
4000 if (!success) {
4001 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4004 + " belongs to component " + item.providerName
4005 + ", as the launcher is unable to bing a new widget id");
4006 }
4007 LauncherModel.deleteItemFromDatabase(this, item);
4008 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004009 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004010
4011 item.appWidgetId = newWidgetId;
4012
4013 // If the widget has a configure activity, it is still needs to set it up, otherwise
4014 // the widget is ready to go.
4015 item.restoreStatus = (appWidgetInfo.configure == null)
4016 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4017 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4018
4019 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004020 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004021 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004022 // The widget was marked as UI not ready, but there is no configure activity to
4023 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004024 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4025 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004026 }
Sunny Goyalff572272014-07-23 13:58:07 -07004027 }
4028
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004029 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004030 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004031 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4032 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004034
Sunny Goyal56c73602015-09-25 12:55:01 -07004035 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004036 if (appWidgetInfo == null) {
4037 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004038 deleteWidgetInfo(item);
4039 return;
4040 }
4041
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004042 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004043 item.minSpanX = appWidgetInfo.minSpanX;
4044 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004045 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004047 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4048 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004049 view.updateIcon(mIconCache);
4050 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004051 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004052 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004053 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004054 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004055 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056
Daniel Sandler843e8602010-06-07 14:59:01 -04004057 if (DEBUG_WIDGETS) {
4058 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4059 + (SystemClock.uptimeMillis()-start) + "ms");
4060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 }
4062
Sunny Goyalff572272014-07-23 13:58:07 -07004063 /**
4064 * Restores a pending widget.
4065 *
4066 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004067 */
4068 private void completeRestoreAppWidget(final int appWidgetId) {
4069 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4070 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4071 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4072 return;
4073 }
4074
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004075 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004076 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4077
4078 mWorkspace.reinflateWidgetsIfNecessary();
4079 LauncherModel.updateItemInDatabase(this, info);
4080 }
4081
Adam Cohen1462de32012-07-24 22:34:36 -07004082 public void onPageBoundSynchronously(int page) {
4083 mSynchronouslyBoundPages.add(page);
4084 }
4085
Sunny Goyal527c7d32015-08-28 15:19:36 -07004086 @Override
4087 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4088 if (mPendingExecutor != null) {
4089 mPendingExecutor.markCompleted();
4090 }
4091 mPendingExecutor = executor;
4092 executor.attachTo(this);
4093 }
4094
4095 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4096 if (mPendingExecutor == executor) {
4097 mPendingExecutor = null;
4098 }
4099 }
4100
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 /**
4102 * Callback saying that there aren't any more items to bind.
4103 *
4104 * Implementation of the method from LauncherModel.Callbacks.
4105 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004106 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004107 Runnable r = new Runnable() {
4108 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004109 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004110 }
4111 };
4112 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004113 return;
4114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 if (mSavedState != null) {
4116 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004117 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004119
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 mSavedState = null;
4121 }
4122
Adam Cohen1462de32012-07-24 22:34:36 -07004123 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004125 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004126 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004127
4128 // If we received the result of any pending adds while the loader was running (e.g. the
4129 // widget configuration forced an orientation change), process them now.
4130 if (sPendingAddItem != null) {
4131 final long screenId = completeAdd(sPendingAddItem);
4132
4133 // TODO: this moves the user to the page where the pending item was added. Ideally,
4134 // the screen would be guaranteed to exist after bind, and the page would be set through
4135 // the workspace restore process.
4136 mWorkspace.post(new Runnable() {
4137 @Override
4138 public void run() {
4139 mWorkspace.snapToScreenId(screenId);
4140 }
4141 });
4142 sPendingAddItem = null;
4143 }
4144
Sunny Goyal756adbc2015-04-16 15:20:51 -07004145 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004146
4147 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004148 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004149 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004150 }
4151
Adam Cohen3ed316a2014-07-23 18:21:20 -07004152 private void sendLoadingCompleteBroadcastIfNecessary() {
4153 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4154 String permission =
4155 getResources().getString(R.string.receive_first_load_broadcast_permission);
4156 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4157 sendBroadcast(intent, permission);
4158 SharedPreferences.Editor editor = mSharedPrefs.edit();
4159 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4160 editor.apply();
4161 }
4162 }
4163
Winson Chunga0b7e862013-09-05 16:03:15 -07004164 public boolean isAllAppsButtonRank(int rank) {
4165 if (mHotseat != null) {
4166 return mHotseat.isAllAppsButtonRank(rank);
4167 }
4168 return false;
4169 }
4170
Winson Chunga2413752012-04-03 14:22:34 -07004171 private boolean canRunNewAppsAnimation() {
4172 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004173 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4174 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004175 }
4176
Winson Chungc9168342013-06-26 14:54:55 -07004177 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004178 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004179 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4180 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004181 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004182 return bounceAnim;
4183 }
4184
Adam Cohen27772732013-11-11 14:00:56 +00004185 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004186 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004187 }
4188
Tony Wickham3a3517f2015-10-06 11:27:04 -07004189 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004190 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004191 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004192 }
4193
Tony Wickham55616cd2015-09-23 14:55:17 -07004194 public int getSearchBarHeight() {
4195 if (mLauncherCallbacks != null) {
4196 return mLauncherCallbacks.getSearchBarHeight();
4197 }
4198 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4199 }
4200
Winson Chung88fa7412015-08-03 14:25:28 -07004201 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004202 if (mSearchDropTargetBar == null) {
4203 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004204 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004205 if (mQsb != null) {
4206 mSearchDropTargetBar.removeView(mQsb);
4207 mQsb = null;
4208 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004209 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004210 }
4211
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004212 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004213 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4214 * multiple calls to bind the same list.)
4215 */
4216 @Thunk ArrayList<AppInfo> mTmpAppsList;
4217 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4218 public void run() {
4219 bindAllApplications(mTmpAppsList);
4220 mTmpAppsList = null;
4221 }
4222 };
4223
4224 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004225 * Add the icons for all apps.
4226 *
4227 * Implementation of the method from LauncherModel.Callbacks.
4228 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004229 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004230 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4231 mTmpAppsList = apps;
4232 return;
4233 }
4234
Winson Chungb745afb2015-03-02 11:51:23 -08004235 if (mAppsView != null) {
4236 mAppsView.setApps(apps);
4237 }
Adam Cohen9211d422014-10-07 18:14:53 -07004238 if (mLauncherCallbacks != null) {
4239 mLauncherCallbacks.bindAllApplications(apps);
4240 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004241 }
4242
4243 /**
4244 * A package was updated.
4245 *
4246 * Implementation of the method from LauncherModel.Callbacks.
4247 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004248 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004249 Runnable r = new Runnable() {
4250 public void run() {
4251 bindAppsUpdated(apps);
4252 }
4253 };
4254 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004255 return;
4256 }
4257
Winson Chungb745afb2015-03-02 11:51:23 -08004258 if (mAppsView != null) {
4259 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 }
4262
Sunny Goyal4390ace2014-10-13 11:33:11 -07004263 @Override
4264 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4265 Runnable r = new Runnable() {
4266 public void run() {
4267 bindWidgetsRestored(widgets);
4268 }
4269 };
4270 if (waitUntilResume(r)) {
4271 return;
4272 }
4273 mWorkspace.widgetsRestored(widgets);
4274 }
4275
Joe Onorato9c1289c2009-08-17 11:03:03 -04004276 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004277 * Some shortcuts were updated in the background.
4278 *
4279 * Implementation of the method from LauncherModel.Callbacks.
4280 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004281 @Override
4282 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4283 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004284 Runnable r = new Runnable() {
4285 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004286 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004287 }
4288 };
4289 if (waitUntilResume(r)) {
4290 return;
4291 }
4292
Sunny Goyal4390ace2014-10-13 11:33:11 -07004293 if (!updated.isEmpty()) {
4294 mWorkspace.updateShortcuts(updated);
4295 }
4296
4297 if (!removed.isEmpty()) {
4298 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4299 for (ShortcutInfo si : removed) {
4300 removedComponents.add(si.getTargetComponent());
4301 }
4302 mWorkspace.removeItemsByComponentName(removedComponents, user);
4303 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004304 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004305 }
4306 }
4307
4308 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004309 * Update the state of a package, typically related to install state.
4310 *
4311 * Implementation of the method from LauncherModel.Callbacks.
4312 */
Sunny Goyale755d462014-07-22 13:48:29 -07004313 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004314 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4315 Runnable r = new Runnable() {
4316 public void run() {
4317 bindRestoreItemsChange(updates);
4318 }
4319 };
4320 if (waitUntilResume(r)) {
4321 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004322 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004323
Sunny Goyal756adbc2015-04-16 15:20:51 -07004324 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004325 }
4326
4327 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004328 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004329 * in addition to specific applications to remove, the reason being that
4330 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004331 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004332 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004334 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004335 public void bindWorkspaceComponentsRemoved(
4336 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4337 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004338 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004339 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004340 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004341 }
Winson Chungd64d1762013-08-20 14:37:16 -07004342 };
4343 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004344 return;
4345 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004346 if (!packageNames.isEmpty()) {
4347 mWorkspace.removeItemsByPackageName(packageNames, user);
4348 }
4349 if (!components.isEmpty()) {
4350 mWorkspace.removeItemsByComponentName(components, user);
4351 }
4352 // Notify the drag controller
4353 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004354
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004355 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004356
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004357 @Override
4358 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4359 Runnable r = new Runnable() {
4360 public void run() {
4361 bindAppInfosRemoved(appInfos);
4362 }
4363 };
4364 if (waitUntilResume(r)) {
4365 return;
Joe Onorato36115782010-06-17 13:28:48 -04004366 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004367
Winson Chungdf95eb12013-10-16 14:57:07 -07004368 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004369 if (mAppsView != null) {
4370 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004372 }
Joe Onoratobe386092009-11-17 17:32:16 -08004373
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004374 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004375 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004376 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004377 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004378 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004379
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004380 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004381 public void bindWidgetsModel(WidgetsModel model) {
4382 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004383 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004384 return;
4385 }
4386
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004387 if (mWidgetsView != null && model != null) {
4388 mWidgetsView.addWidgets(model);
4389 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004390 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004391 }
4392
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004393 @Override
4394 public void notifyWidgetProvidersChanged() {
4395 if (mWorkspace.getState().shouldUpdateWidget) {
4396 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4397 }
4398 }
4399
Winson Chung400438b2011-01-16 17:53:48 -08004400 private int mapConfigurationOriActivityInfoOri(int configOri) {
4401 final Display d = getWindowManager().getDefaultDisplay();
4402 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4403 switch (d.getRotation()) {
4404 case Surface.ROTATION_0:
4405 case Surface.ROTATION_180:
4406 // We are currently in the same basic orientation as the natural orientation
4407 naturalOri = configOri;
4408 break;
4409 case Surface.ROTATION_90:
4410 case Surface.ROTATION_270:
4411 // We are currently in the other basic orientation to the natural orientation
4412 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4413 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4414 break;
4415 }
4416
4417 int[] oriMap = {
4418 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4419 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4420 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4421 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4422 };
4423 // Since the map starts at portrait, we need to offset if this device's natural orientation
4424 // is landscape.
4425 int indexOffset = 0;
4426 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4427 indexOffset = 1;
4428 }
4429 return oriMap[(d.getRotation() + indexOffset) % 4];
4430 }
Adam Cohen446e9402011-09-15 18:21:21 -07004431
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004432 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004433 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004434 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004435 if (Utilities.ATLEAST_JB_MR2) {
4436 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4437 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004438 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4439 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004440 }
Winson Chung4b919f82012-05-01 10:44:08 -07004441 }
4442 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004443
Winson Chung4b919f82012-05-01 10:44:08 -07004444 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004445 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004446 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004447 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004448 } else {
4449 mHandler.postDelayed(new Runnable() {
4450 public void run() {
4451 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4452 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004453 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004454 }
Winson Chung4b919f82012-05-01 10:44:08 -07004455 }
Winson Chung400438b2011-01-16 17:53:48 -08004456 }
4457
Winson Chunge43a1e72014-01-15 10:33:02 -08004458 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004459 if (mLauncherCallbacks != null) {
4460 return mLauncherCallbacks.isLauncherPreinstalled();
4461 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004462 PackageManager pm = getPackageManager();
4463 try {
4464 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4465 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4466 return true;
4467 } else {
4468 return false;
4469 }
4470 } catch (NameNotFoundException e) {
4471 e.printStackTrace();
4472 return false;
4473 }
4474 }
4475
Adam Cohenea90f832014-05-21 15:03:34 -07004476 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004477 * To be overridden by subclasses to indicate that there is an activity to launch
4478 * before showing the standard launcher experience.
4479 */
4480 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004481 if (mLauncherCallbacks != null) {
4482 return mLauncherCallbacks.hasFirstRunActivity();
4483 }
Adam Cohen432609a2014-03-13 17:03:22 -07004484 return false;
4485 }
4486
4487 /**
4488 * To be overridden by subclasses to launch any first run activity
4489 */
4490 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004491 if (mLauncherCallbacks != null) {
4492 return mLauncherCallbacks.getFirstRunActivity();
4493 }
Adam Cohen432609a2014-03-13 17:03:22 -07004494 return null;
4495 }
4496
Winson Chunga6945242014-01-08 14:04:34 -08004497 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004498 return !ActivityManager.isRunningInTestHarness() &&
4499 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004500 }
4501
Adam Cohen4a9423d2014-03-28 14:22:31 -07004502 protected boolean hasRunFirstRunActivity() {
4503 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4504 }
4505
Adam Cohen432609a2014-03-13 17:03:22 -07004506 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004507 if (shouldRunFirstRunActivity() &&
4508 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004509 Intent firstRunIntent = getFirstRunActivity();
4510 if (firstRunIntent != null) {
4511 startActivity(firstRunIntent);
4512 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004513 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004514 }
4515 }
Adam Cohen432609a2014-03-13 17:03:22 -07004516 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004517 }
4518
4519 private void markFirstRunActivityShown() {
4520 SharedPreferences.Editor editor = mSharedPrefs.edit();
4521 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4522 editor.apply();
4523 }
4524
Adam Cohen432609a2014-03-13 17:03:22 -07004525 /**
4526 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4527 * screen that must be displayed and dismissed.
4528 */
4529 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004530 if (mLauncherCallbacks != null) {
4531 return mLauncherCallbacks.hasDismissableIntroScreen();
4532 }
Adam Cohen432609a2014-03-13 17:03:22 -07004533 return false;
4534 }
4535
4536 /**
4537 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4538 */
4539 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004540 if (mLauncherCallbacks != null) {
4541 return mLauncherCallbacks.getIntroScreen();
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543 return null;
4544 }
4545
4546 /**
4547 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4548 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4549 */
4550 private boolean shouldShowIntroScreen() {
4551 return hasDismissableIntroScreen() &&
4552 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4553 }
4554
4555 protected void showIntroScreen() {
4556 View introScreen = getIntroScreen();
4557 changeWallpaperVisiblity(false);
4558 if (introScreen != null) {
4559 mDragLayer.showOverlayView(introScreen);
4560 }
4561 }
4562
4563 public void dismissIntroScreen() {
4564 markIntroScreenDismissed();
4565 if (showFirstRunActivity()) {
4566 // We delay hiding the intro view until the first run activity is showing. This
4567 // avoids a blip.
4568 mWorkspace.postDelayed(new Runnable() {
4569 @Override
4570 public void run() {
4571 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004572 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004573 }
4574 }, ACTIVITY_START_DELAY);
4575 } else {
4576 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004577 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004578 }
4579 changeWallpaperVisiblity(true);
4580 }
4581
4582 private void markIntroScreenDismissed() {
4583 SharedPreferences.Editor editor = mSharedPrefs.edit();
4584 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4585 editor.apply();
4586 }
4587
Adam Cohen091440a2015-03-18 14:16:05 -07004588 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004589 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4590 // on the device, then we always show the first run cling experience (or if there is no
4591 // launcher2). Otherwise, we prompt the user upon started for migration
4592 LauncherClings launcherClings = new LauncherClings(this);
4593 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004594 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004595 if (mModel.canMigrateFromOldLauncherDb(this)) {
4596 launcherClings.showMigrationCling();
4597 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004598 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004599 }
4600 }
4601 }
4602
Winson Chunga6945242014-01-08 14:04:34 -08004603 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004604 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4605 if (mHotseat != null) mHotseat.setAlpha(1f);
4606 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004607 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4608 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004609 }
4610
4611 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004612 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4613 if (mHotseat != null) mHotseat.setAlpha(0f);
4614 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004615 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4616 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004617 }
4618
Winson Chung5ffd51d2015-06-25 11:36:50 -07004619 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004620 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004621 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004622 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004623 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004624 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004625 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4626 if (userHandle != null) {
4627 user = UserHandleCompat.fromUser(userHandle);
4628 }
4629 }
4630 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004631 }
4632
4633 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004634 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004635 if (user == null) {
4636 user = UserHandleCompat.myUserHandle();
4637 }
4638
4639 // Called from search suggestion, add the profile extra to the intent to ensure that we
4640 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004641 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004642 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004643 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004644 return null;
4645 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004646 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004647 }
4648
Winson Chung5ffd51d2015-06-25 11:36:50 -07004649 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004650 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4651 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004652 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004653 UserHandleCompat.myUserHandle());
4654 }
4655
Winson Chung5ffd51d2015-06-25 11:36:50 -07004656 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004657 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4658 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004659 mWorkspace.onExternalDragStartedWithItem(dragView);
4660 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004661 }
4662
Winson Chung5ffd51d2015-06-25 11:36:50 -07004663 protected void moveWorkspaceToDefaultScreen() {
4664 mWorkspace.moveToDefaultScreen(false);
4665 }
4666
Anjali Koppalf5d29132014-02-28 13:33:27 -08004667 @Override
4668 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004669 if (mLauncherCallbacks != null) {
4670 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4671 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004672 }
4673
Winson Chung80baf5a2010-08-09 16:03:15 -07004674 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004675 * Returns a FastBitmapDrawable with the icon, accurately sized.
4676 */
4677 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4678 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4679 d.setFilterBitmap(true);
4680 resizeIconDrawable(d);
4681 return d;
4682 }
4683
4684 /**
4685 * Resizes an icon drawable to the correct icon size.
4686 */
Winson5fbe0742015-09-30 15:33:00 -07004687 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004688 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004689 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004690 }
4691
4692 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004693 * Prints out out state for debugging.
4694 */
4695 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004696 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004697 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004698 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4699 Log.d(TAG, "mRestoring=" + mRestoring);
4700 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004701 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004702 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004703 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004704
Daniel Sandler325dc232013-06-05 22:57:57 -04004705 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004706 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004707
4708 @Override
4709 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4710 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004711 // Dump workspace
4712 writer.println(prefix + "Workspace Items");
4713 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4714 writer.println(prefix + " Homescreen " + i);
4715
4716 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4717 for (int j = 0; j < layout.getChildCount(); j++) {
4718 Object tag = layout.getChildAt(j).getTag();
4719 if (tag != null) {
4720 writer.println(prefix + " " + tag.toString());
4721 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004722 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004723 }
Sunny Goyala1365452015-10-01 15:46:24 -07004724
4725 writer.println(prefix + " Hotseat");
4726 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4727 for (int j = 0; j < layout.getChildCount(); j++) {
4728 Object tag = layout.getChildAt(j).getTag();
4729 if (tag != null) {
4730 writer.println(prefix + " " + tag.toString());
4731 }
4732 }
4733
4734 synchronized (sDumpLogs) {
4735 writer.println();
4736 writer.println(prefix + "Debug logs");
4737 for (String log : sDumpLogs) {
4738 writer.println(prefix + " " + log);
4739 }
4740 }
4741
Adam Cohen9211d422014-10-07 18:14:53 -07004742 if (mLauncherCallbacks != null) {
4743 mLauncherCallbacks.dump(prefix, fd, writer, args);
4744 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004745 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004746
Sunny Goyala1365452015-10-01 15:46:24 -07004747 public static void addDumpLog(String tag, String log) {
4748 Log.d(tag, log);
4749 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004750 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004751 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004752 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004753 }
4754
Adam Cohen59400422014-03-05 18:07:04 -08004755 public static CustomAppWidget getCustomAppWidget(String name) {
4756 return sCustomAppWidgets.get(name);
4757 }
4758
4759 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4760 return sCustomAppWidgets;
4761 }
4762
Sunny Goyal29538332016-02-18 09:10:19 -08004763 public static List<View> getFolderContents(View icon) {
4764 if (icon instanceof FolderIcon) {
4765 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4766 } else {
4767 return Collections.EMPTY_LIST;
4768 }
4769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004770}
Michael Jurka2763be32011-02-24 11:19:57 -08004771
Dan Sandlerd5024042014-01-09 15:01:33 -05004772interface DebugIntents {
4773 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4774 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004775}