blob: ce56740237ca0b6074b318c857f1f61d6a461dd5 [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700110import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700111import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700112import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700113import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700114import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700115import com.android.launcher3.util.ViewOnDrawExecutor;
Sunny Goyal5abb9f82016-02-09 15:22:42 -0800116import com.android.launcher3.util.WallpaperUtils;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700117import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700118import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700119import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700120
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700122import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700137 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Sunny Goyal28c6b962015-10-12 11:42:05 -0700155 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
156
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700157 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
158 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
159 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
160
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700161 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
162
Mathew Inwood876a8462013-06-14 14:12:41 +0100163 /**
164 * IntentStarter uses request codes starting with this. This must be greater than all activity
165 * request codes used internally.
166 */
167 protected static final int REQUEST_LAST = 100;
168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700181 // Type: long
182 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700183 // Type: Content Values / parcelable
184 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000187 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800188 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Adam Cohen432609a2014-03-13 17:03:22 -0700190 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800191 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
192
Adam Cohen3ed316a2014-07-23 18:21:20 -0700193 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
194 static final String ACTION_FIRST_LOAD_COMPLETE =
195 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
196
Sunny Goyal594d76d2014-11-06 10:12:54 -0800197 private static final String QSB_WIDGET_ID = "qsb_widget_id";
198 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
199
Winson Chunga6945242014-01-08 14:04:34 -0800200 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700203 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
204 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Adam Cohenc2d6e892014-10-16 09:49:24 -0700211 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
212 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700213 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
217 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;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700369 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371
372 @Thunk void setOrientation() {
373 if (mRotationEnabled) {
374 unlockScreenOrientation(true);
375 } else {
376 setRequestedOrientation(
377 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400379 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380
Sunny Goyal7779d622015-06-11 16:18:39 -0700381 private Runnable mUpdateOrientationRunnable = new Runnable() {
382 public void run() {
383 setOrientation();
384 }
385 };
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700422
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700424 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Daniel Sandlerff02d492013-08-05 02:12:05 -0400426 mStats = new Stats(this);
427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200439 android.os.Debug.startMethodTracing(
440 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700444
Tony Wickhameef44322015-10-20 13:24:36 -0700445 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
446 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700448 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Joe Onorato7c312c12009-08-13 21:36:53 -0700450 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mSavedState = savedInstanceState;
453 restoreState(mSavedState);
454
455 if (PROFILE_STARTUP) {
456 android.os.Debug.stopMethodTracing();
457 }
458
459 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700460 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 // If the user leaves launcher, then we should just load items asynchronously when
462 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 } else {
465 // We only load the page synchronously if the user rotates (or triggers a
466 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700467 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
476 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800477
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
479 // In case we are on a device with locked rotation, we should look at preferences to check
480 // if the user has specifically allowed rotation.
481 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700482 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700483 }
484
485 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
486 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400487 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700491 if (mLauncherCallbacks.hasLauncherOverlay()) {
492 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700493 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
494 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
495 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700496 mWorkspace.setLauncherOverlay(mLauncherOverlay);
497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700499
500 if (shouldShowIntroScreen()) {
501 showIntroScreen();
502 } else {
503 showFirstRunActivity();
504 showFirstRunClings();
505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
507
Sunny Goyal7779d622015-06-11 16:18:39 -0700508 @Override
509 public void onSettingsChanged(String settings, boolean value) {
510 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
511 mRotationEnabled = value;
512 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
513 mUpdateOrientationRunnable.run();
514 }
515 }
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
527 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
528 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700530 private boolean mWorkspaceImportanceStored = false;
531 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700532 private int mWorkspaceImportanceForAccessibility =
533 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
534 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
535
536 @Override
537 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 return;
540 }
541 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700542 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 if (mWorkspace != null) {
544 mWorkspaceImportanceForAccessibility =
545 mWorkspace.getImportantForAccessibility();
546 mWorkspace.setImportantForAccessibility(
547 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
548 mWorkspaceImportanceStored = true;
549 }
550 if (mHotseat != null) {
551 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
552 mHotseat.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mHotseatImportanceStored = true;
555 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 }
557
558 @Override
559 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700560 if (mWorkspaceImportanceStored && mWorkspace != null) {
561 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
562 }
563 if (mHotseatImportanceStored && mHotseat != null) {
564 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
565 }
566 mWorkspaceImportanceStored = false;
567 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700568 }
569 });
Adam Cohen9211d422014-10-07 18:14:53 -0700570 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700571 }
572
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700573 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700574 public void onLauncherProviderChange() {
575 if (mLauncherCallbacks != null) {
576 mLauncherCallbacks.onLauncherProviderChange();
577 }
578 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579
Winson Chungef7f8742015-06-04 17:18:17 -0700580 /**
581 * Updates the bounds of all the overlays to match the new fixed bounds.
582 */
583 public void updateOverlayBounds(Rect newBounds) {
584 mAppsView.setSearchBarBounds(newBounds);
585 mWidgetsView.setSearchBarBounds(newBounds);
586 }
587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Anton Hanssona2f665f2013-09-26 12:18:32 +0100626 public Stats getStats() {
627 return mStats;
628 }
629
Hyunyoung Song3f471442015-04-08 19:01:34 -0700630 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700631 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
632 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700633 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700634 }
635
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000636 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700637 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
638 // This cast is safe as long as the id < 0x00FFFFFF
639 // Since we jail all the dynamically generated views, there should be no clashes
640 // with any other views.
641 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000642 }
643
644 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
646 * a configuration step, this allows the proper animations to run after other transitions.
647 */
Adam Cohendb364c32014-05-20 14:23:40 -0700648 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700649 long screenId = args.screenId;
650 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
651 // When the screen id represents an actual screen (as opposed to a rank) we make sure
652 // that the drop page actually exists.
653 screenId = ensurePendingDropLayoutExists(args.screenId);
654 }
Adam Cohendb364c32014-05-20 14:23:40 -0700655
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700658 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700659 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700660 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800661 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700662 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700663 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700664 case REQUEST_RECONFIGURE_APPWIDGET:
665 completeRestoreAppWidget(args.appWidgetId);
666 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 }
Michael Jurka27614d22012-04-02 06:40:22 -0700668 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
669 // if you turned the screen off and then back while in All Apps, Launcher would not
670 // return to the workspace. Clearing mAddInfo.container here fixes this issue
671 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700672 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 }
674
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800675 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700677 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700678 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700679 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800680 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700681
Adam Cohenad4e15c2013-10-17 16:21:35 -0700682 Runnable exitSpringLoaded = new Runnable() {
683 @Override
684 public void run() {
685 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
686 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
687 }
688 };
689
Michael Jurka8b805b12012-04-18 14:23:14 -0700690 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700691 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700692 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700693 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
694 if (resultCode == RESULT_CANCELED) {
695 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700696 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700697 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700698 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800699 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700700 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700701
702 // When the user has granted permission to bind widgets, we should check to see if
703 // we can inflate the default search bar widget.
704 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 }
706 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200707 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
708 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700709 // User could have free-scrolled between pages before picking a wallpaper; make sure
710 // we move to the closest one now.
711 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700712 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200713 }
714 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200716
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700718 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700719
Adam Cohendb364c32014-05-20 14:23:40 -0700720 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 // We have special handling for widgets
722 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800723 final int appWidgetId;
724 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
725 : -1;
726 if (widgetId < 0) {
727 appWidgetId = pendingAddWidgetId;
728 } else {
729 appWidgetId = widgetId;
730 }
731
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800733 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700734 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
735 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 result = RESULT_CANCELED;
737 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700738 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 @Override
740 public void run() {
741 exitSpringLoadedDragModeDelayed(false, 0, null);
742 }
743 };
Adam Cohendb364c32014-05-20 14:23:40 -0700744 if (workspaceLocked) {
745 // No need to remove the empty screen if we're mid-binding, as the
746 // the bind will not add the empty screen.
747 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
748 } else {
749 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
750 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
751 }
Winson Chung5aaab772012-04-27 15:24:02 -0700752 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700753 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700754 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
755 // When the screen id represents an actual screen (as opposed to a rank)
756 // we make sure that the drop page actually exists.
757 mPendingAddInfo.screenId =
758 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
759 }
Adam Cohendb364c32014-05-20 14:23:40 -0700760 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
761
762 dropLayout.setDropPending(true);
763 final Runnable onComplete = new Runnable() {
764 @Override
765 public void run() {
766 completeTwoStageWidgetDrop(resultCode, appWidgetId);
767 dropLayout.setDropPending(false);
768 }
769 };
770 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
771 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
772 } else {
773 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
774 mPendingAddInfo);
775 sPendingAddItem = args;
776 }
Winson Chung5aaab772012-04-27 15:24:02 -0700777 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 return;
779 }
780
Sunny Goyalff572272014-07-23 13:58:07 -0700781 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
782 if (resultCode == RESULT_OK) {
783 // Update the widget view.
784 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
785 pendingAddWidgetId, mPendingAddInfo);
786 if (workspaceLocked) {
787 sPendingAddItem = args;
788 } else {
789 completeAdd(args);
790 }
791 }
792 // Leave the widget in the pending state if the user canceled the configure.
793 return;
794 }
795
Sunny Goyalaad90582015-07-09 14:22:50 -0700796 if (requestCode == REQUEST_CREATE_SHORTCUT) {
797 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
798 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
799 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
800 mPendingAddInfo);
801 if (isWorkspaceLocked()) {
802 sPendingAddItem = args;
803 } else {
804 completeAdd(args);
805 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
807 }
808 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700809 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800813 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800814
815 }
816
817 @Override
818 protected void onActivityResult(
819 final int requestCode, final int resultCode, final Intent data) {
820 handleActivityResult(requestCode, resultCode, data);
821 if (mLauncherCallbacks != null) {
822 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
823 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 }
825
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600826 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700827 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600828 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700829 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
830 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
831 View v = null;
832 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
833 if (layout != null) {
834 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
835 }
836 Intent intent = sPendingAddItem.intent;
837 sPendingAddItem = null;
838 if (grantResults.length > 0
839 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
840 startActivity(v, intent, null);
841 } else {
842 // TODO: Show a snack bar with link to settings
843 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
844 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
845 }
846 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600847 if (mLauncherCallbacks != null) {
848 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
849 grantResults);
850 }
851 }
852
Adam Cohendb364c32014-05-20 14:23:40 -0700853 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
854 appWidgetId, ItemInfo info) {
855 PendingAddArguments args = new PendingAddArguments();
856 args.requestCode = requestCode;
857 args.intent = data;
858 args.container = info.container;
859 args.screenId = info.screenId;
860 args.cellX = info.cellX;
861 args.cellY = info.cellY;
862 args.appWidgetId = appWidgetId;
863 return args;
864 }
865
866 /**
867 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
868 *
869 * @param screenId the screen id to check
870 * @return the new screen, or screenId if it exists
871 */
872 private long ensurePendingDropLayoutExists(long screenId) {
873 CellLayout dropLayout =
874 (CellLayout) mWorkspace.getScreenWithId(screenId);
875 if (dropLayout == null) {
876 // it's possible that the add screen was removed because it was
877 // empty and a re-bind occurred
878 mWorkspace.addExtraEmptyScreen();
879 return mWorkspace.commitExtraEmptyScreen();
880 } else {
881 return screenId;
882 }
883 }
884
Adam Cohen091440a2015-03-18 14:16:05 -0700885 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800886 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 Runnable onCompleteRunnable = null;
888 int animationType = 0;
889
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700890 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 if (resultCode == RESULT_OK) {
892 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
893 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 mPendingAddWidgetInfo);
895 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 onCompleteRunnable = new Runnable() {
897 @Override
898 public void run() {
899 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700900 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
902 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 }
904 };
905 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800906 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700909 if (mDragLayer.getAnimatedView() != null) {
910 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
911 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
912 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700913 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 // The animated view may be null in the case of a rotation during widget configuration
915 onCompleteRunnable.run();
916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918
919 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700920 protected void onStop() {
921 super.onStop();
922 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700923
924 if (mLauncherCallbacks != null) {
925 mLauncherCallbacks.onStop();
926 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700927 }
928
929 @Override
930 protected void onStart() {
931 super.onStart();
932 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700933
934 if (mLauncherCallbacks != null) {
935 mLauncherCallbacks.onStart();
936 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700937 }
938
939 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200941 long startTime = 0;
942 if (DEBUG_RESUME_TIME) {
943 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400944 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200945 }
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.preOnResume();
949 }
950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700952
Winson Chung4a2afa32012-07-19 14:53:05 -0700953 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700954 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700955 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800956 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700957 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700958 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700959 // view after launching an app, as they may be depending on the UI to be static to
960 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700961 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700962 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800963 } else if (mOnResumeState == State.WIDGETS) {
964 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700965 }
966 mOnResumeState = State.NONE;
967
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700968 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700969 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
970 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700971
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800972 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700973 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700974 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700975 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700977 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700979 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200980 // We might have postponed some bind calls until onResume (see waitUntilResume) --
981 // execute them here
982 long startTimeCallbacks = 0;
983 if (DEBUG_RESUME_TIME) {
984 startTimeCallbacks = System.currentTimeMillis();
985 }
986
Michael Jurka1e2f4652013-07-08 18:03:46 -0700987 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
988 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200989 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700990 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200991 if (DEBUG_RESUME_TIME) {
992 Log.d(TAG, "Time spent processing callbacks in onResume: " +
993 (System.currentTimeMillis() - startTimeCallbacks));
994 }
Michael Jurka447bf842013-05-15 14:52:15 +0200995 }
Michael Jurka54554252013-08-01 12:52:23 +0200996 if (mOnResumeCallbacks.size() > 0) {
997 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
998 mOnResumeCallbacks.get(i).run();
999 }
1000 mOnResumeCallbacks.clear();
1001 }
Winson Chunge4e50662012-01-23 14:45:13 -08001002
1003 // Reset the pressed state of icons that were locked in the press state while activities
1004 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001005 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001006 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001007 mWaitingForResume.setStayPressed(false);
1008 }
Winson Chung82963d52013-10-09 11:20:57 -07001009
Adam Cohen06dff352012-06-01 17:17:08 -07001010 // It is possible that widgets can receive updates while launcher is not in the foreground.
1011 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1012 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1013 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001014 if (!isWorkspaceLoading()) {
1015 getWorkspace().reinflateWidgetsIfNecessary();
1016 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001017 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001018
Michael Jurka447bf842013-05-15 14:52:15 +02001019 if (DEBUG_RESUME_TIME) {
1020 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1021 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001022
Adam Cohen4b66bf32015-09-18 12:15:19 -07001023 // We want to suppress callbacks about CustomContent being shown if we have just received
1024 // onNewIntent while the user was present within launcher. In that case, we post a call
1025 // to move the user to the main screen (which will occur after onResume). We don't want to
1026 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1027 // suppress here.
1028 if (mWorkspace.getCustomContentCallbacks() != null
1029 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001030 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001031 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001032 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1033 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001034 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001035 }
1036 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001037 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001038 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001039 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001040
Sunny Goyal756adbc2015-04-16 15:20:51 -07001041 if (!isWorkspaceLoading()) {
1042 // Process any items that were added while Launcher was away.
1043 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1044 }
Adam Cohen9211d422014-10-07 18:14:53 -07001045
1046 if (mLauncherCallbacks != null) {
1047 mLauncherCallbacks.onResume();
1048 }
Adam Cohen06dff352012-06-01 17:17:08 -07001049 }
1050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001052 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001053 // Ensure that items added to Launcher are queued until Launcher returns
1054 InstallShortcutReceiver.enableInstallQueue();
1055
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001057 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001058 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001059 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001060
1061 // We call onHide() aggressively. The custom content callbacks should be able to
1062 // debounce excess onHide calls.
1063 if (mWorkspace.getCustomContentCallbacks() != null) {
1064 mWorkspace.getCustomContentCallbacks().onHide();
1065 }
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Adam Cohen9211d422014-10-07 18:14:53 -07001067 if (mLauncherCallbacks != null) {
1068 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001069 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 }
1071
1072 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001073 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1074 // by a onResume or by scrolling otherwise.
1075 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001076
1077 // Custom content is completely hidden
1078 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001079
1080 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1081 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001082
1083 // Indicates whether the user is allowed to scroll away from the custom content.
1084 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
1086
Adam Cohenc2d6e892014-10-16 09:49:24 -07001087 public interface LauncherOverlay {
1088
1089 /**
1090 * Touch interaction leading to overscroll has begun
1091 */
1092 public void onScrollInteractionBegin();
1093
1094 /**
1095 * Touch interaction related to overscroll has ended
1096 */
1097 public void onScrollInteractionEnd();
1098
1099 /**
1100 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1101 * screen (or in the case of RTL, the rightmost screen).
1102 */
1103 public void onScrollChange(int progress, boolean rtl);
1104
1105 /**
1106 * Screen has stopped scrolling
1107 */
1108 public void onScrollSettled();
1109
1110 /**
1111 * This method can be called by the Launcher in order to force the LauncherOverlay
1112 * to exit fully immersive mode.
1113 */
1114 public void forceExitFullImmersion();
1115 }
1116
Jun Mukai8af0cd82015-05-12 12:32:12 -07001117 public interface LauncherSearchCallbacks {
1118 /**
1119 * Called when the search overlay is shown.
1120 */
1121 public void onSearchOverlayOpened();
1122
1123 /**
1124 * Called when the search overlay is dismissed.
1125 */
1126 public void onSearchOverlayClosed();
1127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlayCallbacks {
1130 /**
1131 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1132 * however it doesn't modify any state within the launcher.
1133 */
1134 public boolean canEnterFullImmersion();
1135
1136 /**
1137 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1138 * eg. by occupying the full screen and handling all touch events.
1139 *
1140 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1141 * case, Launcher will modify any necessary state and assumes the overlay is
1142 * handling all interaction. If false, the LauncherOverlay should cancel any
1143 *
1144 */
1145 public boolean enterFullImmersion();
1146
1147 /**
1148 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1149 * full control over UI and state.
1150 */
1151 public void exitFullImmersion();
1152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
1156 @Override
1157 public boolean canEnterFullImmersion() {
1158 return mState == State.WORKSPACE;
1159 }
1160
1161 @Override
1162 public boolean enterFullImmersion() {
1163 if (mState == State.WORKSPACE) {
1164 // When fully immersed, disregard any touches which fall through.
1165 mDragLayer.setBlockTouch(true);
1166 return true;
1167 }
1168 return false;
1169 }
1170
1171 @Override
1172 public void exitFullImmersion() {
1173 mDragLayer.setBlockTouch(false);
1174 }
1175 }
1176
Jorim Jaggid017f882014-01-14 17:08:48 -08001177 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001178 if (mLauncherCallbacks != null) {
1179 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001180 } else {
1181 // On devices with a locked orientation, we will at least have the allow rotation
1182 // setting.
1183 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001184 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001185 }
1186
Adam Cohen9211d422014-10-07 18:14:53 -07001187 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001188 CustomContentCallbacks callbacks, String description) {
1189 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001190 }
1191
Adam Cohenedb40762013-07-18 16:45:45 -07001192 // The custom content needs to offset its content to account for the QSB
1193 public int getTopOffsetForCustomContent() {
1194 return mWorkspace.getPaddingTop();
1195 }
1196
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001197 @Override
1198 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001200 if (mModel.isCurrentCallbacks(this)) {
1201 mModel.stopLoader();
1202 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001203 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1204
Romain Guy13c2e7b2010-03-10 19:45:00 -08001205 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001206 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001208 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 @Override
1210 public void onWindowFocusChanged(boolean hasFocus) {
1211 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001212 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001213
1214 if (mLauncherCallbacks != null) {
1215 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1216 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 private boolean acceptFilter() {
1220 final InputMethodManager inputManager = (InputMethodManager)
1221 getSystemService(Context.INPUT_METHOD_SERVICE);
1222 return !inputManager.isFullscreenMode();
1223 }
1224
1225 @Override
1226 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001227 final int uniChar = event.getUnicodeChar();
1228 final boolean handled = super.onKeyDown(keyCode, event);
1229 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1230 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1232 keyCode, event);
1233 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001234 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001235 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // showSearchDialog()
1237 // If there are multiple keystrokes before the search dialog takes focus,
1238 // onSearchRequested() will be called for every keystroke,
1239 // but it is idempotent, so it's fine.
1240 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
1243
Joe Onorato8a9625e2010-01-28 15:55:35 -08001244 // Eat the long press event so the keyboard doesn't come up.
1245 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1246 return true;
1247 }
1248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 return handled;
1250 }
1251
Winson46163472015-09-22 17:31:56 -07001252 @Override
1253 public boolean onKeyUp(int keyCode, KeyEvent event) {
1254 if (keyCode == KeyEvent.KEYCODE_MENU) {
1255 // Ignore the menu key if we are currently dragging or are on the custom content screen
1256 if (!isOnCustomContent() && !mDragController.isDragging()) {
1257 // Close any open folders
1258 closeFolder();
1259
1260 // Stop resizing any widgets
1261 mWorkspace.exitWidgetResizeMode();
1262
1263 // Show the overview mode if we are on the workspace
1264 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1265 !mWorkspace.isSwitchingState()) {
1266 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001267 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001268 }
1269 }
1270 return true;
1271 }
1272 return super.onKeyUp(keyCode, event);
1273 }
1274
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 private String getTypedText() {
1276 return mDefaultKeySsb.toString();
1277 }
1278
1279 private void clearTypedText() {
1280 mDefaultKeySsb.clear();
1281 mDefaultKeySsb.clearSpans();
1282 Selection.setSelection(mDefaultKeySsb, 0);
1283 }
1284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001286 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1287 * State
1288 */
1289 private static State intToState(int stateOrdinal) {
1290 State state = State.WORKSPACE;
1291 final State[] stateValues = State.values();
1292 for (int i = 0; i < stateValues.length; i++) {
1293 if (stateValues[i].ordinal() == stateOrdinal) {
1294 state = stateValues[i];
1295 break;
1296 }
1297 }
1298 return state;
1299 }
1300
1301 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 * Restores the previous state, if it exists.
1303 *
1304 * @param savedState The previous state.
1305 */
1306 private void restoreState(Bundle savedState) {
1307 if (savedState == null) {
1308 return;
1309 }
1310
Michael Jurka883f55b2010-10-21 15:47:14 -07001311 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001312 if (state == State.APPS || state == State.WIDGETS) {
1313 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001314 }
1315
Adam Cohen21cd0022013-10-09 18:57:02 -07001316 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1317 PagedView.INVALID_RESTORE_PAGE);
1318 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001319 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
Sunny Goyal756cd262015-08-20 12:33:21 -07001322 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1323 if (itemValues != null) {
1324 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001325 AppWidgetProviderInfo info = savedState.getParcelable(
1326 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001327 mPendingAddWidgetInfo = info == null ?
1328 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1329
Adam Cohen4637b5a2013-11-04 18:21:24 -08001330 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001331 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 mRestoring = true;
1333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 /**
1337 * Finds all the views we need and configure them properly.
1338 */
1339 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001340 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001341
Craig Mautner360310b2012-10-26 15:13:08 -07001342 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001343 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001344 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1345 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001346 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001347 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001348
John Spurlock77e1f472013-09-11 10:09:51 -04001349 mLauncherView.setSystemUiVisibility(
1350 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
Winson Chung4c98d922011-05-31 16:50:48 -07001353 // Setup the drag layer
1354 mDragLayer.setup(this, dragController);
1355
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 // Setup the hotseat
1357 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1358 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001359 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 }
1361
Winsone9f27272015-10-13 10:47:51 -07001362 // Setup the overview panel
1363 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001364
Winson Chung4c98d922011-05-31 16:50:48 -07001365 // Setup the workspace
1366 mWorkspace.setHapticFeedbackEnabled(false);
1367 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001368 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001369 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001370
Tony Wickham34d2c912015-09-11 09:27:58 -07001371 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001372 mSearchDropTargetBar = (SearchDropTargetBar)
1373 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001374 // Get the app info bar
1375 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1376 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001377
Winson Chungef7f8742015-06-04 17:18:17 -07001378 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001379 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001380 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyal954b7d32016-02-11 09:28:40 -08001381 mAppsView.setSearchBarController(new DefaultAppSearchController());
1382
1383 // TODO: Reenable this
1384// if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1385// mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1386// } else {
1387// mAppsView.setSearchBarController(new DefaultAppSearchController());
1388// }
Craig Mautner360310b2012-10-26 15:13:08 -07001389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001391 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001392 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001393 dragController.setMoveTarget(mWorkspace);
1394 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001395 if (mSearchDropTargetBar != null) {
1396 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001397 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001398 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001399 if (mAppInfoDropTargetBar != null) {
1400 mAppInfoDropTargetBar.setup(this, dragController);
1401 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001402
Sunny Goyal322d5562015-06-25 19:35:49 -07001403 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1404 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407
Winsone9f27272015-10-13 10:47:51 -07001408 private void setupOverviewPanel() {
1409 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1410
1411 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1412 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1413 @Override
1414 public boolean onLongClick(View v) {
1415 return v.performClick();
1416 }
1417 };
1418
1419 // Bind wallpaper button actions
1420 View wallpaperButton = findViewById(R.id.wallpaper_button);
1421 wallpaperButton.setOnClickListener(new OnClickListener() {
1422 @Override
1423 public void onClick(View view) {
1424 if (!mWorkspace.isSwitchingState()) {
1425 onClickWallpaperPicker(view);
1426 }
1427 }
1428 });
1429 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1430 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1431
1432 // Bind widget button actions
1433 mWidgetsButton = findViewById(R.id.widget_button);
1434 mWidgetsButton.setOnClickListener(new OnClickListener() {
1435 @Override
1436 public void onClick(View view) {
1437 if (!mWorkspace.isSwitchingState()) {
1438 onClickAddWidgetButton(view);
1439 }
1440 }
1441 });
1442 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1443 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1444
1445 // Bind settings actions
1446 View settingsButton = findViewById(R.id.settings_button);
1447 boolean hasSettings = hasSettings();
1448 if (hasSettings) {
1449 settingsButton.setOnClickListener(new OnClickListener() {
1450 @Override
1451 public void onClick(View view) {
1452 if (!mWorkspace.isSwitchingState()) {
1453 onClickSettingsButton(view);
1454 }
1455 }
1456 });
1457 settingsButton.setOnLongClickListener(performClickOnLongClick);
1458 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1459 } else {
1460 settingsButton.setVisibility(View.GONE);
1461 }
1462
1463 mOverviewPanel.setAlpha(0f);
1464 }
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 * Sets the all apps button. This method is called from {@link Hotseat}.
1468 */
1469 public void setAllAppsButton(View allAppsButton) {
1470 mAllAppsButton = allAppsButton;
1471 }
1472
1473 public View getAllAppsButton() {
1474 return mAllAppsButton;
1475 }
1476
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001477 public View getWidgetsButton() {
1478 return mWidgetsButton;
1479 }
1480
Anjali Koppal5ad44842014-03-10 20:34:39 -07001481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 * Creates a view representing a shortcut.
1483 *
1484 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001487 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * @param parent The group the shortcut belongs to.
1494 * @param info The data structure describing the shortcut.
1495 *
1496 * @return A View inflated from layoutResId.
1497 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001498 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001499 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001500 parent, false);
1501 favorite.applyFromShortcutInfo(info, mIconCache);
1502 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001504 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return favorite;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Add a shortcut to the workspace.
1510 *
1511 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Sunny Goyal5c97f512015-05-19 16:03:28 -07001518 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001519 if (info == null) {
1520 return;
1521 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 final View view = createShortcut(info);
1523
Sunny Goyal5c97f512015-05-19 16:03:28 -07001524 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 // First we check if we already know the exact location where we want to add this item.
1526 if (cellX >= 0 && cellY >= 0) {
1527 cellXY[0] = cellX;
1528 cellXY[1] = cellY;
1529 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001530
1531 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001532 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 true, null,null)) {
1534 return;
1535 }
1536 DragObject dragObject = new DragObject();
1537 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001538 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1539 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001540 return;
1541 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 }
1545
1546 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001547 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 return;
1549 }
1550
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001551 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001555 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557 }
1558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001560 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001562 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 */
Adam Cohen091440a2015-03-18 14:16:05 -07001564 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001565 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1566
1567 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001569 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1570 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Adam Cohen59400422014-03-05 18:07:04 -08001573 if (appWidgetInfo.isCustomWidget) {
1574 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001575 }
1576
Adam Cohen59400422014-03-05 18:07:04 -08001577 LauncherAppWidgetInfo launcherInfo;
1578 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1579 launcherInfo.spanX = info.spanX;
1580 launcherInfo.spanY = info.spanY;
1581 launcherInfo.minSpanX = info.minSpanX;
1582 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001583 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001586 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
1588 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001589 if (hostView == null) {
1590 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001591 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1592 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001593 } else {
1594 // The AppWidgetHostView has already been inflated and instantiated
1595 launcherInfo.hostView = hostView;
1596 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001597 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001598 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001600 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
Romain Guycbb89e42009-06-08 15:52:54 -07001602
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001603 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1604 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1605 item.hostView.setTag(item);
1606 item.onBindAppWidget(this);
1607
1608 item.hostView.setFocusable(true);
1609 item.hostView.setOnFocusChangeListener(mFocusHandler);
1610
1611 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1612 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1613
1614 if (!item.isCustomWidget()) {
1615 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1616 }
1617 }
1618
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1620 @Override
1621 public void onReceive(Context context, Intent intent) {
1622 final String action = intent.getAction();
1623 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1624 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001625 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001626 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001627
Winson Chungc100e8e2011-08-09 16:02:43 -07001628 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001629 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001630 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001631 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001632 if (!showWorkspace(false)) {
1633 // If we are already on the workspace, then manually reset all apps
1634 mAppsView.reset();
1635 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001636 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1638 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001639 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001640 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1641 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001642 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001643 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1644 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1645 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001646 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001647 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1648 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 }
1650 }
1651 };
1652
1653 @Override
1654 public void onAttachedToWindow() {
1655 super.onAttachedToWindow();
1656
1657 // Listen for broadcasts related to user-presence
1658 final IntentFilter filter = new IntentFilter();
1659 filter.addAction(Intent.ACTION_SCREEN_OFF);
1660 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001661 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001662 if (ENABLE_DEBUG_INTENTS) {
1663 filter.addAction(DebugIntents.DELETE_DATABASE);
1664 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1665 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001667 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001668 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 mVisible = true;
1670 }
1671
1672 @Override
1673 public void onDetachedFromWindow() {
1674 super.onDetachedFromWindow();
1675 mVisible = false;
1676
Adam Cohend113e0c2010-11-11 10:48:05 -08001677 if (mAttached) {
1678 unregisterReceiver(mReceiver);
1679 mAttached = false;
1680 }
Winson Chungb745afb2015-03-02 11:51:23 -08001681 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 }
1683
1684 public void onWindowVisibilityChanged(int visibility) {
1685 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001686 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001687 // The following code used to be in onResume, but it turns out onResume is called when
1688 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1689 // is a more appropriate event to handle
1690 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001691 if (!mWorkspaceLoading) {
1692 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 // We want to let Launcher draw itself at least once before we force it to build
1694 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001695 // apps is nice and speedy.
1696 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001698 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001699 if (mStarted) return;
1700 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001701 // We delay the layer building a bit in order to give
1702 // other message processing a time to run. In particular
1703 // this avoids a delay in hiding the IME if it was
1704 // currently shown, because doing that may involve
1705 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001706 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001707 final ViewTreeObserver.OnDrawListener listener = this;
1708 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001709 public void run() {
1710 if (mWorkspace != null &&
1711 mWorkspace.getViewTreeObserver() != null) {
1712 mWorkspace.getViewTreeObserver().
1713 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001714 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001715 }
1716 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001717 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001718 }
1719 });
1720 }
1721 clearTypedText();
1722 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 }
1724
Adam Cohen091440a2015-03-18 14:16:05 -07001725 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 mHandler.removeMessages(ADVANCE_MSG);
1727 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1728 mHandler.sendMessageDelayed(msg, delay);
1729 mAutoAdvanceSentTime = System.currentTimeMillis();
1730 }
1731
Adam Cohen091440a2015-03-18 14:16:05 -07001732 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1734 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1735 mAutoAdvanceRunning = autoAdvanceRunning;
1736 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001737 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 sendAdvanceMessage(delay);
1739 } else {
1740 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1743 }
1744 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001745 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
1747 }
1748 }
1749
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001750 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1751
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001753 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 if (msg.what == ADVANCE_MSG) {
1755 int i = 0;
1756 for (View key: mWidgetsToAdvance.keySet()) {
1757 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001758 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 public void run() {
1762 ((Advanceable) v).advance();
1763 }
1764 }, delay);
1765 }
1766 i++;
1767 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001768 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001770 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001772 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001773
Winsonc0b52fe2015-09-09 16:38:15 -07001774 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001775 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1777 if (v instanceof Advanceable) {
1778 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001779 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001780 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 }
1782 }
1783
Winsonc0b52fe2015-09-09 16:38:15 -07001784 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 if (mWidgetsToAdvance.containsKey(hostView)) {
1786 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001787 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001789 }
1790
Hyunyoung Song3f471442015-04-08 19:01:34 -07001791 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001792 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1793 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 }
1795
Winson Chunga6945242014-01-08 14:04:34 -08001796 public DragLayer getDragLayer() {
1797 return mDragLayer;
1798 }
1799
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001800 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001801 return mAppsView;
1802 }
1803
Hyunyoung Song3f471442015-04-08 19:01:34 -07001804 public WidgetsContainerView getWidgetsView() {
1805 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001806 }
1807
Winson Chunga6945242014-01-08 14:04:34 -08001808 public Workspace getWorkspace() {
1809 return mWorkspace;
1810 }
1811
1812 public Hotseat getHotseat() {
1813 return mHotseat;
1814 }
1815
Jorim Jaggid017f882014-01-14 17:08:48 -08001816 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001817 return mOverviewPanel;
1818 }
1819
Winson Chung006ee262015-08-03 14:40:11 -07001820 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001821 return mSearchDropTargetBar;
1822 }
1823
Tony Wickham34d2c912015-09-11 09:27:58 -07001824 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1825 return mAppInfoDropTargetBar;
1826 }
1827
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001828 public LauncherAppWidgetHost getAppWidgetHost() {
1829 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
Romain Guycbb89e42009-06-08 15:52:54 -07001831
Winson Chunga9abd0e2010-10-27 17:18:37 -07001832 public LauncherModel getModel() {
1833 return mModel;
1834 }
1835
Winson Chunga6945242014-01-08 14:04:34 -08001836 protected SharedPreferences getSharedPrefs() {
1837 return mSharedPrefs;
1838 }
1839
Adam Cohen2e6da152015-05-06 11:42:25 -07001840 public DeviceProfile getDeviceProfile() {
1841 return mDeviceProfile;
1842 }
1843
Bjorn Bringertc459e522013-06-07 19:36:01 +01001844 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001845 getWindow().closeAllPanels();
1846
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001847 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001848 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001849 }
1850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 @Override
1852 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001853 long startTime = 0;
1854 if (DEBUG_RESUME_TIME) {
1855 startTime = System.currentTimeMillis();
1856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 super.onNewIntent(intent);
1858
1859 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001860 Folder openFolder = mWorkspace.getOpenFolder();
1861 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1862 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1863 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1864 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1865 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001866 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001867 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001868
Adam Cohen6fecd412013-10-02 17:41:50 -07001869 if (mWorkspace == null) {
1870 // Can be cases where mWorkspace is null, this prevents a NPE
1871 return;
1872 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 // In all these cases, only animate if we're already on home
1874 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001875
Sunny Goyal935fca12015-10-13 10:19:01 -07001876 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 exitSpringLoadedDragMode();
1878
1879 // If we are already on home, then just animate back to the workspace,
1880 // otherwise, just wait until onResume to set the state back to Workspace
1881 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001882 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 } else {
1884 mOnResumeState = State.WORKSPACE;
1885 }
1886
1887 final View v = getWindow().peekDecorView();
1888 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001889 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 INPUT_METHOD_SERVICE);
1891 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1892 }
1893
Winson Chungb745afb2015-03-02 11:51:23 -08001894 // Reset the apps view
1895 if (!alreadyOnHome && mAppsView != null) {
1896 mAppsView.scrollToTop();
1897 }
1898
Hyunyoung Song3f471442015-04-08 19:01:34 -07001899 // Reset the widgets view
1900 if (!alreadyOnHome && mWidgetsView != null) {
1901 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903
Adam Cohen9211d422014-10-07 18:14:53 -07001904 if (mLauncherCallbacks != null) {
1905 mLauncherCallbacks.onHomeIntent();
1906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Adam Cohened307df2013-10-02 09:37:31 -07001908
Adam Cohen9211d422014-10-07 18:14:53 -07001909 if (mLauncherCallbacks != null) {
1910 mLauncherCallbacks.onNewIntent(intent);
1911 }
Winson15f8b172015-08-19 11:02:39 -07001912
1913 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1914 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1915 // animation.
1916 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001917 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1918 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001919 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1920 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001921
1922 // We use this flag to suppress noisy callbacks above custom content state
1923 // from onResume.
1924 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001925 mWorkspace.post(new Runnable() {
1926 @Override
1927 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001928 if (mWorkspace != null) {
1929 mWorkspace.moveToDefaultScreen(true);
1930 }
Winson15f8b172015-08-19 11:02:39 -07001931 }
1932 });
1933 }
1934 }
1935
1936 if (DEBUG_RESUME_TIME) {
1937 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1938 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001942 public void onRestoreInstanceState(Bundle state) {
1943 super.onRestoreInstanceState(state);
1944 for (int page: mSynchronouslyBoundPages) {
1945 mWorkspace.restoreInstanceStateForChild(page);
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
1949 @Override
1950 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001952 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1953 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001954 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001955 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956
Michael Jurka883f55b2010-10-21 15:47:14 -07001957 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001958 Folder openFolder = mWorkspace.getOpenFolder();
1959 if (openFolder != null) {
1960 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001965 ContentValues itemValues = new ContentValues();
1966 mPendingAddInfo.writeToValues(itemValues);
1967 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001968 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001969 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
Hyunyoung Song3f471442015-04-08 19:01:34 -07001972 // Save the current widgets tray?
1973 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001974
1975 if (mLauncherCallbacks != null) {
1976 mLauncherCallbacks.onSaveInstanceState(outState);
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979
1980 @Override
1981 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001983
Winson Chunge7a03942011-08-05 15:05:12 -07001984 // Remove all pending runnables
1985 mHandler.removeMessages(ADVANCE_MSG);
1986 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001987 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001988
Winson Chungcd2b0142011-06-08 16:02:26 -07001989 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001990 // It's possible to receive onDestroy after a new Launcher activity has
1991 // been created. In this case, don't interfere with the new Launcher.
1992 if (mModel.isCurrentCallbacks(this)) {
1993 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001994 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001995 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001998 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002000 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002002 mAppWidgetHost = null;
2003
2004 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005
2006 TextKeyListener.getInstance().release();
2007
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002008 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009
Michael Jurka2ecf9952012-06-18 12:52:28 -07002010 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002011
2012 if (mLauncherCallbacks != null) {
2013 mLauncherCallbacks.onDestroy();
2014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
Adam Cohena9cf38f2011-05-02 15:36:58 -07002017 public DragController getDragController() {
2018 return mDragController;
2019 }
2020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 @Override
2022 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002023 onStartForResult(requestCode);
2024 super.startActivityForResult(intent, requestCode);
2025 }
2026
2027 @Override
2028 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2029 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2030 onStartForResult(requestCode);
2031 try {
2032 super.startIntentSenderForResult(intent, requestCode,
2033 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2034 } catch (IntentSender.SendIntentException e) {
2035 throw new ActivityNotFoundException();
2036 }
2037 }
2038
2039 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002040 if (requestCode >= 0) {
2041 setWaitingForResult(true);
2042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
2044
Winson Chung70d72102011-08-12 11:24:35 -07002045 /**
2046 * Indicates that we want global search for this activity by setting the globalSearch
2047 * argument for {@link #startSearch} to true.
2048 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002050 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002052
Karl Rosaen138a0412009-04-23 19:00:21 -07002053 if (initialQuery == null) {
2054 // Use any text typed in the launcher as the initial query
2055 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 if (appSearchData == null) {
2058 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002059 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
Winson Chungadf0c182012-08-23 14:59:07 -07002061 Rect sourceBounds = new Rect();
2062 if (mSearchDropTargetBar != null) {
2063 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2064 }
Romain Guycbb89e42009-06-08 15:52:54 -07002065
Ian Parkinson047036e2014-09-01 15:40:53 +01002066 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002067 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 if (clearTextImmediately) {
2069 clearTypedText();
2070 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002071
2072 // We need to show the workspace after starting the search
2073 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 }
2075
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 /**
2077 * Start a text search.
2078 *
2079 * @return {@code true} if the search will start immediately, so any further keypresses
2080 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2081 * to buffer keypresses.
2082 */
2083 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002085 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2086 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2087 sourceBounds);
2088 }
2089
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002092 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 }
2094
2095 /**
2096 * Starts the global search activity. This code is a copied from SearchManager
2097 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002098 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002100 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2102 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2103 if (globalSearchActivity == null) {
2104 Log.w(TAG, "No global search activity found.");
2105 return;
2106 }
2107 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2108 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2109 intent.setComponent(globalSearchActivity);
2110 // Make sure that we have a Bundle to put source in
2111 if (appSearchData == null) {
2112 appSearchData = new Bundle();
2113 } else {
2114 appSearchData = new Bundle(appSearchData);
2115 }
Adam Cohen9211d422014-10-07 18:14:53 -07002116 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002117 if (!appSearchData.containsKey("source")) {
2118 appSearchData.putString("source", getPackageName());
2119 }
2120 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2121 if (!TextUtils.isEmpty(initialQuery)) {
2122 intent.putExtra(SearchManager.QUERY, initialQuery);
2123 }
2124 if (selectInitialQuery) {
2125 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2126 }
2127 intent.setSourceBounds(sourceBounds);
2128 try {
2129 startActivity(intent);
2130 } catch (ActivityNotFoundException ex) {
2131 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 }
2134
Winson Chungbedf9232015-07-10 12:38:30 -07002135 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2136 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2137 return;
2138 }
2139
2140 // If not handled, then just start the provided search intent
2141 startActivitySafely(v, searchIntent, null);
2142 }
2143
Yura4f93ec62014-02-04 14:15:21 +00002144 public boolean isOnCustomContent() {
2145 return mWorkspace.isOnOrMovingToCustomContent();
2146 }
2147
Winson Chung70d72102011-08-12 11:24:35 -07002148 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002149 public boolean onPrepareOptionsMenu(Menu menu) {
2150 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002151 if (mLauncherCallbacks != null) {
2152 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2153 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002154 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002157 @Override
2158 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002159 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002160 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002161 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002162 }
2163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 public boolean isWorkspaceLocked() {
2165 return mWorkspaceLoading || mWaitingForResult;
2166 }
2167
Adam Cohen517a7f52014-03-01 12:12:59 -08002168 public boolean isWorkspaceLoading() {
2169 return mWorkspaceLoading;
2170 }
2171
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002172 private void setWorkspaceLoading(boolean value) {
2173 boolean isLocked = isWorkspaceLocked();
2174 mWorkspaceLoading = value;
2175 if (isLocked != isWorkspaceLocked()) {
2176 onWorkspaceLockedChanged();
2177 }
2178 }
2179
2180 private void setWaitingForResult(boolean value) {
2181 boolean isLocked = isWorkspaceLocked();
2182 mWaitingForResult = value;
2183 if (isLocked != isWorkspaceLocked()) {
2184 onWorkspaceLockedChanged();
2185 }
2186 }
2187
Adam Cohen9211d422014-10-07 18:14:53 -07002188 protected void onWorkspaceLockedChanged() {
2189 if (mLauncherCallbacks != null) {
2190 mLauncherCallbacks.onWorkspaceLockedChanged();
2191 }
2192 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002193
Michael Jurka0280c3b2010-09-17 15:00:07 -07002194 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002195 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002196 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002197 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2198 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002199 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002201
Tony Wickhama0628cc2015-10-14 15:23:04 -07002202 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002203 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002204 if (LOGD) {
2205 Log.d(TAG, "Adding widget from drop");
2206 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002207 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2208 }
2209
Sunny Goyale6b63a32015-01-16 16:14:29 -08002210 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002211 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2212 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002213 if (appWidgetInfo.configure != null) {
2214 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002215 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002216
Bjorn Bringert7984c942009-12-09 15:38:25 +00002217 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002218 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2219 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002222 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002223 Runnable onComplete = new Runnable() {
2224 @Override
2225 public void run() {
2226 // Exit spring loaded mode if necessary after adding the widget
2227 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2228 null);
2229 }
2230 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002231 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002232 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002233 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 }
2235 }
Romain Guycbb89e42009-06-08 15:52:54 -07002236
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002237 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002238 // Close any folders that may be open.
2239 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002240 mWorkspace.moveToCustomContentScreen(animate);
2241 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002242
2243 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2244 int[] cell, int spanX, int spanY) {
2245 switch (info.itemType) {
2246 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2247 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2248 int span[] = new int[2];
2249 span[0] = spanX;
2250 span[1] = spanY;
2251 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2252 container, screenId, cell, span);
2253 break;
2254 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2255 processShortcutFromDrop(info.componentName, container, screenId, cell);
2256 break;
2257 default:
2258 throw new IllegalStateException("Unknown item type: " + info.itemType);
2259 }
2260 }
2261
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 /**
2263 * Process a shortcut drop.
2264 *
2265 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002269 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2270 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002271 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274
2275 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.cellX = cell[0];
2277 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002279
2280 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2281 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002282 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002283 }
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 /**
2286 * Process a widget drop.
2287 *
2288 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002292 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2293 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002295 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002297 mPendingAddInfo.minSpanX = info.minSpanX;
2298 mPendingAddInfo.minSpanY = info.minSpanY;
2299
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 mPendingAddInfo.cellX = cell[0];
2302 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002303 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002304 if (span != null) {
2305 mPendingAddInfo.spanX = span[0];
2306 mPendingAddInfo.spanY = span[1];
2307 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308
Adam Cohened66b2b2012-01-23 17:28:51 -08002309 AppWidgetHostView hostView = info.boundWidget;
2310 int appWidgetId;
2311 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002312 // In the case where we've prebound the widget, we remove it from the DragLayer
2313 if (LOGD) {
2314 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2315 }
2316 getDragLayer().removeView(hostView);
2317
Adam Cohened66b2b2012-01-23 17:28:51 -08002318 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002319 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002320
2321 // Clear the boundWidget so that it doesn't get destroyed.
2322 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002324 // In this case, we either need to start an activity to get permission to bind
2325 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002326 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002327 Bundle options = info.bindOptions;
2328
Sunny Goyalffe83f12014-08-14 17:39:34 -07002329 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2330 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002331 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002332 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002333 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002334 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002335 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2336 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2337 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002338 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2339 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002340 // TODO: we need to make sure that this accounts for the options bundle.
2341 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002342 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2343 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002344 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 }
2346
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002349 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 folderInfo.title = getText(R.string.folder_name);
2351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002353 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2354 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002355 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
2357 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 FolderIcon newFolder =
2359 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002362 // Force measure the new folder icon
2363 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2364 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002365 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Winsonc0b52fe2015-09-09 16:38:15 -07002368 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002369 * Unbinds the view for the specified item, and removes the item and all its children.
2370 *
2371 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002372 * @param itemInfo the {@link ItemInfo} for this view.
2373 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002374 */
Winson2949fb52015-09-24 09:56:11 -07002375 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002377 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002378 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2379 if (folderInfo != null) {
2380 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002381 } else {
2382 mWorkspace.removeWorkspaceItem(v);
2383 }
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
2385 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2386 }
2387 } else if (itemInfo instanceof FolderInfo) {
2388 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2389 unbindFolder(folderInfo);
2390 mWorkspace.removeWorkspaceItem(v);
2391 if (deleteFromDb) {
2392 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2393 }
2394 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2395 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002396 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 removeWidgetToAutoAdvance(widgetInfo.hostView);
2398 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002399 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002400 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002401 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002402
Winsonc0b52fe2015-09-09 16:38:15 -07002403 } else {
2404 return false;
2405 }
2406 return true;
2407 }
2408
2409 /**
2410 * Unbinds any launcher references to the folder.
2411 */
2412 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002413 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002414 }
2415
Winsonc0b52fe2015-09-09 16:38:15 -07002416 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002417 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002418 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002419 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002420 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002421 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002422 // Deleting an app widget ID is a void call but writes to disk before returning
2423 // to the caller...
2424 new AsyncTask<Void, Void, Void>() {
2425 public Void doInBackground(Void ... args) {
2426 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2427 return null;
2428 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002429 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002430 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002431 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002432 }
2433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 @Override
2435 public boolean dispatchKeyEvent(KeyEvent event) {
2436 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2437 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002440 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002441 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002442 dumpState();
2443 return true;
2444 }
2445 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 }
2447 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2448 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002449 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 return true;
2451 }
2452 }
2453
2454 return super.dispatchKeyEvent(event);
2455 }
2456
Joe Onorato88ec0992009-11-19 13:16:06 -08002457 @Override
2458 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002459 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2460 return;
2461 }
2462
Sunny Goyal45478022015-06-08 16:52:41 -07002463 if (mDragController.isDragging()) {
2464 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002465 return;
2466 }
2467
Winson Chungb745afb2015-03-02 11:51:23 -08002468 if (isAppsViewVisible()) {
2469 showWorkspace(true);
2470 } else if (isWidgetsViewVisible()) {
2471 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002473 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002475 Folder openFolder = mWorkspace.getOpenFolder();
2476 if (openFolder.isEditingName()) {
2477 openFolder.dismissEditingName();
2478 } else {
2479 closeFolder();
2480 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002481 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002482 mWorkspace.exitWidgetResizeMode();
2483
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 // Back button is a no-op here, but give at least some feedback for the button press
2485 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002486 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002490 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002491 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002492 @Override
2493 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002494 if (mAppWidgetHost != null) {
2495 mAppWidgetHost.startListening();
2496 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002497
2498 // Recreate the QSB, as the widget has been reset.
2499 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002500 }
2501
2502 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 * Launches the intent referred by the clicked shortcut.
2504 *
2505 * @param v The view representing the clicked shortcut.
2506 */
2507 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002508 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2509 // view has detached (it's possible for this to happen if the view is removed mid touch).
2510 if (v.getWindowToken() == null) {
2511 return;
2512 }
2513
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002514 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002515 return;
2516 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002517
Adam Cohen1697b792013-09-17 19:08:21 -07002518 if (v instanceof Workspace) {
2519 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002520 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002521 }
2522 return;
2523 }
2524
2525 if (v instanceof CellLayout) {
2526 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002527 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2528 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002529 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002530 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002531 }
2532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002534 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002537 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002539 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002540 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002542 } else if (tag instanceof AppInfo) {
2543 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002544 } else if (tag instanceof LauncherAppWidgetInfo) {
2545 if (v instanceof PendingAppWidgetHostView) {
2546 onClickPendingWidget((PendingAppWidgetHostView) v);
2547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 }
2549 }
2550
Sunny Goyal70660032015-05-14 00:07:08 -07002551 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002552 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002553 return false;
2554 }
2555
Michael Jurkaaf442092010-06-10 17:01:57 -07002556 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002557 * Event handler for the app widget view which has not fully restored.
2558 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002559 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002560 if (mIsSafeModeEnabled) {
2561 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2562 return;
2563 }
2564
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002566 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002567 int widgetId = info.appWidgetId;
2568 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2569 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002570 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2571 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002572 mPendingAddInfo.copyFrom(info);
2573 mPendingAddWidgetId = widgetId;
2574
Sunny Goyalffe83f12014-08-14 17:39:34 -07002575 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2576 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002577 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 } else if (info.installProgress < 0) {
2579 // The install has not been queued
2580 final String packageName = info.providerName.getPackageName();
2581 showBrokenAppInstallDialog(packageName,
2582 new DialogInterface.OnClickListener() {
2583 public void onClick(DialogInterface dialog, int id) {
2584 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2585 }
2586 });
2587 } else {
2588 // Download has started.
2589 final String packageName = info.providerName.getPackageName();
2590 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002591 }
2592 }
2593
2594 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002595 * Event handler for the "grid" button that appears on the home screen, which
2596 * enters all apps mode.
2597 *
2598 * @param v The view that was clicked.
2599 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 protected void onClickAllAppsButton(View v) {
2601 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002602 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002603 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002604 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002605
2606 if (mLauncherCallbacks != null) {
2607 mLauncherCallbacks.onClickAllAppsButton(v);
2608 }
Winson Chung76648c52015-07-10 14:33:23 -07002609 }
2610 }
2611
2612 protected void onLongClickAllAppsButton(View v) {
2613 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2614 if (!isAppsViewVisible()) {
2615 showAppsView(true /* animated */, false /* resetListToTop */,
2616 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 }
2618 }
2619
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002620 private void showBrokenAppInstallDialog(final String packageName,
2621 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002622 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 .setTitle(R.string.abandoned_promises_title)
2624 .setMessage(R.string.abandoned_promise_explanation)
2625 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2626 .setNeutralButton(R.string.abandoned_clean_this,
2627 new DialogInterface.OnClickListener() {
2628 public void onClick(DialogInterface dialog, int id) {
2629 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2630 mWorkspace.removeAbandonedPromise(packageName, user);
2631 }
2632 })
2633 .create().show();
2634 return;
2635 }
2636
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 /**
2638 * Event handler for an app shortcut click.
2639 *
2640 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2641 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2644 Object tag = v.getTag();
2645 if (!(tag instanceof ShortcutInfo)) {
2646 throw new IllegalArgumentException("Input must be a Shortcut");
2647 }
2648
2649 // Open shortcut
2650 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651
2652 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002653 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2654 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002655 // Launch activity anyway, framework will tell the user why the app is suspended.
2656 } else {
2657 int error = R.string.activity_not_available;
2658 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2659 error = R.string.safemode_shortcut_error;
2660 }
2661 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2662 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002663 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002664 }
2665
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002667 if ((v instanceof BubbleTextView)
2668 && shortcut.isPromise()
2669 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002670 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002671 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 new DialogInterface.OnClickListener() {
2673 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002674 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 return;
2678 }
2679
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002681 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002682
2683 if (mLauncherCallbacks != null) {
2684 mLauncherCallbacks.onClickAppShortcut(v);
2685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 }
2687
Adam Cohen091440a2015-03-18 14:16:05 -07002688 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002690 final ShortcutInfo shortcut;
2691 final Intent intent;
2692 if (tag instanceof ShortcutInfo) {
2693 shortcut = (ShortcutInfo) tag;
2694 intent = shortcut.intent;
2695 int[] pos = new int[2];
2696 v.getLocationOnScreen(pos);
2697 intent.setSourceBounds(new Rect(pos[0], pos[1],
2698 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002699
Sunny Goyal508da152014-08-14 10:53:27 -07002700 } else if (tag instanceof AppInfo) {
2701 shortcut = null;
2702 intent = ((AppInfo) tag).intent;
2703 } else {
2704 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2705 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706
2707 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002708 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709
2710 if (success && v instanceof BubbleTextView) {
2711 mWaitingForResume = (BubbleTextView) v;
2712 mWaitingForResume.setStayPressed(true);
2713 }
2714 }
2715
2716 /**
2717 * Event handler for a folder icon click.
2718 *
2719 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2720 */
2721 protected void onClickFolderIcon(View v) {
2722 if (LOGD) Log.d(TAG, "onClickFolder");
2723 if (!(v instanceof FolderIcon)){
2724 throw new IllegalArgumentException("Input must be a FolderIcon");
2725 }
2726
2727 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002728 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002730 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 }
Adam Cohen9211d422014-10-07 18:14:53 -07002732
2733 if (mLauncherCallbacks != null) {
2734 mLauncherCallbacks.onClickFolderIcon(v);
2735 }
Michael Jurka5130e402011-10-13 04:55:35 -07002736 }
2737
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002738 /**
2739 * Event handler for the (Add) Widgets button that appears after a long press
2740 * on the home screen.
2741 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002742 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002744 if (mIsSafeModeEnabled) {
2745 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2746 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002747 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickAddWidgetButton(view);
2750 }
Adam Cohen9211d422014-10-07 18:14:53 -07002751 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002752 }
2753
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002754 /**
2755 * Event handler for the wallpaper picker button that appears after a long press
2756 * on the home screen.
2757 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002758 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002760 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2761 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2762 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002763 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002764 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002765
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onClickWallpaperPicker(v);
2768 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 }
2770
2771 /**
2772 * Event handler for a click on the settings button that appears after a long press
2773 * on the home screen.
2774 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002775 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002777 if (mLauncherCallbacks != null) {
2778 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002779 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002780 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002781 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 }
2783
Adam Cohen61f560d2013-09-30 15:58:20 -07002784 public View.OnTouchListener getHapticFeedbackTouchListener() {
2785 if (mHapticFeedbackTouchListener == null) {
2786 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002787 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002788 @Override
2789 public boolean onTouch(View v, MotionEvent event) {
2790 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2791 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2792 }
2793 return false;
2794 }
2795 };
2796 }
2797 return mHapticFeedbackTouchListener;
2798 }
2799
Adam Cohen9211d422014-10-07 18:14:53 -07002800 public void onDragStarted(View view) {
2801 if (isOnCustomContent()) {
2802 // Custom content screen doesn't participate in drag and drop. If on custom
2803 // content screen, move to default.
2804 moveWorkspaceToDefaultScreen();
2805 }
2806
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onDragStarted(view);
2809 }
2810 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002811
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002812 /**
2813 * Called when the user stops interacting with the launcher.
2814 * This implies that the user is now on the homescreen and is not doing housekeeping.
2815 */
Adam Cohen9211d422014-10-07 18:14:53 -07002816 protected void onInteractionEnd() {
2817 if (mLauncherCallbacks != null) {
2818 mLauncherCallbacks.onInteractionEnd();
2819 }
2820 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002821
2822 /**
2823 * Called when the user starts interacting with the launcher.
2824 * The possible interactions are:
2825 * - open all apps
2826 * - reorder an app shortcut, or a widget
2827 * - open the overview mode.
2828 * This is a good time to stop doing things that only make sense
2829 * when the user is on the homescreen and not doing housekeeping.
2830 */
Adam Cohen9211d422014-10-07 18:14:53 -07002831 protected void onInteractionBegin() {
2832 if (mLauncherCallbacks != null) {
2833 mLauncherCallbacks.onInteractionBegin();
2834 }
2835 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002836
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 /** Updates the interaction state. */
2838 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002839 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002840 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002841 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2842 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002843 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002844 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002845 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002846 onInteractionEnd();
2847 }
2848 }
2849
Kenny Guyf07af7b2014-07-31 11:39:16 +01002850 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002851 try {
2852 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002853 launcherApps.showAppDetailsForProfile(componentName, user);
2854 } catch (SecurityException e) {
2855 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2856 Log.e(TAG, "Launcher does not have permission to launch settings");
2857 } catch (ActivityNotFoundException e) {
2858 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2859 Log.e(TAG, "Unable to launch settings");
2860 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002861 }
2862
Michael Jurka1e2f4652013-07-08 18:03:46 -07002863 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002864 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2865 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002866 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002867 // System applications cannot be installed. For now, show a toast explaining that.
2868 // We may give them the option of disabling apps this way.
2869 int messageId = R.string.uninstall_system_app_text;
2870 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002871 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 String packageName = componentName.getPackageName();
2874 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 Intent intent = new Intent(
2876 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2878 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002879 if (user != null) {
2880 user.addToIntent(intent, Intent.EXTRA_USER);
2881 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002883 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002885 }
2886
Winson Chung8f1eff72015-05-28 17:33:40 -07002887 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002888 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002890 // Only launch using the new animation if the shortcut has not opted out (this is a
2891 // private contract between launcher and may be ignored in the future).
2892 boolean useLaunchAnimation = (v != null) &&
2893 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002894 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2895 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002896
Kenny Guy1317e2d2014-05-08 18:52:50 +01002897 UserHandleCompat user = null;
2898 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2899 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2900 user = userManager.getUserForSerialNumber(serialNumber);
2901 }
2902
2903 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002904 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002905 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002906 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002907 int left = 0, top = 0;
2908 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2909 if (v instanceof TextView) {
2910 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002911 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2912 if (icon != null) {
2913 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002914 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002915 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002916 width = bounds.width();
2917 height = bounds.height();
2918 }
2919 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002920 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2921 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002922 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002923 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002924 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002925 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002926 // On L devices, we use the device default slide-up transition.
2927 // On L MR1 devices, we a custom version of the slide-up transition which
2928 // doesn't have the delay present in the device default.
2929 opts = ActivityOptions.makeCustomAnimation(this,
2930 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002931 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002932 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002933 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002934
2935 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2936 // Could be launching some bookkeeping activity
2937 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002938 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002939 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002940 launcherApps.startActivityForProfile(intent.getComponent(), user,
2941 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002942 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002943 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002945 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2946 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2947 // corresponding permission. Show the appropriate permission prompt if that
2948 // is the case.
2949 if (intent.getComponent() == null
2950 && Intent.ACTION_CALL.equals(intent.getAction())
2951 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2952 PackageManager.PERMISSION_GRANTED) {
2953 // TODO: Rename sPendingAddItem to a generic name.
2954 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2955 0, (ItemInfo) tag);
2956 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2957 REQUEST_PERMISSION_CALL_PHONE);
2958 return false;
2959 }
2960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002962 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002964 "or use the exported attribute for this activity. "
2965 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002969
Winson Chungbedf9232015-07-10 12:38:30 -07002970 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002971 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002972 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2973 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2974 return false;
2975 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002976 try {
2977 success = startActivity(v, intent, tag);
2978 } catch (ActivityNotFoundException e) {
2979 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2980 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2981 }
2982 return success;
2983 }
2984
Adam Cohen268c4752012-06-06 17:47:33 -07002985 /**
2986 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2987 * in the DragLayer in the exact absolute location of the original FolderIcon.
2988 */
2989 private void copyFolderIconToImage(FolderIcon fi) {
2990 final int width = fi.getMeasuredWidth();
2991 final int height = fi.getMeasuredHeight();
2992
2993 // Lazy load ImageView, Bitmap and Canvas
2994 if (mFolderIconImageView == null) {
2995 mFolderIconImageView = new ImageView(this);
2996 }
2997 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2998 mFolderIconBitmap.getHeight() != height) {
2999 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3000 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3001 }
3002
3003 DragLayer.LayoutParams lp;
3004 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3005 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3006 } else {
3007 lp = new DragLayer.LayoutParams(width, height);
3008 }
3009
Adam Cohen307fe232012-08-16 17:55:58 -07003010 // The layout from which the folder is being opened may be scaled, adjust the starting
3011 // view size by this scale factor.
3012 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003013 lp.customPosition = true;
3014 lp.x = mRectForFolderAnimation.left;
3015 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003016 lp.width = (int) (scale * width);
3017 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003018
3019 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3020 fi.draw(mFolderIconCanvas);
3021 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 if (fi.getFolder() != null) {
3023 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3024 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003025 }
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // Just in case this image view is still in the drag layer from a previous animation,
3027 // we remove it and re-add it.
3028 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3029 mDragLayer.removeView(mFolderIconImageView);
3030 }
3031 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003032 if (fi.getFolder() != null) {
3033 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003034 }
Adam Cohen268c4752012-06-06 17:47:33 -07003035 }
3036
Sunny Goyal08442b82015-10-21 17:15:08 -07003037 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003038 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003039 FolderInfo info = (FolderInfo) fi.getTag();
3040 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3041 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003042 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3043 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003044 }
3045
Adam Cohen268c4752012-06-06 17:47:33 -07003046 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3047 copyFolderIconToImage(fi);
3048 fi.setVisibility(View.INVISIBLE);
3049
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003050 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3051 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003052 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003053 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3054 }
3055 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003057 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003058 oa.end();
3059 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 }
3061
Sunny Goyal935fca12015-10-13 10:19:01 -07003062 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003063 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003064 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003065
Adam Cohen268c4752012-06-06 17:47:33 -07003066 // We remove and re-draw the FolderIcon in-case it has changed
3067 mDragLayer.removeView(mFolderIconImageView);
3068 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003069 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003070 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003071 oa.addListener(new AnimatorListenerAdapter() {
3072 @Override
3073 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003074 if (cl != null) {
3075 cl.clearFolderLeaveBehind();
3076 // Remove the ImageView copy of the FolderIcon and make the original visible.
3077 mDragLayer.removeView(mFolderIconImageView);
3078 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003079 }
3080 }
3081 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003082 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003083 if (!animate) {
3084 oa.end();
3085 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003086 }
3087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003089 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 * is animated relative to the specified View. If the View is null, no animation
3091 * is played.
3092 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003093 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003095 public void openFolder(FolderIcon folderIcon, boolean animate) {
3096 animate &= !Utilities.isPowerSaverOn(this);
3097
Adam Cohenfb91f302012-06-11 15:45:18 -07003098 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003099 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3100 if (openFolder != null && openFolder != folder) {
3101 // Close any open folder before opening a folder.
3102 closeFolder();
3103 }
3104
Adam Cohena9cf38f2011-05-02 15:36:58 -07003105 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106
Adam Cohena9cf38f2011-05-02 15:36:58 -07003107 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108
Sunny Goyalf4066152015-04-15 09:42:19 -07003109 // While the folder is open, the position of the icon cannot change.
3110 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3111
Adam Cohen4554ee12011-08-03 16:13:21 -07003112 // Just verify that the folder hasn't already been added to the DragLayer.
3113 // There was a one-off crash where the folder had a parent already.
3114 if (folder.getParent() == null) {
3115 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003116 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 } else {
3118 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3119 folder.getParent() + ").");
3120 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003121 if (animate) {
3122 folder.animateOpen();
3123 } else {
3124 folder.open();
3125 }
3126 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003127
3128 // Notify the accessibility manager that this folder "window" has appeared and occluded
3129 // the workspace items
3130 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3131 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003132 }
3133
3134 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003135 closeFolder(true);
3136 }
3137
3138 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003139 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003140 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003141 if (folder.isEditingName()) {
3142 folder.dismissEditingName();
3143 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003144 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003145 }
3146 }
3147
Sunny Goyal935fca12015-10-13 10:19:01 -07003148 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003149 animate &= !Utilities.isPowerSaverOn(this);
3150
Adam Cohen4554ee12011-08-03 16:13:21 -07003151 folder.getInfo().opened = false;
3152
3153 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3154 if (parent != null) {
3155 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003156 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003157 if (fi != null) {
3158 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3159 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003160 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003161 if (animate) {
3162 folder.animateClosed();
3163 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003164 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003165 }
Winson Chung83ca4802013-04-12 15:10:52 -07003166
Sunny Goyal935fca12015-10-13 10:19:01 -07003167 // Notify the accessibility manager that this folder "window" has disappeared and no
3168 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003169 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 }
3171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003173 if (!isDraggingEnabled()) return false;
3174 if (isWorkspaceLocked()) return false;
3175 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176
Winson Chung76648c52015-07-10 14:33:23 -07003177 if (v == mAllAppsButton) {
3178 onLongClickAllAppsButton(v);
3179 return true;
3180 }
3181
Adam Cohen1697b792013-09-17 19:08:21 -07003182 if (v instanceof Workspace) {
3183 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003184 if (!mWorkspace.isTouchActive()) {
3185 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003186 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3187 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3188 return true;
3189 } else {
3190 return false;
3191 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003192 } else {
3193 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003194 }
Adam Cohen1697b792013-09-17 19:08:21 -07003195 }
3196
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003197 CellLayout.CellInfo longClickCellInfo = null;
3198 View itemUnderLongClick = null;
3199 if (v.getTag() instanceof ItemInfo) {
3200 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003201 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003202 itemUnderLongClick = longClickCellInfo.cell;
3203 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 }
3205
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 // The hotseat touch handling does not go through Workspace, and we always allow long press
3207 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003208 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003209 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003210 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003211 // User long pressed on empty space
3212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003214 if (mWorkspace.isInOverviewMode()) {
3215 mWorkspace.startReordering(v);
3216 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003217 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003220 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3221 mHotseat.getOrderInHotseat(
3222 longClickCellInfo.cellX,
3223 longClickCellInfo.cellY));
3224 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003226 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 }
3228 }
3229 }
3230 return true;
3231 }
3232
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 boolean isHotseatLayout(View layout) {
3234 return mHotseat != null && layout != null &&
3235 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3236 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003237
Winson Chung3d503fb2011-07-13 17:25:49 -07003238 /**
3239 * Returns the CellLayout of the specified container at the specified screen.
3240 */
Sunny Goyal92820592015-03-02 11:31:35 -08003241 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003242 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3243 if (mHotseat != null) {
3244 return mHotseat.getLayout();
3245 } else {
3246 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003247 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003248 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003249 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003250 }
3251 }
3252
Winson Chungb745afb2015-03-02 11:51:23 -08003253 /**
3254 * For overridden classes.
3255 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003256 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003257 return isAppsViewVisible();
3258 }
3259
3260 public boolean isAppsViewVisible() {
3261 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3262 }
3263
3264 public boolean isWidgetsViewVisible() {
3265 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003266 }
3267
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003268 private void setWorkspaceBackground(int background) {
3269 switch (background) {
3270 case WORKSPACE_BACKGROUND_TRANSPARENT:
3271 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3272 break;
3273 case WORKSPACE_BACKGROUND_BLACK:
3274 getWindow().setBackgroundDrawable(null);
3275 break;
3276 default:
3277 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3278 }
Craig Mautner360310b2012-10-26 15:13:08 -07003279 }
3280
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003281 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003282 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3283 int curflags = getWindow().getAttributes().flags
3284 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3285 if (wpflags != curflags) {
3286 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3287 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003288 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003289 }
3290
Michael Jurkae326f182011-11-21 14:05:46 -08003291 @Override
3292 public void onTrimMemory(int level) {
3293 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003294 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3295 // The widget preview db can result in holding onto over
3296 // 3MB of memory for caching which isn't necessary.
3297 SQLiteDatabase.releaseMemory();
3298
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003299 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003300 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003301 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003302 if (mLauncherCallbacks != null) {
3303 mLauncherCallbacks.onTrimMemory(level);
3304 }
Michael Jurkae326f182011-11-21 14:05:46 -08003305 }
3306
Winson5c6bdbb2015-09-03 11:36:19 -07003307 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003308 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003309 }
3310
Winson5c6bdbb2015-09-03 11:36:19 -07003311 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003312 boolean changed = mState != State.WORKSPACE ||
3313 mWorkspace.getState() != Workspace.State.NORMAL;
3314 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003315 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003316 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003317 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003318
Michael Jurkab3e22d92011-10-31 15:58:33 -07003319 // Set focus to the AppsCustomize button
3320 if (mAllAppsButton != null) {
3321 mAllAppsButton.requestFocus();
3322 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003323 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003324
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003325 // Change the state *after* we've called all the transition code
3326 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003327
Winson Chung5fb63472011-02-02 17:03:37 -08003328 // Resume the auto-advance of widgets
3329 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003330 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003331
Winson Chung0f785722015-04-08 10:27:49 -07003332 if (changed) {
3333 // Send an accessibility event to announce the context change
3334 getWindow().getDecorView()
3335 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003336 }
Winson5c6bdbb2015-09-03 11:36:19 -07003337 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003338 }
3339
Winsone9f27272015-10-13 10:47:51 -07003340 /**
3341 * Shows the overview button.
3342 */
Adam Cohened307df2013-10-02 09:37:31 -07003343 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003344 showOverviewMode(animated, false);
3345 }
3346
3347 /**
3348 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3349 * onto one of the overview panel buttons.
3350 */
3351 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3352 Runnable postAnimRunnable = null;
3353 if (requestButtonFocus) {
3354 postAnimRunnable = new Runnable() {
3355 @Override
3356 public void run() {
3357 // Hitting the menu button when in touch mode does not trigger touch mode to
3358 // be disabled, so if requested, force focus on one of the overview panel
3359 // buttons.
3360 mOverviewPanel.requestFocusFromTouch();
3361 }
3362 };
3363 }
Adam Cohened307df2013-10-02 09:37:31 -07003364 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003365 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003366 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003367 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003368 }
3369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the apps view.
3372 */
Winson Chung76648c52015-07-10 14:33:23 -07003373 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3374 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003375 if (resetListToTop) {
3376 mAppsView.scrollToTop();
3377 }
Winson Chung4ac30062015-05-08 17:34:17 -07003378 if (updatePredictedApps) {
3379 tryAndUpdatePredictedApps();
3380 }
Winson Chung76648c52015-07-10 14:33:23 -07003381 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003382 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383
Winson Chungb745afb2015-03-02 11:51:23 -08003384 /**
3385 * Shows the widgets view.
3386 */
3387 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003388 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003389 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003390 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003391 }
Winson Chung76648c52015-07-10 14:33:23 -07003392 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003393
3394 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003395 @Override
3396 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003398 }
3399 });
Winson Chungb745afb2015-03-02 11:51:23 -08003400 }
3401
3402 /**
3403 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003404 *
3405 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003406 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003407 // TODO: calling method should use the return value so that when {@code false} is returned
3408 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003409 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003410 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3411 mState != State.WIDGETS_SPRING_LOADED) {
3412 return false;
3413 }
3414 if (toState != State.APPS && toState != State.WIDGETS) {
3415 return false;
3416 }
Winson Chungb745afb2015-03-02 11:51:23 -08003417
3418 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003419 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3420 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003421 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003422 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003423 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003426 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427
3428 // Pause the auto-advance of widgets until we are out of AllApps
3429 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003430 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003431 closeFolder();
3432
3433 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003434 getWindow().getDecorView()
3435 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003436 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003437 }
3438
Winson Chungcd99cd32015-04-29 11:03:24 -07003439 /**
3440 * Updates the workspace and interaction state on state change, and return the animation to this
3441 * new state.
3442 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003443 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003444 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003445 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003446 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003447 updateInteraction(fromState, toState);
3448 return anim;
3449 }
3450
Jun Mukaif0033da2015-08-04 18:34:30 -07003451 public void onLauncherClingShown() {
3452 // When a launcher cling appears, it should cover the underlying layers, so their focus
3453 // should be blocked.
3454 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3455 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3456 }
3457 }
3458
3459 public void onLauncherClingDismissed() {
3460 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3461 }
3462
Hyunyoung Song3f471442015-04-08 19:01:34 -07003463 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003464 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003465 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003466 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003467 }
Winson Chungb745afb2015-03-02 11:51:23 -08003468
Winson Chung006ee262015-08-03 14:40:11 -07003469 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003470 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003471 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003472
3473 if (isAppsViewVisible()) {
3474 mState = State.APPS_SPRING_LOADED;
3475 } else if (isWidgetsViewVisible()) {
3476 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003477 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003478 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003479 } else {
3480 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003481 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003482 }
Adam Cohen7777d962011-08-18 18:58:38 -07003483
Hyunyoung Song3f471442015-04-08 19:01:34 -07003484 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003485 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003486 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003487
Winson Chunge7a03942011-08-05 15:05:12 -07003488 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003489 @Override
3490 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003491 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003492 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3493 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003494 // Before we show workspace, hide all apps again because
3495 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3496 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003497 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003498 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003499 } else {
3500 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003501 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003502 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003503 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003504 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003505
Tony Wickhame0c33232016-02-08 11:37:04 -08003506 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003507 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3508 || mState == State.WIDGETS_SPRING_LOADED;
3509 }
3510
Michael Jurkad3ef3062010-11-23 16:23:58 -08003511 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003512 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003513 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003514 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003515 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003516 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003517 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3518 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003519 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003520 }
3521
Winson Chung4ac30062015-05-08 17:34:17 -07003522 /**
3523 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3524 * resumed.
3525 */
3526 private void tryAndUpdatePredictedApps() {
3527 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003528 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003529 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003530 mAppsView.setPredictedApps(apps);
3531 }
3532 }
3533 }
3534
Michael Jurkab3e22d92011-10-31 15:58:33 -07003535 void lockAllApps() {
3536 // TODO
3537 }
3538
3539 void unlockAllApps() {
3540 // TODO
3541 }
3542
Sunny Goyal594d76d2014-11-06 10:12:54 -08003543 protected void disableVoiceButtonProxy(boolean disable) {
3544 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003545 }
3546
Winsonf768d932015-09-25 16:12:35 -07003547 public boolean launcherCallbacksProvidesSearch() {
3548 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3549 }
3550
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003551 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003552 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003553 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003554 }
3555
Adam Cohen24ce0b32014-01-14 16:18:14 -08003556 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003557 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3558 if (searchProvider == null) {
3559 return null;
3560 }
3561
3562 Bundle opts = new Bundle();
3563 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003564 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003565
Tony Wickham3a3517f2015-10-06 11:27:04 -07003566 // Determine the min and max dimensions of the widget.
3567 LauncherAppState app = LauncherAppState.getInstance();
3568 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3569 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3570 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003571 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3572 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003573 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003574 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003575 int minWidth = maxWidth;
3576 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003577 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3578 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3579 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3580 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3581 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003582 }
3583 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3584 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3585 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3586 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003587 if (LOGD) {
3588 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3589 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3590 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003591
Tony Wickham775455c2015-10-16 09:49:32 -07003592 if (mLauncherCallbacks != null) {
3593 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3594 }
3595
Sunny Goyalf7258242015-10-19 16:59:07 -07003596 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003597 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003598 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003599 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003600 || (widgetInfo == null)
3601 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003602 // A valid widget is not already bound.
3603 if (widgetId > -1) {
3604 mAppWidgetHost.deleteAppWidgetId(widgetId);
3605 widgetId = -1;
3606 }
3607
3608 // Try to bind a new widget
3609 widgetId = mAppWidgetHost.allocateAppWidgetId();
3610
3611 if (!AppWidgetManagerCompat.getInstance(this)
3612 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3613 mAppWidgetHost.deleteAppWidgetId(widgetId);
3614 widgetId = -1;
3615 }
3616
Sunny Goyalf7258242015-10-19 16:59:07 -07003617 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003618 .putInt(QSB_WIDGET_ID, widgetId)
3619 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003620 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003621 }
3622
Sunny Goyal8d595092015-07-06 12:22:14 -07003623 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003624 if (widgetId != -1) {
3625 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003626 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003627 mQsb.updateAppWidgetOptions(opts);
3628 mQsb.setPadding(0, 0, 0, 0);
3629 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003630 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003631 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003632 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003633 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003634 }
3635
Sunny Goyal22235bc2015-04-03 09:23:43 -07003636 private void reinflateQSBIfNecessary() {
3637 if (mQsb instanceof LauncherAppWidgetHostView &&
3638 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3639 mSearchDropTargetBar.removeView(mQsb);
3640 mQsb = null;
3641 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3642 }
3643 }
3644
Michael Jurkad7c28052012-04-27 15:43:36 -07003645 @Override
3646 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003647 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003648 final List<CharSequence> text = event.getText();
3649 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003650 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003651 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003652 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003653 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003654 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003655 } else if (mWorkspace != null) {
3656 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003657 } else {
3658 text.add(getString(R.string.all_apps_home_button_label));
3659 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003660 return result;
3661 }
3662
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003663 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003664 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003665 */
Adam Cohen091440a2015-03-18 14:16:05 -07003666 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003667 @Override
3668 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003669 closeSystemDialogs();
3670 }
3671 }
3672
3673 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 * If the activity is currently paused, signal that we need to run the passed Runnable
3675 * in onResume.
3676 *
3677 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003678 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3679 * wrong when we're not running, and if the activity comes back to what the configuration was
3680 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003681 *
3682 * Implementation of the method from LauncherModel.Callbacks.
3683 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003684 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003686 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003687 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003688 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003689 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003690 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003691 }
3692 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003693 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003694 return true;
3695 } else {
3696 return false;
3697 }
3698 }
3699
Michael Jurkac402cd92013-05-20 15:49:32 +02003700 private boolean waitUntilResume(Runnable run) {
3701 return waitUntilResume(run, false);
3702 }
3703
Michael Jurka1e2f4652013-07-08 18:03:46 -07003704 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003705 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003706 }
3707
Michael Jurka7607c2f2013-04-03 14:33:19 -07003708 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003709 * If the activity is currently paused, signal that we need to re-run the loader
3710 * in onResume.
3711 *
3712 * This needs to be called from incoming places where resources might have been loaded
3713 * while we are paused. That is becaues the Configuration might be wrong
3714 * when we're not running, and if it comes back to what it was when we
3715 * were paused, we are not restarted.
3716 *
3717 * Implementation of the method from LauncherModel.Callbacks.
3718 *
3719 * @return true if we are currently paused. The caller might be able to
3720 * skip some work in that case since we will come back again.
3721 */
3722 public boolean setLoadOnResume() {
3723 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003724 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003725 mOnResumeNeedsLoad = true;
3726 return true;
3727 } else {
3728 return false;
3729 }
3730 }
3731
3732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003734 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003736 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003737 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003738 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003739 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003740 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003742
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003744 * Clear any pending bind callbacks. This is called when is loader is planning to
3745 * perform a full rebind from scratch.
3746 */
3747 @Override
3748 public void clearPendingBinds() {
3749 mBindOnResumeCallbacks.clear();
3750 if (mPendingExecutor != null) {
3751 mPendingExecutor.markCompleted();
3752 mPendingExecutor = null;
3753 }
3754 }
3755
3756 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 * Refreshes the shortcuts shown on the workspace.
3758 *
3759 * Implementation of the method from LauncherModel.Callbacks.
3760 */
3761 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003762 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003763
Winson Chungd64d1762013-08-20 14:37:16 -07003764 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003765 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003766 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003767
Michael Jurka05bf644e2011-11-30 20:28:53 -08003768 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003769 if (mHotseat != null) {
3770 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003771 }
3772 }
3773
Adam Cohendcd297f2013-06-18 13:13:40 -07003774 @Override
3775 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003776 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003777
Adam Cohen5084cba2013-09-03 12:01:16 -07003778 // If there are no screens, we need to have an empty screen
3779 if (orderedScreenIds.size() == 0) {
3780 mWorkspace.addExtraEmptyScreen();
3781 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003782
3783 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003784 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003785 if (hasCustomContentToLeft()) {
3786 mWorkspace.createCustomContentContainer();
3787 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003788 }
Winson Chung64359a52013-07-08 17:17:08 -07003789 }
3790
3791 @Override
3792 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003793 int count = orderedScreenIds.size();
3794 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003795 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003796 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003797 }
3798
Winson Chungd64d1762013-08-20 14:37:16 -07003799 public void bindAppsAdded(final ArrayList<Long> newScreens,
3800 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003801 final ArrayList<ItemInfo> addAnimated,
3802 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 Runnable r = new Runnable() {
3804 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003805 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003806 }
3807 };
3808 if (waitUntilResume(r)) {
3809 return;
3810 }
3811
Winson Chungd64d1762013-08-20 14:37:16 -07003812 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003813 if (newScreens != null) {
3814 bindAddScreens(newScreens);
3815 }
Winson Chungd64d1762013-08-20 14:37:16 -07003816
3817 // We add the items without animation on non-visible pages, and with
3818 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003819 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 bindItems(addNotAnimated, 0,
3821 addNotAnimated.size(), false);
3822 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003823 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003824 bindItems(addAnimated, 0,
3825 addAnimated.size(), true);
3826 }
Winson Chungc58497e2013-09-03 17:48:37 -07003827
Winson Chung87412982013-10-03 18:34:14 -07003828 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003829 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003830
Winson Chungb745afb2015-03-02 11:51:23 -08003831 if (addedApps != null && mAppsView != null) {
3832 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003833 }
Winson Chungd64d1762013-08-20 14:37:16 -07003834 }
3835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 * Bind the items start-end from the list.
3838 *
3839 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003840 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003841 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003842 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3843 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003844 Runnable r = new Runnable() {
3845 public void run() {
3846 bindItems(shortcuts, start, end, forceAnimateIcons);
3847 }
3848 };
3849 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003850 return;
3851 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003852
Winson Chungf0c6ae02012-03-21 16:10:31 -07003853 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003854 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3855 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003856 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003857 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003858 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003861
3862 // Short circuit if we are loading dock items for a configuration which has no dock
3863 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3864 mHotseat == null) {
3865 continue;
3866 }
3867
Sunny Goyal639e9062015-08-19 19:17:06 -07003868 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 switch (item.itemType) {
3870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003873 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003874
Winson Chung64359a52013-07-08 17:17:08 -07003875 /*
3876 * TODO: FIX collision case
3877 */
Winson Chungce376632013-07-11 16:12:41 -07003878 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3879 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3880 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003881 View v = cl.getChildAt(item.cellX, item.cellY);
3882 Object tag = v.getTag();
3883 String desc = "Collision while binding workspace item: " + item
3884 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003885 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003886 throw (new RuntimeException(desc));
3887 } else {
3888 Log.d(TAG, desc);
3889 }
Winson Chungce376632013-07-11 16:12:41 -07003890 }
Winson Chung64359a52013-07-08 17:17:08 -07003891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003893 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003894 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003895 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003896 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003898 default:
3899 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003901
3902 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3903 item.cellY, 1, 1);
3904 if (animateIcons) {
3905 // Animate all the applications up now
3906 view.setAlpha(0f);
3907 view.setScaleX(0f);
3908 view.setScaleY(0f);
3909 bounceAnims.add(createNewAppBounceAnimation(view, i));
3910 newShortcutsScreenId = item.screenId;
3911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003912 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003913
Winson Chung997a9232013-07-24 15:33:46 -07003914 if (animateIcons) {
3915 // Animate to the correct page
3916 if (newShortcutsScreenId > -1) {
3917 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003918 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003919 final Runnable startBounceAnimRunnable = new Runnable() {
3920 public void run() {
3921 anim.playTogether(bounceAnims);
3922 anim.start();
3923 }
3924 };
Winson Chung997a9232013-07-24 15:33:46 -07003925 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003926 // We post the animation slightly delayed to prevent slowdowns
3927 // when we are loading right after we return to launcher.
3928 mWorkspace.postDelayed(new Runnable() {
3929 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003930 if (mWorkspace != null) {
3931 mWorkspace.snapToPage(newScreenIndex);
3932 mWorkspace.postDelayed(startBounceAnimRunnable,
3933 NEW_APPS_ANIMATION_DELAY);
3934 }
Winson Chung94d67682013-09-25 16:29:40 -07003935 }
3936 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003937 } else {
3938 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003939 }
3940 }
Winson Chung64359a52013-07-08 17:17:08 -07003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003943 }
3944
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 /**
3946 * Implementation of the method from LauncherModel.Callbacks.
3947 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003948 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003949 Runnable r = new Runnable() {
3950 public void run() {
3951 bindFolders(folders);
3952 }
3953 };
3954 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003955 return;
3956 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003957 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959
3960 /**
3961 * Add the views for a widget to the workspace.
3962 *
3963 * Implementation of the method from LauncherModel.Callbacks.
3964 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003966 Runnable r = new Runnable() {
3967 public void run() {
3968 bindAppWidget(item);
3969 }
3970 };
3971 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 return;
3973 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003974
Daniel Sandler843e8602010-06-07 14:59:01 -04003975 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3976 if (DEBUG_WIDGETS) {
3977 Log.d(TAG, "bindAppWidget: " + item);
3978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 final Workspace workspace = mWorkspace;
3980
Adam Cohen59400422014-03-05 18:07:04 -08003981 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003982 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003983
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003984 if (!mIsSafeModeEnabled
3985 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003986 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3987
Sunny Goyalff572272014-07-23 13:58:07 -07003988 if (appWidgetInfo == null) {
3989 if (DEBUG_WIDGETS) {
3990 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3991 + " belongs to component " + item.providerName
3992 + ", as the povider is null");
3993 }
3994 LauncherModel.deleteItemFromDatabase(this, item);
3995 return;
3996 }
Sunny Goyalff572272014-07-23 13:58:07 -07003997
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003998 // If we do not have a valid id, try to bind an id.
3999 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
4000 // Note: This assumes that the id remap broadcast is received before this step.
4001 // If that is not the case, the id remap will be ignored and user may see the
4002 // click to setup view.
4003 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
4004 pendingInfo.spanX = item.spanX;
4005 pendingInfo.spanY = item.spanY;
4006 pendingInfo.minSpanX = item.minSpanX;
4007 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004008 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004009
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004010 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4011 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4012 newWidgetId, appWidgetInfo, options);
4013
4014 // TODO consider showing a permission dialog when the widget is clicked.
4015 if (!success) {
4016 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4017 if (DEBUG_WIDGETS) {
4018 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4019 + " belongs to component " + item.providerName
4020 + ", as the launcher is unable to bing a new widget id");
4021 }
4022 LauncherModel.deleteItemFromDatabase(this, item);
4023 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004024 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004025
4026 item.appWidgetId = newWidgetId;
4027
4028 // If the widget has a configure activity, it is still needs to set it up, otherwise
4029 // the widget is ready to go.
4030 item.restoreStatus = (appWidgetInfo.configure == null)
4031 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4032 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4033
4034 LauncherModel.updateItemInDatabase(this, item);
4035 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4036 && (appWidgetInfo.configure == null)) {
4037 // If the ID is already valid, verify if we need to configure or not.
4038 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4039 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004040 }
Sunny Goyalff572272014-07-23 13:58:07 -07004041 }
4042
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004043 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004044 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004045 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004046 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4047 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004049
Sunny Goyal56c73602015-09-25 12:55:01 -07004050 // Verify that we own the widget
4051 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4052 if (info == null || appWidgetInfo == null ||
4053 !info.provider.equals(appWidgetInfo.provider)) {
Hyunyoung Song70264a92016-02-03 15:28:47 -08004054 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
4055 + " appWidgetInfo=" + appWidgetInfo);
Sunny Goyal56c73602015-09-25 12:55:01 -07004056 deleteWidgetInfo(item);
4057 return;
4058 }
4059
Sunny Goyal651077b2014-06-30 14:15:31 -07004060 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004061 item.minSpanX = appWidgetInfo.minSpanX;
4062 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004063 } else {
4064 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004065 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4066 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004067 view.updateIcon(mIconCache);
4068 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004069 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004070 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004073 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 workspace.requestLayout();
4075
Daniel Sandler843e8602010-06-07 14:59:01 -04004076 if (DEBUG_WIDGETS) {
4077 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4078 + (SystemClock.uptimeMillis()-start) + "ms");
4079 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 }
4081
Sunny Goyalff572272014-07-23 13:58:07 -07004082 /**
4083 * Restores a pending widget.
4084 *
4085 * @param appWidgetId The app widget id
4086 * @param cellInfo The position on screen where to create the widget.
4087 */
4088 private void completeRestoreAppWidget(final int appWidgetId) {
4089 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4090 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4091 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4092 return;
4093 }
4094
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004095 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004096 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4097
4098 mWorkspace.reinflateWidgetsIfNecessary();
4099 LauncherModel.updateItemInDatabase(this, info);
4100 }
4101
Adam Cohen1462de32012-07-24 22:34:36 -07004102 public void onPageBoundSynchronously(int page) {
4103 mSynchronouslyBoundPages.add(page);
4104 }
4105
Sunny Goyal527c7d32015-08-28 15:19:36 -07004106 @Override
4107 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4108 if (mPendingExecutor != null) {
4109 mPendingExecutor.markCompleted();
4110 }
4111 mPendingExecutor = executor;
4112 executor.attachTo(this);
4113 }
4114
4115 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4116 if (mPendingExecutor == executor) {
4117 mPendingExecutor = null;
4118 }
4119 }
4120
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 /**
4122 * Callback saying that there aren't any more items to bind.
4123 *
4124 * Implementation of the method from LauncherModel.Callbacks.
4125 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004126 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004127 Runnable r = new Runnable() {
4128 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004129 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004130 }
4131 };
4132 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004133 return;
4134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004135 if (mSavedState != null) {
4136 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004137 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004138 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004139
4140 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4141 if (folderId != 0) {
4142 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4143 if (view instanceof FolderIcon) {
4144 FolderIcon icon = (FolderIcon) view;
4145 FolderInfo info = icon.getFolderInfo();
4146 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4147 mWorkspace.getNextPage());
4148 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4149 || info.screenId == currentScreenId) {
4150 // We can show the folder
4151 openFolder(icon, false);
4152 } else {
4153 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4154 " but current screen is " + currentScreenId);
4155 }
4156 }
4157 }
4158
Joe Onorato9c1289c2009-08-17 11:03:03 -04004159 mSavedState = null;
4160 }
4161
Adam Cohen1462de32012-07-24 22:34:36 -07004162 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004164 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004165 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004166
4167 // If we received the result of any pending adds while the loader was running (e.g. the
4168 // widget configuration forced an orientation change), process them now.
4169 if (sPendingAddItem != null) {
4170 final long screenId = completeAdd(sPendingAddItem);
4171
4172 // TODO: this moves the user to the page where the pending item was added. Ideally,
4173 // the screen would be guaranteed to exist after bind, and the page would be set through
4174 // the workspace restore process.
4175 mWorkspace.post(new Runnable() {
4176 @Override
4177 public void run() {
4178 mWorkspace.snapToScreenId(screenId);
4179 }
4180 });
4181 sPendingAddItem = null;
4182 }
4183
Sunny Goyal756adbc2015-04-16 15:20:51 -07004184 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004185
4186 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004187 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004188 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004189 }
4190
Adam Cohen3ed316a2014-07-23 18:21:20 -07004191 private void sendLoadingCompleteBroadcastIfNecessary() {
4192 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4193 String permission =
4194 getResources().getString(R.string.receive_first_load_broadcast_permission);
4195 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4196 sendBroadcast(intent, permission);
4197 SharedPreferences.Editor editor = mSharedPrefs.edit();
4198 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4199 editor.apply();
4200 }
4201 }
4202
Winson Chunga0b7e862013-09-05 16:03:15 -07004203 public boolean isAllAppsButtonRank(int rank) {
4204 if (mHotseat != null) {
4205 return mHotseat.isAllAppsButtonRank(rank);
4206 }
4207 return false;
4208 }
4209
Winson Chunga2413752012-04-03 14:22:34 -07004210 private boolean canRunNewAppsAnimation() {
4211 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004212 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4213 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004214 }
4215
Winson Chungc9168342013-06-26 14:54:55 -07004216 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004217 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004218 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4219 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004220 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004221 return bounceAnim;
4222 }
4223
Adam Cohen27772732013-11-11 14:00:56 +00004224 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004225 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004226 }
4227
Tony Wickham3a3517f2015-10-06 11:27:04 -07004228 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004229 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004230 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004231 }
4232
Tony Wickham55616cd2015-09-23 14:55:17 -07004233 public int getSearchBarHeight() {
4234 if (mLauncherCallbacks != null) {
4235 return mLauncherCallbacks.getSearchBarHeight();
4236 }
4237 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4238 }
4239
Winson Chung88fa7412015-08-03 14:25:28 -07004240 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004241 if (mSearchDropTargetBar == null) {
4242 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004243 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004244 if (mQsb != null) {
4245 mSearchDropTargetBar.removeView(mQsb);
4246 mQsb = null;
4247 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004248 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004249 }
4250
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004251 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004252 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4253 * multiple calls to bind the same list.)
4254 */
4255 @Thunk ArrayList<AppInfo> mTmpAppsList;
4256 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4257 public void run() {
4258 bindAllApplications(mTmpAppsList);
4259 mTmpAppsList = null;
4260 }
4261 };
4262
4263 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004264 * Add the icons for all apps.
4265 *
4266 * Implementation of the method from LauncherModel.Callbacks.
4267 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004268 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004269 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4270 mTmpAppsList = apps;
4271 return;
4272 }
4273
Winson Chungb745afb2015-03-02 11:51:23 -08004274 if (mAppsView != null) {
4275 mAppsView.setApps(apps);
4276 }
Adam Cohen9211d422014-10-07 18:14:53 -07004277 if (mLauncherCallbacks != null) {
4278 mLauncherCallbacks.bindAllApplications(apps);
4279 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004280 }
4281
4282 /**
4283 * A package was updated.
4284 *
4285 * Implementation of the method from LauncherModel.Callbacks.
4286 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004287 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004288 Runnable r = new Runnable() {
4289 public void run() {
4290 bindAppsUpdated(apps);
4291 }
4292 };
4293 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004294 return;
4295 }
4296
Winson Chungb745afb2015-03-02 11:51:23 -08004297 if (mAppsView != null) {
4298 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004299 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004300 }
4301
Sunny Goyal4390ace2014-10-13 11:33:11 -07004302 @Override
4303 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4304 Runnable r = new Runnable() {
4305 public void run() {
4306 bindWidgetsRestored(widgets);
4307 }
4308 };
4309 if (waitUntilResume(r)) {
4310 return;
4311 }
4312 mWorkspace.widgetsRestored(widgets);
4313 }
4314
Joe Onorato9c1289c2009-08-17 11:03:03 -04004315 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004316 * Some shortcuts were updated in the background.
4317 *
4318 * Implementation of the method from LauncherModel.Callbacks.
4319 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004320 @Override
4321 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4322 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004323 Runnable r = new Runnable() {
4324 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004325 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004326 }
4327 };
4328 if (waitUntilResume(r)) {
4329 return;
4330 }
4331
Sunny Goyal4390ace2014-10-13 11:33:11 -07004332 if (!updated.isEmpty()) {
4333 mWorkspace.updateShortcuts(updated);
4334 }
4335
4336 if (!removed.isEmpty()) {
4337 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4338 for (ShortcutInfo si : removed) {
4339 removedComponents.add(si.getTargetComponent());
4340 }
4341 mWorkspace.removeItemsByComponentName(removedComponents, user);
4342 // Notify the drag controller
4343 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004344 }
4345 }
4346
4347 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004348 * Update the state of a package, typically related to install state.
4349 *
4350 * Implementation of the method from LauncherModel.Callbacks.
4351 */
Sunny Goyale755d462014-07-22 13:48:29 -07004352 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004353 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4354 Runnable r = new Runnable() {
4355 public void run() {
4356 bindRestoreItemsChange(updates);
4357 }
4358 };
4359 if (waitUntilResume(r)) {
4360 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004361 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004362
Sunny Goyal756adbc2015-04-16 15:20:51 -07004363 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004364 }
4365
4366 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004367 * A package was uninstalled. We take both the super set of packageNames
4368 * in addition to specific applications to remove, the reason being that
4369 * this can be called when a package is updated as well. In that scenario,
4370 * we only remove specific components from the workspace, where as
4371 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004372 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004373 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004374 * Implementation of the method from LauncherModel.Callbacks.
4375 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004376 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004377 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004378 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004379 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004380 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004381 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004382 }
Winson Chungd64d1762013-08-20 14:37:16 -07004383 };
4384 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004385 return;
4386 }
4387
Sunny Goyal1a745e82014-10-02 15:58:31 -07004388 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004389 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4390 for (AppInfo info : appInfos) {
4391 removedComponents.add(info.componentName);
4392 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004393 if (!packageNames.isEmpty()) {
4394 mWorkspace.removeItemsByPackageName(packageNames, user);
4395 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004396 if (!removedComponents.isEmpty()) {
4397 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004398 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004399 // Notify the drag controller
4400 mDragController.onAppsRemoved(packageNames, removedComponents);
4401
Sunny Goyal1a745e82014-10-02 15:58:31 -07004402 } else {
4403 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004404 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004405
Winson Chungdf95eb12013-10-16 14:57:07 -07004406 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004407 if (mAppsView != null) {
4408 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004409 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004410 }
Joe Onoratobe386092009-11-17 17:32:16 -08004411
Michael Jurkac402cd92013-05-20 15:49:32 +02004412 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004413 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004414 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004415 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004416 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004417
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004418 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004419 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004420 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004421 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004422 return;
4423 }
4424
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004425 if (mWidgetsView != null && model != null) {
4426 mWidgetsView.addWidgets(model);
4427 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004428 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004429 }
4430
Winson Chung400438b2011-01-16 17:53:48 -08004431 private int mapConfigurationOriActivityInfoOri(int configOri) {
4432 final Display d = getWindowManager().getDefaultDisplay();
4433 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4434 switch (d.getRotation()) {
4435 case Surface.ROTATION_0:
4436 case Surface.ROTATION_180:
4437 // We are currently in the same basic orientation as the natural orientation
4438 naturalOri = configOri;
4439 break;
4440 case Surface.ROTATION_90:
4441 case Surface.ROTATION_270:
4442 // We are currently in the other basic orientation to the natural orientation
4443 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4444 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4445 break;
4446 }
4447
4448 int[] oriMap = {
4449 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4450 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4451 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4452 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4453 };
4454 // Since the map starts at portrait, we need to offset if this device's natural orientation
4455 // is landscape.
4456 int indexOffset = 0;
4457 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4458 indexOffset = 1;
4459 }
4460 return oriMap[(d.getRotation() + indexOffset) % 4];
4461 }
Adam Cohen446e9402011-09-15 18:21:21 -07004462
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004463 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004464 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004465 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004466 if (Utilities.ATLEAST_JB_MR2) {
4467 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4468 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004469 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4470 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004471 }
Winson Chung4b919f82012-05-01 10:44:08 -07004472 }
4473 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004474
Winson Chung4b919f82012-05-01 10:44:08 -07004475 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004476 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004477 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004478 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004479 } else {
4480 mHandler.postDelayed(new Runnable() {
4481 public void run() {
4482 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4483 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004484 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004485 }
Winson Chung4b919f82012-05-01 10:44:08 -07004486 }
Winson Chung400438b2011-01-16 17:53:48 -08004487 }
4488
Winson Chunge43a1e72014-01-15 10:33:02 -08004489 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004490 if (mLauncherCallbacks != null) {
4491 return mLauncherCallbacks.isLauncherPreinstalled();
4492 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004493 PackageManager pm = getPackageManager();
4494 try {
4495 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4496 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4497 return true;
4498 } else {
4499 return false;
4500 }
4501 } catch (NameNotFoundException e) {
4502 e.printStackTrace();
4503 return false;
4504 }
4505 }
4506
Adam Cohenea90f832014-05-21 15:03:34 -07004507 /**
4508 * This method indicates whether or not we should suggest default wallpaper dimensions
4509 * when our wallpaper cropper was not yet used to set a wallpaper.
4510 */
4511 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004512 if (mLauncherCallbacks != null) {
4513 return mLauncherCallbacks.overrideWallpaperDimensions();
4514 }
Adam Cohenea90f832014-05-21 15:03:34 -07004515 return true;
4516 }
4517
Adam Cohen432609a2014-03-13 17:03:22 -07004518 /**
4519 * To be overridden by subclasses to indicate that there is an activity to launch
4520 * before showing the standard launcher experience.
4521 */
4522 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004523 if (mLauncherCallbacks != null) {
4524 return mLauncherCallbacks.hasFirstRunActivity();
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return false;
4527 }
4528
4529 /**
4530 * To be overridden by subclasses to launch any first run activity
4531 */
4532 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004533 if (mLauncherCallbacks != null) {
4534 return mLauncherCallbacks.getFirstRunActivity();
4535 }
Adam Cohen432609a2014-03-13 17:03:22 -07004536 return null;
4537 }
4538
Winson Chunga6945242014-01-08 14:04:34 -08004539 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004540 return !ActivityManager.isRunningInTestHarness() &&
4541 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004542 }
4543
Adam Cohen4a9423d2014-03-28 14:22:31 -07004544 protected boolean hasRunFirstRunActivity() {
4545 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4546 }
4547
Adam Cohen432609a2014-03-13 17:03:22 -07004548 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004549 if (shouldRunFirstRunActivity() &&
4550 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004551 Intent firstRunIntent = getFirstRunActivity();
4552 if (firstRunIntent != null) {
4553 startActivity(firstRunIntent);
4554 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004555 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004556 }
4557 }
Adam Cohen432609a2014-03-13 17:03:22 -07004558 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004559 }
4560
4561 private void markFirstRunActivityShown() {
4562 SharedPreferences.Editor editor = mSharedPrefs.edit();
4563 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4564 editor.apply();
4565 }
4566
Adam Cohen432609a2014-03-13 17:03:22 -07004567 /**
4568 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4569 * screen that must be displayed and dismissed.
4570 */
4571 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004572 if (mLauncherCallbacks != null) {
4573 return mLauncherCallbacks.hasDismissableIntroScreen();
4574 }
Adam Cohen432609a2014-03-13 17:03:22 -07004575 return false;
4576 }
4577
4578 /**
4579 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4580 */
4581 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004582 if (mLauncherCallbacks != null) {
4583 return mLauncherCallbacks.getIntroScreen();
4584 }
Adam Cohen432609a2014-03-13 17:03:22 -07004585 return null;
4586 }
4587
4588 /**
4589 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4590 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4591 */
4592 private boolean shouldShowIntroScreen() {
4593 return hasDismissableIntroScreen() &&
4594 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4595 }
4596
4597 protected void showIntroScreen() {
4598 View introScreen = getIntroScreen();
4599 changeWallpaperVisiblity(false);
4600 if (introScreen != null) {
4601 mDragLayer.showOverlayView(introScreen);
4602 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004603 if (mLauncherOverlayContainer != null) {
4604 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4605 }
Adam Cohen432609a2014-03-13 17:03:22 -07004606 }
4607
4608 public void dismissIntroScreen() {
4609 markIntroScreenDismissed();
4610 if (showFirstRunActivity()) {
4611 // We delay hiding the intro view until the first run activity is showing. This
4612 // avoids a blip.
4613 mWorkspace.postDelayed(new Runnable() {
4614 @Override
4615 public void run() {
4616 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004617 if (mLauncherOverlayContainer != null) {
4618 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4619 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004620 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004621 }
4622 }, ACTIVITY_START_DELAY);
4623 } else {
4624 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004625 if (mLauncherOverlayContainer != null) {
4626 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4627 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004628 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004629 }
4630 changeWallpaperVisiblity(true);
4631 }
4632
4633 private void markIntroScreenDismissed() {
4634 SharedPreferences.Editor editor = mSharedPrefs.edit();
4635 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4636 editor.apply();
4637 }
4638
Adam Cohen091440a2015-03-18 14:16:05 -07004639 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004640 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4641 // on the device, then we always show the first run cling experience (or if there is no
4642 // launcher2). Otherwise, we prompt the user upon started for migration
4643 LauncherClings launcherClings = new LauncherClings(this);
4644 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004645 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004646 if (mModel.canMigrateFromOldLauncherDb(this)) {
4647 launcherClings.showMigrationCling();
4648 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004649 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004650 }
4651 }
4652 }
4653
Winson Chunga6945242014-01-08 14:04:34 -08004654 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004655 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4656 if (mHotseat != null) mHotseat.setAlpha(1f);
4657 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004658 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4659 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004660 }
4661
4662 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004663 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4664 if (mHotseat != null) mHotseat.setAlpha(0f);
4665 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004666 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4667 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004668 }
4669
Winson Chung5ffd51d2015-06-25 11:36:50 -07004670 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004671 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004672 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004673 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004674 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004675 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004676 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4677 if (userHandle != null) {
4678 user = UserHandleCompat.fromUser(userHandle);
4679 }
4680 }
4681 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004682 }
4683
4684 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004685 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004686 if (user == null) {
4687 user = UserHandleCompat.myUserHandle();
4688 }
4689
4690 // Called from search suggestion, add the profile extra to the intent to ensure that we
4691 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004692 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004693 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004694 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004695 return null;
4696 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004697 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004698 }
4699
Winson Chung5ffd51d2015-06-25 11:36:50 -07004700 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004701 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4702 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004703 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004704 UserHandleCompat.myUserHandle());
4705 }
4706
Winson Chung5ffd51d2015-06-25 11:36:50 -07004707 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004708 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4709 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004710 mWorkspace.onExternalDragStartedWithItem(dragView);
4711 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004712 }
4713
Winson Chung5ffd51d2015-06-25 11:36:50 -07004714 protected void moveWorkspaceToDefaultScreen() {
4715 mWorkspace.moveToDefaultScreen(false);
4716 }
4717
Anjali Koppalf5d29132014-02-28 13:33:27 -08004718 @Override
4719 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004720 if (mLauncherCallbacks != null) {
4721 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4722 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004723 }
4724
Winson Chung80baf5a2010-08-09 16:03:15 -07004725 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004726 * Returns a FastBitmapDrawable with the icon, accurately sized.
4727 */
4728 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4729 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4730 d.setFilterBitmap(true);
4731 resizeIconDrawable(d);
4732 return d;
4733 }
4734
4735 /**
4736 * Resizes an icon drawable to the correct icon size.
4737 */
Winson5fbe0742015-09-30 15:33:00 -07004738 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004739 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004740 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004741 }
4742
4743 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004744 * Prints out out state for debugging.
4745 */
4746 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004747 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004748 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004749 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4750 Log.d(TAG, "mRestoring=" + mRestoring);
4751 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004752 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004753 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004754 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004755
Daniel Sandler325dc232013-06-05 22:57:57 -04004756 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004757 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004758
4759 @Override
4760 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4761 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004762 // Dump workspace
4763 writer.println(prefix + "Workspace Items");
4764 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4765 writer.println(prefix + " Homescreen " + i);
4766
4767 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4768 for (int j = 0; j < layout.getChildCount(); j++) {
4769 Object tag = layout.getChildAt(j).getTag();
4770 if (tag != null) {
4771 writer.println(prefix + " " + tag.toString());
4772 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004773 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004774 }
Sunny Goyala1365452015-10-01 15:46:24 -07004775
4776 writer.println(prefix + " Hotseat");
4777 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4778 for (int j = 0; j < layout.getChildCount(); j++) {
4779 Object tag = layout.getChildAt(j).getTag();
4780 if (tag != null) {
4781 writer.println(prefix + " " + tag.toString());
4782 }
4783 }
4784
4785 synchronized (sDumpLogs) {
4786 writer.println();
4787 writer.println(prefix + "Debug logs");
4788 for (String log : sDumpLogs) {
4789 writer.println(prefix + " " + log);
4790 }
4791 }
4792
Adam Cohen9211d422014-10-07 18:14:53 -07004793 if (mLauncherCallbacks != null) {
4794 mLauncherCallbacks.dump(prefix, fd, writer, args);
4795 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004796 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004797
Sunny Goyala1365452015-10-01 15:46:24 -07004798 public static void addDumpLog(String tag, String log) {
4799 Log.d(tag, log);
4800 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004801 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004802 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004803 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004804 }
4805
Adam Cohen59400422014-03-05 18:07:04 -08004806 public static CustomAppWidget getCustomAppWidget(String name) {
4807 return sCustomAppWidgets.get(name);
4808 }
4809
4810 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4811 return sCustomAppWidgets;
4812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004813}
Michael Jurka2763be32011-02-24 11:19:57 -08004814
Dan Sandlerd5024042014-01-09 15:01:33 -05004815interface DebugIntents {
4816 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4817 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004818}