blob: 047602257fd9a607843c0a8eba3fe29ca2f1ba25 [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;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800113import com.android.launcher3.userevent.Logger;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700114import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700115import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700116import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700117import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700118import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700119import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700120import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800122import com.android.wallpaperpicker.WallpaperUtils;
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;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700139 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700140 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700141 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700145 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
Dan Sandlerd5024042014-01-09 15:01:33 -0500149 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Sunny Goyal28c6b962015-10-12 11:42:05 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 // To turn on these properties, type
174 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
182 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700183 // Type: int
184 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700185 // Type: long
186 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Adam Cohen3ed316a2014-07-23 18:21:20 -0700197 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
198 static final String ACTION_FIRST_LOAD_COMPLETE =
199 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
200
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 private static final String QSB_WIDGET_ID = "qsb_widget_id";
202 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700207 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
208 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700218 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800230 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700239 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800240 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Winson Chungc51db6a2011-10-05 11:44:49 -0700251 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700252 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
254 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Sunny Goyal594d76d2014-11-06 10:12:54 -0800261 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
265 // scroll issues (because the workspace may not have been measured yet) and extra work.
266 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
267 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
269 private SpannableStringBuilder mDefaultKeySsb = null;
270
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800273 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private boolean mRestoring;
275 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700276 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700280 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 private IconCache mIconCache;
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 Songfbf19cc2016-02-19 12:14:47 -0800369 private Logger mUserEventLogger;
370
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
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800428 mUserEventLogger = new Logger(this);
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429 mStats = new Stats(this);
430
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);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
458 if (PROFILE_STARTUP) {
459 android.os.Debug.stopMethodTracing();
460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700470 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473
474 // For handling default keys
475 mDefaultKeySsb = new SpannableStringBuilder();
476 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800477
478 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
479 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800480
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
482 // In case we are on a device with locked rotation, we should look at preferences to check
483 // if the user has specifically allowed rotation.
484 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700485 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
505 public void onSettingsChanged(String settings, boolean value) {
506 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
507 mRotationEnabled = value;
508 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
509 mUpdateOrientationRunnable.run();
510 }
511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
Sunny Goyal32554d12015-12-03 15:31:25 -0800523 /**
524 * Call this after onCreate to set or clear overlay.
525 */
526 public void setLauncherOverlay(LauncherOverlay overlay) {
527 if (overlay != null) {
528 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
529 }
530 mWorkspace.setLauncherOverlay(overlay);
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700548 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Winson Chungef7f8742015-06-04 17:18:17 -0700586 /**
587 * Updates the bounds of all the overlays to match the new fixed bounds.
588 */
589 public void updateOverlayBounds(Rect newBounds) {
590 mAppsView.setSearchBarBounds(newBounds);
591 mWidgetsView.setSearchBarBounds(newBounds);
592 }
593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Anton Hanssona2f665f2013-09-26 12:18:32 +0100632 public Stats getStats() {
633 return mStats;
634 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800635 public Logger getLogger() {return mUserEventLogger; }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100636
Hyunyoung Song3f471442015-04-08 19:01:34 -0700637 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700638 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
639 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700640 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700641 }
642
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000643 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700644 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
645 // This cast is safe as long as the id < 0x00FFFFFF
646 // Since we jail all the dynamically generated views, there should be no clashes
647 // with any other views.
648 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 }
650
651 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700652 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
653 * a configuration step, this allows the proper animations to run after other transitions.
654 */
Adam Cohendb364c32014-05-20 14:23:40 -0700655 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700656 long screenId = args.screenId;
657 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
658 // When the screen id represents an actual screen (as opposed to a rank) we make sure
659 // that the drop page actually exists.
660 screenId = ensurePendingDropLayoutExists(args.screenId);
661 }
Adam Cohendb364c32014-05-20 14:23:40 -0700662
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700665 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700666 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700667 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700669 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700670 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700671 case REQUEST_RECONFIGURE_APPWIDGET:
672 completeRestoreAppWidget(args.appWidgetId);
673 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 }
Michael Jurka27614d22012-04-02 06:40:22 -0700675 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
676 // if you turned the screen off and then back while in All Apps, Launcher would not
677 // return to the workspace. Clearing mAddInfo.container here fixes this issue
678 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700679 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 }
681
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700683 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700684 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700685 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700686 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800687 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700688
Adam Cohenad4e15c2013-10-17 16:21:35 -0700689 Runnable exitSpringLoaded = new Runnable() {
690 @Override
691 public void run() {
692 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
693 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
694 }
695 };
696
Michael Jurka8b805b12012-04-18 14:23:14 -0700697 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700698 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700699 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
701 if (resultCode == RESULT_CANCELED) {
702 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700703 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800706 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700707 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700708
709 // When the user has granted permission to bind widgets, we should check to see if
710 // we can inflate the default search bar widget.
711 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 }
713 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200714 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
715 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700716 // User could have free-scrolled between pages before picking a wallpaper; make sure
717 // we move to the closest one now.
718 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700719 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 }
721 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200723
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700725 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700726
Adam Cohendb364c32014-05-20 14:23:40 -0700727 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 // We have special handling for widgets
729 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800730 final int appWidgetId;
731 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
732 : -1;
733 if (widgetId < 0) {
734 appWidgetId = pendingAddWidgetId;
735 } else {
736 appWidgetId = widgetId;
737 }
738
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800740 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700741 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
742 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 result = RESULT_CANCELED;
744 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 @Override
747 public void run() {
748 exitSpringLoadedDragModeDelayed(false, 0, null);
749 }
750 };
Adam Cohendb364c32014-05-20 14:23:40 -0700751 if (workspaceLocked) {
752 // No need to remove the empty screen if we're mid-binding, as the
753 // the bind will not add the empty screen.
754 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
755 } else {
756 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
757 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
758 }
Winson Chung5aaab772012-04-27 15:24:02 -0700759 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700761 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
762 // When the screen id represents an actual screen (as opposed to a rank)
763 // we make sure that the drop page actually exists.
764 mPendingAddInfo.screenId =
765 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
766 }
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
768
769 dropLayout.setDropPending(true);
770 final Runnable onComplete = new Runnable() {
771 @Override
772 public void run() {
773 completeTwoStageWidgetDrop(resultCode, appWidgetId);
774 dropLayout.setDropPending(false);
775 }
776 };
777 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
779 } else {
780 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
781 mPendingAddInfo);
782 sPendingAddItem = args;
783 }
Winson Chung5aaab772012-04-27 15:24:02 -0700784 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 return;
786 }
787
Sunny Goyalff572272014-07-23 13:58:07 -0700788 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
789 if (resultCode == RESULT_OK) {
790 // Update the widget view.
791 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
792 pendingAddWidgetId, mPendingAddInfo);
793 if (workspaceLocked) {
794 sPendingAddItem = args;
795 } else {
796 completeAdd(args);
797 }
798 }
799 // Leave the widget in the pending state if the user canceled the configure.
800 return;
801 }
802
Sunny Goyalaad90582015-07-09 14:22:50 -0700803 if (requestCode == REQUEST_CREATE_SHORTCUT) {
804 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
805 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
806 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
807 mPendingAddInfo);
808 if (isWorkspaceLocked()) {
809 sPendingAddItem = args;
810 } else {
811 completeAdd(args);
812 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
813 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
814 }
815 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700816 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
817 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800821
822 }
823
824 @Override
825 protected void onActivityResult(
826 final int requestCode, final int resultCode, final Intent data) {
827 handleActivityResult(requestCode, resultCode, data);
828 if (mLauncherCallbacks != null) {
829 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 }
832
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600833 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700834 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600835 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
837 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
838 View v = null;
839 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
840 if (layout != null) {
841 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
842 }
843 Intent intent = sPendingAddItem.intent;
844 sPendingAddItem = null;
845 if (grantResults.length > 0
846 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
847 startActivity(v, intent, null);
848 } else {
849 // TODO: Show a snack bar with link to settings
850 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
851 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
852 }
853 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 if (mLauncherCallbacks != null) {
855 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
856 grantResults);
857 }
858 }
859
Adam Cohendb364c32014-05-20 14:23:40 -0700860 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
861 appWidgetId, ItemInfo info) {
862 PendingAddArguments args = new PendingAddArguments();
863 args.requestCode = requestCode;
864 args.intent = data;
865 args.container = info.container;
866 args.screenId = info.screenId;
867 args.cellX = info.cellX;
868 args.cellY = info.cellY;
869 args.appWidgetId = appWidgetId;
870 return args;
871 }
872
873 /**
874 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
875 *
876 * @param screenId the screen id to check
877 * @return the new screen, or screenId if it exists
878 */
879 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800880 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700881 if (dropLayout == null) {
882 // it's possible that the add screen was removed because it was
883 // empty and a re-bind occurred
884 mWorkspace.addExtraEmptyScreen();
885 return mWorkspace.commitExtraEmptyScreen();
886 } else {
887 return screenId;
888 }
889 }
890
Adam Cohen091440a2015-03-18 14:16:05 -0700891 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800892 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 Runnable onCompleteRunnable = null;
894 int animationType = 0;
895
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 if (resultCode == RESULT_OK) {
898 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
899 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700900 mPendingAddWidgetInfo);
901 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 onCompleteRunnable = new Runnable() {
903 @Override
904 public void run() {
905 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
908 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
910 };
911 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800912 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 if (mDragLayer.getAnimatedView() != null) {
916 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
917 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
918 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700919 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 // The animated view may be null in the case of a rotation during widget configuration
921 onCompleteRunnable.run();
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 protected void onStop() {
927 super.onStop();
928 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700929
930 if (mLauncherCallbacks != null) {
931 mLauncherCallbacks.onStop();
932 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700933 }
934
935 @Override
936 protected void onStart() {
937 super.onStart();
938 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700939
940 if (mLauncherCallbacks != null) {
941 mLauncherCallbacks.onStart();
942 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 }
944
945 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200947 long startTime = 0;
948 if (DEBUG_RESUME_TIME) {
949 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400950 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200951 }
Adam Cohen9211d422014-10-07 18:14:53 -0700952
953 if (mLauncherCallbacks != null) {
954 mLauncherCallbacks.preOnResume();
955 }
956
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800958 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700959
Winson Chung4a2afa32012-07-19 14:53:05 -0700960 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700961 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800963 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700964 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700965 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700966 // view after launching an app, as they may be depending on the UI to be static to
967 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700968 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700969 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800970 } else if (mOnResumeState == State.WIDGETS) {
971 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700972 }
973 mOnResumeState = State.NONE;
974
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700975 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700976 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
977 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700978
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800979 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700980 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700981 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700982 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700984 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700986 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200987 // We might have postponed some bind calls until onResume (see waitUntilResume) --
988 // execute them here
989 long startTimeCallbacks = 0;
990 if (DEBUG_RESUME_TIME) {
991 startTimeCallbacks = System.currentTimeMillis();
992 }
993
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
995 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200998 if (DEBUG_RESUME_TIME) {
999 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1000 (System.currentTimeMillis() - startTimeCallbacks));
1001 }
Michael Jurka447bf842013-05-15 14:52:15 +02001002 }
Michael Jurka54554252013-08-01 12:52:23 +02001003 if (mOnResumeCallbacks.size() > 0) {
1004 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1005 mOnResumeCallbacks.get(i).run();
1006 }
1007 mOnResumeCallbacks.clear();
1008 }
Winson Chunge4e50662012-01-23 14:45:13 -08001009
1010 // Reset the pressed state of icons that were locked in the press state while activities
1011 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001012 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001013 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001014 mWaitingForResume.setStayPressed(false);
1015 }
Winson Chung82963d52013-10-09 11:20:57 -07001016
Adam Cohen06dff352012-06-01 17:17:08 -07001017 // It is possible that widgets can receive updates while launcher is not in the foreground.
1018 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1019 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1020 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001021 if (!isWorkspaceLoading()) {
1022 getWorkspace().reinflateWidgetsIfNecessary();
1023 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001024 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001025
Michael Jurka447bf842013-05-15 14:52:15 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1028 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001029
Adam Cohen4b66bf32015-09-18 12:15:19 -07001030 // We want to suppress callbacks about CustomContent being shown if we have just received
1031 // onNewIntent while the user was present within launcher. In that case, we post a call
1032 // to move the user to the main screen (which will occur after onResume). We don't want to
1033 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1034 // suppress here.
1035 if (mWorkspace.getCustomContentCallbacks() != null
1036 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001037 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001038 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001039 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1040 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001041 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001042 }
1043 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001044 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001045 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001046 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001047
Sunny Goyal756adbc2015-04-16 15:20:51 -07001048 if (!isWorkspaceLoading()) {
1049 // Process any items that were added while Launcher was away.
1050 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1051 }
Adam Cohen9211d422014-10-07 18:14:53 -07001052
1053 if (mLauncherCallbacks != null) {
1054 mLauncherCallbacks.onResume();
1055 }
Adam Cohen06dff352012-06-01 17:17:08 -07001056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001060 // Ensure that items added to Launcher are queued until Launcher returns
1061 InstallShortcutReceiver.enableInstallQueue();
1062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001064 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001065 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001066 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001067
1068 // We call onHide() aggressively. The custom content callbacks should be able to
1069 // debounce excess onHide calls.
1070 if (mWorkspace.getCustomContentCallbacks() != null) {
1071 mWorkspace.getCustomContentCallbacks().onHide();
1072 }
Romain Guycbb89e42009-06-08 15:52:54 -07001073
Adam Cohen9211d422014-10-07 18:14:53 -07001074 if (mLauncherCallbacks != null) {
1075 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078
1079 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001080 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1081 // by a onResume or by scrolling otherwise.
1082 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001083
1084 // Custom content is completely hidden
1085 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001086
1087 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1088 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001089
1090 // Indicates whether the user is allowed to scroll away from the custom content.
1091 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
Adam Cohenc2d6e892014-10-16 09:49:24 -07001094 public interface LauncherOverlay {
1095
1096 /**
1097 * Touch interaction leading to overscroll has begun
1098 */
1099 public void onScrollInteractionBegin();
1100
1101 /**
1102 * Touch interaction related to overscroll has ended
1103 */
1104 public void onScrollInteractionEnd();
1105
1106 /**
1107 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1108 * screen (or in the case of RTL, the rightmost screen).
1109 */
1110 public void onScrollChange(int progress, boolean rtl);
1111
1112 /**
1113 * Screen has stopped scrolling
1114 */
1115 public void onScrollSettled();
1116
1117 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001118 * Called when the launcher is ready to use the overlay
1119 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001120 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001121 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 }
1123
Jun Mukai8af0cd82015-05-12 12:32:12 -07001124 public interface LauncherSearchCallbacks {
1125 /**
1126 * Called when the search overlay is shown.
1127 */
1128 public void onSearchOverlayOpened();
1129
1130 /**
1131 * Called when the search overlay is dismissed.
1132 */
1133 public void onSearchOverlayClosed();
1134 }
1135
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 public interface LauncherOverlayCallbacks {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138 }
1139
1140 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1141
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 }
1143
Jorim Jaggid017f882014-01-14 17:08:48 -08001144 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001145 if (mLauncherCallbacks != null) {
1146 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001147 } else {
1148 // On devices with a locked orientation, we will at least have the allow rotation
1149 // setting.
1150 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001151 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001152 }
1153
Adam Cohen9211d422014-10-07 18:14:53 -07001154 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001155 CustomContentCallbacks callbacks, String description) {
1156 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001157 }
1158
Adam Cohenedb40762013-07-18 16:45:45 -07001159 // The custom content needs to offset its content to account for the QSB
1160 public int getTopOffsetForCustomContent() {
1161 return mWorkspace.getPaddingTop();
1162 }
1163
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001164 @Override
1165 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001167 if (mModel.isCurrentCallbacks(this)) {
1168 mModel.stopLoader();
1169 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001170 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1171
Romain Guy13c2e7b2010-03-10 19:45:00 -08001172 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001173 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001176 @Override
1177 public void onWindowFocusChanged(boolean hasFocus) {
1178 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001179 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001180
1181 if (mLauncherCallbacks != null) {
1182 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1183 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 private boolean acceptFilter() {
1187 final InputMethodManager inputManager = (InputMethodManager)
1188 getSystemService(Context.INPUT_METHOD_SERVICE);
1189 return !inputManager.isFullscreenMode();
1190 }
1191
1192 @Override
1193 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001194 final int uniChar = event.getUnicodeChar();
1195 final boolean handled = super.onKeyDown(keyCode, event);
1196 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1197 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1199 keyCode, event);
1200 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001201 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001202 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 // showSearchDialog()
1204 // If there are multiple keystrokes before the search dialog takes focus,
1205 // onSearchRequested() will be called for every keystroke,
1206 // but it is idempotent, so it's fine.
1207 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
1209 }
1210
Joe Onorato8a9625e2010-01-28 15:55:35 -08001211 // Eat the long press event so the keyboard doesn't come up.
1212 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1213 return true;
1214 }
1215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 return handled;
1217 }
1218
Winson46163472015-09-22 17:31:56 -07001219 @Override
1220 public boolean onKeyUp(int keyCode, KeyEvent event) {
1221 if (keyCode == KeyEvent.KEYCODE_MENU) {
1222 // Ignore the menu key if we are currently dragging or are on the custom content screen
1223 if (!isOnCustomContent() && !mDragController.isDragging()) {
1224 // Close any open folders
1225 closeFolder();
1226
1227 // Stop resizing any widgets
1228 mWorkspace.exitWidgetResizeMode();
1229
1230 // Show the overview mode if we are on the workspace
1231 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1232 !mWorkspace.isSwitchingState()) {
1233 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001234 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001235 }
1236 }
1237 return true;
1238 }
1239 return super.onKeyUp(keyCode, event);
1240 }
1241
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 private String getTypedText() {
1243 return mDefaultKeySsb.toString();
1244 }
1245
1246 private void clearTypedText() {
1247 mDefaultKeySsb.clear();
1248 mDefaultKeySsb.clearSpans();
1249 Selection.setSelection(mDefaultKeySsb, 0);
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001253 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1254 * State
1255 */
1256 private static State intToState(int stateOrdinal) {
1257 State state = State.WORKSPACE;
1258 final State[] stateValues = State.values();
1259 for (int i = 0; i < stateValues.length; i++) {
1260 if (stateValues[i].ordinal() == stateOrdinal) {
1261 state = stateValues[i];
1262 break;
1263 }
1264 }
1265 return state;
1266 }
1267
1268 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 * Restores the previous state, if it exists.
1270 *
1271 * @param savedState The previous state.
1272 */
1273 private void restoreState(Bundle savedState) {
1274 if (savedState == null) {
1275 return;
1276 }
1277
Michael Jurka883f55b2010-10-21 15:47:14 -07001278 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001279 if (state == State.APPS || state == State.WIDGETS) {
1280 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001281 }
1282
Adam Cohen21cd0022013-10-09 18:57:02 -07001283 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1284 PagedView.INVALID_RESTORE_PAGE);
1285 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001286 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288
Sunny Goyal756cd262015-08-20 12:33:21 -07001289 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1290 if (itemValues != null) {
1291 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001292 AppWidgetProviderInfo info = savedState.getParcelable(
1293 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001294 mPendingAddWidgetInfo = info == null ?
1295 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1296
Adam Cohen4637b5a2013-11-04 18:21:24 -08001297 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001298 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 mRestoring = true;
1300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
1303 /**
1304 * Finds all the views we need and configure them properly.
1305 */
1306 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001307 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001308
Craig Mautner360310b2012-10-26 15:13:08 -07001309 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001310 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001311 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1312 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001313 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001314 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001315
John Spurlock77e1f472013-09-11 10:09:51 -04001316 mLauncherView.setSystemUiVisibility(
1317 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001318 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the drag layer
1321 mDragLayer.setup(this, dragController);
1322
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 // Setup the hotseat
1324 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1325 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001326 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 }
1328
Winsone9f27272015-10-13 10:47:51 -07001329 // Setup the overview panel
1330 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001331
Winson Chung4c98d922011-05-31 16:50:48 -07001332 // Setup the workspace
1333 mWorkspace.setHapticFeedbackEnabled(false);
1334 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001335 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001336 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001337
Tony Wickham34d2c912015-09-11 09:27:58 -07001338 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001339 mSearchDropTargetBar = (SearchDropTargetBar)
1340 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001341 // Get the app info bar
1342 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1343 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001344
Winson Chungef7f8742015-06-04 17:18:17 -07001345 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001346 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001347 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001348 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1349 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1350 } else {
1351 mAppsView.setSearchBarController(new DefaultAppSearchController());
1352 }
Craig Mautner360310b2012-10-26 15:13:08 -07001353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001355 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001356 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001357 dragController.setMoveTarget(mWorkspace);
1358 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001359 if (mSearchDropTargetBar != null) {
1360 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001361 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001362 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001363 if (mAppInfoDropTargetBar != null) {
1364 mAppInfoDropTargetBar.setup(this, dragController);
1365 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001366
Sunny Goyal322d5562015-06-25 19:35:49 -07001367 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1368 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
Winsone9f27272015-10-13 10:47:51 -07001372 private void setupOverviewPanel() {
1373 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1374
1375 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1376 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1377 @Override
1378 public boolean onLongClick(View v) {
1379 return v.performClick();
1380 }
1381 };
1382
1383 // Bind wallpaper button actions
1384 View wallpaperButton = findViewById(R.id.wallpaper_button);
1385 wallpaperButton.setOnClickListener(new OnClickListener() {
1386 @Override
1387 public void onClick(View view) {
1388 if (!mWorkspace.isSwitchingState()) {
1389 onClickWallpaperPicker(view);
1390 }
1391 }
1392 });
1393 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1394 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395
1396 // Bind widget button actions
1397 mWidgetsButton = findViewById(R.id.widget_button);
1398 mWidgetsButton.setOnClickListener(new OnClickListener() {
1399 @Override
1400 public void onClick(View view) {
1401 if (!mWorkspace.isSwitchingState()) {
1402 onClickAddWidgetButton(view);
1403 }
1404 }
1405 });
1406 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1407 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1408
1409 // Bind settings actions
1410 View settingsButton = findViewById(R.id.settings_button);
1411 boolean hasSettings = hasSettings();
1412 if (hasSettings) {
1413 settingsButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickSettingsButton(view);
1418 }
1419 }
1420 });
1421 settingsButton.setOnLongClickListener(performClickOnLongClick);
1422 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423 } else {
1424 settingsButton.setVisibility(View.GONE);
1425 }
1426
1427 mOverviewPanel.setAlpha(0f);
1428 }
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001431 * Sets the all apps button. This method is called from {@link Hotseat}.
1432 */
1433 public void setAllAppsButton(View allAppsButton) {
1434 mAllAppsButton = allAppsButton;
1435 }
1436
1437 public View getAllAppsButton() {
1438 return mAllAppsButton;
1439 }
1440
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001441 public View getWidgetsButton() {
1442 return mWidgetsButton;
1443 }
1444
Anjali Koppal5ad44842014-03-10 20:34:39 -07001445 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 * Creates a view representing a shortcut.
1447 *
1448 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001450 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001451 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
1454 /**
1455 * Creates a view representing a shortcut inflated from the specified resource.
1456 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 * @param parent The group the shortcut belongs to.
1458 * @param info The data structure describing the shortcut.
1459 *
1460 * @return A View inflated from layoutResId.
1461 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001462 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001463 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001464 parent, false);
1465 favorite.applyFromShortcutInfo(info, mIconCache);
1466 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001468 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 return favorite;
1470 }
1471
1472 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * Add a shortcut to the workspace.
1474 *
1475 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 int cellY) {
1479 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001480 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Sunny Goyal5c97f512015-05-19 16:03:28 -07001482 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001483 if (info == null) {
1484 return;
1485 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001486 final View view = createShortcut(info);
1487
Sunny Goyal5c97f512015-05-19 16:03:28 -07001488 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001489 // First we check if we already know the exact location where we want to add this item.
1490 if (cellX >= 0 && cellY >= 0) {
1491 cellXY[0] = cellX;
1492 cellXY[1] = cellY;
1493 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001494
1495 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001496 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001497 true, null,null)) {
1498 return;
1499 }
1500 DragObject dragObject = new DragObject();
1501 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001502 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1503 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001504 return;
1505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001506 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001508 }
1509
1510 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001511 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 return;
1513 }
1514
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001515 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
1517 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521 }
1522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001526 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 */
Adam Cohen091440a2015-03-18 14:16:05 -07001528 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001529 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1530
1531 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001533 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1534 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Adam Cohen59400422014-03-05 18:07:04 -08001537 if (appWidgetInfo.isCustomWidget) {
1538 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001539 }
1540
Adam Cohen59400422014-03-05 18:07:04 -08001541 LauncherAppWidgetInfo launcherInfo;
1542 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1543 launcherInfo.spanX = info.spanX;
1544 launcherInfo.spanY = info.spanY;
1545 launcherInfo.minSpanX = info.minSpanX;
1546 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001547 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 if (hostView == null) {
1554 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001555 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1556 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001557 } else {
1558 // The AppWidgetHostView has already been inflated and instantiated
1559 launcherInfo.hostView = hostView;
1560 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001562 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001564 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001567 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1568 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1569 item.hostView.setTag(item);
1570 item.onBindAppWidget(this);
1571
1572 item.hostView.setFocusable(true);
1573 item.hostView.setOnFocusChangeListener(mFocusHandler);
1574
1575 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1576 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1577
1578 if (!item.isCustomWidget()) {
1579 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1580 }
1581 }
1582
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1584 @Override
1585 public void onReceive(Context context, Intent intent) {
1586 final String action = intent.getAction();
1587 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1588 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001589 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001590 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001591
Winson Chungc100e8e2011-08-09 16:02:43 -07001592 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001593 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001594 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001595 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001596 if (!showWorkspace(false)) {
1597 // If we are already on the workspace, then manually reset all apps
1598 mAppsView.reset();
1599 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1602 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001603 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001604 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1605 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001606 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001607 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1608 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1609 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001610 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001611 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1612 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 }
1614 }
1615 };
1616
1617 @Override
1618 public void onAttachedToWindow() {
1619 super.onAttachedToWindow();
1620
1621 // Listen for broadcasts related to user-presence
1622 final IntentFilter filter = new IntentFilter();
1623 filter.addAction(Intent.ACTION_SCREEN_OFF);
1624 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001625 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 if (ENABLE_DEBUG_INTENTS) {
1627 filter.addAction(DebugIntents.DELETE_DATABASE);
1628 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1629 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001631 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001632 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 mVisible = true;
1634 }
1635
1636 @Override
1637 public void onDetachedFromWindow() {
1638 super.onDetachedFromWindow();
1639 mVisible = false;
1640
Adam Cohend113e0c2010-11-11 10:48:05 -08001641 if (mAttached) {
1642 unregisterReceiver(mReceiver);
1643 mAttached = false;
1644 }
Winson Chungb745afb2015-03-02 11:51:23 -08001645 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 }
1647
1648 public void onWindowVisibilityChanged(int visibility) {
1649 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001650 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001651 // The following code used to be in onResume, but it turns out onResume is called when
1652 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1653 // is a more appropriate event to handle
1654 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001655 if (!mWorkspaceLoading) {
1656 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001657 // We want to let Launcher draw itself at least once before we force it to build
1658 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001659 // apps is nice and speedy.
1660 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001661 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001662 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001663 if (mStarted) return;
1664 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001665 // We delay the layer building a bit in order to give
1666 // other message processing a time to run. In particular
1667 // this avoids a delay in hiding the IME if it was
1668 // currently shown, because doing that may involve
1669 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001670 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001671 final ViewTreeObserver.OnDrawListener listener = this;
1672 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001673 public void run() {
1674 if (mWorkspace != null &&
1675 mWorkspace.getViewTreeObserver() != null) {
1676 mWorkspace.getViewTreeObserver().
1677 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001679 }
1680 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001681 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001682 }
1683 });
1684 }
1685 clearTypedText();
1686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 }
1688
Adam Cohen091440a2015-03-18 14:16:05 -07001689 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 mHandler.removeMessages(ADVANCE_MSG);
1691 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1692 mHandler.sendMessageDelayed(msg, delay);
1693 mAutoAdvanceSentTime = System.currentTimeMillis();
1694 }
1695
Adam Cohen091440a2015-03-18 14:16:05 -07001696 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1698 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1699 mAutoAdvanceRunning = autoAdvanceRunning;
1700 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001701 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 sendAdvanceMessage(delay);
1703 } else {
1704 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001705 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1707 }
1708 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001709 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 }
1711 }
1712 }
1713
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001714 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1715
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001717 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 if (msg.what == ADVANCE_MSG) {
1719 int i = 0;
1720 for (View key: mWidgetsToAdvance.keySet()) {
1721 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001722 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 public void run() {
1726 ((Advanceable) v).advance();
1727 }
1728 }, delay);
1729 }
1730 i++;
1731 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001732 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001737
Winsonc0b52fe2015-09-09 16:38:15 -07001738 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001739 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1741 if (v instanceof Advanceable) {
1742 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001743 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001744 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746 }
1747
Winsonc0b52fe2015-09-09 16:38:15 -07001748 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 if (mWidgetsToAdvance.containsKey(hostView)) {
1750 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001751 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001753 }
1754
Hyunyoung Song3f471442015-04-08 19:01:34 -07001755 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001756 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1757 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
1759
Winson Chunga6945242014-01-08 14:04:34 -08001760 public DragLayer getDragLayer() {
1761 return mDragLayer;
1762 }
1763
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001764 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001765 return mAppsView;
1766 }
1767
Hyunyoung Song3f471442015-04-08 19:01:34 -07001768 public WidgetsContainerView getWidgetsView() {
1769 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001770 }
1771
Winson Chunga6945242014-01-08 14:04:34 -08001772 public Workspace getWorkspace() {
1773 return mWorkspace;
1774 }
1775
1776 public Hotseat getHotseat() {
1777 return mHotseat;
1778 }
1779
Jorim Jaggid017f882014-01-14 17:08:48 -08001780 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001781 return mOverviewPanel;
1782 }
1783
Winson Chung006ee262015-08-03 14:40:11 -07001784 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001785 return mSearchDropTargetBar;
1786 }
1787
Tony Wickham34d2c912015-09-11 09:27:58 -07001788 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1789 return mAppInfoDropTargetBar;
1790 }
1791
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001792 public LauncherAppWidgetHost getAppWidgetHost() {
1793 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
Romain Guycbb89e42009-06-08 15:52:54 -07001795
Winson Chunga9abd0e2010-10-27 17:18:37 -07001796 public LauncherModel getModel() {
1797 return mModel;
1798 }
1799
Winson Chunga6945242014-01-08 14:04:34 -08001800 protected SharedPreferences getSharedPrefs() {
1801 return mSharedPrefs;
1802 }
1803
Adam Cohen2e6da152015-05-06 11:42:25 -07001804 public DeviceProfile getDeviceProfile() {
1805 return mDeviceProfile;
1806 }
1807
Bjorn Bringertc459e522013-06-07 19:36:01 +01001808 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001809 getWindow().closeAllPanels();
1810
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001811 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001812 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001813 }
1814
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 @Override
1816 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001817 long startTime = 0;
1818 if (DEBUG_RESUME_TIME) {
1819 startTime = System.currentTimeMillis();
1820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 super.onNewIntent(intent);
1822
1823 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001824 Folder openFolder = mWorkspace.getOpenFolder();
1825 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1826 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1827 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1828 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1829 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001830 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001832
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 if (mWorkspace == null) {
1834 // Can be cases where mWorkspace is null, this prevents a NPE
1835 return;
1836 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001837 // In all these cases, only animate if we're already on home
1838 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001839
Sunny Goyal935fca12015-10-13 10:19:01 -07001840 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 exitSpringLoadedDragMode();
1842
1843 // If we are already on home, then just animate back to the workspace,
1844 // otherwise, just wait until onResume to set the state back to Workspace
1845 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001846 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 } else {
1848 mOnResumeState = State.WORKSPACE;
1849 }
1850
1851 final View v = getWindow().peekDecorView();
1852 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001853 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 INPUT_METHOD_SERVICE);
1855 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1856 }
1857
Winson Chungb745afb2015-03-02 11:51:23 -08001858 // Reset the apps view
1859 if (!alreadyOnHome && mAppsView != null) {
1860 mAppsView.scrollToTop();
1861 }
1862
Hyunyoung Song3f471442015-04-08 19:01:34 -07001863 // Reset the widgets view
1864 if (!alreadyOnHome && mWidgetsView != null) {
1865 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001867
Adam Cohen9211d422014-10-07 18:14:53 -07001868 if (mLauncherCallbacks != null) {
1869 mLauncherCallbacks.onHomeIntent();
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Adam Cohened307df2013-10-02 09:37:31 -07001872
Adam Cohen9211d422014-10-07 18:14:53 -07001873 if (mLauncherCallbacks != null) {
1874 mLauncherCallbacks.onNewIntent(intent);
1875 }
Winson15f8b172015-08-19 11:02:39 -07001876
1877 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1878 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1879 // animation.
1880 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001881 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1882 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001883 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1884 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001885
1886 // We use this flag to suppress noisy callbacks above custom content state
1887 // from onResume.
1888 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001889 mWorkspace.post(new Runnable() {
1890 @Override
1891 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001892 if (mWorkspace != null) {
1893 mWorkspace.moveToDefaultScreen(true);
1894 }
Winson15f8b172015-08-19 11:02:39 -07001895 }
1896 });
1897 }
1898 }
1899
1900 if (DEBUG_RESUME_TIME) {
1901 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903 }
1904
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001906 public void onRestoreInstanceState(Bundle state) {
1907 super.onRestoreInstanceState(state);
1908 for (int page: mSynchronouslyBoundPages) {
1909 mWorkspace.restoreInstanceStateForChild(page);
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912
1913 @Override
1914 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001915 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001916 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1917 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001918 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001919 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920
Michael Jurka883f55b2010-10-21 15:47:14 -07001921 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001922 Folder openFolder = mWorkspace.getOpenFolder();
1923 if (openFolder != null) {
1924 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001929 ContentValues itemValues = new ContentValues();
1930 mPendingAddInfo.writeToValues(itemValues);
1931 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001932 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001933 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935
Hyunyoung Song3f471442015-04-08 19:01:34 -07001936 // Save the current widgets tray?
1937 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001938
1939 if (mLauncherCallbacks != null) {
1940 mLauncherCallbacks.onSaveInstanceState(outState);
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
1944 @Override
1945 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Winson Chunge7a03942011-08-05 15:05:12 -07001948 // Remove all pending runnables
1949 mHandler.removeMessages(ADVANCE_MSG);
1950 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001951 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001952
Winson Chungcd2b0142011-06-08 16:02:26 -07001953 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001954 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001955
1956 // It's possible to receive onDestroy after a new Launcher activity has
1957 // been created. In this case, don't interfere with the new Launcher.
1958 if (mModel.isCurrentCallbacks(this)) {
1959 mModel.stopLoader();
1960 app.setLauncher(null);
1961 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001964 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001966 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001968 mAppWidgetHost = null;
1969
1970 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
1972 TextKeyListener.getInstance().release();
1973
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001974 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001975
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001976 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001977 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001978 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001979 mWorkspace = null;
1980 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001981
Michael Jurka2ecf9952012-06-18 12:52:28 -07001982 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001983
1984 if (mLauncherCallbacks != null) {
1985 mLauncherCallbacks.onDestroy();
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988
Adam Cohena9cf38f2011-05-02 15:36:58 -07001989 public DragController getDragController() {
1990 return mDragController;
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
1994 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001995 onStartForResult(requestCode);
1996 super.startActivityForResult(intent, requestCode);
1997 }
1998
1999 @Override
2000 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2001 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2002 onStartForResult(requestCode);
2003 try {
2004 super.startIntentSenderForResult(intent, requestCode,
2005 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2006 } catch (IntentSender.SendIntentException e) {
2007 throw new ActivityNotFoundException();
2008 }
2009 }
2010
2011 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002012 if (requestCode >= 0) {
2013 setWaitingForResult(true);
2014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
Winson Chung70d72102011-08-12 11:24:35 -07002017 /**
2018 * Indicates that we want global search for this activity by setting the globalSearch
2019 * argument for {@link #startSearch} to true.
2020 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002022 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002024
Karl Rosaen138a0412009-04-23 19:00:21 -07002025 if (initialQuery == null) {
2026 // Use any text typed in the launcher as the initial query
2027 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 if (appSearchData == null) {
2030 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002031 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
Winson Chungadf0c182012-08-23 14:59:07 -07002033 Rect sourceBounds = new Rect();
2034 if (mSearchDropTargetBar != null) {
2035 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2036 }
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Ian Parkinson047036e2014-09-01 15:40:53 +01002038 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002039 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002040 if (clearTextImmediately) {
2041 clearTypedText();
2042 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002043
2044 // We need to show the workspace after starting the search
2045 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002046 }
2047
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 /**
2049 * Start a text search.
2050 *
2051 * @return {@code true} if the search will start immediately, so any further keypresses
2052 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2053 * to buffer keypresses.
2054 */
2055 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002057 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2058 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2059 sourceBounds);
2060 }
2061
Michael Jurkaa33411c2012-06-14 16:18:21 -07002062 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002064 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002065 }
2066
2067 /**
2068 * Starts the global search activity. This code is a copied from SearchManager
2069 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002072 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2074 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2075 if (globalSearchActivity == null) {
2076 Log.w(TAG, "No global search activity found.");
2077 return;
2078 }
2079 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2080 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2081 intent.setComponent(globalSearchActivity);
2082 // Make sure that we have a Bundle to put source in
2083 if (appSearchData == null) {
2084 appSearchData = new Bundle();
2085 } else {
2086 appSearchData = new Bundle(appSearchData);
2087 }
Adam Cohen9211d422014-10-07 18:14:53 -07002088 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 if (!appSearchData.containsKey("source")) {
2090 appSearchData.putString("source", getPackageName());
2091 }
2092 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2093 if (!TextUtils.isEmpty(initialQuery)) {
2094 intent.putExtra(SearchManager.QUERY, initialQuery);
2095 }
2096 if (selectInitialQuery) {
2097 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2098 }
2099 intent.setSourceBounds(sourceBounds);
2100 try {
2101 startActivity(intent);
2102 } catch (ActivityNotFoundException ex) {
2103 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106
Yura4f93ec62014-02-04 14:15:21 +00002107 public boolean isOnCustomContent() {
2108 return mWorkspace.isOnOrMovingToCustomContent();
2109 }
2110
Winson Chung70d72102011-08-12 11:24:35 -07002111 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002112 public boolean onPrepareOptionsMenu(Menu menu) {
2113 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002114 if (mLauncherCallbacks != null) {
2115 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2116 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002117 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002120 @Override
2121 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002122 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002123 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002124 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002125 }
2126
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 public boolean isWorkspaceLocked() {
2128 return mWorkspaceLoading || mWaitingForResult;
2129 }
2130
Adam Cohen517a7f52014-03-01 12:12:59 -08002131 public boolean isWorkspaceLoading() {
2132 return mWorkspaceLoading;
2133 }
2134
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002135 private void setWorkspaceLoading(boolean value) {
2136 boolean isLocked = isWorkspaceLocked();
2137 mWorkspaceLoading = value;
2138 if (isLocked != isWorkspaceLocked()) {
2139 onWorkspaceLockedChanged();
2140 }
2141 }
2142
2143 private void setWaitingForResult(boolean value) {
2144 boolean isLocked = isWorkspaceLocked();
2145 mWaitingForResult = value;
2146 if (isLocked != isWorkspaceLocked()) {
2147 onWorkspaceLockedChanged();
2148 }
2149 }
2150
Adam Cohen9211d422014-10-07 18:14:53 -07002151 protected void onWorkspaceLockedChanged() {
2152 if (mLauncherCallbacks != null) {
2153 mLauncherCallbacks.onWorkspaceLockedChanged();
2154 }
2155 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002156
Michael Jurka0280c3b2010-09-17 15:00:07 -07002157 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2161 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002162 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002164
Tony Wickhama0628cc2015-10-14 15:23:04 -07002165 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002166 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002167 if (LOGD) {
2168 Log.d(TAG, "Adding widget from drop");
2169 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002170 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2171 }
2172
Sunny Goyale6b63a32015-01-16 16:14:29 -08002173 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002174 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2175 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002176 if (appWidgetInfo.configure != null) {
2177 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002178 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002179
Bjorn Bringert7984c942009-12-09 15:38:25 +00002180 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002181 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2182 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002185 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002186 Runnable onComplete = new Runnable() {
2187 @Override
2188 public void run() {
2189 // Exit spring loaded mode if necessary after adding the widget
2190 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2191 null);
2192 }
2193 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002194 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002196 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198 }
Romain Guycbb89e42009-06-08 15:52:54 -07002199
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002200 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002201 // Close any folders that may be open.
2202 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002203 mWorkspace.moveToCustomContentScreen(animate);
2204 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002205
2206 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2207 int[] cell, int spanX, int spanY) {
2208 switch (info.itemType) {
2209 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2210 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2211 int span[] = new int[2];
2212 span[0] = spanX;
2213 span[1] = spanY;
2214 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2215 container, screenId, cell, span);
2216 break;
2217 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2218 processShortcutFromDrop(info.componentName, container, screenId, cell);
2219 break;
2220 default:
2221 throw new IllegalStateException("Unknown item type: " + info.itemType);
2222 }
2223 }
2224
Adam Cohenfbba09b2011-07-18 21:43:05 -07002225 /**
2226 * Process a shortcut drop.
2227 *
2228 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002230 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002231 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002232 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2233 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237
2238 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 mPendingAddInfo.cellX = cell[0];
2240 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242
2243 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2244 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002245 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 }
2247
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 /**
2249 * Process a widget drop.
2250 *
2251 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002255 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2256 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 mPendingAddInfo.minSpanX = info.minSpanX;
2261 mPendingAddInfo.minSpanY = info.minSpanY;
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.cellX = cell[0];
2265 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 if (span != null) {
2268 mPendingAddInfo.spanX = span[0];
2269 mPendingAddInfo.spanY = span[1];
2270 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271
Adam Cohened66b2b2012-01-23 17:28:51 -08002272 AppWidgetHostView hostView = info.boundWidget;
2273 int appWidgetId;
2274 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002275 // In the case where we've prebound the widget, we remove it from the DragLayer
2276 if (LOGD) {
2277 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2278 }
2279 getDragLayer().removeView(hostView);
2280
Adam Cohened66b2b2012-01-23 17:28:51 -08002281 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002282 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002283
2284 // Clear the boundWidget so that it doesn't get destroyed.
2285 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002286 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002287 // In this case, we either need to start an activity to get permission to bind
2288 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002290 Bundle options = info.bindOptions;
2291
Sunny Goyalffe83f12014-08-14 17:39:34 -07002292 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2293 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002294 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002295 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002298 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2299 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2300 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002301 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2302 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002303 // TODO: we need to make sure that this accounts for the options bundle.
2304 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002305 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2306 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 }
2309
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002312 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 folderInfo.title = getText(R.string.folder_name);
2314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002316 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2317 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002318 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319
2320 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 FolderIcon newFolder =
2322 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002325 // Force measure the new folder icon
2326 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2327 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002328 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 }
Romain Guycbb89e42009-06-08 15:52:54 -07002330
Winsonc0b52fe2015-09-09 16:38:15 -07002331 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002332 * Unbinds the view for the specified item, and removes the item and all its children.
2333 *
2334 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002335 * @param itemInfo the {@link ItemInfo} for this view.
2336 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002337 */
Winson2949fb52015-09-24 09:56:11 -07002338 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002339 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002340 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002341 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2342 if (folderInfo != null) {
2343 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002344 } else {
2345 mWorkspace.removeWorkspaceItem(v);
2346 }
Winsonc0b52fe2015-09-09 16:38:15 -07002347 if (deleteFromDb) {
2348 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2349 }
2350 } else if (itemInfo instanceof FolderInfo) {
2351 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2352 unbindFolder(folderInfo);
2353 mWorkspace.removeWorkspaceItem(v);
2354 if (deleteFromDb) {
2355 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2356 }
2357 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2358 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002359 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002360 removeWidgetToAutoAdvance(widgetInfo.hostView);
2361 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002362 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002363 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002364 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002365
Winsonc0b52fe2015-09-09 16:38:15 -07002366 } else {
2367 return false;
2368 }
2369 return true;
2370 }
2371
2372 /**
2373 * Unbinds any launcher references to the folder.
2374 */
2375 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002376 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002377 }
2378
Winsonc0b52fe2015-09-09 16:38:15 -07002379 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002381 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002383 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002385 // Deleting an app widget ID is a void call but writes to disk before returning
2386 // to the caller...
2387 new AsyncTask<Void, Void, Void>() {
2388 public Void doInBackground(Void ... args) {
2389 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2390 return null;
2391 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002392 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002393 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002395 }
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 @Override
2398 public boolean dispatchKeyEvent(KeyEvent event) {
2399 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2400 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002402 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002403 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002404 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002405 dumpState();
2406 return true;
2407 }
2408 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002409 }
2410 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2411 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002412 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 return true;
2414 }
2415 }
2416
2417 return super.dispatchKeyEvent(event);
2418 }
2419
Joe Onorato88ec0992009-11-19 13:16:06 -08002420 @Override
2421 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002422 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2423 return;
2424 }
2425
Sunny Goyal45478022015-06-08 16:52:41 -07002426 if (mDragController.isDragging()) {
2427 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002428 return;
2429 }
2430
Winson Chungb745afb2015-03-02 11:51:23 -08002431 if (isAppsViewVisible()) {
2432 showWorkspace(true);
2433 } else if (isWidgetsViewVisible()) {
2434 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002435 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002436 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002437 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002438 Folder openFolder = mWorkspace.getOpenFolder();
2439 if (openFolder.isEditingName()) {
2440 openFolder.dismissEditingName();
2441 } else {
2442 closeFolder();
2443 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002444 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002445 mWorkspace.exitWidgetResizeMode();
2446
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002447 // Back button is a no-op here, but give at least some feedback for the button press
2448 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002449 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002453 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002454 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002455 @Override
2456 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002457 if (mAppWidgetHost != null) {
2458 mAppWidgetHost.startListening();
2459 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002460
2461 // Recreate the QSB, as the widget has been reset.
2462 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002463 }
2464
2465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 * Launches the intent referred by the clicked shortcut.
2467 *
2468 * @param v The view representing the clicked shortcut.
2469 */
2470 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002471 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2472 // view has detached (it's possible for this to happen if the view is removed mid touch).
2473 if (v.getWindowToken() == null) {
2474 return;
2475 }
2476
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002477 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 return;
2479 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002480
Adam Cohen1697b792013-09-17 19:08:21 -07002481 if (v instanceof Workspace) {
2482 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002483 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
2485 return;
2486 }
2487
2488 if (v instanceof CellLayout) {
2489 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002490 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2491 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002493 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
2495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002497 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002503 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002505 } else if (tag instanceof AppInfo) {
2506 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002507 } else if (tag instanceof LauncherAppWidgetInfo) {
2508 if (v instanceof PendingAppWidgetHostView) {
2509 onClickPendingWidget((PendingAppWidgetHostView) v);
2510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 }
2512 }
2513
Sunny Goyal70660032015-05-14 00:07:08 -07002514 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002515 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002516 return false;
2517 }
2518
Michael Jurkaaf442092010-06-10 17:01:57 -07002519 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002520 * Event handler for the app widget view which has not fully restored.
2521 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002522 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002523 if (mIsSafeModeEnabled) {
2524 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2525 return;
2526 }
2527
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002528 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002529 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002530 int widgetId = info.appWidgetId;
2531 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2532 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002533 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2534 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002535 mPendingAddInfo.copyFrom(info);
2536 mPendingAddWidgetId = widgetId;
2537
Sunny Goyalffe83f12014-08-14 17:39:34 -07002538 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2539 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002540 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002541 } else if (info.installProgress < 0) {
2542 // The install has not been queued
2543 final String packageName = info.providerName.getPackageName();
2544 showBrokenAppInstallDialog(packageName,
2545 new DialogInterface.OnClickListener() {
2546 public void onClick(DialogInterface dialog, int id) {
2547 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2548 }
2549 });
2550 } else {
2551 // Download has started.
2552 final String packageName = info.providerName.getPackageName();
2553 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 }
2555 }
2556
2557 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002558 * Event handler for the "grid" button that appears on the home screen, which
2559 * enters all apps mode.
2560 *
2561 * @param v The view that was clicked.
2562 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002563 protected void onClickAllAppsButton(View v) {
2564 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002565 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002566 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002567 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002568
2569 if (mLauncherCallbacks != null) {
2570 mLauncherCallbacks.onClickAllAppsButton(v);
2571 }
Winson Chung76648c52015-07-10 14:33:23 -07002572 }
2573 }
2574
2575 protected void onLongClickAllAppsButton(View v) {
2576 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2577 if (!isAppsViewVisible()) {
2578 showAppsView(true /* animated */, false /* resetListToTop */,
2579 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 }
2581 }
2582
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 private void showBrokenAppInstallDialog(final String packageName,
2584 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002585 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 .setTitle(R.string.abandoned_promises_title)
2587 .setMessage(R.string.abandoned_promise_explanation)
2588 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2589 .setNeutralButton(R.string.abandoned_clean_this,
2590 new DialogInterface.OnClickListener() {
2591 public void onClick(DialogInterface dialog, int id) {
2592 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2593 mWorkspace.removeAbandonedPromise(packageName, user);
2594 }
2595 })
2596 .create().show();
2597 return;
2598 }
2599
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 /**
2601 * Event handler for an app shortcut click.
2602 *
2603 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2604 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002605 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2607 Object tag = v.getTag();
2608 if (!(tag instanceof ShortcutInfo)) {
2609 throw new IllegalArgumentException("Input must be a Shortcut");
2610 }
2611
2612 // Open shortcut
2613 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002614
2615 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002616 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2617 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002618 // Launch activity anyway, framework will tell the user why the app is suspended.
2619 } else {
2620 int error = R.string.activity_not_available;
2621 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2622 error = R.string.safemode_shortcut_error;
2623 }
2624 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2625 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002626 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002627 }
2628
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002630 if ((v instanceof BubbleTextView)
2631 && shortcut.isPromise()
2632 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002634 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002637 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002644 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002645
2646 if (mLauncherCallbacks != null) {
2647 mLauncherCallbacks.onClickAppShortcut(v);
2648 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 }
2650
Adam Cohen091440a2015-03-18 14:16:05 -07002651 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002653 final ShortcutInfo shortcut;
2654 final Intent intent;
2655 if (tag instanceof ShortcutInfo) {
2656 shortcut = (ShortcutInfo) tag;
2657 intent = shortcut.intent;
2658 int[] pos = new int[2];
2659 v.getLocationOnScreen(pos);
2660 intent.setSourceBounds(new Rect(pos[0], pos[1],
2661 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002662
Sunny Goyal508da152014-08-14 10:53:27 -07002663 } else if (tag instanceof AppInfo) {
2664 shortcut = null;
2665 intent = ((AppInfo) tag).intent;
2666 } else {
2667 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2668 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669
2670 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002671 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672
2673 if (success && v instanceof BubbleTextView) {
2674 mWaitingForResume = (BubbleTextView) v;
2675 mWaitingForResume.setStayPressed(true);
2676 }
2677 }
2678
2679 /**
2680 * Event handler for a folder icon click.
2681 *
2682 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2683 */
2684 protected void onClickFolderIcon(View v) {
2685 if (LOGD) Log.d(TAG, "onClickFolder");
2686 if (!(v instanceof FolderIcon)){
2687 throw new IllegalArgumentException("Input must be a FolderIcon");
2688 }
2689
2690 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002691 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002693 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694 }
Adam Cohen9211d422014-10-07 18:14:53 -07002695
2696 if (mLauncherCallbacks != null) {
2697 mLauncherCallbacks.onClickFolderIcon(v);
2698 }
Michael Jurka5130e402011-10-13 04:55:35 -07002699 }
2700
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002701 /**
2702 * Event handler for the (Add) Widgets button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002707 if (mIsSafeModeEnabled) {
2708 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2709 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002710 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002711 if (mLauncherCallbacks != null) {
2712 mLauncherCallbacks.onClickAddWidgetButton(view);
2713 }
Adam Cohen9211d422014-10-07 18:14:53 -07002714 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002715 }
2716
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 /**
2718 * Event handler for the wallpaper picker button that appears after a long press
2719 * on the home screen.
2720 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002721 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002723 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2724 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2725 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002726 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002727 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002728
2729 if (mLauncherCallbacks != null) {
2730 mLauncherCallbacks.onClickWallpaperPicker(v);
2731 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 }
2733
2734 /**
2735 * Event handler for a click on the settings button that appears after a long press
2736 * on the home screen.
2737 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002738 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002740 if (mLauncherCallbacks != null) {
2741 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002742 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002743 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002744 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 }
2746
Adam Cohen61f560d2013-09-30 15:58:20 -07002747 public View.OnTouchListener getHapticFeedbackTouchListener() {
2748 if (mHapticFeedbackTouchListener == null) {
2749 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002750 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002751 @Override
2752 public boolean onTouch(View v, MotionEvent event) {
2753 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2754 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2755 }
2756 return false;
2757 }
2758 };
2759 }
2760 return mHapticFeedbackTouchListener;
2761 }
2762
Adam Cohen9211d422014-10-07 18:14:53 -07002763 public void onDragStarted(View view) {
2764 if (isOnCustomContent()) {
2765 // Custom content screen doesn't participate in drag and drop. If on custom
2766 // content screen, move to default.
2767 moveWorkspaceToDefaultScreen();
2768 }
2769
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onDragStarted(view);
2772 }
2773 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002774
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775 /**
2776 * Called when the user stops interacting with the launcher.
2777 * This implies that the user is now on the homescreen and is not doing housekeeping.
2778 */
Adam Cohen9211d422014-10-07 18:14:53 -07002779 protected void onInteractionEnd() {
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onInteractionEnd();
2782 }
2783 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784
2785 /**
2786 * Called when the user starts interacting with the launcher.
2787 * The possible interactions are:
2788 * - open all apps
2789 * - reorder an app shortcut, or a widget
2790 * - open the overview mode.
2791 * This is a good time to stop doing things that only make sense
2792 * when the user is on the homescreen and not doing housekeeping.
2793 */
Adam Cohen9211d422014-10-07 18:14:53 -07002794 protected void onInteractionBegin() {
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onInteractionBegin();
2797 }
2798 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002799
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 /** Updates the interaction state. */
2801 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002804 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2805 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002808 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 onInteractionEnd();
2810 }
2811 }
2812
Kenny Guyf07af7b2014-07-31 11:39:16 +01002813 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002814 try {
2815 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002816 launcherApps.showAppDetailsForProfile(componentName, user);
2817 } catch (SecurityException e) {
2818 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2819 Log.e(TAG, "Launcher does not have permission to launch settings");
2820 } catch (ActivityNotFoundException e) {
2821 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2822 Log.e(TAG, "Unable to launch settings");
2823 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002824 }
2825
Michael Jurka1e2f4652013-07-08 18:03:46 -07002826 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002827 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2828 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002829 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002830 // System applications cannot be installed. For now, show a toast explaining that.
2831 // We may give them the option of disabling apps this way.
2832 int messageId = R.string.uninstall_system_app_text;
2833 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002834 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002835 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002836 String packageName = componentName.getPackageName();
2837 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002838 Intent intent = new Intent(
2839 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002840 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2841 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002842 if (user != null) {
2843 user.addToIntent(intent, Intent.EXTRA_USER);
2844 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002845 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002846 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002847 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002848 }
2849
Winson Chung8f1eff72015-05-28 17:33:40 -07002850 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002851 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002853 // Only launch using the new animation if the shortcut has not opted out (this is a
2854 // private contract between launcher and may be ignored in the future).
2855 boolean useLaunchAnimation = (v != null) &&
2856 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002857 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2858 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002859
Kenny Guy1317e2d2014-05-08 18:52:50 +01002860 UserHandleCompat user = null;
2861 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2862 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2863 user = userManager.getUserForSerialNumber(serialNumber);
2864 }
2865
2866 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002867 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002868 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002869 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002870 int left = 0, top = 0;
2871 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2872 if (v instanceof TextView) {
2873 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002874 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2875 if (icon != null) {
2876 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002877 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002878 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002879 width = bounds.width();
2880 height = bounds.height();
2881 }
2882 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002883 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2884 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002885 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002886 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002887 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002888 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002889 // On L devices, we use the device default slide-up transition.
2890 // On L MR1 devices, we a custom version of the slide-up transition which
2891 // doesn't have the delay present in the device default.
2892 opts = ActivityOptions.makeCustomAnimation(this,
2893 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002894 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002895 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002896 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002897
2898 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2899 // Could be launching some bookkeeping activity
2900 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002901 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002902 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002903 launcherApps.startActivityForProfile(intent.getComponent(), user,
2904 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002905 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002906 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002908 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2909 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2910 // corresponding permission. Show the appropriate permission prompt if that
2911 // is the case.
2912 if (intent.getComponent() == null
2913 && Intent.ACTION_CALL.equals(intent.getAction())
2914 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2915 PackageManager.PERMISSION_GRANTED) {
2916 // TODO: Rename sPendingAddItem to a generic name.
2917 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2918 0, (ItemInfo) tag);
2919 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2920 REQUEST_PERMISSION_CALL_PHONE);
2921 return false;
2922 }
2923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002925 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002927 "or use the exported attribute for this activity. "
2928 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002930 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002932
Winson Chungbedf9232015-07-10 12:38:30 -07002933 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002934 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002935 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2936 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2937 return false;
2938 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002939 try {
2940 success = startActivity(v, intent, tag);
2941 } catch (ActivityNotFoundException e) {
2942 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2943 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2944 }
2945 return success;
2946 }
2947
Adam Cohen268c4752012-06-06 17:47:33 -07002948 /**
2949 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2950 * in the DragLayer in the exact absolute location of the original FolderIcon.
2951 */
2952 private void copyFolderIconToImage(FolderIcon fi) {
2953 final int width = fi.getMeasuredWidth();
2954 final int height = fi.getMeasuredHeight();
2955
2956 // Lazy load ImageView, Bitmap and Canvas
2957 if (mFolderIconImageView == null) {
2958 mFolderIconImageView = new ImageView(this);
2959 }
2960 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2961 mFolderIconBitmap.getHeight() != height) {
2962 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2963 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2964 }
2965
2966 DragLayer.LayoutParams lp;
2967 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2968 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2969 } else {
2970 lp = new DragLayer.LayoutParams(width, height);
2971 }
2972
Adam Cohen307fe232012-08-16 17:55:58 -07002973 // The layout from which the folder is being opened may be scaled, adjust the starting
2974 // view size by this scale factor.
2975 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002976 lp.customPosition = true;
2977 lp.x = mRectForFolderAnimation.left;
2978 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002979 lp.width = (int) (scale * width);
2980 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002981
2982 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2983 fi.draw(mFolderIconCanvas);
2984 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002985 if (fi.getFolder() != null) {
2986 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2987 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002988 }
Adam Cohen268c4752012-06-06 17:47:33 -07002989 // Just in case this image view is still in the drag layer from a previous animation,
2990 // we remove it and re-add it.
2991 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2992 mDragLayer.removeView(mFolderIconImageView);
2993 }
2994 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002995 if (fi.getFolder() != null) {
2996 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 }
2999
Sunny Goyal08442b82015-10-21 17:15:08 -07003000 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003001 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003002 FolderInfo info = (FolderInfo) fi.getTag();
3003 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3004 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003005 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3006 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003007 }
3008
Adam Cohen268c4752012-06-06 17:47:33 -07003009 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3010 copyFolderIconToImage(fi);
3011 fi.setVisibility(View.INVISIBLE);
3012
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003013 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3014 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003015 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003016 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3017 }
3018 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003019 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003020 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003021 oa.end();
3022 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003023 }
3024
Sunny Goyal935fca12015-10-13 10:19:01 -07003025 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003026 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003027 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003028
Adam Cohen268c4752012-06-06 17:47:33 -07003029 // We remove and re-draw the FolderIcon in-case it has changed
3030 mDragLayer.removeView(mFolderIconImageView);
3031 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003032 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003033 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003034 oa.addListener(new AnimatorListenerAdapter() {
3035 @Override
3036 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003037 if (cl != null) {
3038 cl.clearFolderLeaveBehind();
3039 // Remove the ImageView copy of the FolderIcon and make the original visible.
3040 mDragLayer.removeView(mFolderIconImageView);
3041 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003042 }
3043 }
3044 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003045 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003046 if (!animate) {
3047 oa.end();
3048 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003049 }
3050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003052 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 * is animated relative to the specified View. If the View is null, no animation
3054 * is played.
3055 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003056 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003058 public void openFolder(FolderIcon folderIcon, boolean animate) {
3059 animate &= !Utilities.isPowerSaverOn(this);
3060
Adam Cohenfb91f302012-06-11 15:45:18 -07003061 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003062 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3063 if (openFolder != null && openFolder != folder) {
3064 // Close any open folder before opening a folder.
3065 closeFolder();
3066 }
3067
Adam Cohena9cf38f2011-05-02 15:36:58 -07003068 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Adam Cohena9cf38f2011-05-02 15:36:58 -07003070 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071
Sunny Goyalf4066152015-04-15 09:42:19 -07003072 // While the folder is open, the position of the icon cannot change.
3073 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3074
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 // Just verify that the folder hasn't already been added to the DragLayer.
3076 // There was a one-off crash where the folder had a parent already.
3077 if (folder.getParent() == null) {
3078 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003079 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 } else {
3081 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3082 folder.getParent() + ").");
3083 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003084 if (animate) {
3085 folder.animateOpen();
3086 } else {
3087 folder.open();
3088 }
3089 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003090
3091 // Notify the accessibility manager that this folder "window" has appeared and occluded
3092 // the workspace items
3093 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3094 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003095 }
3096
3097 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003098 closeFolder(true);
3099 }
3100
3101 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003102 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003104 if (folder.isEditingName()) {
3105 folder.dismissEditingName();
3106 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003107 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003108 }
3109 }
3110
Sunny Goyal935fca12015-10-13 10:19:01 -07003111 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003112 animate &= !Utilities.isPowerSaverOn(this);
3113
Adam Cohen4554ee12011-08-03 16:13:21 -07003114 folder.getInfo().opened = false;
3115
3116 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3117 if (parent != null) {
3118 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003119 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003120 if (fi != null) {
3121 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3122 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003124 if (animate) {
3125 folder.animateClosed();
3126 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003127 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003128 }
Winson Chung83ca4802013-04-12 15:10:52 -07003129
Sunny Goyal935fca12015-10-13 10:19:01 -07003130 // Notify the accessibility manager that this folder "window" has disappeared and no
3131 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003132 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003133 }
3134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003136 if (!isDraggingEnabled()) return false;
3137 if (isWorkspaceLocked()) return false;
3138 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139
Winson Chung76648c52015-07-10 14:33:23 -07003140 if (v == mAllAppsButton) {
3141 onLongClickAllAppsButton(v);
3142 return true;
3143 }
3144
Adam Cohen1697b792013-09-17 19:08:21 -07003145 if (v instanceof Workspace) {
3146 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003147 if (!mWorkspace.isTouchActive()) {
3148 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003149 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3150 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3151 return true;
3152 } else {
3153 return false;
3154 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003155 } else {
3156 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003157 }
Adam Cohen1697b792013-09-17 19:08:21 -07003158 }
3159
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003160 CellLayout.CellInfo longClickCellInfo = null;
3161 View itemUnderLongClick = null;
3162 if (v.getTag() instanceof ItemInfo) {
3163 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003164 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003165 itemUnderLongClick = longClickCellInfo.cell;
3166 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 }
3168
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 // The hotseat touch handling does not go through Workspace, and we always allow long press
3170 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003171 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003172 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003173 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003174 // User long pressed on empty space
3175 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3176 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003177 if (mWorkspace.isInOverviewMode()) {
3178 mWorkspace.startReordering(v);
3179 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003180 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003183 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3184 mHotseat.getOrderInHotseat(
3185 longClickCellInfo.cellX,
3186 longClickCellInfo.cellY));
3187 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003189 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 }
3191 }
3192 }
3193 return true;
3194 }
3195
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 boolean isHotseatLayout(View layout) {
3197 return mHotseat != null && layout != null &&
3198 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3199 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003200
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 /**
3202 * Returns the CellLayout of the specified container at the specified screen.
3203 */
Sunny Goyal92820592015-03-02 11:31:35 -08003204 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3206 if (mHotseat != null) {
3207 return mHotseat.getLayout();
3208 } else {
3209 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003210 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003212 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003213 }
3214 }
3215
Winson Chungb745afb2015-03-02 11:51:23 -08003216 /**
3217 * For overridden classes.
3218 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003219 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003220 return isAppsViewVisible();
3221 }
3222
3223 public boolean isAppsViewVisible() {
3224 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3225 }
3226
3227 public boolean isWidgetsViewVisible() {
3228 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003229 }
3230
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003231 private void setWorkspaceBackground(int background) {
3232 switch (background) {
3233 case WORKSPACE_BACKGROUND_TRANSPARENT:
3234 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3235 break;
3236 case WORKSPACE_BACKGROUND_BLACK:
3237 getWindow().setBackgroundDrawable(null);
3238 break;
3239 default:
3240 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3241 }
Craig Mautner360310b2012-10-26 15:13:08 -07003242 }
3243
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003244 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003245 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3246 int curflags = getWindow().getAttributes().flags
3247 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3248 if (wpflags != curflags) {
3249 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3250 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003251 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003252 }
3253
Michael Jurkae326f182011-11-21 14:05:46 -08003254 @Override
3255 public void onTrimMemory(int level) {
3256 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003257 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3258 // The widget preview db can result in holding onto over
3259 // 3MB of memory for caching which isn't necessary.
3260 SQLiteDatabase.releaseMemory();
3261
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003262 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003263 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003264 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003265 if (mLauncherCallbacks != null) {
3266 mLauncherCallbacks.onTrimMemory(level);
3267 }
Michael Jurkae326f182011-11-21 14:05:46 -08003268 }
3269
Winson5c6bdbb2015-09-03 11:36:19 -07003270 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003271 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003272 }
3273
Winson5c6bdbb2015-09-03 11:36:19 -07003274 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003275 boolean changed = mState != State.WORKSPACE ||
3276 mWorkspace.getState() != Workspace.State.NORMAL;
3277 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003279 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003280 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003281
Michael Jurkab3e22d92011-10-31 15:58:33 -07003282 // Set focus to the AppsCustomize button
3283 if (mAllAppsButton != null) {
3284 mAllAppsButton.requestFocus();
3285 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003286 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003287
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003288 // Change the state *after* we've called all the transition code
3289 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003290
Winson Chung5fb63472011-02-02 17:03:37 -08003291 // Resume the auto-advance of widgets
3292 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003293 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003294
Winson Chung0f785722015-04-08 10:27:49 -07003295 if (changed) {
3296 // Send an accessibility event to announce the context change
3297 getWindow().getDecorView()
3298 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003299 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -08003300 mUserEventLogger.resetElapsedContainerMillis();
Winson5c6bdbb2015-09-03 11:36:19 -07003301 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003302 }
3303
Winsone9f27272015-10-13 10:47:51 -07003304 /**
3305 * Shows the overview button.
3306 */
Adam Cohened307df2013-10-02 09:37:31 -07003307 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003308 showOverviewMode(animated, false);
3309 }
3310
3311 /**
3312 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3313 * onto one of the overview panel buttons.
3314 */
3315 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3316 Runnable postAnimRunnable = null;
3317 if (requestButtonFocus) {
3318 postAnimRunnable = new Runnable() {
3319 @Override
3320 public void run() {
3321 // Hitting the menu button when in touch mode does not trigger touch mode to
3322 // be disabled, so if requested, force focus on one of the overview panel
3323 // buttons.
3324 mOverviewPanel.requestFocusFromTouch();
3325 }
3326 };
3327 }
Adam Cohened307df2013-10-02 09:37:31 -07003328 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003329 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003330 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003331 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003332 }
3333
Winson Chungb745afb2015-03-02 11:51:23 -08003334 /**
3335 * Shows the apps view.
3336 */
Winson Chung76648c52015-07-10 14:33:23 -07003337 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3338 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003339 if (resetListToTop) {
3340 mAppsView.scrollToTop();
3341 }
Winson Chung4ac30062015-05-08 17:34:17 -07003342 if (updatePredictedApps) {
3343 tryAndUpdatePredictedApps();
3344 }
Winson Chung76648c52015-07-10 14:33:23 -07003345 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003346 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347
Winson Chungb745afb2015-03-02 11:51:23 -08003348 /**
3349 * Shows the widgets view.
3350 */
3351 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003352 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003353 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003354 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003355 }
Winson Chung76648c52015-07-10 14:33:23 -07003356 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003357
3358 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003359 @Override
3360 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003361 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003362 }
3363 });
Winson Chungb745afb2015-03-02 11:51:23 -08003364 }
3365
3366 /**
3367 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003368 *
3369 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003370 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003371 // TODO: calling method should use the return value so that when {@code false} is returned
3372 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003373 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003374 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3375 mState != State.WIDGETS_SPRING_LOADED) {
3376 return false;
3377 }
3378 if (toState != State.APPS && toState != State.WIDGETS) {
3379 return false;
3380 }
Winson Chungb745afb2015-03-02 11:51:23 -08003381
3382 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003383 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3384 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003385 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003386 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003387 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003388
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003390 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003391
3392 // Pause the auto-advance of widgets until we are out of AllApps
3393 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003394 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003395 closeFolder();
3396
3397 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003398 getWindow().getDecorView()
3399 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003400 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003401 }
3402
Winson Chungcd99cd32015-04-29 11:03:24 -07003403 /**
3404 * Updates the workspace and interaction state on state change, and return the animation to this
3405 * new state.
3406 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003407 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003408 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003409 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003410 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003411 updateInteraction(fromState, toState);
3412 return anim;
3413 }
3414
Jun Mukaif0033da2015-08-04 18:34:30 -07003415 public void onLauncherClingShown() {
3416 // When a launcher cling appears, it should cover the underlying layers, so their focus
3417 // should be blocked.
3418 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3419 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3420 }
3421 }
3422
3423 public void onLauncherClingDismissed() {
3424 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3425 }
3426
Hyunyoung Song3f471442015-04-08 19:01:34 -07003427 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003428 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003429 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003430 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003431 }
Winson Chungb745afb2015-03-02 11:51:23 -08003432
Winson Chung006ee262015-08-03 14:40:11 -07003433 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003434 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003435 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003436
3437 if (isAppsViewVisible()) {
3438 mState = State.APPS_SPRING_LOADED;
3439 } else if (isWidgetsViewVisible()) {
3440 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003441 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003442 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003443 } else {
3444 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003445 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003446 }
Adam Cohen7777d962011-08-18 18:58:38 -07003447
Hyunyoung Song3f471442015-04-08 19:01:34 -07003448 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003449 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003450 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003451
Winson Chunge7a03942011-08-05 15:05:12 -07003452 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003453 @Override
3454 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003455 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003456 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3457 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003458 // Before we show workspace, hide all apps again because
3459 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3460 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003461 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003462 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003463 } else {
3464 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003465 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003466 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003467 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003468 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003469
Tony Wickhame0c33232016-02-08 11:37:04 -08003470 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003471 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3472 || mState == State.WIDGETS_SPRING_LOADED;
3473 }
3474
Michael Jurkad3ef3062010-11-23 16:23:58 -08003475 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003476 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003477 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003478 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003479 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003480 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003481 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3482 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003483 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003484 }
3485
Winson Chung4ac30062015-05-08 17:34:17 -07003486 /**
3487 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3488 * resumed.
3489 */
3490 private void tryAndUpdatePredictedApps() {
3491 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003492 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003493 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003494 mAppsView.setPredictedApps(apps);
3495 }
3496 }
3497 }
3498
Michael Jurkab3e22d92011-10-31 15:58:33 -07003499 void lockAllApps() {
3500 // TODO
3501 }
3502
3503 void unlockAllApps() {
3504 // TODO
3505 }
3506
Sunny Goyal594d76d2014-11-06 10:12:54 -08003507 protected void disableVoiceButtonProxy(boolean disable) {
3508 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003509 }
3510
Winsonf768d932015-09-25 16:12:35 -07003511 public boolean launcherCallbacksProvidesSearch() {
3512 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3513 }
3514
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003515 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003516 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003517 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003518 }
3519
Adam Cohen24ce0b32014-01-14 16:18:14 -08003520 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003521 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3522 if (searchProvider == null) {
3523 return null;
3524 }
3525
3526 Bundle opts = new Bundle();
3527 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003528 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003529
Tony Wickham3a3517f2015-10-06 11:27:04 -07003530 // Determine the min and max dimensions of the widget.
3531 LauncherAppState app = LauncherAppState.getInstance();
3532 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3533 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3534 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003535 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3536 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003537 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003538 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003539 int minWidth = maxWidth;
3540 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003541 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3542 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3543 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3544 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3545 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003546 }
3547 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3548 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3549 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3550 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003551 if (LOGD) {
3552 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3553 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3554 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003555
Tony Wickham775455c2015-10-16 09:49:32 -07003556 if (mLauncherCallbacks != null) {
3557 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3558 }
3559
Sunny Goyalf7258242015-10-19 16:59:07 -07003560 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003561 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003562 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003563 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003564 || (widgetInfo == null)
3565 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003566 // A valid widget is not already bound.
3567 if (widgetId > -1) {
3568 mAppWidgetHost.deleteAppWidgetId(widgetId);
3569 widgetId = -1;
3570 }
3571
3572 // Try to bind a new widget
3573 widgetId = mAppWidgetHost.allocateAppWidgetId();
3574
3575 if (!AppWidgetManagerCompat.getInstance(this)
3576 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3577 mAppWidgetHost.deleteAppWidgetId(widgetId);
3578 widgetId = -1;
3579 }
3580
Sunny Goyalf7258242015-10-19 16:59:07 -07003581 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 .putInt(QSB_WIDGET_ID, widgetId)
3583 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003584 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003585 }
3586
Sunny Goyal8d595092015-07-06 12:22:14 -07003587 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003588 if (widgetId != -1) {
3589 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003590 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003591 mQsb.updateAppWidgetOptions(opts);
3592 mQsb.setPadding(0, 0, 0, 0);
3593 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003594 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003596 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003597 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003598 }
3599
Sunny Goyal22235bc2015-04-03 09:23:43 -07003600 private void reinflateQSBIfNecessary() {
3601 if (mQsb instanceof LauncherAppWidgetHostView &&
3602 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3603 mSearchDropTargetBar.removeView(mQsb);
3604 mQsb = null;
3605 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3606 }
3607 }
3608
Michael Jurkad7c28052012-04-27 15:43:36 -07003609 @Override
3610 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003611 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003612 final List<CharSequence> text = event.getText();
3613 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003614 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003615 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003616 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003617 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003618 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003619 } else if (mWorkspace != null) {
3620 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003621 } else {
3622 text.add(getString(R.string.all_apps_home_button_label));
3623 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003624 return result;
3625 }
3626
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003627 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003628 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003629 */
Adam Cohen091440a2015-03-18 14:16:05 -07003630 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003631 @Override
3632 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003633 closeSystemDialogs();
3634 }
3635 }
3636
3637 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003638 * If the activity is currently paused, signal that we need to run the passed Runnable
3639 * in onResume.
3640 *
3641 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003642 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3643 * wrong when we're not running, and if the activity comes back to what the configuration was
3644 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003645 *
3646 * Implementation of the method from LauncherModel.Callbacks.
3647 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003648 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003649 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003650 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003651 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003652 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003653 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003654 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003655 }
3656 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003657 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003658 return true;
3659 } else {
3660 return false;
3661 }
3662 }
3663
Michael Jurkac402cd92013-05-20 15:49:32 +02003664 private boolean waitUntilResume(Runnable run) {
3665 return waitUntilResume(run, false);
3666 }
3667
Michael Jurka1e2f4652013-07-08 18:03:46 -07003668 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003669 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003670 }
3671
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003673 * If the activity is currently paused, signal that we need to re-run the loader
3674 * in onResume.
3675 *
3676 * This needs to be called from incoming places where resources might have been loaded
3677 * while we are paused. That is becaues the Configuration might be wrong
3678 * when we're not running, and if it comes back to what it was when we
3679 * were paused, we are not restarted.
3680 *
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 *
3683 * @return true if we are currently paused. The caller might be able to
3684 * skip some work in that case since we will come back again.
3685 */
3686 public boolean setLoadOnResume() {
3687 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003688 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003689 mOnResumeNeedsLoad = true;
3690 return true;
3691 } else {
3692 return false;
3693 }
3694 }
3695
3696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003700 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003701 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003702 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003703 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003704 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003706
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003708 * Clear any pending bind callbacks. This is called when is loader is planning to
3709 * perform a full rebind from scratch.
3710 */
3711 @Override
3712 public void clearPendingBinds() {
3713 mBindOnResumeCallbacks.clear();
3714 if (mPendingExecutor != null) {
3715 mPendingExecutor.markCompleted();
3716 mPendingExecutor = null;
3717 }
3718 }
3719
3720 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 * Refreshes the shortcuts shown on the workspace.
3722 *
3723 * Implementation of the method from LauncherModel.Callbacks.
3724 */
3725 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003726 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003727
Winson Chungd64d1762013-08-20 14:37:16 -07003728 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003729 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003730 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003731
Michael Jurka05bf644e2011-11-30 20:28:53 -08003732 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003733 if (mHotseat != null) {
3734 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 }
3736 }
3737
Adam Cohendcd297f2013-06-18 13:13:40 -07003738 @Override
3739 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003740 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003741
Adam Cohen5084cba2013-09-03 12:01:16 -07003742 // If there are no screens, we need to have an empty screen
3743 if (orderedScreenIds.size() == 0) {
3744 mWorkspace.addExtraEmptyScreen();
3745 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003746
3747 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003748 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003749 if (hasCustomContentToLeft()) {
3750 mWorkspace.createCustomContentContainer();
3751 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003752 }
Winson Chung64359a52013-07-08 17:17:08 -07003753 }
3754
3755 @Override
3756 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003757 int count = orderedScreenIds.size();
3758 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003759 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003760 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 }
3762
Winson Chungd64d1762013-08-20 14:37:16 -07003763 public void bindAppsAdded(final ArrayList<Long> newScreens,
3764 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003765 final ArrayList<ItemInfo> addAnimated,
3766 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003767 Runnable r = new Runnable() {
3768 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003769 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003770 }
3771 };
3772 if (waitUntilResume(r)) {
3773 return;
3774 }
3775
Winson Chungd64d1762013-08-20 14:37:16 -07003776 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003777 if (newScreens != null) {
3778 bindAddScreens(newScreens);
3779 }
Winson Chungd64d1762013-08-20 14:37:16 -07003780
3781 // We add the items without animation on non-visible pages, and with
3782 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003783 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 bindItems(addNotAnimated, 0,
3785 addNotAnimated.size(), false);
3786 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003787 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003788 bindItems(addAnimated, 0,
3789 addAnimated.size(), true);
3790 }
Winson Chungc58497e2013-09-03 17:48:37 -07003791
Winson Chung87412982013-10-03 18:34:14 -07003792 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003793 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003794
Winson Chungb745afb2015-03-02 11:51:23 -08003795 if (addedApps != null && mAppsView != null) {
3796 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003797 }
Winson Chungd64d1762013-08-20 14:37:16 -07003798 }
3799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003800 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 * Bind the items start-end from the list.
3802 *
3803 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003805 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003806 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3807 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003808 Runnable r = new Runnable() {
3809 public void run() {
3810 bindItems(shortcuts, start, end, forceAnimateIcons);
3811 }
3812 };
3813 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003814 return;
3815 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003816
Winson Chungf0c6ae02012-03-21 16:10:31 -07003817 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003818 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3819 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003820 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003822 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003823 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003825
3826 // Short circuit if we are loading dock items for a configuration which has no dock
3827 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3828 mHotseat == null) {
3829 continue;
3830 }
3831
Sunny Goyal639e9062015-08-19 19:17:06 -07003832 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 switch (item.itemType) {
3834 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3835 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003837 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003838
Winson Chung64359a52013-07-08 17:17:08 -07003839 /*
3840 * TODO: FIX collision case
3841 */
Winson Chungce376632013-07-11 16:12:41 -07003842 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3843 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3844 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003845 View v = cl.getChildAt(item.cellX, item.cellY);
3846 Object tag = v.getTag();
3847 String desc = "Collision while binding workspace item: " + item
3848 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003849 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003850 throw (new RuntimeException(desc));
3851 } else {
3852 Log.d(TAG, desc);
3853 }
Winson Chungce376632013-07-11 16:12:41 -07003854 }
Winson Chung64359a52013-07-08 17:17:08 -07003855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003857 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003858 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003859 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003860 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003862 default:
3863 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003865
3866 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3867 item.cellY, 1, 1);
3868 if (animateIcons) {
3869 // Animate all the applications up now
3870 view.setAlpha(0f);
3871 view.setScaleX(0f);
3872 view.setScaleY(0f);
3873 bounceAnims.add(createNewAppBounceAnimation(view, i));
3874 newShortcutsScreenId = item.screenId;
3875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003876 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003877
Winson Chung997a9232013-07-24 15:33:46 -07003878 if (animateIcons) {
3879 // Animate to the correct page
3880 if (newShortcutsScreenId > -1) {
3881 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003882 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003883 final Runnable startBounceAnimRunnable = new Runnable() {
3884 public void run() {
3885 anim.playTogether(bounceAnims);
3886 anim.start();
3887 }
3888 };
Winson Chung997a9232013-07-24 15:33:46 -07003889 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003890 // We post the animation slightly delayed to prevent slowdowns
3891 // when we are loading right after we return to launcher.
3892 mWorkspace.postDelayed(new Runnable() {
3893 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003894 if (mWorkspace != null) {
3895 mWorkspace.snapToPage(newScreenIndex);
3896 mWorkspace.postDelayed(startBounceAnimRunnable,
3897 NEW_APPS_ANIMATION_DELAY);
3898 }
Winson Chung94d67682013-09-25 16:29:40 -07003899 }
3900 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003901 } else {
3902 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003903 }
3904 }
Winson Chung64359a52013-07-08 17:17:08 -07003905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003907 }
3908
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909 /**
3910 * Implementation of the method from LauncherModel.Callbacks.
3911 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003912 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003913 Runnable r = new Runnable() {
3914 public void run() {
3915 bindFolders(folders);
3916 }
3917 };
3918 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003919 return;
3920 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003921 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923
3924 /**
3925 * Add the views for a widget to the workspace.
3926 *
3927 * Implementation of the method from LauncherModel.Callbacks.
3928 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003929 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003930 Runnable r = new Runnable() {
3931 public void run() {
3932 bindAppWidget(item);
3933 }
3934 };
3935 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003936 return;
3937 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003938
Daniel Sandler843e8602010-06-07 14:59:01 -04003939 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3940 if (DEBUG_WIDGETS) {
3941 Log.d(TAG, "bindAppWidget: " + item);
3942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 final Workspace workspace = mWorkspace;
3944
Adam Cohen59400422014-03-05 18:07:04 -08003945 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003946 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003947
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003948 if (!mIsSafeModeEnabled
3949 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003950 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3951
Sunny Goyalff572272014-07-23 13:58:07 -07003952 if (appWidgetInfo == null) {
3953 if (DEBUG_WIDGETS) {
3954 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3955 + " belongs to component " + item.providerName
3956 + ", as the povider is null");
3957 }
3958 LauncherModel.deleteItemFromDatabase(this, item);
3959 return;
3960 }
Sunny Goyalff572272014-07-23 13:58:07 -07003961
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003962 // If we do not have a valid id, try to bind an id.
3963 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3964 // Note: This assumes that the id remap broadcast is received before this step.
3965 // If that is not the case, the id remap will be ignored and user may see the
3966 // click to setup view.
3967 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3968 pendingInfo.spanX = item.spanX;
3969 pendingInfo.spanY = item.spanY;
3970 pendingInfo.minSpanX = item.minSpanX;
3971 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003972 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003973
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003974 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3975 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3976 newWidgetId, appWidgetInfo, options);
3977
3978 // TODO consider showing a permission dialog when the widget is clicked.
3979 if (!success) {
3980 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3981 if (DEBUG_WIDGETS) {
3982 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3983 + " belongs to component " + item.providerName
3984 + ", as the launcher is unable to bing a new widget id");
3985 }
3986 LauncherModel.deleteItemFromDatabase(this, item);
3987 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003988 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003989
3990 item.appWidgetId = newWidgetId;
3991
3992 // If the widget has a configure activity, it is still needs to set it up, otherwise
3993 // the widget is ready to go.
3994 item.restoreStatus = (appWidgetInfo.configure == null)
3995 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3996 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3997
3998 LauncherModel.updateItemInDatabase(this, item);
3999 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4000 && (appWidgetInfo.configure == null)) {
4001 // If the ID is already valid, verify if we need to configure or not.
4002 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4003 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004004 }
Sunny Goyalff572272014-07-23 13:58:07 -07004005 }
4006
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004007 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004008 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004009 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004010 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4011 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004012 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004013
Sunny Goyal56c73602015-09-25 12:55:01 -07004014 // Verify that we own the widget
4015 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4016 if (info == null || appWidgetInfo == null ||
4017 !info.provider.equals(appWidgetInfo.provider)) {
Hyunyoung Song70264a92016-02-03 15:28:47 -08004018 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
4019 + " appWidgetInfo=" + appWidgetInfo);
Sunny Goyal56c73602015-09-25 12:55:01 -07004020 deleteWidgetInfo(item);
4021 return;
4022 }
4023
Sunny Goyal651077b2014-06-30 14:15:31 -07004024 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004025 item.minSpanX = appWidgetInfo.minSpanX;
4026 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004027 } else {
4028 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004029 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4030 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004031 view.updateIcon(mIconCache);
4032 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004034 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004035 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004037 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038 workspace.requestLayout();
4039
Daniel Sandler843e8602010-06-07 14:59:01 -04004040 if (DEBUG_WIDGETS) {
4041 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4042 + (SystemClock.uptimeMillis()-start) + "ms");
4043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 }
4045
Sunny Goyalff572272014-07-23 13:58:07 -07004046 /**
4047 * Restores a pending widget.
4048 *
4049 * @param appWidgetId The app widget id
4050 * @param cellInfo The position on screen where to create the widget.
4051 */
4052 private void completeRestoreAppWidget(final int appWidgetId) {
4053 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4054 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4055 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4056 return;
4057 }
4058
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004059 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004060 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4061
4062 mWorkspace.reinflateWidgetsIfNecessary();
4063 LauncherModel.updateItemInDatabase(this, info);
4064 }
4065
Adam Cohen1462de32012-07-24 22:34:36 -07004066 public void onPageBoundSynchronously(int page) {
4067 mSynchronouslyBoundPages.add(page);
4068 }
4069
Sunny Goyal527c7d32015-08-28 15:19:36 -07004070 @Override
4071 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4072 if (mPendingExecutor != null) {
4073 mPendingExecutor.markCompleted();
4074 }
4075 mPendingExecutor = executor;
4076 executor.attachTo(this);
4077 }
4078
4079 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4080 if (mPendingExecutor == executor) {
4081 mPendingExecutor = null;
4082 }
4083 }
4084
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085 /**
4086 * Callback saying that there aren't any more items to bind.
4087 *
4088 * Implementation of the method from LauncherModel.Callbacks.
4089 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004090 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004091 Runnable r = new Runnable() {
4092 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004093 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004094 }
4095 };
4096 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004097 return;
4098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 if (mSavedState != null) {
4100 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004101 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004103
4104 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4105 if (folderId != 0) {
4106 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4107 if (view instanceof FolderIcon) {
4108 FolderIcon icon = (FolderIcon) view;
4109 FolderInfo info = icon.getFolderInfo();
4110 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4111 mWorkspace.getNextPage());
4112 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4113 || info.screenId == currentScreenId) {
4114 // We can show the folder
4115 openFolder(icon, false);
4116 } else {
4117 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4118 " but current screen is " + currentScreenId);
4119 }
4120 }
4121 }
4122
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 mSavedState = null;
4124 }
4125
Adam Cohen1462de32012-07-24 22:34:36 -07004126 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004128 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004129 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004130
4131 // If we received the result of any pending adds while the loader was running (e.g. the
4132 // widget configuration forced an orientation change), process them now.
4133 if (sPendingAddItem != null) {
4134 final long screenId = completeAdd(sPendingAddItem);
4135
4136 // TODO: this moves the user to the page where the pending item was added. Ideally,
4137 // the screen would be guaranteed to exist after bind, and the page would be set through
4138 // the workspace restore process.
4139 mWorkspace.post(new Runnable() {
4140 @Override
4141 public void run() {
4142 mWorkspace.snapToScreenId(screenId);
4143 }
4144 });
4145 sPendingAddItem = null;
4146 }
4147
Sunny Goyal756adbc2015-04-16 15:20:51 -07004148 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004149
4150 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004151 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004152 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004153 }
4154
Adam Cohen3ed316a2014-07-23 18:21:20 -07004155 private void sendLoadingCompleteBroadcastIfNecessary() {
4156 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4157 String permission =
4158 getResources().getString(R.string.receive_first_load_broadcast_permission);
4159 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4160 sendBroadcast(intent, permission);
4161 SharedPreferences.Editor editor = mSharedPrefs.edit();
4162 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4163 editor.apply();
4164 }
4165 }
4166
Winson Chunga0b7e862013-09-05 16:03:15 -07004167 public boolean isAllAppsButtonRank(int rank) {
4168 if (mHotseat != null) {
4169 return mHotseat.isAllAppsButtonRank(rank);
4170 }
4171 return false;
4172 }
4173
Winson Chunga2413752012-04-03 14:22:34 -07004174 private boolean canRunNewAppsAnimation() {
4175 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004176 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4177 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004178 }
4179
Winson Chungc9168342013-06-26 14:54:55 -07004180 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004181 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004182 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4183 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004184 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004185 return bounceAnim;
4186 }
4187
Adam Cohen27772732013-11-11 14:00:56 +00004188 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004189 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004190 }
4191
Tony Wickham3a3517f2015-10-06 11:27:04 -07004192 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004193 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004194 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004195 }
4196
Tony Wickham55616cd2015-09-23 14:55:17 -07004197 public int getSearchBarHeight() {
4198 if (mLauncherCallbacks != null) {
4199 return mLauncherCallbacks.getSearchBarHeight();
4200 }
4201 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4202 }
4203
Winson Chung88fa7412015-08-03 14:25:28 -07004204 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004205 if (mSearchDropTargetBar == null) {
4206 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004207 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004208 if (mQsb != null) {
4209 mSearchDropTargetBar.removeView(mQsb);
4210 mQsb = null;
4211 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004212 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004213 }
4214
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004215 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004216 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4217 * multiple calls to bind the same list.)
4218 */
4219 @Thunk ArrayList<AppInfo> mTmpAppsList;
4220 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4221 public void run() {
4222 bindAllApplications(mTmpAppsList);
4223 mTmpAppsList = null;
4224 }
4225 };
4226
4227 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004228 * Add the icons for all apps.
4229 *
4230 * Implementation of the method from LauncherModel.Callbacks.
4231 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004232 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004233 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4234 mTmpAppsList = apps;
4235 return;
4236 }
4237
Winson Chungb745afb2015-03-02 11:51:23 -08004238 if (mAppsView != null) {
4239 mAppsView.setApps(apps);
4240 }
Adam Cohen9211d422014-10-07 18:14:53 -07004241 if (mLauncherCallbacks != null) {
4242 mLauncherCallbacks.bindAllApplications(apps);
4243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004244 }
4245
4246 /**
4247 * A package was updated.
4248 *
4249 * Implementation of the method from LauncherModel.Callbacks.
4250 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004251 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004252 Runnable r = new Runnable() {
4253 public void run() {
4254 bindAppsUpdated(apps);
4255 }
4256 };
4257 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004258 return;
4259 }
4260
Winson Chungb745afb2015-03-02 11:51:23 -08004261 if (mAppsView != null) {
4262 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004263 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004264 }
4265
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 @Override
4267 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4268 Runnable r = new Runnable() {
4269 public void run() {
4270 bindWidgetsRestored(widgets);
4271 }
4272 };
4273 if (waitUntilResume(r)) {
4274 return;
4275 }
4276 mWorkspace.widgetsRestored(widgets);
4277 }
4278
Joe Onorato9c1289c2009-08-17 11:03:03 -04004279 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004280 * Some shortcuts were updated in the background.
4281 *
4282 * Implementation of the method from LauncherModel.Callbacks.
4283 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004284 @Override
4285 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4286 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004287 Runnable r = new Runnable() {
4288 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004289 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004290 }
4291 };
4292 if (waitUntilResume(r)) {
4293 return;
4294 }
4295
Sunny Goyal4390ace2014-10-13 11:33:11 -07004296 if (!updated.isEmpty()) {
4297 mWorkspace.updateShortcuts(updated);
4298 }
4299
4300 if (!removed.isEmpty()) {
4301 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4302 for (ShortcutInfo si : removed) {
4303 removedComponents.add(si.getTargetComponent());
4304 }
4305 mWorkspace.removeItemsByComponentName(removedComponents, user);
4306 // Notify the drag controller
4307 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004308 }
4309 }
4310
4311 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004312 * Update the state of a package, typically related to install state.
4313 *
4314 * Implementation of the method from LauncherModel.Callbacks.
4315 */
Sunny Goyale755d462014-07-22 13:48:29 -07004316 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004317 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4318 Runnable r = new Runnable() {
4319 public void run() {
4320 bindRestoreItemsChange(updates);
4321 }
4322 };
4323 if (waitUntilResume(r)) {
4324 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004325 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004326
Sunny Goyal756adbc2015-04-16 15:20:51 -07004327 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004328 }
4329
4330 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004331 * A package was uninstalled. We take both the super set of packageNames
4332 * in addition to specific applications to remove, the reason being that
4333 * this can be called when a package is updated as well. In that scenario,
4334 * we only remove specific components from the workspace, where as
4335 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004336 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004337 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004338 * Implementation of the method from LauncherModel.Callbacks.
4339 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004340 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004341 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004342 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004343 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004344 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004345 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004346 }
Winson Chungd64d1762013-08-20 14:37:16 -07004347 };
4348 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004349 return;
4350 }
4351
Sunny Goyal1a745e82014-10-02 15:58:31 -07004352 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004353 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4354 for (AppInfo info : appInfos) {
4355 removedComponents.add(info.componentName);
4356 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004357 if (!packageNames.isEmpty()) {
4358 mWorkspace.removeItemsByPackageName(packageNames, user);
4359 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004360 if (!removedComponents.isEmpty()) {
4361 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004363 // Notify the drag controller
4364 mDragController.onAppsRemoved(packageNames, removedComponents);
4365
Sunny Goyal1a745e82014-10-02 15:58:31 -07004366 } else {
4367 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004368 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004369
Winson Chungdf95eb12013-10-16 14:57:07 -07004370 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004371 if (mAppsView != null) {
4372 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004374 }
Joe Onoratobe386092009-11-17 17:32:16 -08004375
Michael Jurkac402cd92013-05-20 15:49:32 +02004376 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004377 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004378 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004379 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004380 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004381
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004382 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004383 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004384 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004385 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004386 return;
4387 }
4388
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004389 if (mWidgetsView != null && model != null) {
4390 mWidgetsView.addWidgets(model);
4391 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004392 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004393 }
4394
Winson Chung400438b2011-01-16 17:53:48 -08004395 private int mapConfigurationOriActivityInfoOri(int configOri) {
4396 final Display d = getWindowManager().getDefaultDisplay();
4397 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4398 switch (d.getRotation()) {
4399 case Surface.ROTATION_0:
4400 case Surface.ROTATION_180:
4401 // We are currently in the same basic orientation as the natural orientation
4402 naturalOri = configOri;
4403 break;
4404 case Surface.ROTATION_90:
4405 case Surface.ROTATION_270:
4406 // We are currently in the other basic orientation to the natural orientation
4407 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4408 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4409 break;
4410 }
4411
4412 int[] oriMap = {
4413 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4414 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4415 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4416 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4417 };
4418 // Since the map starts at portrait, we need to offset if this device's natural orientation
4419 // is landscape.
4420 int indexOffset = 0;
4421 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4422 indexOffset = 1;
4423 }
4424 return oriMap[(d.getRotation() + indexOffset) % 4];
4425 }
Adam Cohen446e9402011-09-15 18:21:21 -07004426
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004427 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004428 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004429 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004430 if (Utilities.ATLEAST_JB_MR2) {
4431 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4432 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004433 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4434 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004435 }
Winson Chung4b919f82012-05-01 10:44:08 -07004436 }
4437 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004438
Winson Chung4b919f82012-05-01 10:44:08 -07004439 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004440 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004441 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004442 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004443 } else {
4444 mHandler.postDelayed(new Runnable() {
4445 public void run() {
4446 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4447 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004448 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004449 }
Winson Chung4b919f82012-05-01 10:44:08 -07004450 }
Winson Chung400438b2011-01-16 17:53:48 -08004451 }
4452
Winson Chunge43a1e72014-01-15 10:33:02 -08004453 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 return mLauncherCallbacks.isLauncherPreinstalled();
4456 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004457 PackageManager pm = getPackageManager();
4458 try {
4459 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4460 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4461 return true;
4462 } else {
4463 return false;
4464 }
4465 } catch (NameNotFoundException e) {
4466 e.printStackTrace();
4467 return false;
4468 }
4469 }
4470
Adam Cohenea90f832014-05-21 15:03:34 -07004471 /**
4472 * This method indicates whether or not we should suggest default wallpaper dimensions
4473 * when our wallpaper cropper was not yet used to set a wallpaper.
4474 */
4475 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004476 if (mLauncherCallbacks != null) {
4477 return mLauncherCallbacks.overrideWallpaperDimensions();
4478 }
Adam Cohenea90f832014-05-21 15:03:34 -07004479 return true;
4480 }
4481
Adam Cohen432609a2014-03-13 17:03:22 -07004482 /**
4483 * To be overridden by subclasses to indicate that there is an activity to launch
4484 * before showing the standard launcher experience.
4485 */
4486 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004487 if (mLauncherCallbacks != null) {
4488 return mLauncherCallbacks.hasFirstRunActivity();
4489 }
Adam Cohen432609a2014-03-13 17:03:22 -07004490 return false;
4491 }
4492
4493 /**
4494 * To be overridden by subclasses to launch any first run activity
4495 */
4496 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004497 if (mLauncherCallbacks != null) {
4498 return mLauncherCallbacks.getFirstRunActivity();
4499 }
Adam Cohen432609a2014-03-13 17:03:22 -07004500 return null;
4501 }
4502
Winson Chunga6945242014-01-08 14:04:34 -08004503 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004504 return !ActivityManager.isRunningInTestHarness() &&
4505 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004506 }
4507
Adam Cohen4a9423d2014-03-28 14:22:31 -07004508 protected boolean hasRunFirstRunActivity() {
4509 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4510 }
4511
Adam Cohen432609a2014-03-13 17:03:22 -07004512 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004513 if (shouldRunFirstRunActivity() &&
4514 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004515 Intent firstRunIntent = getFirstRunActivity();
4516 if (firstRunIntent != null) {
4517 startActivity(firstRunIntent);
4518 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004519 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004520 }
4521 }
Adam Cohen432609a2014-03-13 17:03:22 -07004522 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004523 }
4524
4525 private void markFirstRunActivityShown() {
4526 SharedPreferences.Editor editor = mSharedPrefs.edit();
4527 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4528 editor.apply();
4529 }
4530
Adam Cohen432609a2014-03-13 17:03:22 -07004531 /**
4532 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4533 * screen that must be displayed and dismissed.
4534 */
4535 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004536 if (mLauncherCallbacks != null) {
4537 return mLauncherCallbacks.hasDismissableIntroScreen();
4538 }
Adam Cohen432609a2014-03-13 17:03:22 -07004539 return false;
4540 }
4541
4542 /**
4543 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4544 */
4545 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004546 if (mLauncherCallbacks != null) {
4547 return mLauncherCallbacks.getIntroScreen();
4548 }
Adam Cohen432609a2014-03-13 17:03:22 -07004549 return null;
4550 }
4551
4552 /**
4553 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4554 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4555 */
4556 private boolean shouldShowIntroScreen() {
4557 return hasDismissableIntroScreen() &&
4558 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4559 }
4560
4561 protected void showIntroScreen() {
4562 View introScreen = getIntroScreen();
4563 changeWallpaperVisiblity(false);
4564 if (introScreen != null) {
4565 mDragLayer.showOverlayView(introScreen);
4566 }
4567 }
4568
4569 public void dismissIntroScreen() {
4570 markIntroScreenDismissed();
4571 if (showFirstRunActivity()) {
4572 // We delay hiding the intro view until the first run activity is showing. This
4573 // avoids a blip.
4574 mWorkspace.postDelayed(new Runnable() {
4575 @Override
4576 public void run() {
4577 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004578 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004579 }
4580 }, ACTIVITY_START_DELAY);
4581 } else {
4582 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004583 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004584 }
4585 changeWallpaperVisiblity(true);
4586 }
4587
4588 private void markIntroScreenDismissed() {
4589 SharedPreferences.Editor editor = mSharedPrefs.edit();
4590 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4591 editor.apply();
4592 }
4593
Adam Cohen091440a2015-03-18 14:16:05 -07004594 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004595 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4596 // on the device, then we always show the first run cling experience (or if there is no
4597 // launcher2). Otherwise, we prompt the user upon started for migration
4598 LauncherClings launcherClings = new LauncherClings(this);
4599 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004600 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004601 if (mModel.canMigrateFromOldLauncherDb(this)) {
4602 launcherClings.showMigrationCling();
4603 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004604 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004605 }
4606 }
4607 }
4608
Winson Chunga6945242014-01-08 14:04:34 -08004609 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004610 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4611 if (mHotseat != null) mHotseat.setAlpha(1f);
4612 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004613 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4614 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004615 }
4616
4617 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004618 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4619 if (mHotseat != null) mHotseat.setAlpha(0f);
4620 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004621 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4622 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004623 }
4624
Winson Chung5ffd51d2015-06-25 11:36:50 -07004625 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004626 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004627 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004628 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004629 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004630 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004631 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4632 if (userHandle != null) {
4633 user = UserHandleCompat.fromUser(userHandle);
4634 }
4635 }
4636 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004637 }
4638
4639 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004640 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004641 if (user == null) {
4642 user = UserHandleCompat.myUserHandle();
4643 }
4644
4645 // Called from search suggestion, add the profile extra to the intent to ensure that we
4646 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004647 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004648 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004649 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004650 return null;
4651 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004652 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004653 }
4654
Winson Chung5ffd51d2015-06-25 11:36:50 -07004655 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004656 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4657 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004658 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004659 UserHandleCompat.myUserHandle());
4660 }
4661
Winson Chung5ffd51d2015-06-25 11:36:50 -07004662 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004663 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4664 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004665 mWorkspace.onExternalDragStartedWithItem(dragView);
4666 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004667 }
4668
Winson Chung5ffd51d2015-06-25 11:36:50 -07004669 protected void moveWorkspaceToDefaultScreen() {
4670 mWorkspace.moveToDefaultScreen(false);
4671 }
4672
Anjali Koppalf5d29132014-02-28 13:33:27 -08004673 @Override
4674 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004675 if (mLauncherCallbacks != null) {
4676 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4677 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004678 }
4679
Winson Chung80baf5a2010-08-09 16:03:15 -07004680 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004681 * Returns a FastBitmapDrawable with the icon, accurately sized.
4682 */
4683 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4684 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4685 d.setFilterBitmap(true);
4686 resizeIconDrawable(d);
4687 return d;
4688 }
4689
4690 /**
4691 * Resizes an icon drawable to the correct icon size.
4692 */
Winson5fbe0742015-09-30 15:33:00 -07004693 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004694 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004695 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004696 }
4697
4698 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004699 * Prints out out state for debugging.
4700 */
4701 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004702 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004703 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004704 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4705 Log.d(TAG, "mRestoring=" + mRestoring);
4706 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004707 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004708 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004709 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004710
Daniel Sandler325dc232013-06-05 22:57:57 -04004711 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004712 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004713
4714 @Override
4715 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4716 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004717 // Dump workspace
4718 writer.println(prefix + "Workspace Items");
4719 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4720 writer.println(prefix + " Homescreen " + i);
4721
4722 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4723 for (int j = 0; j < layout.getChildCount(); j++) {
4724 Object tag = layout.getChildAt(j).getTag();
4725 if (tag != null) {
4726 writer.println(prefix + " " + tag.toString());
4727 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004728 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004729 }
Sunny Goyala1365452015-10-01 15:46:24 -07004730
4731 writer.println(prefix + " Hotseat");
4732 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4733 for (int j = 0; j < layout.getChildCount(); j++) {
4734 Object tag = layout.getChildAt(j).getTag();
4735 if (tag != null) {
4736 writer.println(prefix + " " + tag.toString());
4737 }
4738 }
4739
4740 synchronized (sDumpLogs) {
4741 writer.println();
4742 writer.println(prefix + "Debug logs");
4743 for (String log : sDumpLogs) {
4744 writer.println(prefix + " " + log);
4745 }
4746 }
4747
Adam Cohen9211d422014-10-07 18:14:53 -07004748 if (mLauncherCallbacks != null) {
4749 mLauncherCallbacks.dump(prefix, fd, writer, args);
4750 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004751 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004752
Sunny Goyala1365452015-10-01 15:46:24 -07004753 public static void addDumpLog(String tag, String log) {
4754 Log.d(tag, log);
4755 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004756 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004757 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004758 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004759 }
4760
Adam Cohen59400422014-03-05 18:07:04 -08004761 public static CustomAppWidget getCustomAppWidget(String name) {
4762 return sCustomAppWidgets.get(name);
4763 }
4764
4765 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4766 return sCustomAppWidgets;
4767 }
4768
Sunny Goyal29538332016-02-18 09:10:19 -08004769 public static List<View> getFolderContents(View icon) {
4770 if (icon instanceof FolderIcon) {
4771 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4772 } else {
4773 return Collections.EMPTY_LIST;
4774 }
4775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004776}
Michael Jurka2763be32011-02-24 11:19:57 -08004777
Dan Sandlerd5024042014-01-09 15:01:33 -05004778interface DebugIntents {
4779 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4780 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004781}