blob: 06810b434fbd48f0978c905f22030872c86b6369 [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;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700116import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700117import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700118import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800119import com.android.wallpaperpicker.WallpaperUtils;
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;
Sunny Goyal29538332016-02-18 09:10:19 -0800126import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700130import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700138 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700142 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Sunny Goyal28c6b962015-10-12 11:42:05 -0700156 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
157
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700158 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
159 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
160 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
161
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700162 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
163
Mathew Inwood876a8462013-06-14 14:12:41 +0100164 /**
165 * IntentStarter uses request codes starting with this. This must be greater than all activity
166 * request codes used internally.
167 */
168 protected static final int REQUEST_LAST = 100;
169
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 // To turn on these properties, type
171 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700182 // Type: long
183 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700184 // Type: Content Values / parcelable
185 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700186 // Type: parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Adam Cohen432609a2014-03-13 17:03:22 -0700191 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800192 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
193
Adam Cohen3ed316a2014-07-23 18:21:20 -0700194 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
195 static final String ACTION_FIRST_LOAD_COMPLETE =
196 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
197
Sunny Goyal594d76d2014-11-06 10:12:54 -0800198 private static final String QSB_WIDGET_ID = "qsb_widget_id";
199 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
200
Winson Chunga6945242014-01-08 14:04:34 -0800201 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Adam Cohenc2d6e892014-10-16 09:49:24 -0700212 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
213 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700214 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
218 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700219 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Winson Chunga2413752012-04-03 14:22:34 -0700221 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700222 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
223 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700225
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800226 private final BroadcastReceiver mCloseSystemDialogsReceiver
227 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700230 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800231 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800233 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700234
235 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalffe83f12014-08-14 17:39:34 -0700237 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700240 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800241 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800242 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243
Michael Jurka0280c3b2010-09-17 15:00:07 -0700244 private int[] mTmpAddItemCellCoordinates = new int[2];
245
Sunny Goyal316490e2015-06-02 09:38:28 -0700246 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800247 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700248
Michael Jurka838a4ca2011-02-07 13:33:06 -0800249 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700250 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700251
Winson Chungc51db6a2011-10-05 11:44:49 -0700252 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700253 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700254
255 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700256 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700258 // Main container view and the model for the widget tray screen.
259 @Thunk WidgetsContainerView mWidgetsView;
260 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
Sunny Goyal594d76d2014-11-06 10:12:54 -0800262 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Adam Cohen091440a2015-03-18 14:16:05 -0700272 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700281 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700287 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800288 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289
Sunny Goyal639e9062015-08-19 19:17:06 -0700290 private LauncherClings mClings;
291
Sunny Goyale2df0622015-04-24 11:27:00 -0700292 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700293
Adam Cohen61f560d2013-09-30 15:58:20 -0700294 private View.OnTouchListener mHapticFeedbackTouchListener;
295
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 // Related to the auto-advancing of widgets
297 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 private static final int ADVANCE_INTERVAL = 20000;
299 private static final int ADVANCE_STAGGER = 250;
300
301 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800309
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700311
Adam Cohen1462de32012-07-24 22:34:36 -0700312 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700313 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700314
Sunny Goyala1365452015-10-01 15:46:24 -0700315 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
316 private static final Date sDateStamp = new Date();
317 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700318 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Winson Chungf0c6ae02012-03-21 16:10:31 -0700324 // Holds the page that we need to animate to, and the icon views that we need to animate up
325 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700326 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700327 private Bitmap mFolderIconBitmap;
328 private Canvas mFolderIconCanvas;
329 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700331 private DeviceProfile mDeviceProfile;
332
Adam Cohen4b66bf32015-09-18 12:15:19 -0700333 private boolean mMoveToDefaultScreenFromNewIntent;
334
Winson Chung13eb5272015-05-11 16:30:13 -0700335 // This is set to the view that launched the activity that navigated the user away from
336 // launcher. Since there is no callback for when the activity has finished launching, enable
337 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 private BubbleTextView mWaitingForResume;
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
341 new HashMap<String, CustomAppWidget>();
342
Adam Cohen59400422014-03-05 18:07:04 -0800343 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700344 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
345 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800346 }
347 }
348
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700351 if (mWorkspace != null) {
352 mWorkspace.buildPageHardwareLayers();
353 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 }
355 };
356
Adam Cohendb364c32014-05-20 14:23:40 -0700357 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358
Adam Cohen091440a2015-03-18 14:16:05 -0700359 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int requestCode;
361 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700362 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700363 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int cellX;
365 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700366 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 }
368
Daniel Sandlerff02d492013-08-05 02:12:05 -0400369 private Stats mStats;
Adam Cohenf9c184a2016-01-15 16:47:43 -0800370 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal7779d622015-06-11 16:18:39 -0700382 private Runnable mUpdateOrientationRunnable = new Runnable() {
383 public void run() {
384 setOrientation();
385 }
386 };
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 mDeviceProfile = getResources().getConfiguration().orientation
415 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700416 app.getInvariantDeviceProfile().landscapeProfile
417 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700418
Sunny Goyalf7258242015-10-19 16:59:07 -0700419 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700420 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800421 mModel = app.setLauncher(this);
422 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700425 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Daniel Sandlerff02d492013-08-05 02:12:05 -0400427 mStats = new Stats(this);
428
Sunny Goyalffe83f12014-08-14 17:39:34 -0700429 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700430
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
432 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700434 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
435 // this also ensures that any synchronous binding below doesn't re-trigger another
436 // LauncherModel load.
437 mPaused = false;
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200440 android.os.Debug.startMethodTracing(
441 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
Tony Wickhameef44322015-10-20 13:24:36 -0700446 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
447 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700449 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
477 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700479 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
480 // In case we are on a device with locked rotation, we should look at preferences to check
481 // if the user has specifically allowed rotation.
482 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700483 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 if (mLauncherCallbacks.hasLauncherOverlay()) {
493 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700494 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
495 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
496 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 mWorkspace.setLauncherOverlay(mLauncherOverlay);
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700500
501 if (shouldShowIntroScreen()) {
502 showIntroScreen();
503 } else {
504 showFirstRunActivity();
505 showFirstRunClings();
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
508
Sunny Goyal7779d622015-06-11 16:18:39 -0700509 @Override
510 public void onSettingsChanged(String settings, boolean value) {
511 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
512 mRotationEnabled = value;
513 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
514 mUpdateOrientationRunnable.run();
515 }
516 }
517 }
518
Adam Cohen9211d422014-10-07 18:14:53 -0700519 private LauncherCallbacks mLauncherCallbacks;
520
521 public void onPostCreate(Bundle savedInstanceState) {
522 super.onPostCreate(savedInstanceState);
523 if (mLauncherCallbacks != null) {
524 mLauncherCallbacks.onPostCreate(savedInstanceState);
525 }
526 }
527
528 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
529 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700531 private boolean mWorkspaceImportanceStored = false;
532 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700533 private int mWorkspaceImportanceForAccessibility =
534 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
535 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
536
537 @Override
538 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 return;
541 }
542 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700543 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspace != null) {
545 mWorkspaceImportanceForAccessibility =
546 mWorkspace.getImportantForAccessibility();
547 mWorkspace.setImportantForAccessibility(
548 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
549 mWorkspaceImportanceStored = true;
550 }
551 if (mHotseat != null) {
552 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
553 mHotseat.setImportantForAccessibility(
554 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
555 mHotseatImportanceStored = true;
556 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700557 }
558
559 @Override
560 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700561 if (mWorkspaceImportanceStored && mWorkspace != null) {
562 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
563 }
564 if (mHotseatImportanceStored && mHotseat != null) {
565 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
566 }
567 mWorkspaceImportanceStored = false;
568 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700569 }
570 });
Adam Cohen9211d422014-10-07 18:14:53 -0700571 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700572 }
573
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700574 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700575 public void onLauncherProviderChange() {
576 if (mLauncherCallbacks != null) {
577 mLauncherCallbacks.onLauncherProviderChange();
578 }
579 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700580
Winson Chungef7f8742015-06-04 17:18:17 -0700581 /**
582 * Updates the bounds of all the overlays to match the new fixed bounds.
583 */
584 public void updateOverlayBounds(Rect newBounds) {
585 mAppsView.setSearchBarBounds(newBounds);
586 mWidgetsView.setSearchBarBounds(newBounds);
587 }
588
Adam Cohen9211d422014-10-07 18:14:53 -0700589 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 return mLauncherCallbacks.hasCustomContentToLeft();
593 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 return false;
595 }
596
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 * {@link #addToCustomContentPage}. This will only be invoked if
600 * {@link #hasCustomContentToLeft()} is {@code true}.
601 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 mLauncherCallbacks.populateCustomContentContainer();
605 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 }
607
608 /**
609 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
610 * ensure the custom content page is added or removed if necessary.
611 */
612 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600613 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 // Not bound yet, wait for bindScreens to be called.
615 return;
616 }
617
618 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
619 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500620 mWorkspace.createCustomContentContainer();
621 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
623 mWorkspace.removeCustomContentPage();
624 }
625 }
626
Anton Hanssona2f665f2013-09-26 12:18:32 +0100627 public Stats getStats() {
628 return mStats;
629 }
630
Hyunyoung Song3f471442015-04-08 19:01:34 -0700631 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700632 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
633 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700634 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700635 }
636
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000637 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700638 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
639 // This cast is safe as long as the id < 0x00FFFFFF
640 // Since we jail all the dynamically generated views, there should be no clashes
641 // with any other views.
642 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000643 }
644
645 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
647 * a configuration step, this allows the proper animations to run after other transitions.
648 */
Adam Cohendb364c32014-05-20 14:23:40 -0700649 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700650 long screenId = args.screenId;
651 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
652 // When the screen id represents an actual screen (as opposed to a rank) we make sure
653 // that the drop page actually exists.
654 screenId = ensurePendingDropLayoutExists(args.screenId);
655 }
Adam Cohendb364c32014-05-20 14:23:40 -0700656
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700659 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700663 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700664 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700665 case REQUEST_RECONFIGURE_APPWIDGET:
666 completeRestoreAppWidget(args.appWidgetId);
667 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 }
Michael Jurka27614d22012-04-02 06:40:22 -0700669 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
670 // if you turned the screen off and then back while in All Apps, Launcher would not
671 // return to the workspace. Clearing mAddInfo.container here fixes this issue
672 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700673 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 }
675
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800676 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700678 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700679 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700680 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800681 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700682
Adam Cohenad4e15c2013-10-17 16:21:35 -0700683 Runnable exitSpringLoaded = new Runnable() {
684 @Override
685 public void run() {
686 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
687 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
688 }
689 };
690
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700692 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700693 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
695 if (resultCode == RESULT_CANCELED) {
696 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700697 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700698 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800700 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700701 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700702
703 // When the user has granted permission to bind widgets, we should check to see if
704 // we can inflate the default search bar widget.
705 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 }
707 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200708 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
709 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700710 // User could have free-scrolled between pages before picking a wallpaper; make sure
711 // we move to the closest one now.
712 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700713 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200714 }
715 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200717
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700719 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700720
Adam Cohendb364c32014-05-20 14:23:40 -0700721 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800722 // We have special handling for widgets
723 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800724 final int appWidgetId;
725 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
726 : -1;
727 if (widgetId < 0) {
728 appWidgetId = pendingAddWidgetId;
729 } else {
730 appWidgetId = widgetId;
731 }
732
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800734 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700735 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
736 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 result = RESULT_CANCELED;
738 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700739 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 @Override
741 public void run() {
742 exitSpringLoadedDragModeDelayed(false, 0, null);
743 }
744 };
Adam Cohendb364c32014-05-20 14:23:40 -0700745 if (workspaceLocked) {
746 // No need to remove the empty screen if we're mid-binding, as the
747 // the bind will not add the empty screen.
748 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
749 } else {
750 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
751 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
752 }
Winson Chung5aaab772012-04-27 15:24:02 -0700753 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700754 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700755 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
756 // When the screen id represents an actual screen (as opposed to a rank)
757 // we make sure that the drop page actually exists.
758 mPendingAddInfo.screenId =
759 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
760 }
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
762
763 dropLayout.setDropPending(true);
764 final Runnable onComplete = new Runnable() {
765 @Override
766 public void run() {
767 completeTwoStageWidgetDrop(resultCode, appWidgetId);
768 dropLayout.setDropPending(false);
769 }
770 };
771 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
773 } else {
774 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
775 mPendingAddInfo);
776 sPendingAddItem = args;
777 }
Winson Chung5aaab772012-04-27 15:24:02 -0700778 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 return;
780 }
781
Sunny Goyalff572272014-07-23 13:58:07 -0700782 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
783 if (resultCode == RESULT_OK) {
784 // Update the widget view.
785 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
786 pendingAddWidgetId, mPendingAddInfo);
787 if (workspaceLocked) {
788 sPendingAddItem = args;
789 } else {
790 completeAdd(args);
791 }
792 }
793 // Leave the widget in the pending state if the user canceled the configure.
794 return;
795 }
796
Sunny Goyalaad90582015-07-09 14:22:50 -0700797 if (requestCode == REQUEST_CREATE_SHORTCUT) {
798 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
799 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
800 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
801 mPendingAddInfo);
802 if (isWorkspaceLocked()) {
803 sPendingAddItem = args;
804 } else {
805 completeAdd(args);
806 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808 }
809 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
811 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800815
816 }
817
818 @Override
819 protected void onActivityResult(
820 final int requestCode, final int resultCode, final Intent data) {
821 handleActivityResult(requestCode, resultCode, data);
822 if (mLauncherCallbacks != null) {
823 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
824 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800825 }
826
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600827 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700828 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600829 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700830 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
831 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
832 View v = null;
833 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
834 if (layout != null) {
835 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
836 }
837 Intent intent = sPendingAddItem.intent;
838 sPendingAddItem = null;
839 if (grantResults.length > 0
840 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
841 startActivity(v, intent, null);
842 } else {
843 // TODO: Show a snack bar with link to settings
844 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
845 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
846 }
847 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
850 grantResults);
851 }
852 }
853
Adam Cohendb364c32014-05-20 14:23:40 -0700854 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
855 appWidgetId, ItemInfo info) {
856 PendingAddArguments args = new PendingAddArguments();
857 args.requestCode = requestCode;
858 args.intent = data;
859 args.container = info.container;
860 args.screenId = info.screenId;
861 args.cellX = info.cellX;
862 args.cellY = info.cellY;
863 args.appWidgetId = appWidgetId;
864 return args;
865 }
866
867 /**
868 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
869 *
870 * @param screenId the screen id to check
871 * @return the new screen, or screenId if it exists
872 */
873 private long ensurePendingDropLayoutExists(long screenId) {
874 CellLayout dropLayout =
875 (CellLayout) mWorkspace.getScreenWithId(screenId);
876 if (dropLayout == null) {
877 // it's possible that the add screen was removed because it was
878 // empty and a re-bind occurred
879 mWorkspace.addExtraEmptyScreen();
880 return mWorkspace.commitExtraEmptyScreen();
881 } else {
882 return screenId;
883 }
884 }
885
Adam Cohen091440a2015-03-18 14:16:05 -0700886 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800887 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 Runnable onCompleteRunnable = null;
889 int animationType = 0;
890
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 if (resultCode == RESULT_OK) {
893 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
894 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700895 mPendingAddWidgetInfo);
896 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 onCompleteRunnable = new Runnable() {
898 @Override
899 public void run() {
900 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700901 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700902 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
903 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 }
905 };
906 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800907 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 if (mDragLayer.getAnimatedView() != null) {
911 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
912 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
913 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700914 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 // The animated view may be null in the case of a rotation during widget configuration
916 onCompleteRunnable.run();
917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700921 protected void onStop() {
922 super.onStop();
923 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700924
925 if (mLauncherCallbacks != null) {
926 mLauncherCallbacks.onStop();
927 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700928 }
929
930 @Override
931 protected void onStart() {
932 super.onStart();
933 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700934
935 if (mLauncherCallbacks != null) {
936 mLauncherCallbacks.onStart();
937 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700938 }
939
940 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200942 long startTime = 0;
943 if (DEBUG_RESUME_TIME) {
944 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400945 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200946 }
Adam Cohen9211d422014-10-07 18:14:53 -0700947
948 if (mLauncherCallbacks != null) {
949 mLauncherCallbacks.preOnResume();
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700953
Winson Chung4a2afa32012-07-19 14:53:05 -0700954 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700955 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700956 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800957 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700958 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700959 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700960 // view after launching an app, as they may be depending on the UI to be static to
961 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700962 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700963 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800964 } else if (mOnResumeState == State.WIDGETS) {
965 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700966 }
967 mOnResumeState = State.NONE;
968
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700969 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700970 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
971 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700972
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800973 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700974 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700975 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700976 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700978 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700980 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200981 // We might have postponed some bind calls until onResume (see waitUntilResume) --
982 // execute them here
983 long startTimeCallbacks = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTimeCallbacks = System.currentTimeMillis();
986 }
987
Michael Jurka1e2f4652013-07-08 18:03:46 -0700988 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
989 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700991 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200992 if (DEBUG_RESUME_TIME) {
993 Log.d(TAG, "Time spent processing callbacks in onResume: " +
994 (System.currentTimeMillis() - startTimeCallbacks));
995 }
Michael Jurka447bf842013-05-15 14:52:15 +0200996 }
Michael Jurka54554252013-08-01 12:52:23 +0200997 if (mOnResumeCallbacks.size() > 0) {
998 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
999 mOnResumeCallbacks.get(i).run();
1000 }
1001 mOnResumeCallbacks.clear();
1002 }
Winson Chunge4e50662012-01-23 14:45:13 -08001003
1004 // Reset the pressed state of icons that were locked in the press state while activities
1005 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001006 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001007 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001008 mWaitingForResume.setStayPressed(false);
1009 }
Winson Chung82963d52013-10-09 11:20:57 -07001010
Adam Cohen06dff352012-06-01 17:17:08 -07001011 // It is possible that widgets can receive updates while launcher is not in the foreground.
1012 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1013 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1014 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001015 if (!isWorkspaceLoading()) {
1016 getWorkspace().reinflateWidgetsIfNecessary();
1017 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001018 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001019
Michael Jurka447bf842013-05-15 14:52:15 +02001020 if (DEBUG_RESUME_TIME) {
1021 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1022 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001023
Adam Cohen4b66bf32015-09-18 12:15:19 -07001024 // We want to suppress callbacks about CustomContent being shown if we have just received
1025 // onNewIntent while the user was present within launcher. In that case, we post a call
1026 // to move the user to the main screen (which will occur after onResume). We don't want to
1027 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1028 // suppress here.
1029 if (mWorkspace.getCustomContentCallbacks() != null
1030 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001031 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001032 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001033 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1034 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001035 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001036 }
1037 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001038 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001039 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001040 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001041
Sunny Goyal756adbc2015-04-16 15:20:51 -07001042 if (!isWorkspaceLoading()) {
1043 // Process any items that were added while Launcher was away.
1044 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1045 }
Adam Cohen9211d422014-10-07 18:14:53 -07001046
1047 if (mLauncherCallbacks != null) {
1048 mLauncherCallbacks.onResume();
1049 }
Adam Cohen06dff352012-06-01 17:17:08 -07001050 }
1051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001054 // Ensure that items added to Launcher are queued until Launcher returns
1055 InstallShortcutReceiver.enableInstallQueue();
1056
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001058 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001059 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001060 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001061
1062 // We call onHide() aggressively. The custom content callbacks should be able to
1063 // debounce excess onHide calls.
1064 if (mWorkspace.getCustomContentCallbacks() != null) {
1065 mWorkspace.getCustomContentCallbacks().onHide();
1066 }
Romain Guycbb89e42009-06-08 15:52:54 -07001067
Adam Cohen9211d422014-10-07 18:14:53 -07001068 if (mLauncherCallbacks != null) {
1069 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 }
1072
1073 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001074 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1075 // by a onResume or by scrolling otherwise.
1076 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001077
1078 // Custom content is completely hidden
1079 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001080
1081 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1082 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001083
1084 // Indicates whether the user is allowed to scroll away from the custom content.
1085 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
1087
Adam Cohenc2d6e892014-10-16 09:49:24 -07001088 public interface LauncherOverlay {
1089
1090 /**
1091 * Touch interaction leading to overscroll has begun
1092 */
1093 public void onScrollInteractionBegin();
1094
1095 /**
1096 * Touch interaction related to overscroll has ended
1097 */
1098 public void onScrollInteractionEnd();
1099
1100 /**
1101 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1102 * screen (or in the case of RTL, the rightmost screen).
1103 */
1104 public void onScrollChange(int progress, boolean rtl);
1105
1106 /**
1107 * Screen has stopped scrolling
1108 */
1109 public void onScrollSettled();
1110
1111 /**
1112 * This method can be called by the Launcher in order to force the LauncherOverlay
1113 * to exit fully immersive mode.
1114 */
1115 public void forceExitFullImmersion();
1116 }
1117
Jun Mukai8af0cd82015-05-12 12:32:12 -07001118 public interface LauncherSearchCallbacks {
1119 /**
1120 * Called when the search overlay is shown.
1121 */
1122 public void onSearchOverlayOpened();
1123
1124 /**
1125 * Called when the search overlay is dismissed.
1126 */
1127 public void onSearchOverlayClosed();
1128 }
1129
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 public interface LauncherOverlayCallbacks {
1131 /**
1132 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1133 * however it doesn't modify any state within the launcher.
1134 */
1135 public boolean canEnterFullImmersion();
1136
1137 /**
1138 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1139 * eg. by occupying the full screen and handling all touch events.
1140 *
1141 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1142 * case, Launcher will modify any necessary state and assumes the overlay is
1143 * handling all interaction. If false, the LauncherOverlay should cancel any
1144 *
1145 */
1146 public boolean enterFullImmersion();
1147
1148 /**
1149 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1150 * full control over UI and state.
1151 */
1152 public void exitFullImmersion();
1153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
1157 @Override
1158 public boolean canEnterFullImmersion() {
1159 return mState == State.WORKSPACE;
1160 }
1161
1162 @Override
1163 public boolean enterFullImmersion() {
1164 if (mState == State.WORKSPACE) {
1165 // When fully immersed, disregard any touches which fall through.
1166 mDragLayer.setBlockTouch(true);
1167 return true;
1168 }
1169 return false;
1170 }
1171
1172 @Override
1173 public void exitFullImmersion() {
1174 mDragLayer.setBlockTouch(false);
1175 }
1176 }
1177
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001179 if (mLauncherCallbacks != null) {
1180 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001181 } else {
1182 // On devices with a locked orientation, we will at least have the allow rotation
1183 // setting.
1184 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001185 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001186 }
1187
Adam Cohen9211d422014-10-07 18:14:53 -07001188 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001189 CustomContentCallbacks callbacks, String description) {
1190 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001191 }
1192
Adam Cohenedb40762013-07-18 16:45:45 -07001193 // The custom content needs to offset its content to account for the QSB
1194 public int getTopOffsetForCustomContent() {
1195 return mWorkspace.getPaddingTop();
1196 }
1197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001198 @Override
1199 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001201 if (mModel.isCurrentCallbacks(this)) {
1202 mModel.stopLoader();
1203 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001204 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1205
Romain Guy13c2e7b2010-03-10 19:45:00 -08001206 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001207 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 @Override
1211 public void onWindowFocusChanged(boolean hasFocus) {
1212 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001213 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001214
1215 if (mLauncherCallbacks != null) {
1216 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1217 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 private boolean acceptFilter() {
1221 final InputMethodManager inputManager = (InputMethodManager)
1222 getSystemService(Context.INPUT_METHOD_SERVICE);
1223 return !inputManager.isFullscreenMode();
1224 }
1225
1226 @Override
1227 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001228 final int uniChar = event.getUnicodeChar();
1229 final boolean handled = super.onKeyDown(keyCode, event);
1230 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1231 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1233 keyCode, event);
1234 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001236 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // showSearchDialog()
1238 // If there are multiple keystrokes before the search dialog takes focus,
1239 // onSearchRequested() will be called for every keystroke,
1240 // but it is idempotent, so it's fine.
1241 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
1244
Joe Onorato8a9625e2010-01-28 15:55:35 -08001245 // Eat the long press event so the keyboard doesn't come up.
1246 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1247 return true;
1248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 return handled;
1251 }
1252
Winson46163472015-09-22 17:31:56 -07001253 @Override
1254 public boolean onKeyUp(int keyCode, KeyEvent event) {
1255 if (keyCode == KeyEvent.KEYCODE_MENU) {
1256 // Ignore the menu key if we are currently dragging or are on the custom content screen
1257 if (!isOnCustomContent() && !mDragController.isDragging()) {
1258 // Close any open folders
1259 closeFolder();
1260
1261 // Stop resizing any widgets
1262 mWorkspace.exitWidgetResizeMode();
1263
1264 // Show the overview mode if we are on the workspace
1265 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1266 !mWorkspace.isSwitchingState()) {
1267 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001268 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001269 }
1270 }
1271 return true;
1272 }
1273 return super.onKeyUp(keyCode, event);
1274 }
1275
Karl Rosaen138a0412009-04-23 19:00:21 -07001276 private String getTypedText() {
1277 return mDefaultKeySsb.toString();
1278 }
1279
1280 private void clearTypedText() {
1281 mDefaultKeySsb.clear();
1282 mDefaultKeySsb.clearSpans();
1283 Selection.setSelection(mDefaultKeySsb, 0);
1284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001287 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1288 * State
1289 */
1290 private static State intToState(int stateOrdinal) {
1291 State state = State.WORKSPACE;
1292 final State[] stateValues = State.values();
1293 for (int i = 0; i < stateValues.length; i++) {
1294 if (stateValues[i].ordinal() == stateOrdinal) {
1295 state = stateValues[i];
1296 break;
1297 }
1298 }
1299 return state;
1300 }
1301
1302 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 * Restores the previous state, if it exists.
1304 *
1305 * @param savedState The previous state.
1306 */
1307 private void restoreState(Bundle savedState) {
1308 if (savedState == null) {
1309 return;
1310 }
1311
Michael Jurka883f55b2010-10-21 15:47:14 -07001312 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001313 if (state == State.APPS || state == State.WIDGETS) {
1314 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001315 }
1316
Adam Cohen21cd0022013-10-09 18:57:02 -07001317 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1318 PagedView.INVALID_RESTORE_PAGE);
1319 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001320 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
Sunny Goyal756cd262015-08-20 12:33:21 -07001323 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1324 if (itemValues != null) {
1325 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001326 AppWidgetProviderInfo info = savedState.getParcelable(
1327 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001328 mPendingAddWidgetInfo = info == null ?
1329 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1330
Adam Cohen4637b5a2013-11-04 18:21:24 -08001331 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001332 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 mRestoring = true;
1334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
1337 /**
1338 * Finds all the views we need and configure them properly.
1339 */
1340 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001341 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001342
Craig Mautner360310b2012-10-26 15:13:08 -07001343 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001344 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001345 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1346 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001347 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001348 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001349
John Spurlock77e1f472013-09-11 10:09:51 -04001350 mLauncherView.setSystemUiVisibility(
1351 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001352 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353
Winson Chung4c98d922011-05-31 16:50:48 -07001354 // Setup the drag layer
1355 mDragLayer.setup(this, dragController);
1356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 // Setup the hotseat
1358 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1359 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001360 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 }
1362
Winsone9f27272015-10-13 10:47:51 -07001363 // Setup the overview panel
1364 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001365
Winson Chung4c98d922011-05-31 16:50:48 -07001366 // Setup the workspace
1367 mWorkspace.setHapticFeedbackEnabled(false);
1368 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001369 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001370 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001371
Tony Wickham34d2c912015-09-11 09:27:58 -07001372 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001373 mSearchDropTargetBar = (SearchDropTargetBar)
1374 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001375 // Get the app info bar
1376 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1377 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001378
Winson Chungef7f8742015-06-04 17:18:17 -07001379 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001380 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001381 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001382 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1383 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1384 } else {
1385 mAppsView.setSearchBarController(new DefaultAppSearchController());
1386 }
Craig Mautner360310b2012-10-26 15:13:08 -07001387
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001389 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001390 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001391 dragController.setMoveTarget(mWorkspace);
1392 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001393 if (mSearchDropTargetBar != null) {
1394 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001395 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001396 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001397 if (mAppInfoDropTargetBar != null) {
1398 mAppInfoDropTargetBar.setup(this, dragController);
1399 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001400
Sunny Goyal322d5562015-06-25 19:35:49 -07001401 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1402 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
Winsone9f27272015-10-13 10:47:51 -07001406 private void setupOverviewPanel() {
1407 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1408
1409 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1410 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1411 @Override
1412 public boolean onLongClick(View v) {
1413 return v.performClick();
1414 }
1415 };
1416
1417 // Bind wallpaper button actions
1418 View wallpaperButton = findViewById(R.id.wallpaper_button);
1419 wallpaperButton.setOnClickListener(new OnClickListener() {
1420 @Override
1421 public void onClick(View view) {
1422 if (!mWorkspace.isSwitchingState()) {
1423 onClickWallpaperPicker(view);
1424 }
1425 }
1426 });
1427 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1428 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1429
1430 // Bind widget button actions
1431 mWidgetsButton = findViewById(R.id.widget_button);
1432 mWidgetsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickAddWidgetButton(view);
1437 }
1438 }
1439 });
1440 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1441 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442
1443 // Bind settings actions
1444 View settingsButton = findViewById(R.id.settings_button);
1445 boolean hasSettings = hasSettings();
1446 if (hasSettings) {
1447 settingsButton.setOnClickListener(new OnClickListener() {
1448 @Override
1449 public void onClick(View view) {
1450 if (!mWorkspace.isSwitchingState()) {
1451 onClickSettingsButton(view);
1452 }
1453 }
1454 });
1455 settingsButton.setOnLongClickListener(performClickOnLongClick);
1456 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1457 } else {
1458 settingsButton.setVisibility(View.GONE);
1459 }
1460
1461 mOverviewPanel.setAlpha(0f);
1462 }
1463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 * Sets the all apps button. This method is called from {@link Hotseat}.
1466 */
1467 public void setAllAppsButton(View allAppsButton) {
1468 mAllAppsButton = allAppsButton;
1469 }
1470
1471 public View getAllAppsButton() {
1472 return mAllAppsButton;
1473 }
1474
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001475 public View getWidgetsButton() {
1476 return mWidgetsButton;
1477 }
1478
Anjali Koppal5ad44842014-03-10 20:34:39 -07001479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 * Creates a view representing a shortcut.
1481 *
1482 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001484 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001485 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
1488 /**
1489 * Creates a view representing a shortcut inflated from the specified resource.
1490 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 * @param parent The group the shortcut belongs to.
1492 * @param info The data structure describing the shortcut.
1493 *
1494 * @return A View inflated from layoutResId.
1495 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001496 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001497 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001498 parent, false);
1499 favorite.applyFromShortcutInfo(info, mIconCache);
1500 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001502 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 return favorite;
1504 }
1505
1506 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 * Add a shortcut to the workspace.
1508 *
1509 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001511 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001512 int cellY) {
1513 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001515
Sunny Goyal5c97f512015-05-19 16:03:28 -07001516 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001517 if (info == null) {
1518 return;
1519 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001520 final View view = createShortcut(info);
1521
Sunny Goyal5c97f512015-05-19 16:03:28 -07001522 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001523 // First we check if we already know the exact location where we want to add this item.
1524 if (cellX >= 0 && cellY >= 0) {
1525 cellXY[0] = cellX;
1526 cellXY[1] = cellY;
1527 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001528
1529 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001531 true, null,null)) {
1532 return;
1533 }
1534 DragObject dragObject = new DragObject();
1535 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001536 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1537 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001538 return;
1539 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001540 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001541 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 }
1543
1544 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001545 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001546 return;
1547 }
1548
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001549 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550
1551 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555 }
1556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001558 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001560 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 */
Adam Cohen091440a2015-03-18 14:16:05 -07001562 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001563 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1564
1565 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001567 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1568 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001589 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1590 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001591 } else {
1592 // The AppWidgetHostView has already been inflated and instantiated
1593 launcherInfo.hostView = hostView;
1594 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001595 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001598 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Romain Guycbb89e42009-06-08 15:52:54 -07001600
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1602 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1603 item.hostView.setTag(item);
1604 item.onBindAppWidget(this);
1605
1606 item.hostView.setFocusable(true);
1607 item.hostView.setOnFocusChangeListener(mFocusHandler);
1608
1609 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1610 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1611
1612 if (!item.isCustomWidget()) {
1613 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1614 }
1615 }
1616
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1618 @Override
1619 public void onReceive(Context context, Intent intent) {
1620 final String action = intent.getAction();
1621 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1622 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001623 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001624 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001625
Winson Chungc100e8e2011-08-09 16:02:43 -07001626 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001627 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001628 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001629 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001630 if (!showWorkspace(false)) {
1631 // If we are already on the workspace, then manually reset all apps
1632 mAppsView.reset();
1633 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1636 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001637 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001638 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1639 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001640 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1642 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1643 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001644 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001645 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1646 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 }
1648 }
1649 };
1650
1651 @Override
1652 public void onAttachedToWindow() {
1653 super.onAttachedToWindow();
1654
1655 // Listen for broadcasts related to user-presence
1656 final IntentFilter filter = new IntentFilter();
1657 filter.addAction(Intent.ACTION_SCREEN_OFF);
1658 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001659 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001660 if (ENABLE_DEBUG_INTENTS) {
1661 filter.addAction(DebugIntents.DELETE_DATABASE);
1662 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001665 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001666 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 mVisible = true;
1668 }
1669
1670 @Override
1671 public void onDetachedFromWindow() {
1672 super.onDetachedFromWindow();
1673 mVisible = false;
1674
Adam Cohend113e0c2010-11-11 10:48:05 -08001675 if (mAttached) {
1676 unregisterReceiver(mReceiver);
1677 mAttached = false;
1678 }
Winson Chungb745afb2015-03-02 11:51:23 -08001679 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001680 }
1681
1682 public void onWindowVisibilityChanged(int visibility) {
1683 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001684 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001685 // The following code used to be in onResume, but it turns out onResume is called when
1686 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1687 // is a more appropriate event to handle
1688 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001689 if (!mWorkspaceLoading) {
1690 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001691 // We want to let Launcher draw itself at least once before we force it to build
1692 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001693 // apps is nice and speedy.
1694 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001695 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001696 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 if (mStarted) return;
1698 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001699 // We delay the layer building a bit in order to give
1700 // other message processing a time to run. In particular
1701 // this avoids a delay in hiding the IME if it was
1702 // currently shown, because doing that may involve
1703 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001704 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 final ViewTreeObserver.OnDrawListener listener = this;
1706 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001707 public void run() {
1708 if (mWorkspace != null &&
1709 mWorkspace.getViewTreeObserver() != null) {
1710 mWorkspace.getViewTreeObserver().
1711 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001712 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001713 }
1714 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001715 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001716 }
1717 });
1718 }
1719 clearTypedText();
1720 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 }
1722
Adam Cohen091440a2015-03-18 14:16:05 -07001723 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 mHandler.removeMessages(ADVANCE_MSG);
1725 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1726 mHandler.sendMessageDelayed(msg, delay);
1727 mAutoAdvanceSentTime = System.currentTimeMillis();
1728 }
1729
Adam Cohen091440a2015-03-18 14:16:05 -07001730 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1732 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1733 mAutoAdvanceRunning = autoAdvanceRunning;
1734 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001735 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 sendAdvanceMessage(delay);
1737 } else {
1738 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001739 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1741 }
1742 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001743 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
1745 }
1746 }
1747
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001748 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1749
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001751 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 if (msg.what == ADVANCE_MSG) {
1753 int i = 0;
1754 for (View key: mWidgetsToAdvance.keySet()) {
1755 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001756 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001758 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 public void run() {
1760 ((Advanceable) v).advance();
1761 }
1762 }, delay);
1763 }
1764 i++;
1765 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001766 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001768 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001770 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001771
Winsonc0b52fe2015-09-09 16:38:15 -07001772 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001773 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1775 if (v instanceof Advanceable) {
1776 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001777 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001778 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
1780 }
1781
Winsonc0b52fe2015-09-09 16:38:15 -07001782 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 if (mWidgetsToAdvance.containsKey(hostView)) {
1784 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001785 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 }
1788
Hyunyoung Song3f471442015-04-08 19:01:34 -07001789 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001790 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1791 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793
Winson Chunga6945242014-01-08 14:04:34 -08001794 public DragLayer getDragLayer() {
1795 return mDragLayer;
1796 }
1797
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001798 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001799 return mAppsView;
1800 }
1801
Hyunyoung Song3f471442015-04-08 19:01:34 -07001802 public WidgetsContainerView getWidgetsView() {
1803 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001804 }
1805
Winson Chunga6945242014-01-08 14:04:34 -08001806 public Workspace getWorkspace() {
1807 return mWorkspace;
1808 }
1809
1810 public Hotseat getHotseat() {
1811 return mHotseat;
1812 }
1813
Jorim Jaggid017f882014-01-14 17:08:48 -08001814 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001815 return mOverviewPanel;
1816 }
1817
Winson Chung006ee262015-08-03 14:40:11 -07001818 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001819 return mSearchDropTargetBar;
1820 }
1821
Tony Wickham34d2c912015-09-11 09:27:58 -07001822 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1823 return mAppInfoDropTargetBar;
1824 }
1825
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001826 public LauncherAppWidgetHost getAppWidgetHost() {
1827 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
Romain Guycbb89e42009-06-08 15:52:54 -07001829
Winson Chunga9abd0e2010-10-27 17:18:37 -07001830 public LauncherModel getModel() {
1831 return mModel;
1832 }
1833
Winson Chunga6945242014-01-08 14:04:34 -08001834 protected SharedPreferences getSharedPrefs() {
1835 return mSharedPrefs;
1836 }
1837
Adam Cohen2e6da152015-05-06 11:42:25 -07001838 public DeviceProfile getDeviceProfile() {
1839 return mDeviceProfile;
1840 }
1841
Bjorn Bringertc459e522013-06-07 19:36:01 +01001842 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001843 getWindow().closeAllPanels();
1844
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001845 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001846 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001847 }
1848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 @Override
1850 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001851 long startTime = 0;
1852 if (DEBUG_RESUME_TIME) {
1853 startTime = System.currentTimeMillis();
1854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 super.onNewIntent(intent);
1856
1857 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001858 Folder openFolder = mWorkspace.getOpenFolder();
1859 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1860 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1861 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1862 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1863 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001864 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001865 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001866
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 if (mWorkspace == null) {
1868 // Can be cases where mWorkspace is null, this prevents a NPE
1869 return;
1870 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001871 // In all these cases, only animate if we're already on home
1872 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001873
Sunny Goyal935fca12015-10-13 10:19:01 -07001874 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 exitSpringLoadedDragMode();
1876
1877 // If we are already on home, then just animate back to the workspace,
1878 // otherwise, just wait until onResume to set the state back to Workspace
1879 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001880 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 } else {
1882 mOnResumeState = State.WORKSPACE;
1883 }
1884
1885 final View v = getWindow().peekDecorView();
1886 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001887 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 INPUT_METHOD_SERVICE);
1889 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1890 }
1891
Winson Chungb745afb2015-03-02 11:51:23 -08001892 // Reset the apps view
1893 if (!alreadyOnHome && mAppsView != null) {
1894 mAppsView.scrollToTop();
1895 }
1896
Hyunyoung Song3f471442015-04-08 19:01:34 -07001897 // Reset the widgets view
1898 if (!alreadyOnHome && mWidgetsView != null) {
1899 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001900 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001901
Adam Cohen9211d422014-10-07 18:14:53 -07001902 if (mLauncherCallbacks != null) {
1903 mLauncherCallbacks.onHomeIntent();
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
Adam Cohened307df2013-10-02 09:37:31 -07001906
Adam Cohen9211d422014-10-07 18:14:53 -07001907 if (mLauncherCallbacks != null) {
1908 mLauncherCallbacks.onNewIntent(intent);
1909 }
Winson15f8b172015-08-19 11:02:39 -07001910
1911 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1912 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1913 // animation.
1914 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001915 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1916 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001917 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1918 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001919
1920 // We use this flag to suppress noisy callbacks above custom content state
1921 // from onResume.
1922 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001923 mWorkspace.post(new Runnable() {
1924 @Override
1925 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001926 if (mWorkspace != null) {
1927 mWorkspace.moveToDefaultScreen(true);
1928 }
Winson15f8b172015-08-19 11:02:39 -07001929 }
1930 });
1931 }
1932 }
1933
1934 if (DEBUG_RESUME_TIME) {
1935 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1936 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001937 }
1938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001940 public void onRestoreInstanceState(Bundle state) {
1941 super.onRestoreInstanceState(state);
1942 for (int page: mSynchronouslyBoundPages) {
1943 mWorkspace.restoreInstanceStateForChild(page);
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
1947 @Override
1948 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001949 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001950 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1951 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001952 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001953 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Michael Jurka883f55b2010-10-21 15:47:14 -07001955 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001956 Folder openFolder = mWorkspace.getOpenFolder();
1957 if (openFolder != null) {
1958 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001963 ContentValues itemValues = new ContentValues();
1964 mPendingAddInfo.writeToValues(itemValues);
1965 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001967 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
Hyunyoung Song3f471442015-04-08 19:01:34 -07001970 // Save the current widgets tray?
1971 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001972
1973 if (mLauncherCallbacks != null) {
1974 mLauncherCallbacks.onSaveInstanceState(outState);
1975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
1978 @Override
1979 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001981
Winson Chunge7a03942011-08-05 15:05:12 -07001982 // Remove all pending runnables
1983 mHandler.removeMessages(ADVANCE_MSG);
1984 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001985 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001986
Winson Chungcd2b0142011-06-08 16:02:26 -07001987 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001988 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001989
1990 // 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();
1994 app.setLauncher(null);
1995 }
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
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002010 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002011 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002012 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002013 mWorkspace = null;
2014 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002015
Michael Jurka2ecf9952012-06-18 12:52:28 -07002016 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002017
2018 if (mLauncherCallbacks != null) {
2019 mLauncherCallbacks.onDestroy();
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Adam Cohena9cf38f2011-05-02 15:36:58 -07002023 public DragController getDragController() {
2024 return mDragController;
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
2028 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002029 onStartForResult(requestCode);
2030 super.startActivityForResult(intent, requestCode);
2031 }
2032
2033 @Override
2034 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2035 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2036 onStartForResult(requestCode);
2037 try {
2038 super.startIntentSenderForResult(intent, requestCode,
2039 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2040 } catch (IntentSender.SendIntentException e) {
2041 throw new ActivityNotFoundException();
2042 }
2043 }
2044
2045 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002046 if (requestCode >= 0) {
2047 setWaitingForResult(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 /**
2052 * Indicates that we want global search for this activity by setting the globalSearch
2053 * argument for {@link #startSearch} to true.
2054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002056 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 if (initialQuery == null) {
2060 // Use any text typed in the launcher as the initial query
2061 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 if (appSearchData == null) {
2064 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002065 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Winson Chungadf0c182012-08-23 14:59:07 -07002067 Rect sourceBounds = new Rect();
2068 if (mSearchDropTargetBar != null) {
2069 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2070 }
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 if (clearTextImmediately) {
2075 clearTypedText();
2076 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002077
2078 // We need to show the workspace after starting the search
2079 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 }
2081
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 /**
2083 * Start a text search.
2084 *
2085 * @return {@code true} if the search will start immediately, so any further keypresses
2086 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2087 * to buffer keypresses.
2088 */
2089 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002091 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2092 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2093 sourceBounds);
2094 }
2095
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002098 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 }
2100
2101 /**
2102 * Starts the global search activity. This code is a copied from SearchManager
2103 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002104 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002106 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2108 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2109 if (globalSearchActivity == null) {
2110 Log.w(TAG, "No global search activity found.");
2111 return;
2112 }
2113 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2114 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2115 intent.setComponent(globalSearchActivity);
2116 // Make sure that we have a Bundle to put source in
2117 if (appSearchData == null) {
2118 appSearchData = new Bundle();
2119 } else {
2120 appSearchData = new Bundle(appSearchData);
2121 }
Adam Cohen9211d422014-10-07 18:14:53 -07002122 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002123 if (!appSearchData.containsKey("source")) {
2124 appSearchData.putString("source", getPackageName());
2125 }
2126 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2127 if (!TextUtils.isEmpty(initialQuery)) {
2128 intent.putExtra(SearchManager.QUERY, initialQuery);
2129 }
2130 if (selectInitialQuery) {
2131 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2132 }
2133 intent.setSourceBounds(sourceBounds);
2134 try {
2135 startActivity(intent);
2136 } catch (ActivityNotFoundException ex) {
2137 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
2140
Winson Chungbedf9232015-07-10 12:38:30 -07002141 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2142 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2143 return;
2144 }
2145
2146 // If not handled, then just start the provided search intent
2147 startActivitySafely(v, searchIntent, null);
2148 }
2149
Yura4f93ec62014-02-04 14:15:21 +00002150 public boolean isOnCustomContent() {
2151 return mWorkspace.isOnOrMovingToCustomContent();
2152 }
2153
Winson Chung70d72102011-08-12 11:24:35 -07002154 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002155 public boolean onPrepareOptionsMenu(Menu menu) {
2156 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002157 if (mLauncherCallbacks != null) {
2158 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2159 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002160 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 @Override
2164 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002165 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002166 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002167 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002168 }
2169
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 public boolean isWorkspaceLocked() {
2171 return mWorkspaceLoading || mWaitingForResult;
2172 }
2173
Adam Cohen517a7f52014-03-01 12:12:59 -08002174 public boolean isWorkspaceLoading() {
2175 return mWorkspaceLoading;
2176 }
2177
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178 private void setWorkspaceLoading(boolean value) {
2179 boolean isLocked = isWorkspaceLocked();
2180 mWorkspaceLoading = value;
2181 if (isLocked != isWorkspaceLocked()) {
2182 onWorkspaceLockedChanged();
2183 }
2184 }
2185
2186 private void setWaitingForResult(boolean value) {
2187 boolean isLocked = isWorkspaceLocked();
2188 mWaitingForResult = value;
2189 if (isLocked != isWorkspaceLocked()) {
2190 onWorkspaceLockedChanged();
2191 }
2192 }
2193
Adam Cohen9211d422014-10-07 18:14:53 -07002194 protected void onWorkspaceLockedChanged() {
2195 if (mLauncherCallbacks != null) {
2196 mLauncherCallbacks.onWorkspaceLockedChanged();
2197 }
2198 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002199
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002201 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002203 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2204 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002205 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002206 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002207
Tony Wickhama0628cc2015-10-14 15:23:04 -07002208 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002209 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002210 if (LOGD) {
2211 Log.d(TAG, "Adding widget from drop");
2212 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002213 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2214 }
2215
Sunny Goyale6b63a32015-01-16 16:14:29 -08002216 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002217 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2218 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002219 if (appWidgetInfo.configure != null) {
2220 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002221 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002222
Bjorn Bringert7984c942009-12-09 15:38:25 +00002223 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002224 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2225 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002228 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002229 Runnable onComplete = new Runnable() {
2230 @Override
2231 public void run() {
2232 // Exit spring loaded mode if necessary after adding the widget
2233 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2234 null);
2235 }
2236 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002238 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002239 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241 }
Romain Guycbb89e42009-06-08 15:52:54 -07002242
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002243 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002244 // Close any folders that may be open.
2245 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002246 mWorkspace.moveToCustomContentScreen(animate);
2247 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002248
2249 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2250 int[] cell, int spanX, int spanY) {
2251 switch (info.itemType) {
2252 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2253 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2254 int span[] = new int[2];
2255 span[0] = spanX;
2256 span[1] = spanY;
2257 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2258 container, screenId, cell, span);
2259 break;
2260 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2261 processShortcutFromDrop(info.componentName, container, screenId, cell);
2262 break;
2263 default:
2264 throw new IllegalStateException("Unknown item type: " + info.itemType);
2265 }
2266 }
2267
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 /**
2269 * Process a shortcut drop.
2270 *
2271 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002275 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2276 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280
2281 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.cellX = cell[0];
2283 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285
2286 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2287 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002288 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 }
2290
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 /**
2292 * Process a widget drop.
2293 *
2294 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002298 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2299 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002303 mPendingAddInfo.minSpanX = info.minSpanX;
2304 mPendingAddInfo.minSpanY = info.minSpanY;
2305
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 mPendingAddInfo.cellX = cell[0];
2308 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002310 if (span != null) {
2311 mPendingAddInfo.spanX = span[0];
2312 mPendingAddInfo.spanY = span[1];
2313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 AppWidgetHostView hostView = info.boundWidget;
2316 int appWidgetId;
2317 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002318 // In the case where we've prebound the widget, we remove it from the DragLayer
2319 if (LOGD) {
2320 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2321 }
2322 getDragLayer().removeView(hostView);
2323
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002325 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002326
2327 // Clear the boundWidget so that it doesn't get destroyed.
2328 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 // In this case, we either need to start an activity to get permission to bind
2331 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002332 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002333 Bundle options = info.bindOptions;
2334
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2336 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002337 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002338 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002339 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002340 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002341 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2342 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2343 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002344 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2345 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002346 // TODO: we need to make sure that this accounts for the options bundle.
2347 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002348 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2349 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 }
2352
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002355 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 folderInfo.title = getText(R.string.folder_name);
2357
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002359 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2360 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002361 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362
2363 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002364 FolderIcon newFolder =
2365 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002366 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002367 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002368 // Force measure the new folder icon
2369 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2370 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002371 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 }
Romain Guycbb89e42009-06-08 15:52:54 -07002373
Winsonc0b52fe2015-09-09 16:38:15 -07002374 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002375 * Unbinds the view for the specified item, and removes the item and all its children.
2376 *
2377 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002378 * @param itemInfo the {@link ItemInfo} for this view.
2379 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002380 */
Winson2949fb52015-09-24 09:56:11 -07002381 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002382 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002383 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002384 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2385 if (folderInfo != null) {
2386 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002387 } else {
2388 mWorkspace.removeWorkspaceItem(v);
2389 }
Winsonc0b52fe2015-09-09 16:38:15 -07002390 if (deleteFromDb) {
2391 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2392 }
2393 } else if (itemInfo instanceof FolderInfo) {
2394 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2395 unbindFolder(folderInfo);
2396 mWorkspace.removeWorkspaceItem(v);
2397 if (deleteFromDb) {
2398 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2399 }
2400 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2401 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002402 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002403 removeWidgetToAutoAdvance(widgetInfo.hostView);
2404 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002405 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002406 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002408
Winsonc0b52fe2015-09-09 16:38:15 -07002409 } else {
2410 return false;
2411 }
2412 return true;
2413 }
2414
2415 /**
2416 * Unbinds any launcher references to the folder.
2417 */
2418 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002419 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 }
2421
Winsonc0b52fe2015-09-09 16:38:15 -07002422 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002423 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002424 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002425 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002426 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002427 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002428 // Deleting an app widget ID is a void call but writes to disk before returning
2429 // to the caller...
2430 new AsyncTask<Void, Void, Void>() {
2431 public Void doInBackground(Void ... args) {
2432 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2433 return null;
2434 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002435 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002436 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002437 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002438 }
2439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 @Override
2441 public boolean dispatchKeyEvent(KeyEvent event) {
2442 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2443 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002445 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002446 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002447 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002448 dumpState();
2449 return true;
2450 }
2451 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002452 }
2453 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2454 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 return true;
2457 }
2458 }
2459
2460 return super.dispatchKeyEvent(event);
2461 }
2462
Joe Onorato88ec0992009-11-19 13:16:06 -08002463 @Override
2464 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002465 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2466 return;
2467 }
2468
Sunny Goyal45478022015-06-08 16:52:41 -07002469 if (mDragController.isDragging()) {
2470 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002471 return;
2472 }
2473
Winson Chungb745afb2015-03-02 11:51:23 -08002474 if (isAppsViewVisible()) {
2475 showWorkspace(true);
2476 } else if (isWidgetsViewVisible()) {
2477 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002478 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002479 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002480 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002481 Folder openFolder = mWorkspace.getOpenFolder();
2482 if (openFolder.isEditingName()) {
2483 openFolder.dismissEditingName();
2484 } else {
2485 closeFolder();
2486 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002487 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002488 mWorkspace.exitWidgetResizeMode();
2489
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 // Back button is a no-op here, but give at least some feedback for the button press
2491 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002492 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002496 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002497 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002498 @Override
2499 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002500 if (mAppWidgetHost != null) {
2501 mAppWidgetHost.startListening();
2502 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002503
2504 // Recreate the QSB, as the widget has been reset.
2505 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 }
2507
2508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 * Launches the intent referred by the clicked shortcut.
2510 *
2511 * @param v The view representing the clicked shortcut.
2512 */
2513 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002514 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2515 // view has detached (it's possible for this to happen if the view is removed mid touch).
2516 if (v.getWindowToken() == null) {
2517 return;
2518 }
2519
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002520 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 return;
2522 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002523
Adam Cohen1697b792013-09-17 19:08:21 -07002524 if (v instanceof Workspace) {
2525 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002526 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002527 }
2528 return;
2529 }
2530
2531 if (v instanceof CellLayout) {
2532 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002533 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2534 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002535 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002536 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002537 }
2538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002540 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002543 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002545 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002546 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002547 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002548 } else if (tag instanceof AppInfo) {
2549 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002550 } else if (tag instanceof LauncherAppWidgetInfo) {
2551 if (v instanceof PendingAppWidgetHostView) {
2552 onClickPendingWidget((PendingAppWidgetHostView) v);
2553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 }
2555 }
2556
Sunny Goyal70660032015-05-14 00:07:08 -07002557 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002558 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002559 return false;
2560 }
2561
Michael Jurkaaf442092010-06-10 17:01:57 -07002562 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002563 * Event handler for the app widget view which has not fully restored.
2564 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002566 if (mIsSafeModeEnabled) {
2567 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2568 return;
2569 }
2570
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002572 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002573 int widgetId = info.appWidgetId;
2574 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2575 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002576 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2577 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002578 mPendingAddInfo.copyFrom(info);
2579 mPendingAddWidgetId = widgetId;
2580
Sunny Goyalffe83f12014-08-14 17:39:34 -07002581 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2582 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002583 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 } else if (info.installProgress < 0) {
2585 // The install has not been queued
2586 final String packageName = info.providerName.getPackageName();
2587 showBrokenAppInstallDialog(packageName,
2588 new DialogInterface.OnClickListener() {
2589 public void onClick(DialogInterface dialog, int id) {
2590 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2591 }
2592 });
2593 } else {
2594 // Download has started.
2595 final String packageName = info.providerName.getPackageName();
2596 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002597 }
2598 }
2599
2600 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002601 * Event handler for the "grid" button that appears on the home screen, which
2602 * enters all apps mode.
2603 *
2604 * @param v The view that was clicked.
2605 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 protected void onClickAllAppsButton(View v) {
2607 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002608 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002609 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002610 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002611
2612 if (mLauncherCallbacks != null) {
2613 mLauncherCallbacks.onClickAllAppsButton(v);
2614 }
Winson Chung76648c52015-07-10 14:33:23 -07002615 }
2616 }
2617
2618 protected void onLongClickAllAppsButton(View v) {
2619 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2620 if (!isAppsViewVisible()) {
2621 showAppsView(true /* animated */, false /* resetListToTop */,
2622 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 }
2624 }
2625
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002626 private void showBrokenAppInstallDialog(final String packageName,
2627 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002628 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002629 .setTitle(R.string.abandoned_promises_title)
2630 .setMessage(R.string.abandoned_promise_explanation)
2631 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2632 .setNeutralButton(R.string.abandoned_clean_this,
2633 new DialogInterface.OnClickListener() {
2634 public void onClick(DialogInterface dialog, int id) {
2635 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2636 mWorkspace.removeAbandonedPromise(packageName, user);
2637 }
2638 })
2639 .create().show();
2640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 /**
2644 * Event handler for an app shortcut click.
2645 *
2646 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2647 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2650 Object tag = v.getTag();
2651 if (!(tag instanceof ShortcutInfo)) {
2652 throw new IllegalArgumentException("Input must be a Shortcut");
2653 }
2654
2655 // Open shortcut
2656 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002657
2658 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002659 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2660 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002661 // Launch activity anyway, framework will tell the user why the app is suspended.
2662 } else {
2663 int error = R.string.activity_not_available;
2664 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2665 error = R.string.safemode_shortcut_error;
2666 }
2667 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2668 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002669 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002670 }
2671
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002673 if ((v instanceof BubbleTextView)
2674 && shortcut.isPromise()
2675 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002676 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002677 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 new DialogInterface.OnClickListener() {
2679 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002680 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 return;
2684 }
2685
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002687 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002688
2689 if (mLauncherCallbacks != null) {
2690 mLauncherCallbacks.onClickAppShortcut(v);
2691 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 }
2693
Adam Cohen091440a2015-03-18 14:16:05 -07002694 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002696 final ShortcutInfo shortcut;
2697 final Intent intent;
2698 if (tag instanceof ShortcutInfo) {
2699 shortcut = (ShortcutInfo) tag;
2700 intent = shortcut.intent;
2701 int[] pos = new int[2];
2702 v.getLocationOnScreen(pos);
2703 intent.setSourceBounds(new Rect(pos[0], pos[1],
2704 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002705
Sunny Goyal508da152014-08-14 10:53:27 -07002706 } else if (tag instanceof AppInfo) {
2707 shortcut = null;
2708 intent = ((AppInfo) tag).intent;
2709 } else {
2710 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2711 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712
2713 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002714 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715
2716 if (success && v instanceof BubbleTextView) {
2717 mWaitingForResume = (BubbleTextView) v;
2718 mWaitingForResume.setStayPressed(true);
2719 }
2720 }
2721
2722 /**
2723 * Event handler for a folder icon click.
2724 *
2725 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2726 */
2727 protected void onClickFolderIcon(View v) {
2728 if (LOGD) Log.d(TAG, "onClickFolder");
2729 if (!(v instanceof FolderIcon)){
2730 throw new IllegalArgumentException("Input must be a FolderIcon");
2731 }
2732
2733 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002734 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002736 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 }
Adam Cohen9211d422014-10-07 18:14:53 -07002738
2739 if (mLauncherCallbacks != null) {
2740 mLauncherCallbacks.onClickFolderIcon(v);
2741 }
Michael Jurka5130e402011-10-13 04:55:35 -07002742 }
2743
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002744 /**
2745 * Event handler for the (Add) Widgets button that appears after a long press
2746 * on the home screen.
2747 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002748 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002749 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002750 if (mIsSafeModeEnabled) {
2751 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2752 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002753 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002754 if (mLauncherCallbacks != null) {
2755 mLauncherCallbacks.onClickAddWidgetButton(view);
2756 }
Adam Cohen9211d422014-10-07 18:14:53 -07002757 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002758 }
2759
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002760 /**
2761 * Event handler for the wallpaper picker button that appears after a long press
2762 * on the home screen.
2763 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002764 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002765 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002766 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2767 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2768 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002769 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002770 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002771
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onClickWallpaperPicker(v);
2774 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002775 }
2776
2777 /**
2778 * Event handler for a click on the settings button that appears after a long press
2779 * on the home screen.
2780 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002781 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002785 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002786 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002787 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002788 }
2789
Adam Cohen61f560d2013-09-30 15:58:20 -07002790 public View.OnTouchListener getHapticFeedbackTouchListener() {
2791 if (mHapticFeedbackTouchListener == null) {
2792 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002793 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002794 @Override
2795 public boolean onTouch(View v, MotionEvent event) {
2796 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2797 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2798 }
2799 return false;
2800 }
2801 };
2802 }
2803 return mHapticFeedbackTouchListener;
2804 }
2805
Adam Cohen9211d422014-10-07 18:14:53 -07002806 public void onDragStarted(View view) {
2807 if (isOnCustomContent()) {
2808 // Custom content screen doesn't participate in drag and drop. If on custom
2809 // content screen, move to default.
2810 moveWorkspaceToDefaultScreen();
2811 }
2812
2813 if (mLauncherCallbacks != null) {
2814 mLauncherCallbacks.onDragStarted(view);
2815 }
2816 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002817
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002818 /**
2819 * Called when the user stops interacting with the launcher.
2820 * This implies that the user is now on the homescreen and is not doing housekeeping.
2821 */
Adam Cohen9211d422014-10-07 18:14:53 -07002822 protected void onInteractionEnd() {
2823 if (mLauncherCallbacks != null) {
2824 mLauncherCallbacks.onInteractionEnd();
2825 }
2826 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002827
2828 /**
2829 * Called when the user starts interacting with the launcher.
2830 * The possible interactions are:
2831 * - open all apps
2832 * - reorder an app shortcut, or a widget
2833 * - open the overview mode.
2834 * This is a good time to stop doing things that only make sense
2835 * when the user is on the homescreen and not doing housekeeping.
2836 */
Adam Cohen9211d422014-10-07 18:14:53 -07002837 protected void onInteractionBegin() {
2838 if (mLauncherCallbacks != null) {
2839 mLauncherCallbacks.onInteractionBegin();
2840 }
2841 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002842
Winson Chungcd99cd32015-04-29 11:03:24 -07002843 /** Updates the interaction state. */
2844 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002845 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002846 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002847 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2848 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002849 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002850 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002851 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002852 onInteractionEnd();
2853 }
2854 }
2855
Kenny Guyf07af7b2014-07-31 11:39:16 +01002856 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002857 try {
2858 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002859 launcherApps.showAppDetailsForProfile(componentName, user);
2860 } catch (SecurityException e) {
2861 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2862 Log.e(TAG, "Launcher does not have permission to launch settings");
2863 } catch (ActivityNotFoundException e) {
2864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2865 Log.e(TAG, "Unable to launch settings");
2866 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002867 }
2868
Michael Jurka1e2f4652013-07-08 18:03:46 -07002869 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002870 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2871 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002872 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002873 // System applications cannot be installed. For now, show a toast explaining that.
2874 // We may give them the option of disabling apps this way.
2875 int messageId = R.string.uninstall_system_app_text;
2876 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002877 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002878 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002879 String packageName = componentName.getPackageName();
2880 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002881 Intent intent = new Intent(
2882 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002883 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2884 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002885 if (user != null) {
2886 user.addToIntent(intent, Intent.EXTRA_USER);
2887 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002888 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002889 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002890 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002891 }
2892
Winson Chung8f1eff72015-05-28 17:33:40 -07002893 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002894 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002896 // Only launch using the new animation if the shortcut has not opted out (this is a
2897 // private contract between launcher and may be ignored in the future).
2898 boolean useLaunchAnimation = (v != null) &&
2899 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002900 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2901 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002902
Kenny Guy1317e2d2014-05-08 18:52:50 +01002903 UserHandleCompat user = null;
2904 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2905 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2906 user = userManager.getUserForSerialNumber(serialNumber);
2907 }
2908
2909 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002910 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002911 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002912 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002913 int left = 0, top = 0;
2914 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2915 if (v instanceof TextView) {
2916 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002917 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2918 if (icon != null) {
2919 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002920 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002921 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002922 width = bounds.width();
2923 height = bounds.height();
2924 }
2925 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002926 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2927 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002928 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002929 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002930 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002931 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002932 // On L devices, we use the device default slide-up transition.
2933 // On L MR1 devices, we a custom version of the slide-up transition which
2934 // doesn't have the delay present in the device default.
2935 opts = ActivityOptions.makeCustomAnimation(this,
2936 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002937 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002938 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002939 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002940
2941 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2942 // Could be launching some bookkeeping activity
2943 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002944 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002945 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002946 launcherApps.startActivityForProfile(intent.getComponent(), user,
2947 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002948 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002949 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002951 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2952 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2953 // corresponding permission. Show the appropriate permission prompt if that
2954 // is the case.
2955 if (intent.getComponent() == null
2956 && Intent.ACTION_CALL.equals(intent.getAction())
2957 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2958 PackageManager.PERMISSION_GRANTED) {
2959 // TODO: Rename sPendingAddItem to a generic name.
2960 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2961 0, (ItemInfo) tag);
2962 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2963 REQUEST_PERMISSION_CALL_PHONE);
2964 return false;
2965 }
2966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002968 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002970 "or use the exported attribute for this activity. "
2971 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002973 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002975
Winson Chungbedf9232015-07-10 12:38:30 -07002976 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002977 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002978 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2979 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2980 return false;
2981 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002982 try {
2983 success = startActivity(v, intent, tag);
2984 } catch (ActivityNotFoundException e) {
2985 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2986 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2987 }
2988 return success;
2989 }
2990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 /**
2992 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2993 * in the DragLayer in the exact absolute location of the original FolderIcon.
2994 */
2995 private void copyFolderIconToImage(FolderIcon fi) {
2996 final int width = fi.getMeasuredWidth();
2997 final int height = fi.getMeasuredHeight();
2998
2999 // Lazy load ImageView, Bitmap and Canvas
3000 if (mFolderIconImageView == null) {
3001 mFolderIconImageView = new ImageView(this);
3002 }
3003 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3004 mFolderIconBitmap.getHeight() != height) {
3005 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3006 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3007 }
3008
3009 DragLayer.LayoutParams lp;
3010 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3011 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3012 } else {
3013 lp = new DragLayer.LayoutParams(width, height);
3014 }
3015
Adam Cohen307fe232012-08-16 17:55:58 -07003016 // The layout from which the folder is being opened may be scaled, adjust the starting
3017 // view size by this scale factor.
3018 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003019 lp.customPosition = true;
3020 lp.x = mRectForFolderAnimation.left;
3021 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003022 lp.width = (int) (scale * width);
3023 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003024
3025 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3026 fi.draw(mFolderIconCanvas);
3027 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003028 if (fi.getFolder() != null) {
3029 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3030 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003031 }
Adam Cohen268c4752012-06-06 17:47:33 -07003032 // Just in case this image view is still in the drag layer from a previous animation,
3033 // we remove it and re-add it.
3034 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3035 mDragLayer.removeView(mFolderIconImageView);
3036 }
3037 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003038 if (fi.getFolder() != null) {
3039 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003040 }
Adam Cohen268c4752012-06-06 17:47:33 -07003041 }
3042
Sunny Goyal08442b82015-10-21 17:15:08 -07003043 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003044 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003045 FolderInfo info = (FolderInfo) fi.getTag();
3046 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3047 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003048 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3049 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003050 }
3051
Adam Cohen268c4752012-06-06 17:47:33 -07003052 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3053 copyFolderIconToImage(fi);
3054 fi.setVisibility(View.INVISIBLE);
3055
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003056 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3057 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003058 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003059 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3060 }
3061 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003062 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003063 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003064 oa.end();
3065 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003066 }
3067
Sunny Goyal935fca12015-10-13 10:19:01 -07003068 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003069 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003070 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003071
Adam Cohen268c4752012-06-06 17:47:33 -07003072 // We remove and re-draw the FolderIcon in-case it has changed
3073 mDragLayer.removeView(mFolderIconImageView);
3074 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003075 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003076 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003077 oa.addListener(new AnimatorListenerAdapter() {
3078 @Override
3079 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003080 if (cl != null) {
3081 cl.clearFolderLeaveBehind();
3082 // Remove the ImageView copy of the FolderIcon and make the original visible.
3083 mDragLayer.removeView(mFolderIconImageView);
3084 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003085 }
3086 }
3087 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003088 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003089 if (!animate) {
3090 oa.end();
3091 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003092 }
3093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003095 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 * is animated relative to the specified View. If the View is null, no animation
3097 * is played.
3098 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003099 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003101 public void openFolder(FolderIcon folderIcon, boolean animate) {
3102 animate &= !Utilities.isPowerSaverOn(this);
3103
Adam Cohenfb91f302012-06-11 15:45:18 -07003104 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003105 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3106 if (openFolder != null && openFolder != folder) {
3107 // Close any open folder before opening a folder.
3108 closeFolder();
3109 }
3110
Adam Cohena9cf38f2011-05-02 15:36:58 -07003111 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114
Sunny Goyalf4066152015-04-15 09:42:19 -07003115 // While the folder is open, the position of the icon cannot change.
3116 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3117
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 // Just verify that the folder hasn't already been added to the DragLayer.
3119 // There was a one-off crash where the folder had a parent already.
3120 if (folder.getParent() == null) {
3121 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003122 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 } else {
3124 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3125 folder.getParent() + ").");
3126 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003127 if (animate) {
3128 folder.animateOpen();
3129 } else {
3130 folder.open();
3131 }
3132 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003133
3134 // Notify the accessibility manager that this folder "window" has appeared and occluded
3135 // the workspace items
3136 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3137 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003138 }
3139
3140 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003141 closeFolder(true);
3142 }
3143
3144 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003145 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003146 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003147 if (folder.isEditingName()) {
3148 folder.dismissEditingName();
3149 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003150 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003151 }
3152 }
3153
Sunny Goyal935fca12015-10-13 10:19:01 -07003154 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003155 animate &= !Utilities.isPowerSaverOn(this);
3156
Adam Cohen4554ee12011-08-03 16:13:21 -07003157 folder.getInfo().opened = false;
3158
3159 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3160 if (parent != null) {
3161 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003162 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003163 if (fi != null) {
3164 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3165 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003167 if (animate) {
3168 folder.animateClosed();
3169 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003170 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003171 }
Winson Chung83ca4802013-04-12 15:10:52 -07003172
Sunny Goyal935fca12015-10-13 10:19:01 -07003173 // Notify the accessibility manager that this folder "window" has disappeared and no
3174 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003175 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 }
3177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003179 if (!isDraggingEnabled()) return false;
3180 if (isWorkspaceLocked()) return false;
3181 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182
Winson Chung76648c52015-07-10 14:33:23 -07003183 if (v == mAllAppsButton) {
3184 onLongClickAllAppsButton(v);
3185 return true;
3186 }
3187
Adam Cohen1697b792013-09-17 19:08:21 -07003188 if (v instanceof Workspace) {
3189 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003190 if (!mWorkspace.isTouchActive()) {
3191 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003192 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3193 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3194 return true;
3195 } else {
3196 return false;
3197 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003198 } else {
3199 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003200 }
Adam Cohen1697b792013-09-17 19:08:21 -07003201 }
3202
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003203 CellLayout.CellInfo longClickCellInfo = null;
3204 View itemUnderLongClick = null;
3205 if (v.getTag() instanceof ItemInfo) {
3206 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003207 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003208 itemUnderLongClick = longClickCellInfo.cell;
3209 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 }
3211
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 // The hotseat touch handling does not go through Workspace, and we always allow long press
3213 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003214 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003215 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003216 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003217 // User long pressed on empty space
3218 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3219 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003220 if (mWorkspace.isInOverviewMode()) {
3221 mWorkspace.startReordering(v);
3222 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003223 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003226 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3227 mHotseat.getOrderInHotseat(
3228 longClickCellInfo.cellX,
3229 longClickCellInfo.cellY));
3230 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003232 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 }
3234 }
3235 }
3236 return true;
3237 }
3238
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 boolean isHotseatLayout(View layout) {
3240 return mHotseat != null && layout != null &&
3241 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3242 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003243
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 /**
3245 * Returns the CellLayout of the specified container at the specified screen.
3246 */
Sunny Goyal92820592015-03-02 11:31:35 -08003247 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003248 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3249 if (mHotseat != null) {
3250 return mHotseat.getLayout();
3251 } else {
3252 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003253 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003254 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003255 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003256 }
3257 }
3258
Winson Chungb745afb2015-03-02 11:51:23 -08003259 /**
3260 * For overridden classes.
3261 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003262 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003263 return isAppsViewVisible();
3264 }
3265
3266 public boolean isAppsViewVisible() {
3267 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3268 }
3269
3270 public boolean isWidgetsViewVisible() {
3271 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003272 }
3273
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003274 private void setWorkspaceBackground(int background) {
3275 switch (background) {
3276 case WORKSPACE_BACKGROUND_TRANSPARENT:
3277 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3278 break;
3279 case WORKSPACE_BACKGROUND_BLACK:
3280 getWindow().setBackgroundDrawable(null);
3281 break;
3282 default:
3283 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3284 }
Craig Mautner360310b2012-10-26 15:13:08 -07003285 }
3286
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003287 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003288 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3289 int curflags = getWindow().getAttributes().flags
3290 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3291 if (wpflags != curflags) {
3292 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3293 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003294 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003295 }
3296
Michael Jurkae326f182011-11-21 14:05:46 -08003297 @Override
3298 public void onTrimMemory(int level) {
3299 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003300 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3301 // The widget preview db can result in holding onto over
3302 // 3MB of memory for caching which isn't necessary.
3303 SQLiteDatabase.releaseMemory();
3304
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003305 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003306 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003307 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003308 if (mLauncherCallbacks != null) {
3309 mLauncherCallbacks.onTrimMemory(level);
3310 }
Michael Jurkae326f182011-11-21 14:05:46 -08003311 }
3312
Winson5c6bdbb2015-09-03 11:36:19 -07003313 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003314 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003315 }
3316
Winson5c6bdbb2015-09-03 11:36:19 -07003317 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003318 boolean changed = mState != State.WORKSPACE ||
3319 mWorkspace.getState() != Workspace.State.NORMAL;
3320 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003322 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003323 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003324
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325 // Set focus to the AppsCustomize button
3326 if (mAllAppsButton != null) {
3327 mAllAppsButton.requestFocus();
3328 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003329 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003330
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003331 // Change the state *after* we've called all the transition code
3332 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003333
Winson Chung5fb63472011-02-02 17:03:37 -08003334 // Resume the auto-advance of widgets
3335 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003336 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003337
Winson Chung0f785722015-04-08 10:27:49 -07003338 if (changed) {
3339 // Send an accessibility event to announce the context change
3340 getWindow().getDecorView()
3341 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003342 }
Winson5c6bdbb2015-09-03 11:36:19 -07003343 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003344 }
3345
Winsone9f27272015-10-13 10:47:51 -07003346 /**
3347 * Shows the overview button.
3348 */
Adam Cohened307df2013-10-02 09:37:31 -07003349 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003350 showOverviewMode(animated, false);
3351 }
3352
3353 /**
3354 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3355 * onto one of the overview panel buttons.
3356 */
3357 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3358 Runnable postAnimRunnable = null;
3359 if (requestButtonFocus) {
3360 postAnimRunnable = new Runnable() {
3361 @Override
3362 public void run() {
3363 // Hitting the menu button when in touch mode does not trigger touch mode to
3364 // be disabled, so if requested, force focus on one of the overview panel
3365 // buttons.
3366 mOverviewPanel.requestFocusFromTouch();
3367 }
3368 };
3369 }
Adam Cohened307df2013-10-02 09:37:31 -07003370 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003371 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003372 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003373 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003374 }
3375
Winson Chungb745afb2015-03-02 11:51:23 -08003376 /**
3377 * Shows the apps view.
3378 */
Winson Chung76648c52015-07-10 14:33:23 -07003379 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3380 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003381 if (resetListToTop) {
3382 mAppsView.scrollToTop();
3383 }
Winson Chung4ac30062015-05-08 17:34:17 -07003384 if (updatePredictedApps) {
3385 tryAndUpdatePredictedApps();
3386 }
Winson Chung76648c52015-07-10 14:33:23 -07003387 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003388 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389
Winson Chungb745afb2015-03-02 11:51:23 -08003390 /**
3391 * Shows the widgets view.
3392 */
3393 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003394 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003395 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003397 }
Winson Chung76648c52015-07-10 14:33:23 -07003398 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003399
3400 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003401 @Override
3402 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003403 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003404 }
3405 });
Winson Chungb745afb2015-03-02 11:51:23 -08003406 }
3407
3408 /**
3409 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003410 *
3411 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003412 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003413 // TODO: calling method should use the return value so that when {@code false} is returned
3414 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003415 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003416 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3417 mState != State.WIDGETS_SPRING_LOADED) {
3418 return false;
3419 }
3420 if (toState != State.APPS && toState != State.WIDGETS) {
3421 return false;
3422 }
Winson Chungb745afb2015-03-02 11:51:23 -08003423
3424 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003425 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3426 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003427 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003428 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003429 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003430
Michael Jurkab3e22d92011-10-31 15:58:33 -07003431 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003432 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003433
3434 // Pause the auto-advance of widgets until we are out of AllApps
3435 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003436 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003437 closeFolder();
3438
3439 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003440 getWindow().getDecorView()
3441 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003442 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003443 }
3444
Winson Chungcd99cd32015-04-29 11:03:24 -07003445 /**
3446 * Updates the workspace and interaction state on state change, and return the animation to this
3447 * new state.
3448 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003449 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003450 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003451 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003452 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003453 updateInteraction(fromState, toState);
3454 return anim;
3455 }
3456
Jun Mukaif0033da2015-08-04 18:34:30 -07003457 public void onLauncherClingShown() {
3458 // When a launcher cling appears, it should cover the underlying layers, so their focus
3459 // should be blocked.
3460 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3461 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3462 }
3463 }
3464
3465 public void onLauncherClingDismissed() {
3466 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3467 }
3468
Hyunyoung Song3f471442015-04-08 19:01:34 -07003469 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003470 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003471 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003472 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003473 }
Winson Chungb745afb2015-03-02 11:51:23 -08003474
Winson Chung006ee262015-08-03 14:40:11 -07003475 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003476 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003477 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003478
3479 if (isAppsViewVisible()) {
3480 mState = State.APPS_SPRING_LOADED;
3481 } else if (isWidgetsViewVisible()) {
3482 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003483 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003485 } else {
3486 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003487 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003488 }
Adam Cohen7777d962011-08-18 18:58:38 -07003489
Hyunyoung Song3f471442015-04-08 19:01:34 -07003490 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003491 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003492 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003493
Winson Chunge7a03942011-08-05 15:05:12 -07003494 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 @Override
3496 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003497 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003498 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3499 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003500 // Before we show workspace, hide all apps again because
3501 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3502 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003503 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003504 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003505 } else {
3506 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003507 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003508 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003509 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003510 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003511
Tony Wickhame0c33232016-02-08 11:37:04 -08003512 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003513 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3514 || mState == State.WIDGETS_SPRING_LOADED;
3515 }
3516
Michael Jurkad3ef3062010-11-23 16:23:58 -08003517 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003518 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003519 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003520 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003521 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003522 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003523 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3524 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003525 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003526 }
3527
Winson Chung4ac30062015-05-08 17:34:17 -07003528 /**
3529 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3530 * resumed.
3531 */
3532 private void tryAndUpdatePredictedApps() {
3533 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003534 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003535 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003536 mAppsView.setPredictedApps(apps);
3537 }
3538 }
3539 }
3540
Michael Jurkab3e22d92011-10-31 15:58:33 -07003541 void lockAllApps() {
3542 // TODO
3543 }
3544
3545 void unlockAllApps() {
3546 // TODO
3547 }
3548
Sunny Goyal594d76d2014-11-06 10:12:54 -08003549 protected void disableVoiceButtonProxy(boolean disable) {
3550 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003551 }
3552
Winsonf768d932015-09-25 16:12:35 -07003553 public boolean launcherCallbacksProvidesSearch() {
3554 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3555 }
3556
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003557 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003558 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003559 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003560 }
3561
Adam Cohen24ce0b32014-01-14 16:18:14 -08003562 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003563 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3564 if (searchProvider == null) {
3565 return null;
3566 }
3567
3568 Bundle opts = new Bundle();
3569 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003570 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003571
Tony Wickham3a3517f2015-10-06 11:27:04 -07003572 // Determine the min and max dimensions of the widget.
3573 LauncherAppState app = LauncherAppState.getInstance();
3574 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3575 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3576 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003577 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3578 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003579 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003580 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003581 int minWidth = maxWidth;
3582 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003583 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3584 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3585 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3586 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3587 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003588 }
3589 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3590 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3591 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3592 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003593 if (LOGD) {
3594 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3595 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3596 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003597
Tony Wickham775455c2015-10-16 09:49:32 -07003598 if (mLauncherCallbacks != null) {
3599 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3600 }
3601
Sunny Goyalf7258242015-10-19 16:59:07 -07003602 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003603 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003604 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003605 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003606 || (widgetInfo == null)
3607 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003608 // A valid widget is not already bound.
3609 if (widgetId > -1) {
3610 mAppWidgetHost.deleteAppWidgetId(widgetId);
3611 widgetId = -1;
3612 }
3613
3614 // Try to bind a new widget
3615 widgetId = mAppWidgetHost.allocateAppWidgetId();
3616
3617 if (!AppWidgetManagerCompat.getInstance(this)
3618 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3619 mAppWidgetHost.deleteAppWidgetId(widgetId);
3620 widgetId = -1;
3621 }
3622
Sunny Goyalf7258242015-10-19 16:59:07 -07003623 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003624 .putInt(QSB_WIDGET_ID, widgetId)
3625 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003626 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003627 }
3628
Sunny Goyal8d595092015-07-06 12:22:14 -07003629 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003630 if (widgetId != -1) {
3631 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003632 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003633 mQsb.updateAppWidgetOptions(opts);
3634 mQsb.setPadding(0, 0, 0, 0);
3635 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003636 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003637 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003638 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003639 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003640 }
3641
Sunny Goyal22235bc2015-04-03 09:23:43 -07003642 private void reinflateQSBIfNecessary() {
3643 if (mQsb instanceof LauncherAppWidgetHostView &&
3644 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3645 mSearchDropTargetBar.removeView(mQsb);
3646 mQsb = null;
3647 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3648 }
3649 }
3650
Michael Jurkad7c28052012-04-27 15:43:36 -07003651 @Override
3652 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003653 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003654 final List<CharSequence> text = event.getText();
3655 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003656 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003657 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003658 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003659 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003660 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003661 } else if (mWorkspace != null) {
3662 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003663 } else {
3664 text.add(getString(R.string.all_apps_home_button_label));
3665 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003666 return result;
3667 }
3668
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003669 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003670 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003671 */
Adam Cohen091440a2015-03-18 14:16:05 -07003672 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003673 @Override
3674 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003675 closeSystemDialogs();
3676 }
3677 }
3678
3679 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003680 * If the activity is currently paused, signal that we need to run the passed Runnable
3681 * in onResume.
3682 *
3683 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003684 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3685 * wrong when we're not running, and if the activity comes back to what the configuration was
3686 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003687 *
3688 * Implementation of the method from LauncherModel.Callbacks.
3689 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003690 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003691 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003692 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003693 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003694 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003695 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003696 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003697 }
3698 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003699 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003700 return true;
3701 } else {
3702 return false;
3703 }
3704 }
3705
Michael Jurkac402cd92013-05-20 15:49:32 +02003706 private boolean waitUntilResume(Runnable run) {
3707 return waitUntilResume(run, false);
3708 }
3709
Michael Jurka1e2f4652013-07-08 18:03:46 -07003710 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003711 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003712 }
3713
Michael Jurka7607c2f2013-04-03 14:33:19 -07003714 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003715 * If the activity is currently paused, signal that we need to re-run the loader
3716 * in onResume.
3717 *
3718 * This needs to be called from incoming places where resources might have been loaded
3719 * while we are paused. That is becaues the Configuration might be wrong
3720 * when we're not running, and if it comes back to what it was when we
3721 * were paused, we are not restarted.
3722 *
3723 * Implementation of the method from LauncherModel.Callbacks.
3724 *
3725 * @return true if we are currently paused. The caller might be able to
3726 * skip some work in that case since we will come back again.
3727 */
3728 public boolean setLoadOnResume() {
3729 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003730 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003731 mOnResumeNeedsLoad = true;
3732 return true;
3733 } else {
3734 return false;
3735 }
3736 }
3737
3738 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003740 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003742 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003743 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003744 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003745 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003748
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003750 * Clear any pending bind callbacks. This is called when is loader is planning to
3751 * perform a full rebind from scratch.
3752 */
3753 @Override
3754 public void clearPendingBinds() {
3755 mBindOnResumeCallbacks.clear();
3756 if (mPendingExecutor != null) {
3757 mPendingExecutor.markCompleted();
3758 mPendingExecutor = null;
3759 }
3760 }
3761
3762 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 * Refreshes the shortcuts shown on the workspace.
3764 *
3765 * Implementation of the method from LauncherModel.Callbacks.
3766 */
3767 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003768 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003769
Winson Chungd64d1762013-08-20 14:37:16 -07003770 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003771 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003772 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003773
Michael Jurka05bf644e2011-11-30 20:28:53 -08003774 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003775 if (mHotseat != null) {
3776 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003777 }
3778 }
3779
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 @Override
3781 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003782 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003783
Adam Cohen5084cba2013-09-03 12:01:16 -07003784 // If there are no screens, we need to have an empty screen
3785 if (orderedScreenIds.size() == 0) {
3786 mWorkspace.addExtraEmptyScreen();
3787 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003788
3789 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003790 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003791 if (hasCustomContentToLeft()) {
3792 mWorkspace.createCustomContentContainer();
3793 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003794 }
Winson Chung64359a52013-07-08 17:17:08 -07003795 }
3796
3797 @Override
3798 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003799 int count = orderedScreenIds.size();
3800 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003801 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003802 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003803 }
3804
Winson Chungd64d1762013-08-20 14:37:16 -07003805 public void bindAppsAdded(final ArrayList<Long> newScreens,
3806 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003807 final ArrayList<ItemInfo> addAnimated,
3808 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003809 Runnable r = new Runnable() {
3810 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003811 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003812 }
3813 };
3814 if (waitUntilResume(r)) {
3815 return;
3816 }
3817
Winson Chungd64d1762013-08-20 14:37:16 -07003818 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003819 if (newScreens != null) {
3820 bindAddScreens(newScreens);
3821 }
Winson Chungd64d1762013-08-20 14:37:16 -07003822
3823 // We add the items without animation on non-visible pages, and with
3824 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003825 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 bindItems(addNotAnimated, 0,
3827 addNotAnimated.size(), false);
3828 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003829 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003830 bindItems(addAnimated, 0,
3831 addAnimated.size(), true);
3832 }
Winson Chungc58497e2013-09-03 17:48:37 -07003833
Winson Chung87412982013-10-03 18:34:14 -07003834 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003835 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003836
Winson Chungb745afb2015-03-02 11:51:23 -08003837 if (addedApps != null && mAppsView != null) {
3838 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003839 }
Winson Chungd64d1762013-08-20 14:37:16 -07003840 }
3841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003842 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 * Bind the items start-end from the list.
3844 *
3845 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003846 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003847 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003848 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3849 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003850 Runnable r = new Runnable() {
3851 public void run() {
3852 bindItems(shortcuts, start, end, forceAnimateIcons);
3853 }
3854 };
3855 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003856 return;
3857 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003858
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003860 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3861 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003862 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003864 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003865 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003867
3868 // Short circuit if we are loading dock items for a configuration which has no dock
3869 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3870 mHotseat == null) {
3871 continue;
3872 }
3873
Sunny Goyal639e9062015-08-19 19:17:06 -07003874 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 switch (item.itemType) {
3876 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3877 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003878 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003879 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003880
Winson Chung64359a52013-07-08 17:17:08 -07003881 /*
3882 * TODO: FIX collision case
3883 */
Winson Chungce376632013-07-11 16:12:41 -07003884 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3885 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3886 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003887 View v = cl.getChildAt(item.cellX, item.cellY);
3888 Object tag = v.getTag();
3889 String desc = "Collision while binding workspace item: " + item
3890 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003891 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003892 throw (new RuntimeException(desc));
3893 } else {
3894 Log.d(TAG, desc);
3895 }
Winson Chungce376632013-07-11 16:12:41 -07003896 }
Winson Chung64359a52013-07-08 17:17:08 -07003897 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003899 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003900 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003901 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003902 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003904 default:
3905 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003907
3908 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3909 item.cellY, 1, 1);
3910 if (animateIcons) {
3911 // Animate all the applications up now
3912 view.setAlpha(0f);
3913 view.setScaleX(0f);
3914 view.setScaleY(0f);
3915 bounceAnims.add(createNewAppBounceAnimation(view, i));
3916 newShortcutsScreenId = item.screenId;
3917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003918 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003919
Winson Chung997a9232013-07-24 15:33:46 -07003920 if (animateIcons) {
3921 // Animate to the correct page
3922 if (newShortcutsScreenId > -1) {
3923 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003924 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003925 final Runnable startBounceAnimRunnable = new Runnable() {
3926 public void run() {
3927 anim.playTogether(bounceAnims);
3928 anim.start();
3929 }
3930 };
Winson Chung997a9232013-07-24 15:33:46 -07003931 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003932 // We post the animation slightly delayed to prevent slowdowns
3933 // when we are loading right after we return to launcher.
3934 mWorkspace.postDelayed(new Runnable() {
3935 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003936 if (mWorkspace != null) {
3937 mWorkspace.snapToPage(newScreenIndex);
3938 mWorkspace.postDelayed(startBounceAnimRunnable,
3939 NEW_APPS_ANIMATION_DELAY);
3940 }
Winson Chung94d67682013-09-25 16:29:40 -07003941 }
3942 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003943 } else {
3944 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003945 }
3946 }
Winson Chung64359a52013-07-08 17:17:08 -07003947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003949 }
3950
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 /**
3952 * Implementation of the method from LauncherModel.Callbacks.
3953 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003954 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003955 Runnable r = new Runnable() {
3956 public void run() {
3957 bindFolders(folders);
3958 }
3959 };
3960 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003961 return;
3962 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003963 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965
3966 /**
3967 * Add the views for a widget to the workspace.
3968 *
3969 * Implementation of the method from LauncherModel.Callbacks.
3970 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003971 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003972 Runnable r = new Runnable() {
3973 public void run() {
3974 bindAppWidget(item);
3975 }
3976 };
3977 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003978 return;
3979 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003980
Daniel Sandler843e8602010-06-07 14:59:01 -04003981 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3982 if (DEBUG_WIDGETS) {
3983 Log.d(TAG, "bindAppWidget: " + item);
3984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003985 final Workspace workspace = mWorkspace;
3986
Adam Cohen59400422014-03-05 18:07:04 -08003987 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003988 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003989
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003990 if (!mIsSafeModeEnabled
3991 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003992 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3993
Sunny Goyalff572272014-07-23 13:58:07 -07003994 if (appWidgetInfo == null) {
3995 if (DEBUG_WIDGETS) {
3996 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3997 + " belongs to component " + item.providerName
3998 + ", as the povider is null");
3999 }
4000 LauncherModel.deleteItemFromDatabase(this, item);
4001 return;
4002 }
Sunny Goyalff572272014-07-23 13:58:07 -07004003
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004004 // If we do not have a valid id, try to bind an id.
4005 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
4006 // Note: This assumes that the id remap broadcast is received before this step.
4007 // If that is not the case, the id remap will be ignored and user may see the
4008 // click to setup view.
4009 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
4010 pendingInfo.spanX = item.spanX;
4011 pendingInfo.spanY = item.spanY;
4012 pendingInfo.minSpanX = item.minSpanX;
4013 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004014 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004015
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004016 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4017 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4018 newWidgetId, appWidgetInfo, options);
4019
4020 // TODO consider showing a permission dialog when the widget is clicked.
4021 if (!success) {
4022 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4023 if (DEBUG_WIDGETS) {
4024 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4025 + " belongs to component " + item.providerName
4026 + ", as the launcher is unable to bing a new widget id");
4027 }
4028 LauncherModel.deleteItemFromDatabase(this, item);
4029 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004030 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004031
4032 item.appWidgetId = newWidgetId;
4033
4034 // If the widget has a configure activity, it is still needs to set it up, otherwise
4035 // the widget is ready to go.
4036 item.restoreStatus = (appWidgetInfo.configure == null)
4037 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4038 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4039
4040 LauncherModel.updateItemInDatabase(this, item);
4041 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4042 && (appWidgetInfo.configure == null)) {
4043 // If the ID is already valid, verify if we need to configure or not.
4044 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4045 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004046 }
Sunny Goyalff572272014-07-23 13:58:07 -07004047 }
4048
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004049 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004051 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004052 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4053 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004054 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004055
Sunny Goyal56c73602015-09-25 12:55:01 -07004056 // Verify that we own the widget
4057 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4058 if (info == null || appWidgetInfo == null ||
4059 !info.provider.equals(appWidgetInfo.provider)) {
Hyunyoung Song70264a92016-02-03 15:28:47 -08004060 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
4061 + " appWidgetInfo=" + appWidgetInfo);
Sunny Goyal56c73602015-09-25 12:55:01 -07004062 deleteWidgetInfo(item);
4063 return;
4064 }
4065
Sunny Goyal651077b2014-06-30 14:15:31 -07004066 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004067 item.minSpanX = appWidgetInfo.minSpanX;
4068 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004069 } else {
4070 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004071 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4072 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004073 view.updateIcon(mIconCache);
4074 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004075 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004076 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004077 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004079 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 workspace.requestLayout();
4081
Daniel Sandler843e8602010-06-07 14:59:01 -04004082 if (DEBUG_WIDGETS) {
4083 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4084 + (SystemClock.uptimeMillis()-start) + "ms");
4085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 }
4087
Sunny Goyalff572272014-07-23 13:58:07 -07004088 /**
4089 * Restores a pending widget.
4090 *
4091 * @param appWidgetId The app widget id
4092 * @param cellInfo The position on screen where to create the widget.
4093 */
4094 private void completeRestoreAppWidget(final int appWidgetId) {
4095 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4096 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4097 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4098 return;
4099 }
4100
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004101 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004102 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4103
4104 mWorkspace.reinflateWidgetsIfNecessary();
4105 LauncherModel.updateItemInDatabase(this, info);
4106 }
4107
Adam Cohen1462de32012-07-24 22:34:36 -07004108 public void onPageBoundSynchronously(int page) {
4109 mSynchronouslyBoundPages.add(page);
4110 }
4111
Sunny Goyal527c7d32015-08-28 15:19:36 -07004112 @Override
4113 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4114 if (mPendingExecutor != null) {
4115 mPendingExecutor.markCompleted();
4116 }
4117 mPendingExecutor = executor;
4118 executor.attachTo(this);
4119 }
4120
4121 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4122 if (mPendingExecutor == executor) {
4123 mPendingExecutor = null;
4124 }
4125 }
4126
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127 /**
4128 * Callback saying that there aren't any more items to bind.
4129 *
4130 * Implementation of the method from LauncherModel.Callbacks.
4131 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004132 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004133 Runnable r = new Runnable() {
4134 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004135 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004136 }
4137 };
4138 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004139 return;
4140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 if (mSavedState != null) {
4142 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004143 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004145
4146 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4147 if (folderId != 0) {
4148 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4149 if (view instanceof FolderIcon) {
4150 FolderIcon icon = (FolderIcon) view;
4151 FolderInfo info = icon.getFolderInfo();
4152 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4153 mWorkspace.getNextPage());
4154 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4155 || info.screenId == currentScreenId) {
4156 // We can show the folder
4157 openFolder(icon, false);
4158 } else {
4159 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4160 " but current screen is " + currentScreenId);
4161 }
4162 }
4163 }
4164
Joe Onorato9c1289c2009-08-17 11:03:03 -04004165 mSavedState = null;
4166 }
4167
Adam Cohen1462de32012-07-24 22:34:36 -07004168 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004169
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004170 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004171 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004172
4173 // If we received the result of any pending adds while the loader was running (e.g. the
4174 // widget configuration forced an orientation change), process them now.
4175 if (sPendingAddItem != null) {
4176 final long screenId = completeAdd(sPendingAddItem);
4177
4178 // TODO: this moves the user to the page where the pending item was added. Ideally,
4179 // the screen would be guaranteed to exist after bind, and the page would be set through
4180 // the workspace restore process.
4181 mWorkspace.post(new Runnable() {
4182 @Override
4183 public void run() {
4184 mWorkspace.snapToScreenId(screenId);
4185 }
4186 });
4187 sPendingAddItem = null;
4188 }
4189
Sunny Goyal756adbc2015-04-16 15:20:51 -07004190 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004191
4192 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004193 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004194 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004195 }
4196
Adam Cohen3ed316a2014-07-23 18:21:20 -07004197 private void sendLoadingCompleteBroadcastIfNecessary() {
4198 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4199 String permission =
4200 getResources().getString(R.string.receive_first_load_broadcast_permission);
4201 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4202 sendBroadcast(intent, permission);
4203 SharedPreferences.Editor editor = mSharedPrefs.edit();
4204 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4205 editor.apply();
4206 }
4207 }
4208
Winson Chunga0b7e862013-09-05 16:03:15 -07004209 public boolean isAllAppsButtonRank(int rank) {
4210 if (mHotseat != null) {
4211 return mHotseat.isAllAppsButtonRank(rank);
4212 }
4213 return false;
4214 }
4215
Winson Chunga2413752012-04-03 14:22:34 -07004216 private boolean canRunNewAppsAnimation() {
4217 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004218 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4219 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004220 }
4221
Winson Chungc9168342013-06-26 14:54:55 -07004222 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004223 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004224 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4225 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004226 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004227 return bounceAnim;
4228 }
4229
Adam Cohen27772732013-11-11 14:00:56 +00004230 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004231 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004232 }
4233
Tony Wickham3a3517f2015-10-06 11:27:04 -07004234 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004235 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004236 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004237 }
4238
Tony Wickham55616cd2015-09-23 14:55:17 -07004239 public int getSearchBarHeight() {
4240 if (mLauncherCallbacks != null) {
4241 return mLauncherCallbacks.getSearchBarHeight();
4242 }
4243 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4244 }
4245
Winson Chung88fa7412015-08-03 14:25:28 -07004246 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004247 if (mSearchDropTargetBar == null) {
4248 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004249 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004250 if (mQsb != null) {
4251 mSearchDropTargetBar.removeView(mQsb);
4252 mQsb = null;
4253 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004254 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004255 }
4256
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004257 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004258 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4259 * multiple calls to bind the same list.)
4260 */
4261 @Thunk ArrayList<AppInfo> mTmpAppsList;
4262 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4263 public void run() {
4264 bindAllApplications(mTmpAppsList);
4265 mTmpAppsList = null;
4266 }
4267 };
4268
4269 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004270 * Add the icons for all apps.
4271 *
4272 * Implementation of the method from LauncherModel.Callbacks.
4273 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004274 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004275 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4276 mTmpAppsList = apps;
4277 return;
4278 }
4279
Winson Chungb745afb2015-03-02 11:51:23 -08004280 if (mAppsView != null) {
4281 mAppsView.setApps(apps);
4282 }
Adam Cohen9211d422014-10-07 18:14:53 -07004283 if (mLauncherCallbacks != null) {
4284 mLauncherCallbacks.bindAllApplications(apps);
4285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004286 }
4287
4288 /**
4289 * A package was updated.
4290 *
4291 * Implementation of the method from LauncherModel.Callbacks.
4292 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004293 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004294 Runnable r = new Runnable() {
4295 public void run() {
4296 bindAppsUpdated(apps);
4297 }
4298 };
4299 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004300 return;
4301 }
4302
Winson Chungb745afb2015-03-02 11:51:23 -08004303 if (mAppsView != null) {
4304 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004306 }
4307
Sunny Goyal4390ace2014-10-13 11:33:11 -07004308 @Override
4309 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4310 Runnable r = new Runnable() {
4311 public void run() {
4312 bindWidgetsRestored(widgets);
4313 }
4314 };
4315 if (waitUntilResume(r)) {
4316 return;
4317 }
4318 mWorkspace.widgetsRestored(widgets);
4319 }
4320
Joe Onorato9c1289c2009-08-17 11:03:03 -04004321 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004322 * Some shortcuts were updated in the background.
4323 *
4324 * Implementation of the method from LauncherModel.Callbacks.
4325 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004326 @Override
4327 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4328 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004329 Runnable r = new Runnable() {
4330 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004331 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004332 }
4333 };
4334 if (waitUntilResume(r)) {
4335 return;
4336 }
4337
Sunny Goyal4390ace2014-10-13 11:33:11 -07004338 if (!updated.isEmpty()) {
4339 mWorkspace.updateShortcuts(updated);
4340 }
4341
4342 if (!removed.isEmpty()) {
4343 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4344 for (ShortcutInfo si : removed) {
4345 removedComponents.add(si.getTargetComponent());
4346 }
4347 mWorkspace.removeItemsByComponentName(removedComponents, user);
4348 // Notify the drag controller
4349 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004350 }
4351 }
4352
4353 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004354 * Update the state of a package, typically related to install state.
4355 *
4356 * Implementation of the method from LauncherModel.Callbacks.
4357 */
Sunny Goyale755d462014-07-22 13:48:29 -07004358 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004359 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4360 Runnable r = new Runnable() {
4361 public void run() {
4362 bindRestoreItemsChange(updates);
4363 }
4364 };
4365 if (waitUntilResume(r)) {
4366 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004367 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004368
Sunny Goyal756adbc2015-04-16 15:20:51 -07004369 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004370 }
4371
4372 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004373 * A package was uninstalled. We take both the super set of packageNames
4374 * in addition to specific applications to remove, the reason being that
4375 * this can be called when a package is updated as well. In that scenario,
4376 * we only remove specific components from the workspace, where as
4377 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004378 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004379 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004380 * Implementation of the method from LauncherModel.Callbacks.
4381 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004382 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004383 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004384 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004385 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004386 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004387 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004388 }
Winson Chungd64d1762013-08-20 14:37:16 -07004389 };
4390 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004391 return;
4392 }
4393
Sunny Goyal1a745e82014-10-02 15:58:31 -07004394 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004395 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4396 for (AppInfo info : appInfos) {
4397 removedComponents.add(info.componentName);
4398 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004399 if (!packageNames.isEmpty()) {
4400 mWorkspace.removeItemsByPackageName(packageNames, user);
4401 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004402 if (!removedComponents.isEmpty()) {
4403 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004404 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004405 // Notify the drag controller
4406 mDragController.onAppsRemoved(packageNames, removedComponents);
4407
Sunny Goyal1a745e82014-10-02 15:58:31 -07004408 } else {
4409 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004410 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004411
Winson Chungdf95eb12013-10-16 14:57:07 -07004412 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004413 if (mAppsView != null) {
4414 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004416 }
Joe Onoratobe386092009-11-17 17:32:16 -08004417
Michael Jurkac402cd92013-05-20 15:49:32 +02004418 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004419 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004420 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004421 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004422 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004423
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004424 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004425 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004426 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004427 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004428 return;
4429 }
4430
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004431 if (mWidgetsView != null && model != null) {
4432 mWidgetsView.addWidgets(model);
4433 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004434 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004435 }
4436
Winson Chung400438b2011-01-16 17:53:48 -08004437 private int mapConfigurationOriActivityInfoOri(int configOri) {
4438 final Display d = getWindowManager().getDefaultDisplay();
4439 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4440 switch (d.getRotation()) {
4441 case Surface.ROTATION_0:
4442 case Surface.ROTATION_180:
4443 // We are currently in the same basic orientation as the natural orientation
4444 naturalOri = configOri;
4445 break;
4446 case Surface.ROTATION_90:
4447 case Surface.ROTATION_270:
4448 // We are currently in the other basic orientation to the natural orientation
4449 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4450 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4451 break;
4452 }
4453
4454 int[] oriMap = {
4455 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4456 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4457 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4458 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4459 };
4460 // Since the map starts at portrait, we need to offset if this device's natural orientation
4461 // is landscape.
4462 int indexOffset = 0;
4463 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4464 indexOffset = 1;
4465 }
4466 return oriMap[(d.getRotation() + indexOffset) % 4];
4467 }
Adam Cohen446e9402011-09-15 18:21:21 -07004468
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004469 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004470 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004471 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004472 if (Utilities.ATLEAST_JB_MR2) {
4473 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4474 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004475 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4476 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004477 }
Winson Chung4b919f82012-05-01 10:44:08 -07004478 }
4479 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004480
Winson Chung4b919f82012-05-01 10:44:08 -07004481 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004482 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004483 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004484 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004485 } else {
4486 mHandler.postDelayed(new Runnable() {
4487 public void run() {
4488 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4489 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004490 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004491 }
Winson Chung4b919f82012-05-01 10:44:08 -07004492 }
Winson Chung400438b2011-01-16 17:53:48 -08004493 }
4494
Winson Chunge43a1e72014-01-15 10:33:02 -08004495 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004496 if (mLauncherCallbacks != null) {
4497 return mLauncherCallbacks.isLauncherPreinstalled();
4498 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004499 PackageManager pm = getPackageManager();
4500 try {
4501 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4502 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4503 return true;
4504 } else {
4505 return false;
4506 }
4507 } catch (NameNotFoundException e) {
4508 e.printStackTrace();
4509 return false;
4510 }
4511 }
4512
Adam Cohenea90f832014-05-21 15:03:34 -07004513 /**
4514 * This method indicates whether or not we should suggest default wallpaper dimensions
4515 * when our wallpaper cropper was not yet used to set a wallpaper.
4516 */
4517 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004518 if (mLauncherCallbacks != null) {
4519 return mLauncherCallbacks.overrideWallpaperDimensions();
4520 }
Adam Cohenea90f832014-05-21 15:03:34 -07004521 return true;
4522 }
4523
Adam Cohen432609a2014-03-13 17:03:22 -07004524 /**
4525 * To be overridden by subclasses to indicate that there is an activity to launch
4526 * before showing the standard launcher experience.
4527 */
4528 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004529 if (mLauncherCallbacks != null) {
4530 return mLauncherCallbacks.hasFirstRunActivity();
4531 }
Adam Cohen432609a2014-03-13 17:03:22 -07004532 return false;
4533 }
4534
4535 /**
4536 * To be overridden by subclasses to launch any first run activity
4537 */
4538 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004539 if (mLauncherCallbacks != null) {
4540 return mLauncherCallbacks.getFirstRunActivity();
4541 }
Adam Cohen432609a2014-03-13 17:03:22 -07004542 return null;
4543 }
4544
Winson Chunga6945242014-01-08 14:04:34 -08004545 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004546 return !ActivityManager.isRunningInTestHarness() &&
4547 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004548 }
4549
Adam Cohen4a9423d2014-03-28 14:22:31 -07004550 protected boolean hasRunFirstRunActivity() {
4551 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4552 }
4553
Adam Cohen432609a2014-03-13 17:03:22 -07004554 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004555 if (shouldRunFirstRunActivity() &&
4556 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004557 Intent firstRunIntent = getFirstRunActivity();
4558 if (firstRunIntent != null) {
4559 startActivity(firstRunIntent);
4560 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004561 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004562 }
4563 }
Adam Cohen432609a2014-03-13 17:03:22 -07004564 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004565 }
4566
4567 private void markFirstRunActivityShown() {
4568 SharedPreferences.Editor editor = mSharedPrefs.edit();
4569 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4570 editor.apply();
4571 }
4572
Adam Cohen432609a2014-03-13 17:03:22 -07004573 /**
4574 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4575 * screen that must be displayed and dismissed.
4576 */
4577 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004578 if (mLauncherCallbacks != null) {
4579 return mLauncherCallbacks.hasDismissableIntroScreen();
4580 }
Adam Cohen432609a2014-03-13 17:03:22 -07004581 return false;
4582 }
4583
4584 /**
4585 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4586 */
4587 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004588 if (mLauncherCallbacks != null) {
4589 return mLauncherCallbacks.getIntroScreen();
4590 }
Adam Cohen432609a2014-03-13 17:03:22 -07004591 return null;
4592 }
4593
4594 /**
4595 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4596 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4597 */
4598 private boolean shouldShowIntroScreen() {
4599 return hasDismissableIntroScreen() &&
4600 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4601 }
4602
4603 protected void showIntroScreen() {
4604 View introScreen = getIntroScreen();
4605 changeWallpaperVisiblity(false);
4606 if (introScreen != null) {
4607 mDragLayer.showOverlayView(introScreen);
4608 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004609 if (mLauncherOverlayContainer != null) {
4610 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4611 }
Adam Cohen432609a2014-03-13 17:03:22 -07004612 }
4613
4614 public void dismissIntroScreen() {
4615 markIntroScreenDismissed();
4616 if (showFirstRunActivity()) {
4617 // We delay hiding the intro view until the first run activity is showing. This
4618 // avoids a blip.
4619 mWorkspace.postDelayed(new Runnable() {
4620 @Override
4621 public void run() {
4622 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004623 if (mLauncherOverlayContainer != null) {
4624 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4625 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004626 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004627 }
4628 }, ACTIVITY_START_DELAY);
4629 } else {
4630 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004631 if (mLauncherOverlayContainer != null) {
4632 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4633 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004634 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004635 }
4636 changeWallpaperVisiblity(true);
4637 }
4638
4639 private void markIntroScreenDismissed() {
4640 SharedPreferences.Editor editor = mSharedPrefs.edit();
4641 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4642 editor.apply();
4643 }
4644
Adam Cohen091440a2015-03-18 14:16:05 -07004645 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004646 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4647 // on the device, then we always show the first run cling experience (or if there is no
4648 // launcher2). Otherwise, we prompt the user upon started for migration
4649 LauncherClings launcherClings = new LauncherClings(this);
4650 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004651 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004652 if (mModel.canMigrateFromOldLauncherDb(this)) {
4653 launcherClings.showMigrationCling();
4654 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004655 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004656 }
4657 }
4658 }
4659
Winson Chunga6945242014-01-08 14:04:34 -08004660 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004661 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4662 if (mHotseat != null) mHotseat.setAlpha(1f);
4663 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004664 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4665 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004666 }
4667
4668 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004669 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4670 if (mHotseat != null) mHotseat.setAlpha(0f);
4671 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004672 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4673 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004674 }
4675
Winson Chung5ffd51d2015-06-25 11:36:50 -07004676 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004677 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004679 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004680 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004681 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004682 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4683 if (userHandle != null) {
4684 user = UserHandleCompat.fromUser(userHandle);
4685 }
4686 }
4687 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004688 }
4689
4690 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004691 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004692 if (user == null) {
4693 user = UserHandleCompat.myUserHandle();
4694 }
4695
4696 // Called from search suggestion, add the profile extra to the intent to ensure that we
4697 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004698 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004699 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004700 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004701 return null;
4702 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004703 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004704 }
4705
Winson Chung5ffd51d2015-06-25 11:36:50 -07004706 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004707 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4708 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004709 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004710 UserHandleCompat.myUserHandle());
4711 }
4712
Winson Chung5ffd51d2015-06-25 11:36:50 -07004713 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004714 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4715 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004716 mWorkspace.onExternalDragStartedWithItem(dragView);
4717 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004718 }
4719
Winson Chung5ffd51d2015-06-25 11:36:50 -07004720 protected void moveWorkspaceToDefaultScreen() {
4721 mWorkspace.moveToDefaultScreen(false);
4722 }
4723
Anjali Koppalf5d29132014-02-28 13:33:27 -08004724 @Override
4725 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004726 if (mLauncherCallbacks != null) {
4727 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4728 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004729 }
4730
Winson Chung80baf5a2010-08-09 16:03:15 -07004731 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004732 * Returns a FastBitmapDrawable with the icon, accurately sized.
4733 */
4734 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4735 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4736 d.setFilterBitmap(true);
4737 resizeIconDrawable(d);
4738 return d;
4739 }
4740
4741 /**
4742 * Resizes an icon drawable to the correct icon size.
4743 */
Winson5fbe0742015-09-30 15:33:00 -07004744 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004745 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004746 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004747 }
4748
4749 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004750 * Prints out out state for debugging.
4751 */
4752 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004753 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004754 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004755 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4756 Log.d(TAG, "mRestoring=" + mRestoring);
4757 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004758 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004759 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004760 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004761
Daniel Sandler325dc232013-06-05 22:57:57 -04004762 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004763 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004764
4765 @Override
4766 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4767 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004768 // Dump workspace
4769 writer.println(prefix + "Workspace Items");
4770 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4771 writer.println(prefix + " Homescreen " + i);
4772
4773 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4774 for (int j = 0; j < layout.getChildCount(); j++) {
4775 Object tag = layout.getChildAt(j).getTag();
4776 if (tag != null) {
4777 writer.println(prefix + " " + tag.toString());
4778 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004779 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004780 }
Sunny Goyala1365452015-10-01 15:46:24 -07004781
4782 writer.println(prefix + " Hotseat");
4783 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4784 for (int j = 0; j < layout.getChildCount(); j++) {
4785 Object tag = layout.getChildAt(j).getTag();
4786 if (tag != null) {
4787 writer.println(prefix + " " + tag.toString());
4788 }
4789 }
4790
4791 synchronized (sDumpLogs) {
4792 writer.println();
4793 writer.println(prefix + "Debug logs");
4794 for (String log : sDumpLogs) {
4795 writer.println(prefix + " " + log);
4796 }
4797 }
4798
Adam Cohen9211d422014-10-07 18:14:53 -07004799 if (mLauncherCallbacks != null) {
4800 mLauncherCallbacks.dump(prefix, fd, writer, args);
4801 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004802 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004803
Sunny Goyala1365452015-10-01 15:46:24 -07004804 public static void addDumpLog(String tag, String log) {
4805 Log.d(tag, log);
4806 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004807 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004808 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004809 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004810 }
4811
Adam Cohen59400422014-03-05 18:07:04 -08004812 public static CustomAppWidget getCustomAppWidget(String name) {
4813 return sCustomAppWidgets.get(name);
4814 }
4815
4816 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4817 return sCustomAppWidgets;
4818 }
4819
Sunny Goyal29538332016-02-18 09:10:19 -08004820 public static List<View> getFolderContents(View icon) {
4821 if (icon instanceof FolderIcon) {
4822 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4823 } else {
4824 return Collections.EMPTY_LIST;
4825 }
4826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004827}
Michael Jurka2763be32011-02-24 11:19:57 -08004828
Dan Sandlerd5024042014-01-09 15:01:33 -05004829interface DebugIntents {
4830 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4831 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004832}