blob: e4115275222c28b8c115febef26a64c3d55ea561 [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;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800111import com.android.launcher3.logging.UserEventLogger;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800113import com.android.launcher3.logging.LoggerUtils;
114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700116import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700117import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800123import com.android.wallpaperpicker.WallpaperUtils;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700131import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800135import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700141 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700142 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
176 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Adam Cohen3ed316a2014-07-23 18:21:20 -0700197 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
198 static final String ACTION_FIRST_LOAD_COMPLETE =
199 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
200
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 private static final String QSB_WIDGET_ID = "qsb_widget_id";
202 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700207 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
208 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700218 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800230 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700239 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800240 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Winson Chungc51db6a2011-10-05 11:44:49 -0700251 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700252 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
254 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Sunny Goyal594d76d2014-11-06 10:12:54 -0800261 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
265 // scroll issues (because the workspace may not have been measured yet) and extra work.
266 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
267 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
269 private SpannableStringBuilder mDefaultKeySsb = null;
270
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800273 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private boolean mRestoring;
275 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700276 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700280 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700286 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Sunny Goyal639e9062015-08-19 19:17:06 -0700289 private LauncherClings mClings;
290
Sunny Goyale2df0622015-04-24 11:27:00 -0700291 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700313
Sunny Goyala1365452015-10-01 15:46:24 -0700314 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
315 private static final Date sDateStamp = new Date();
316 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700317 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800369 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800370
Adam Cohenf9c184a2016-01-15 16:47:43 -0800371 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal7779d622015-06-11 16:18:39 -0700383 private Runnable mUpdateOrientationRunnable = new Runnable() {
384 public void run() {
385 setOrientation();
386 }
387 };
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700415 mDeviceProfile = getResources().getConfiguration().orientation
416 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700417 app.getInvariantDeviceProfile().landscapeProfile
418 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700419
Sunny Goyalf7258242015-10-19 16:59:07 -0700420 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800429 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
Tony Wickhameef44322015-10-20 13:24:36 -0700448 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
449 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700451 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
458 if (PROFILE_STARTUP) {
459 android.os.Debug.stopMethodTracing();
460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700470 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473
474 // For handling default keys
475 mDefaultKeySsb = new SpannableStringBuilder();
476 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800477
478 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
479 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800480
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800481 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 // In case we are on a device with locked rotation, we should look at preferences to check
483 // if the user has specifically allowed rotation.
484 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700485 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
505 public void onSettingsChanged(String settings, boolean value) {
506 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
507 mRotationEnabled = value;
508 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
509 mUpdateOrientationRunnable.run();
510 }
511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
Sunny Goyal32554d12015-12-03 15:31:25 -0800523 /**
524 * Call this after onCreate to set or clear overlay.
525 */
526 public void setLauncherOverlay(LauncherOverlay overlay) {
527 if (overlay != null) {
528 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
529 }
530 mWorkspace.setLauncherOverlay(overlay);
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700548 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Winson Chungef7f8742015-06-04 17:18:17 -0700586 /**
587 * Updates the bounds of all the overlays to match the new fixed bounds.
588 */
589 public void updateOverlayBounds(Rect newBounds) {
590 mAppsView.setSearchBarBounds(newBounds);
591 mWidgetsView.setSearchBarBounds(newBounds);
592 }
593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Anton Hanssona2f665f2013-09-26 12:18:32 +0100632 public Stats getStats() {
633 return mStats;
634 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800635
636 /**
637 * Logger object is a singleton and does not have to be coupled with the foreground activity.
638 * Since most user event logging is done on the UI, the object is retrieved from the
639 * callback for convenience.
640 */
641 private void initLogger() {
642 if (mLauncherCallbacks != null) {
643 mUserEventLogger = mLauncherCallbacks.getLogger();
644 }
645 if (mUserEventLogger == null) {
646 mUserEventLogger = new UserEventLogger() {
647 @Override
648 public void processEvent(LauncherLogProto.LauncherEvent ev) {
649 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
650 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
651 LoggerUtils.getActionStr(ev.action),
652 LoggerUtils.getTargetStr(ev.srcTarget),
653 ev.elapsedContainerMillis,
654 ev.elapsedSessionMillis));
655 }
656 }
657 };
658 }
659 }
660
661 public UserEventLogger getLogger() {
662 return mUserEventLogger;
663 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100664
Hyunyoung Song3f471442015-04-08 19:01:34 -0700665 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700666 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
667 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700668 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700669 }
670
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000671 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700672 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
673 // This cast is safe as long as the id < 0x00FFFFFF
674 // Since we jail all the dynamically generated views, there should be no clashes
675 // with any other views.
676 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000677 }
678
679 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
681 * a configuration step, this allows the proper animations to run after other transitions.
682 */
Adam Cohendb364c32014-05-20 14:23:40 -0700683 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700684 long screenId = args.screenId;
685 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
686 // When the screen id represents an actual screen (as opposed to a rank) we make sure
687 // that the drop page actually exists.
688 screenId = ensurePendingDropLayoutExists(args.screenId);
689 }
Adam Cohendb364c32014-05-20 14:23:40 -0700690
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700693 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700694 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700697 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700698 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700699 case REQUEST_RECONFIGURE_APPWIDGET:
700 completeRestoreAppWidget(args.appWidgetId);
701 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 }
Michael Jurka27614d22012-04-02 06:40:22 -0700703 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
704 // if you turned the screen off and then back while in All Apps, Launcher would not
705 // return to the workspace. Clearing mAddInfo.container here fixes this issue
706 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700707 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 }
709
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800710 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700712 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700713 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700714 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800715 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700716
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 Runnable exitSpringLoaded = new Runnable() {
718 @Override
719 public void run() {
720 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
721 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
722 }
723 };
724
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700726 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
729 if (resultCode == RESULT_CANCELED) {
730 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700731 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800734 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700736
737 // When the user has granted permission to bind widgets, we should check to see if
738 // we can inflate the default search bar widget.
739 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 }
741 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
743 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700744 // User could have free-scrolled between pages before picking a wallpaper; make sure
745 // we move to the closest one now.
746 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700747 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200748 }
749 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200751
Adam Cohened66b2b2012-01-23 17:28:51 -0800752 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700753 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700754
Adam Cohendb364c32014-05-20 14:23:40 -0700755 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 // We have special handling for widgets
757 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800758 final int appWidgetId;
759 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
760 : -1;
761 if (widgetId < 0) {
762 appWidgetId = pendingAddWidgetId;
763 } else {
764 appWidgetId = widgetId;
765 }
766
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800768 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700769 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
770 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 result = RESULT_CANCELED;
772 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700773 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 @Override
775 public void run() {
776 exitSpringLoadedDragModeDelayed(false, 0, null);
777 }
778 };
Adam Cohendb364c32014-05-20 14:23:40 -0700779 if (workspaceLocked) {
780 // No need to remove the empty screen if we're mid-binding, as the
781 // the bind will not add the empty screen.
782 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
783 } else {
784 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
785 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
786 }
Winson Chung5aaab772012-04-27 15:24:02 -0700787 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700788 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700789 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
790 // When the screen id represents an actual screen (as opposed to a rank)
791 // we make sure that the drop page actually exists.
792 mPendingAddInfo.screenId =
793 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
794 }
Adam Cohendb364c32014-05-20 14:23:40 -0700795 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
796
797 dropLayout.setDropPending(true);
798 final Runnable onComplete = new Runnable() {
799 @Override
800 public void run() {
801 completeTwoStageWidgetDrop(resultCode, appWidgetId);
802 dropLayout.setDropPending(false);
803 }
804 };
805 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
807 } else {
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
809 mPendingAddInfo);
810 sPendingAddItem = args;
811 }
Winson Chung5aaab772012-04-27 15:24:02 -0700812 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800813 return;
814 }
815
Sunny Goyalff572272014-07-23 13:58:07 -0700816 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
817 if (resultCode == RESULT_OK) {
818 // Update the widget view.
819 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
820 pendingAddWidgetId, mPendingAddInfo);
821 if (workspaceLocked) {
822 sPendingAddItem = args;
823 } else {
824 completeAdd(args);
825 }
826 }
827 // Leave the widget in the pending state if the user canceled the configure.
828 return;
829 }
830
Sunny Goyalaad90582015-07-09 14:22:50 -0700831 if (requestCode == REQUEST_CREATE_SHORTCUT) {
832 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
833 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
834 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
835 mPendingAddInfo);
836 if (isWorkspaceLocked()) {
837 sPendingAddItem = args;
838 } else {
839 completeAdd(args);
840 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 }
843 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
845 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800849
850 }
851
852 @Override
853 protected void onActivityResult(
854 final int requestCode, final int resultCode, final Intent data) {
855 handleActivityResult(requestCode, resultCode, data);
856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 }
860
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600861 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700862 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600863 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
865 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
866 View v = null;
867 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
868 if (layout != null) {
869 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
870 }
871 Intent intent = sPendingAddItem.intent;
872 sPendingAddItem = null;
873 if (grantResults.length > 0
874 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
875 startActivity(v, intent, null);
876 } else {
877 // TODO: Show a snack bar with link to settings
878 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
879 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
880 }
881 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600882 if (mLauncherCallbacks != null) {
883 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
884 grantResults);
885 }
886 }
887
Adam Cohendb364c32014-05-20 14:23:40 -0700888 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
889 appWidgetId, ItemInfo info) {
890 PendingAddArguments args = new PendingAddArguments();
891 args.requestCode = requestCode;
892 args.intent = data;
893 args.container = info.container;
894 args.screenId = info.screenId;
895 args.cellX = info.cellX;
896 args.cellY = info.cellY;
897 args.appWidgetId = appWidgetId;
898 return args;
899 }
900
901 /**
902 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
903 *
904 * @param screenId the screen id to check
905 * @return the new screen, or screenId if it exists
906 */
907 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800908 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700909 if (dropLayout == null) {
910 // it's possible that the add screen was removed because it was
911 // empty and a re-bind occurred
912 mWorkspace.addExtraEmptyScreen();
913 return mWorkspace.commitExtraEmptyScreen();
914 } else {
915 return screenId;
916 }
917 }
918
Adam Cohen091440a2015-03-18 14:16:05 -0700919 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800920 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 Runnable onCompleteRunnable = null;
922 int animationType = 0;
923
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 if (resultCode == RESULT_OK) {
926 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
927 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 mPendingAddWidgetInfo);
929 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 onCompleteRunnable = new Runnable() {
931 @Override
932 public void run() {
933 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
936 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 }
938 };
939 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 if (mDragLayer.getAnimatedView() != null) {
944 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
945 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
946 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700947 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 // The animated view may be null in the case of a rotation during widget configuration
949 onCompleteRunnable.run();
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 protected void onStop() {
955 super.onStop();
956 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700957
958 if (mLauncherCallbacks != null) {
959 mLauncherCallbacks.onStop();
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
964 protected void onStart() {
965 super.onStart();
966 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStart();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200975 long startTime = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400978 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.preOnResume();
983 }
984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800986 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700987
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700989 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800991 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700992 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700993 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700994 // view after launching an app, as they may be depending on the UI to be static to
995 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700996 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700997 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800998 } else if (mOnResumeState == State.WIDGETS) {
999 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 }
1001 mOnResumeState = State.NONE;
1002
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001003 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001004 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1005 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001006
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001007 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001009 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001010 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1016 // execute them here
1017 long startTimeCallbacks = 0;
1018 if (DEBUG_RESUME_TIME) {
1019 startTimeCallbacks = System.currentTimeMillis();
1020 }
1021
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1023 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1028 (System.currentTimeMillis() - startTimeCallbacks));
1029 }
Michael Jurka447bf842013-05-15 14:52:15 +02001030 }
Michael Jurka54554252013-08-01 12:52:23 +02001031 if (mOnResumeCallbacks.size() > 0) {
1032 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1033 mOnResumeCallbacks.get(i).run();
1034 }
1035 mOnResumeCallbacks.clear();
1036 }
Winson Chunge4e50662012-01-23 14:45:13 -08001037
1038 // Reset the pressed state of icons that were locked in the press state while activities
1039 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001040 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001041 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 mWaitingForResume.setStayPressed(false);
1043 }
Winson Chung82963d52013-10-09 11:20:57 -07001044
Adam Cohen06dff352012-06-01 17:17:08 -07001045 // It is possible that widgets can receive updates while launcher is not in the foreground.
1046 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1047 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1048 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001049 if (!isWorkspaceLoading()) {
1050 getWorkspace().reinflateWidgetsIfNecessary();
1051 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001052 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001053
Michael Jurka447bf842013-05-15 14:52:15 +02001054 if (DEBUG_RESUME_TIME) {
1055 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1056 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001057
Adam Cohen4b66bf32015-09-18 12:15:19 -07001058 // We want to suppress callbacks about CustomContent being shown if we have just received
1059 // onNewIntent while the user was present within launcher. In that case, we post a call
1060 // to move the user to the main screen (which will occur after onResume). We don't want to
1061 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1062 // suppress here.
1063 if (mWorkspace.getCustomContentCallbacks() != null
1064 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001065 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001066 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001067 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1068 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001069 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001070 }
1071 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001072 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001073 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001074 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001075
Sunny Goyal756adbc2015-04-16 15:20:51 -07001076 if (!isWorkspaceLoading()) {
1077 // Process any items that were added while Launcher was away.
1078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1079 }
Adam Cohen9211d422014-10-07 18:14:53 -07001080
1081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onResume();
1083 }
Adam Cohen06dff352012-06-01 17:17:08 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001088 // Ensure that items added to Launcher are queued until Launcher returns
1089 InstallShortcutReceiver.enableInstallQueue();
1090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001093 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001094 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001095
1096 // We call onHide() aggressively. The custom content callbacks should be able to
1097 // debounce excess onHide calls.
1098 if (mWorkspace.getCustomContentCallbacks() != null) {
1099 mWorkspace.getCustomContentCallbacks().onHide();
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen9211d422014-10-07 18:14:53 -07001102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
1107 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001108 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1109 // by a onResume or by scrolling otherwise.
1110 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111
1112 // Custom content is completely hidden
1113 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001114
1115 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1116 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001117
1118 // Indicates whether the user is allowed to scroll away from the custom content.
1119 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlay {
1123
1124 /**
1125 * Touch interaction leading to overscroll has begun
1126 */
1127 public void onScrollInteractionBegin();
1128
1129 /**
1130 * Touch interaction related to overscroll has ended
1131 */
1132 public void onScrollInteractionEnd();
1133
1134 /**
1135 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1136 * screen (or in the case of RTL, the rightmost screen).
1137 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001138 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139
1140 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001141 * Called when the launcher is ready to use the overlay
1142 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001144 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 }
1146
Jun Mukai8af0cd82015-05-12 12:32:12 -07001147 public interface LauncherSearchCallbacks {
1148 /**
1149 * Called when the search overlay is shown.
1150 */
1151 public void onSearchOverlayOpened();
1152
1153 /**
1154 * Called when the search overlay is dismissed.
1155 */
1156 public void onSearchOverlayClosed();
1157 }
1158
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001160 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
1163 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1164
Sunny Goyalc86df472016-02-25 09:19:38 -08001165 public void onScrollChanged(float progress) {
1166 if (mWorkspace != null) {
1167 mWorkspace.onOverlayScrollChanged(progress);
1168 }
1169 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 }
1171
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001173 if (mLauncherCallbacks != null) {
1174 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001175 } else {
1176 // On devices with a locked orientation, we will at least have the allow rotation
1177 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001178 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001179 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 }
1181
Adam Cohen9211d422014-10-07 18:14:53 -07001182 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001183 CustomContentCallbacks callbacks, String description) {
1184 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001185 }
1186
Adam Cohenedb40762013-07-18 16:45:45 -07001187 // The custom content needs to offset its content to account for the QSB
1188 public int getTopOffsetForCustomContent() {
1189 return mWorkspace.getPaddingTop();
1190 }
1191
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 @Override
1193 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001195 if (mModel.isCurrentCallbacks(this)) {
1196 mModel.stopLoader();
1197 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001198 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1199
Romain Guy13c2e7b2010-03-10 19:45:00 -08001200 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001201 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001202
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 @Override
1205 public void onWindowFocusChanged(boolean hasFocus) {
1206 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001207 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001208
1209 if (mLauncherCallbacks != null) {
1210 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1211 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 private boolean acceptFilter() {
1215 final InputMethodManager inputManager = (InputMethodManager)
1216 getSystemService(Context.INPUT_METHOD_SERVICE);
1217 return !inputManager.isFullscreenMode();
1218 }
1219
1220 @Override
1221 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001222 final int uniChar = event.getUnicodeChar();
1223 final boolean handled = super.onKeyDown(keyCode, event);
1224 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1225 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1227 keyCode, event);
1228 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001230 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 // showSearchDialog()
1232 // If there are multiple keystrokes before the search dialog takes focus,
1233 // onSearchRequested() will be called for every keystroke,
1234 // but it is idempotent, so it's fine.
1235 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
1238
Joe Onorato8a9625e2010-01-28 15:55:35 -08001239 // Eat the long press event so the keyboard doesn't come up.
1240 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1241 return true;
1242 }
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 return handled;
1245 }
1246
Winson46163472015-09-22 17:31:56 -07001247 @Override
1248 public boolean onKeyUp(int keyCode, KeyEvent event) {
1249 if (keyCode == KeyEvent.KEYCODE_MENU) {
1250 // Ignore the menu key if we are currently dragging or are on the custom content screen
1251 if (!isOnCustomContent() && !mDragController.isDragging()) {
1252 // Close any open folders
1253 closeFolder();
1254
1255 // Stop resizing any widgets
1256 mWorkspace.exitWidgetResizeMode();
1257
1258 // Show the overview mode if we are on the workspace
1259 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1260 !mWorkspace.isSwitchingState()) {
1261 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001262 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001263 }
1264 }
1265 return true;
1266 }
1267 return super.onKeyUp(keyCode, event);
1268 }
1269
Karl Rosaen138a0412009-04-23 19:00:21 -07001270 private String getTypedText() {
1271 return mDefaultKeySsb.toString();
1272 }
1273
1274 private void clearTypedText() {
1275 mDefaultKeySsb.clear();
1276 mDefaultKeySsb.clearSpans();
1277 Selection.setSelection(mDefaultKeySsb, 0);
1278 }
1279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001281 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1282 * State
1283 */
1284 private static State intToState(int stateOrdinal) {
1285 State state = State.WORKSPACE;
1286 final State[] stateValues = State.values();
1287 for (int i = 0; i < stateValues.length; i++) {
1288 if (stateValues[i].ordinal() == stateOrdinal) {
1289 state = stateValues[i];
1290 break;
1291 }
1292 }
1293 return state;
1294 }
1295
1296 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 * Restores the previous state, if it exists.
1298 *
1299 * @param savedState The previous state.
1300 */
1301 private void restoreState(Bundle savedState) {
1302 if (savedState == null) {
1303 return;
1304 }
1305
Michael Jurka883f55b2010-10-21 15:47:14 -07001306 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001307 if (state == State.APPS || state == State.WIDGETS) {
1308 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001309 }
1310
Adam Cohen21cd0022013-10-09 18:57:02 -07001311 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1312 PagedView.INVALID_RESTORE_PAGE);
1313 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001314 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316
Sunny Goyal756cd262015-08-20 12:33:21 -07001317 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1318 if (itemValues != null) {
1319 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001320 AppWidgetProviderInfo info = savedState.getParcelable(
1321 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001322 mPendingAddWidgetInfo = info == null ?
1323 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1324
Adam Cohen4637b5a2013-11-04 18:21:24 -08001325 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001326 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 mRestoring = true;
1328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
1331 /**
1332 * Finds all the views we need and configure them properly.
1333 */
1334 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001335 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001336
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001338 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001339 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1340 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001341 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001342 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001343
John Spurlock77e1f472013-09-11 10:09:51 -04001344 mLauncherView.setSystemUiVisibility(
1345 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001346 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
Winson Chung4c98d922011-05-31 16:50:48 -07001348 // Setup the drag layer
1349 mDragLayer.setup(this, dragController);
1350
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 // Setup the hotseat
1352 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1353 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001354 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 }
1356
Winsone9f27272015-10-13 10:47:51 -07001357 // Setup the overview panel
1358 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001359
Winson Chung4c98d922011-05-31 16:50:48 -07001360 // Setup the workspace
1361 mWorkspace.setHapticFeedbackEnabled(false);
1362 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001363 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001364 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001365
Tony Wickham34d2c912015-09-11 09:27:58 -07001366 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001367 mSearchDropTargetBar = (SearchDropTargetBar)
1368 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001369 // Get the app info bar
1370 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1371 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001372
Winson Chungef7f8742015-06-04 17:18:17 -07001373 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001374 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001375 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001376 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1377 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1378 } else {
1379 mAppsView.setSearchBarController(new DefaultAppSearchController());
1380 }
Craig Mautner360310b2012-10-26 15:13:08 -07001381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001383 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001384 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001385 dragController.setMoveTarget(mWorkspace);
1386 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001387 if (mSearchDropTargetBar != null) {
1388 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001389 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001390 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001391 if (mAppInfoDropTargetBar != null) {
1392 mAppInfoDropTargetBar.setup(this, dragController);
1393 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001394
Sunny Goyal322d5562015-06-25 19:35:49 -07001395 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1396 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Winsone9f27272015-10-13 10:47:51 -07001400 private void setupOverviewPanel() {
1401 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1402
1403 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1404 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1405 @Override
1406 public boolean onLongClick(View v) {
1407 return v.performClick();
1408 }
1409 };
1410
1411 // Bind wallpaper button actions
1412 View wallpaperButton = findViewById(R.id.wallpaper_button);
1413 wallpaperButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickWallpaperPicker(view);
1418 }
1419 }
1420 });
1421 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1422 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 // Bind widget button actions
1425 mWidgetsButton = findViewById(R.id.widget_button);
1426 mWidgetsButton.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View view) {
1429 if (!mWorkspace.isSwitchingState()) {
1430 onClickAddWidgetButton(view);
1431 }
1432 }
1433 });
1434 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1435 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 // Bind settings actions
1438 View settingsButton = findViewById(R.id.settings_button);
1439 boolean hasSettings = hasSettings();
1440 if (hasSettings) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickSettingsButton(view);
1446 }
1447 }
1448 });
1449 settingsButton.setOnLongClickListener(performClickOnLongClick);
1450 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451 } else {
1452 settingsButton.setVisibility(View.GONE);
1453 }
1454
1455 mOverviewPanel.setAlpha(0f);
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
1460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
1465 public View getAllAppsButton() {
1466 return mAllAppsButton;
1467 }
1468
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001469 public View getWidgetsButton() {
1470 return mWidgetsButton;
1471 }
1472
Anjali Koppal5ad44842014-03-10 20:34:39 -07001473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Creates a view representing a shortcut.
1475 *
1476 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 /**
1483 * Creates a view representing a shortcut inflated from the specified resource.
1484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001491 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 parent, false);
1493 favorite.applyFromShortcutInfo(info, mIconCache);
1494 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001496 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return favorite;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Add a shortcut to the workspace.
1502 *
1503 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int cellY) {
1507 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Sunny Goyal5c97f512015-05-19 16:03:28 -07001510 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001511 if (info == null) {
1512 return;
1513 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 final View view = createShortcut(info);
1515
Sunny Goyal5c97f512015-05-19 16:03:28 -07001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 // First we check if we already know the exact location where we want to add this item.
1518 if (cellX >= 0 && cellY >= 0) {
1519 cellXY[0] = cellX;
1520 cellXY[1] = cellY;
1521 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001522
1523 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 true, null,null)) {
1526 return;
1527 }
1528 DragObject dragObject = new DragObject();
1529 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1531 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 return;
1533 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 }
1537
1538 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001539 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 return;
1541 }
1542
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001543 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
1545 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001552 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001554 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 */
Adam Cohen091440a2015-03-18 14:16:05 -07001556 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001557 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1558
1559 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001560 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001561 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohen59400422014-03-05 18:07:04 -08001564 if (appWidgetInfo.isCustomWidget) {
1565 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001566 }
1567
Adam Cohen59400422014-03-05 18:07:04 -08001568 LauncherAppWidgetInfo launcherInfo;
1569 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1570 launcherInfo.spanX = info.spanX;
1571 launcherInfo.spanY = info.spanY;
1572 launcherInfo.minSpanX = info.minSpanX;
1573 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001574 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001577 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 if (hostView == null) {
1581 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001582 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1583 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 } else {
1585 // The AppWidgetHostView has already been inflated and instantiated
1586 launcherInfo.hostView = hostView;
1587 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001588 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001589 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001591 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Romain Guycbb89e42009-06-08 15:52:54 -07001593
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001594 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1595 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1596 item.hostView.setTag(item);
1597 item.onBindAppWidget(this);
1598
1599 item.hostView.setFocusable(true);
1600 item.hostView.setOnFocusChangeListener(mFocusHandler);
1601
1602 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1603 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1604
1605 if (!item.isCustomWidget()) {
1606 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1607 }
1608 }
1609
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1611 @Override
1612 public void onReceive(Context context, Intent intent) {
1613 final String action = intent.getAction();
1614 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1615 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001616 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001617 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001618
Winson Chungc100e8e2011-08-09 16:02:43 -07001619 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001620 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001621 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001622 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001623 if (!showWorkspace(false)) {
1624 // If we are already on the workspace, then manually reset all apps
1625 mAppsView.reset();
1626 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001627 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1629 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001630 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001631 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1632 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001633 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001634 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1635 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1636 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001637 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001638 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1639 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 }
1641 }
1642 };
1643
1644 @Override
1645 public void onAttachedToWindow() {
1646 super.onAttachedToWindow();
1647
1648 // Listen for broadcasts related to user-presence
1649 final IntentFilter filter = new IntentFilter();
1650 filter.addAction(Intent.ACTION_SCREEN_OFF);
1651 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001652 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001653 if (ENABLE_DEBUG_INTENTS) {
1654 filter.addAction(DebugIntents.DELETE_DATABASE);
1655 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1656 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001657 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001659 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001661
1662 if (mLauncherCallbacks != null) {
1663 mLauncherCallbacks.onAttachedToWindow();
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
1667 @Override
1668 public void onDetachedFromWindow() {
1669 super.onDetachedFromWindow();
1670 mVisible = false;
1671
Adam Cohend113e0c2010-11-11 10:48:05 -08001672 if (mAttached) {
1673 unregisterReceiver(mReceiver);
1674 mAttached = false;
1675 }
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001677
1678 if (mLauncherCallbacks != null) {
1679 mLauncherCallbacks.onDetachedFromWindow();
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 }
1682
1683 public void onWindowVisibilityChanged(int visibility) {
1684 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001685 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001686 // The following code used to be in onResume, but it turns out onResume is called when
1687 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1688 // is a more appropriate event to handle
1689 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 if (!mWorkspaceLoading) {
1691 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001692 // We want to let Launcher draw itself at least once before we force it to build
1693 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001694 // apps is nice and speedy.
1695 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001697 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 if (mStarted) return;
1699 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001700 // We delay the layer building a bit in order to give
1701 // other message processing a time to run. In particular
1702 // this avoids a delay in hiding the IME if it was
1703 // currently shown, because doing that may involve
1704 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001705 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001706 final ViewTreeObserver.OnDrawListener listener = this;
1707 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001708 public void run() {
1709 if (mWorkspace != null &&
1710 mWorkspace.getViewTreeObserver() != null) {
1711 mWorkspace.getViewTreeObserver().
1712 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001714 }
1715 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001717 }
1718 });
1719 }
1720 clearTypedText();
1721 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 }
1723
Adam Cohen091440a2015-03-18 14:16:05 -07001724 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 mHandler.removeMessages(ADVANCE_MSG);
1726 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1727 mHandler.sendMessageDelayed(msg, delay);
1728 mAutoAdvanceSentTime = System.currentTimeMillis();
1729 }
1730
Adam Cohen091440a2015-03-18 14:16:05 -07001731 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1733 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1734 mAutoAdvanceRunning = autoAdvanceRunning;
1735 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001736 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 sendAdvanceMessage(delay);
1738 } else {
1739 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1742 }
1743 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001744 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746 }
1747 }
1748
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1750
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 if (msg.what == ADVANCE_MSG) {
1754 int i = 0;
1755 for (View key: mWidgetsToAdvance.keySet()) {
1756 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001757 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 public void run() {
1761 ((Advanceable) v).advance();
1762 }
1763 }, delay);
1764 }
1765 i++;
1766 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001767 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001769 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001771 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001772
Winsonc0b52fe2015-09-09 16:38:15 -07001773 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001774 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1776 if (v instanceof Advanceable) {
1777 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001778 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001779 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781 }
1782
Winsonc0b52fe2015-09-09 16:38:15 -07001783 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 if (mWidgetsToAdvance.containsKey(hostView)) {
1785 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001786 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 }
1789
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001791 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1792 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public DragLayer getDragLayer() {
1796 return mDragLayer;
1797 }
1798
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001799 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001800 return mAppsView;
1801 }
1802
Hyunyoung Song3f471442015-04-08 19:01:34 -07001803 public WidgetsContainerView getWidgetsView() {
1804 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001805 }
1806
Winson Chunga6945242014-01-08 14:04:34 -08001807 public Workspace getWorkspace() {
1808 return mWorkspace;
1809 }
1810
1811 public Hotseat getHotseat() {
1812 return mHotseat;
1813 }
1814
Jorim Jaggid017f882014-01-14 17:08:48 -08001815 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001816 return mOverviewPanel;
1817 }
1818
Winson Chung006ee262015-08-03 14:40:11 -07001819 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mSearchDropTargetBar;
1821 }
1822
Tony Wickham34d2c912015-09-11 09:27:58 -07001823 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1824 return mAppInfoDropTargetBar;
1825 }
1826
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001827 public LauncherAppWidgetHost getAppWidgetHost() {
1828 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
Romain Guycbb89e42009-06-08 15:52:54 -07001830
Winson Chunga9abd0e2010-10-27 17:18:37 -07001831 public LauncherModel getModel() {
1832 return mModel;
1833 }
1834
Winson Chunga6945242014-01-08 14:04:34 -08001835 protected SharedPreferences getSharedPrefs() {
1836 return mSharedPrefs;
1837 }
1838
Adam Cohen2e6da152015-05-06 11:42:25 -07001839 public DeviceProfile getDeviceProfile() {
1840 return mDeviceProfile;
1841 }
1842
Bjorn Bringertc459e522013-06-07 19:36:01 +01001843 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001844 getWindow().closeAllPanels();
1845
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001846 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001847 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
1851 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001852 long startTime = 0;
1853 if (DEBUG_RESUME_TIME) {
1854 startTime = System.currentTimeMillis();
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 super.onNewIntent(intent);
1857
1858 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001859 Folder openFolder = mWorkspace.getOpenFolder();
1860 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1861 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1862 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1863 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1864 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001865 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001867
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 if (mWorkspace == null) {
1869 // Can be cases where mWorkspace is null, this prevents a NPE
1870 return;
1871 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 // In all these cases, only animate if we're already on home
1873 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001874
Sunny Goyal935fca12015-10-13 10:19:01 -07001875 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 exitSpringLoadedDragMode();
1877
1878 // If we are already on home, then just animate back to the workspace,
1879 // otherwise, just wait until onResume to set the state back to Workspace
1880 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001881 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001882 } else {
1883 mOnResumeState = State.WORKSPACE;
1884 }
1885
1886 final View v = getWindow().peekDecorView();
1887 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001888 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 INPUT_METHOD_SERVICE);
1890 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1891 }
1892
Winson Chungb745afb2015-03-02 11:51:23 -08001893 // Reset the apps view
1894 if (!alreadyOnHome && mAppsView != null) {
1895 mAppsView.scrollToTop();
1896 }
1897
Hyunyoung Song3f471442015-04-08 19:01:34 -07001898 // Reset the widgets view
1899 if (!alreadyOnHome && mWidgetsView != null) {
1900 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001902
Adam Cohen9211d422014-10-07 18:14:53 -07001903 if (mLauncherCallbacks != null) {
1904 mLauncherCallbacks.onHomeIntent();
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
Adam Cohened307df2013-10-02 09:37:31 -07001907
Adam Cohen9211d422014-10-07 18:14:53 -07001908 if (mLauncherCallbacks != null) {
1909 mLauncherCallbacks.onNewIntent(intent);
1910 }
Winson15f8b172015-08-19 11:02:39 -07001911
1912 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1913 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1914 // animation.
1915 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001916 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1917 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001918 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1919 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001920
1921 // We use this flag to suppress noisy callbacks above custom content state
1922 // from onResume.
1923 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001924 mWorkspace.post(new Runnable() {
1925 @Override
1926 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001927 if (mWorkspace != null) {
1928 mWorkspace.moveToDefaultScreen(true);
1929 }
Winson15f8b172015-08-19 11:02:39 -07001930 }
1931 });
1932 }
1933 }
1934
1935 if (DEBUG_RESUME_TIME) {
1936 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1937 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001938 }
1939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001941 public void onRestoreInstanceState(Bundle state) {
1942 super.onRestoreInstanceState(state);
1943 for (int page: mSynchronouslyBoundPages) {
1944 mWorkspace.restoreInstanceStateForChild(page);
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
1948 @Override
1949 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001950 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001951 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1952 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001954 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Michael Jurka883f55b2010-10-21 15:47:14 -07001956 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001957 // We close any open folder since it will not be re-opened, and we need to make sure
1958 // this state is reflected.
1959 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1960 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001964 ContentValues itemValues = new ContentValues();
1965 mPendingAddInfo.writeToValues(itemValues);
1966 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
Hyunyoung Song3f471442015-04-08 19:01:34 -07001971 // Save the current widgets tray?
1972 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001973
1974 if (mLauncherCallbacks != null) {
1975 mLauncherCallbacks.onSaveInstanceState(outState);
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
1979 @Override
1980 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001982
Winson Chunge7a03942011-08-05 15:05:12 -07001983 // Remove all pending runnables
1984 mHandler.removeMessages(ADVANCE_MSG);
1985 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001986 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001987
Winson Chungcd2b0142011-06-08 16:02:26 -07001988 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001989 // It's possible to receive onDestroy after a new Launcher activity has
1990 // been created. In this case, don't interfere with the new Launcher.
1991 if (mModel.isCurrentCallbacks(this)) {
1992 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001993 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001994 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001997 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001999 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002001 mAppWidgetHost = null;
2002
2003 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004
2005 TextKeyListener.getInstance().release();
2006
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002007 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002008
Michael Jurka2ecf9952012-06-18 12:52:28 -07002009 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002010
2011 if (mLauncherCallbacks != null) {
2012 mLauncherCallbacks.onDestroy();
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
Adam Cohena9cf38f2011-05-02 15:36:58 -07002016 public DragController getDragController() {
2017 return mDragController;
2018 }
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 @Override
2021 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002022 onStartForResult(requestCode);
2023 super.startActivityForResult(intent, requestCode);
2024 }
2025
2026 @Override
2027 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2028 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2029 onStartForResult(requestCode);
2030 try {
2031 super.startIntentSenderForResult(intent, requestCode,
2032 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2033 } catch (IntentSender.SendIntentException e) {
2034 throw new ActivityNotFoundException();
2035 }
2036 }
2037
2038 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002039 if (requestCode >= 0) {
2040 setWaitingForResult(true);
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043
Winson Chung70d72102011-08-12 11:24:35 -07002044 /**
2045 * Indicates that we want global search for this activity by setting the globalSearch
2046 * argument for {@link #startSearch} to true.
2047 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002049 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002051
Karl Rosaen138a0412009-04-23 19:00:21 -07002052 if (initialQuery == null) {
2053 // Use any text typed in the launcher as the initial query
2054 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 if (appSearchData == null) {
2057 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002058 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Winson Chungadf0c182012-08-23 14:59:07 -07002060 Rect sourceBounds = new Rect();
2061 if (mSearchDropTargetBar != null) {
2062 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2063 }
Romain Guycbb89e42009-06-08 15:52:54 -07002064
Ian Parkinson047036e2014-09-01 15:40:53 +01002065 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002067 if (clearTextImmediately) {
2068 clearTypedText();
2069 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002070
2071 // We need to show the workspace after starting the search
2072 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 }
2074
Ian Parkinson047036e2014-09-01 15:40:53 +01002075 /**
2076 * Start a text search.
2077 *
2078 * @return {@code true} if the search will start immediately, so any further keypresses
2079 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2080 * to buffer keypresses.
2081 */
2082 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002083 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002084 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2085 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2086 sourceBounds);
2087 }
2088
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002091 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 }
2093
2094 /**
2095 * Starts the global search activity. This code is a copied from SearchManager
2096 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002098 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2101 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2102 if (globalSearchActivity == null) {
2103 Log.w(TAG, "No global search activity found.");
2104 return;
2105 }
2106 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2107 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2108 intent.setComponent(globalSearchActivity);
2109 // Make sure that we have a Bundle to put source in
2110 if (appSearchData == null) {
2111 appSearchData = new Bundle();
2112 } else {
2113 appSearchData = new Bundle(appSearchData);
2114 }
Adam Cohen9211d422014-10-07 18:14:53 -07002115 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002116 if (!appSearchData.containsKey("source")) {
2117 appSearchData.putString("source", getPackageName());
2118 }
2119 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2120 if (!TextUtils.isEmpty(initialQuery)) {
2121 intent.putExtra(SearchManager.QUERY, initialQuery);
2122 }
2123 if (selectInitialQuery) {
2124 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2125 }
2126 intent.setSourceBounds(sourceBounds);
2127 try {
2128 startActivity(intent);
2129 } catch (ActivityNotFoundException ex) {
2130 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 }
2133
Yura4f93ec62014-02-04 14:15:21 +00002134 public boolean isOnCustomContent() {
2135 return mWorkspace.isOnOrMovingToCustomContent();
2136 }
2137
Winson Chung70d72102011-08-12 11:24:35 -07002138 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002139 public boolean onPrepareOptionsMenu(Menu menu) {
2140 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002141 if (mLauncherCallbacks != null) {
2142 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2143 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002144 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 @Override
2148 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002149 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002150 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002151 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002152 }
2153
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154 public boolean isWorkspaceLocked() {
2155 return mWorkspaceLoading || mWaitingForResult;
2156 }
2157
Adam Cohen517a7f52014-03-01 12:12:59 -08002158 public boolean isWorkspaceLoading() {
2159 return mWorkspaceLoading;
2160 }
2161
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002162 private void setWorkspaceLoading(boolean value) {
2163 boolean isLocked = isWorkspaceLocked();
2164 mWorkspaceLoading = value;
2165 if (isLocked != isWorkspaceLocked()) {
2166 onWorkspaceLockedChanged();
2167 }
2168 }
2169
2170 private void setWaitingForResult(boolean value) {
2171 boolean isLocked = isWorkspaceLocked();
2172 mWaitingForResult = value;
2173 if (isLocked != isWorkspaceLocked()) {
2174 onWorkspaceLockedChanged();
2175 }
2176 }
2177
Adam Cohen9211d422014-10-07 18:14:53 -07002178 protected void onWorkspaceLockedChanged() {
2179 if (mLauncherCallbacks != null) {
2180 mLauncherCallbacks.onWorkspaceLockedChanged();
2181 }
2182 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002183
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002185 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002186 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002187 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2188 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002189 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002190 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002191
Tony Wickhama0628cc2015-10-14 15:23:04 -07002192 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002193 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002194 if (LOGD) {
2195 Log.d(TAG, "Adding widget from drop");
2196 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002197 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2198 }
2199
Sunny Goyale6b63a32015-01-16 16:14:29 -08002200 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002201 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2202 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 if (appWidgetInfo.configure != null) {
2204 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002205 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002206
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002208 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2209 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002212 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002213 Runnable onComplete = new Runnable() {
2214 @Override
2215 public void run() {
2216 // Exit spring loaded mode if necessary after adding the widget
2217 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2218 null);
2219 }
2220 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002222 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002223 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 }
2225 }
Romain Guycbb89e42009-06-08 15:52:54 -07002226
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002227 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002228 // Close any folders that may be open.
2229 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 mWorkspace.moveToCustomContentScreen(animate);
2231 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002232
2233 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2234 int[] cell, int spanX, int spanY) {
2235 switch (info.itemType) {
2236 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2237 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2238 int span[] = new int[2];
2239 span[0] = spanX;
2240 span[1] = spanY;
2241 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2242 container, screenId, cell, span);
2243 break;
2244 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2245 processShortcutFromDrop(info.componentName, container, screenId, cell);
2246 break;
2247 default:
2248 throw new IllegalStateException("Unknown item type: " + info.itemType);
2249 }
2250 }
2251
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 /**
2253 * Process a shortcut drop.
2254 *
2255 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002259 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2260 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264
2265 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.cellX = cell[0];
2267 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269
2270 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2271 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002272 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002273 }
2274
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 /**
2276 * Process a widget drop.
2277 *
2278 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002282 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2283 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002287 mPendingAddInfo.minSpanX = info.minSpanX;
2288 mPendingAddInfo.minSpanY = info.minSpanY;
2289
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 mPendingAddInfo.cellX = cell[0];
2292 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002294 if (span != null) {
2295 mPendingAddInfo.spanX = span[0];
2296 mPendingAddInfo.spanY = span[1];
2297 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002298
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 AppWidgetHostView hostView = info.boundWidget;
2300 int appWidgetId;
2301 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002302 // In the case where we've prebound the widget, we remove it from the DragLayer
2303 if (LOGD) {
2304 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2305 }
2306 getDragLayer().removeView(hostView);
2307
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002309 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002310
2311 // Clear the boundWidget so that it doesn't get destroyed.
2312 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002313 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002314 // In this case, we either need to start an activity to get permission to bind
2315 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002316 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002317 Bundle options = info.bindOptions;
2318
Sunny Goyalffe83f12014-08-14 17:39:34 -07002319 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2320 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002321 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002322 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002323 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002324 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002325 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2326 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2327 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002328 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2329 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002330 // TODO: we need to make sure that this accounts for the options bundle.
2331 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002332 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2333 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002334 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 }
2336
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002339 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 folderInfo.title = getText(R.string.folder_name);
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002343 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2344 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002345 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346
2347 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 FolderIcon newFolder =
2349 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002350 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002352 // Force measure the new folder icon
2353 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2354 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002355 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 }
Romain Guycbb89e42009-06-08 15:52:54 -07002357
Winsonc0b52fe2015-09-09 16:38:15 -07002358 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002359 * Unbinds the view for the specified item, and removes the item and all its children.
2360 *
2361 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002362 * @param itemInfo the {@link ItemInfo} for this view.
2363 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002364 */
Winson2949fb52015-09-24 09:56:11 -07002365 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002366 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002367 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002368 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2369 if (folderInfo != null) {
2370 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002371 } else {
2372 mWorkspace.removeWorkspaceItem(v);
2373 }
Winsonc0b52fe2015-09-09 16:38:15 -07002374 if (deleteFromDb) {
2375 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2376 }
2377 } else if (itemInfo instanceof FolderInfo) {
2378 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2379 unbindFolder(folderInfo);
2380 mWorkspace.removeWorkspaceItem(v);
2381 if (deleteFromDb) {
2382 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2383 }
2384 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2385 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002386 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002387 removeWidgetToAutoAdvance(widgetInfo.hostView);
2388 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002389 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002390 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002391 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002392
Winsonc0b52fe2015-09-09 16:38:15 -07002393 } else {
2394 return false;
2395 }
2396 return true;
2397 }
2398
2399 /**
2400 * Unbinds any launcher references to the folder.
2401 */
2402 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002403 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 }
2405
Winsonc0b52fe2015-09-09 16:38:15 -07002406 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002407 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002408 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002410 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002411 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002412 // Deleting an app widget ID is a void call but writes to disk before returning
2413 // to the caller...
2414 new AsyncTask<Void, Void, Void>() {
2415 public Void doInBackground(Void ... args) {
2416 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2417 return null;
2418 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002419 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002420 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002421 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002422 }
2423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 @Override
2425 public boolean dispatchKeyEvent(KeyEvent event) {
2426 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2427 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002429 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002430 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002431 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002432 dumpState();
2433 return true;
2434 }
2435 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002436 }
2437 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2438 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 return true;
2441 }
2442 }
2443
2444 return super.dispatchKeyEvent(event);
2445 }
2446
Joe Onorato88ec0992009-11-19 13:16:06 -08002447 @Override
2448 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002449 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2450 return;
2451 }
2452
Sunny Goyal45478022015-06-08 16:52:41 -07002453 if (mDragController.isDragging()) {
2454 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002455 return;
2456 }
2457
Winson Chungb745afb2015-03-02 11:51:23 -08002458 if (isAppsViewVisible()) {
2459 showWorkspace(true);
2460 } else if (isWidgetsViewVisible()) {
2461 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002462 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002463 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002465 Folder openFolder = mWorkspace.getOpenFolder();
2466 if (openFolder.isEditingName()) {
2467 openFolder.dismissEditingName();
2468 } else {
2469 closeFolder();
2470 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002471 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002472 mWorkspace.exitWidgetResizeMode();
2473
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 // Back button is a no-op here, but give at least some feedback for the button press
2475 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002476 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002477 }
2478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002480 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002481 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002482 @Override
2483 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002484 if (mAppWidgetHost != null) {
2485 mAppWidgetHost.startListening();
2486 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002487
2488 // Recreate the QSB, as the widget has been reset.
2489 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002490 }
2491
2492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 * Launches the intent referred by the clicked shortcut.
2494 *
2495 * @param v The view representing the clicked shortcut.
2496 */
2497 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002498 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2499 // view has detached (it's possible for this to happen if the view is removed mid touch).
2500 if (v.getWindowToken() == null) {
2501 return;
2502 }
2503
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002504 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002505 return;
2506 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002507
Adam Cohen1697b792013-09-17 19:08:21 -07002508 if (v instanceof Workspace) {
2509 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002510 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
2512 return;
2513 }
2514
2515 if (v instanceof CellLayout) {
2516 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002517 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2518 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002520 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002521 }
2522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002524 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002530 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002532 } else if (tag instanceof AppInfo) {
2533 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002534 } else if (tag instanceof LauncherAppWidgetInfo) {
2535 if (v instanceof PendingAppWidgetHostView) {
2536 onClickPendingWidget((PendingAppWidgetHostView) v);
2537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 }
2539 }
2540
Sunny Goyal70660032015-05-14 00:07:08 -07002541 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002542 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002543 return false;
2544 }
2545
Michael Jurkaaf442092010-06-10 17:01:57 -07002546 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002547 * Event handler for the app widget view which has not fully restored.
2548 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002549 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002550 if (mIsSafeModeEnabled) {
2551 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2552 return;
2553 }
2554
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002556 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002557 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002558 LauncherAppWidgetProviderInfo appWidgetInfo =
2559 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002560 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002561 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002562 mPendingAddInfo.copyFrom(info);
2563 mPendingAddWidgetId = widgetId;
2564
Sunny Goyalffe83f12014-08-14 17:39:34 -07002565 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2566 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 } else if (info.installProgress < 0) {
2569 // The install has not been queued
2570 final String packageName = info.providerName.getPackageName();
2571 showBrokenAppInstallDialog(packageName,
2572 new DialogInterface.OnClickListener() {
2573 public void onClick(DialogInterface dialog, int id) {
2574 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2575 }
2576 });
2577 } else {
2578 // Download has started.
2579 final String packageName = info.providerName.getPackageName();
2580 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002581 }
2582 }
2583
2584 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002585 * Event handler for the "grid" button that appears on the home screen, which
2586 * enters all apps mode.
2587 *
2588 * @param v The view that was clicked.
2589 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002590 protected void onClickAllAppsButton(View v) {
2591 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002592 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002593 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002594 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002595
2596 if (mLauncherCallbacks != null) {
2597 mLauncherCallbacks.onClickAllAppsButton(v);
2598 }
Winson Chung76648c52015-07-10 14:33:23 -07002599 }
2600 }
2601
2602 protected void onLongClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2604 if (!isAppsViewVisible()) {
2605 showAppsView(true /* animated */, false /* resetListToTop */,
2606 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 }
2608 }
2609
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002610 private void showBrokenAppInstallDialog(final String packageName,
2611 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002612 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002613 .setTitle(R.string.abandoned_promises_title)
2614 .setMessage(R.string.abandoned_promise_explanation)
2615 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2616 .setNeutralButton(R.string.abandoned_clean_this,
2617 new DialogInterface.OnClickListener() {
2618 public void onClick(DialogInterface dialog, int id) {
2619 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2620 mWorkspace.removeAbandonedPromise(packageName, user);
2621 }
2622 })
2623 .create().show();
2624 return;
2625 }
2626
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 /**
2628 * Event handler for an app shortcut click.
2629 *
2630 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2631 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002633 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2634 Object tag = v.getTag();
2635 if (!(tag instanceof ShortcutInfo)) {
2636 throw new IllegalArgumentException("Input must be a Shortcut");
2637 }
2638
2639 // Open shortcut
2640 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002641
2642 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002643 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2644 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002645 // Launch activity anyway, framework will tell the user why the app is suspended.
2646 } else {
2647 int error = R.string.activity_not_available;
2648 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2649 error = R.string.safemode_shortcut_error;
2650 }
2651 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2652 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002653 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002654 }
2655
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002657 if ((v instanceof BubbleTextView)
2658 && shortcut.isPromise()
2659 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002660 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002661 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 new DialogInterface.OnClickListener() {
2663 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002664 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 return;
2668 }
2669
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002671 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002672
2673 if (mLauncherCallbacks != null) {
2674 mLauncherCallbacks.onClickAppShortcut(v);
2675 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 }
2677
Adam Cohen091440a2015-03-18 14:16:05 -07002678 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002680 final ShortcutInfo shortcut;
2681 final Intent intent;
2682 if (tag instanceof ShortcutInfo) {
2683 shortcut = (ShortcutInfo) tag;
2684 intent = shortcut.intent;
2685 int[] pos = new int[2];
2686 v.getLocationOnScreen(pos);
2687 intent.setSourceBounds(new Rect(pos[0], pos[1],
2688 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002689
Sunny Goyal508da152014-08-14 10:53:27 -07002690 } else if (tag instanceof AppInfo) {
2691 shortcut = null;
2692 intent = ((AppInfo) tag).intent;
2693 } else {
2694 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2695 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696
2697 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002698 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699
2700 if (success && v instanceof BubbleTextView) {
2701 mWaitingForResume = (BubbleTextView) v;
2702 mWaitingForResume.setStayPressed(true);
2703 }
2704 }
2705
2706 /**
2707 * Event handler for a folder icon click.
2708 *
2709 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2710 */
2711 protected void onClickFolderIcon(View v) {
2712 if (LOGD) Log.d(TAG, "onClickFolder");
2713 if (!(v instanceof FolderIcon)){
2714 throw new IllegalArgumentException("Input must be a FolderIcon");
2715 }
2716
2717 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002718 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002720 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721 }
Adam Cohen9211d422014-10-07 18:14:53 -07002722
2723 if (mLauncherCallbacks != null) {
2724 mLauncherCallbacks.onClickFolderIcon(v);
2725 }
Michael Jurka5130e402011-10-13 04:55:35 -07002726 }
2727
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002728 /**
2729 * Event handler for the (Add) Widgets button that appears after a long press
2730 * on the home screen.
2731 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002732 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002734 if (mIsSafeModeEnabled) {
2735 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2736 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002737 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002738 if (mLauncherCallbacks != null) {
2739 mLauncherCallbacks.onClickAddWidgetButton(view);
2740 }
Adam Cohen9211d422014-10-07 18:14:53 -07002741 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002742 }
2743
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 /**
2745 * Event handler for the wallpaper picker button that appears after a long press
2746 * on the home screen.
2747 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002748 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002749 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002750 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2751 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2752 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002753 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002754 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002755
2756 if (mLauncherCallbacks != null) {
2757 mLauncherCallbacks.onClickWallpaperPicker(v);
2758 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 }
2760
2761 /**
2762 * Event handler for a click on the settings button that appears after a long press
2763 * on the home screen.
2764 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002765 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002767 if (mLauncherCallbacks != null) {
2768 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002769 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002770 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002771 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 }
2773
Adam Cohen61f560d2013-09-30 15:58:20 -07002774 public View.OnTouchListener getHapticFeedbackTouchListener() {
2775 if (mHapticFeedbackTouchListener == null) {
2776 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002777 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002778 @Override
2779 public boolean onTouch(View v, MotionEvent event) {
2780 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2781 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2782 }
2783 return false;
2784 }
2785 };
2786 }
2787 return mHapticFeedbackTouchListener;
2788 }
2789
Adam Cohen9211d422014-10-07 18:14:53 -07002790 public void onDragStarted(View view) {
2791 if (isOnCustomContent()) {
2792 // Custom content screen doesn't participate in drag and drop. If on custom
2793 // content screen, move to default.
2794 moveWorkspaceToDefaultScreen();
2795 }
2796
2797 if (mLauncherCallbacks != null) {
2798 mLauncherCallbacks.onDragStarted(view);
2799 }
2800 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002801
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802 /**
2803 * Called when the user stops interacting with the launcher.
2804 * This implies that the user is now on the homescreen and is not doing housekeeping.
2805 */
Adam Cohen9211d422014-10-07 18:14:53 -07002806 protected void onInteractionEnd() {
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onInteractionEnd();
2809 }
2810 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002811
2812 /**
2813 * Called when the user starts interacting with the launcher.
2814 * The possible interactions are:
2815 * - open all apps
2816 * - reorder an app shortcut, or a widget
2817 * - open the overview mode.
2818 * This is a good time to stop doing things that only make sense
2819 * when the user is on the homescreen and not doing housekeeping.
2820 */
Adam Cohen9211d422014-10-07 18:14:53 -07002821 protected void onInteractionBegin() {
2822 if (mLauncherCallbacks != null) {
2823 mLauncherCallbacks.onInteractionBegin();
2824 }
2825 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002826
Winson Chungcd99cd32015-04-29 11:03:24 -07002827 /** Updates the interaction state. */
2828 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002829 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002831 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2832 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002833 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002834 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002835 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002836 onInteractionEnd();
2837 }
2838 }
2839
Kenny Guyf07af7b2014-07-31 11:39:16 +01002840 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 try {
2842 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002843 launcherApps.showAppDetailsForProfile(componentName, user);
2844 } catch (SecurityException e) {
2845 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2846 Log.e(TAG, "Launcher does not have permission to launch settings");
2847 } catch (ActivityNotFoundException e) {
2848 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2849 Log.e(TAG, "Unable to launch settings");
2850 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002851 }
2852
Michael Jurka1e2f4652013-07-08 18:03:46 -07002853 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002854 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2855 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002856 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002857 // System applications cannot be installed. For now, show a toast explaining that.
2858 // We may give them the option of disabling apps this way.
2859 int messageId = R.string.uninstall_system_app_text;
2860 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002861 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002862 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002863 String packageName = componentName.getPackageName();
2864 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002865 Intent intent = new Intent(
2866 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002867 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2868 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002869 if (user != null) {
2870 user.addToIntent(intent, Intent.EXTRA_USER);
2871 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002875 }
2876
Winson Chung8f1eff72015-05-28 17:33:40 -07002877 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002878 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002880 // Only launch using the new animation if the shortcut has not opted out (this is a
2881 // private contract between launcher and may be ignored in the future).
2882 boolean useLaunchAnimation = (v != null) &&
2883 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002884 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2885 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002886
Kenny Guy1317e2d2014-05-08 18:52:50 +01002887 UserHandleCompat user = null;
2888 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2889 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2890 user = userManager.getUserForSerialNumber(serialNumber);
2891 }
2892
2893 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002894 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002895 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002896 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002897 int left = 0, top = 0;
2898 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2899 if (v instanceof TextView) {
2900 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002901 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2902 if (icon != null) {
2903 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002904 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002905 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002906 width = bounds.width();
2907 height = bounds.height();
2908 }
2909 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002910 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2911 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002912 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002913 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002914 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002915 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002916 // On L devices, we use the device default slide-up transition.
2917 // On L MR1 devices, we a custom version of the slide-up transition which
2918 // doesn't have the delay present in the device default.
2919 opts = ActivityOptions.makeCustomAnimation(this,
2920 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002921 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002922 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002923 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002924
2925 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2926 // Could be launching some bookkeeping activity
2927 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002928 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002929 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002930 launcherApps.startActivityForProfile(intent.getComponent(), user,
2931 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002932 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002933 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002935 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2936 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2937 // corresponding permission. Show the appropriate permission prompt if that
2938 // is the case.
2939 if (intent.getComponent() == null
2940 && Intent.ACTION_CALL.equals(intent.getAction())
2941 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2942 PackageManager.PERMISSION_GRANTED) {
2943 // TODO: Rename sPendingAddItem to a generic name.
2944 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2945 0, (ItemInfo) tag);
2946 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2947 REQUEST_PERMISSION_CALL_PHONE);
2948 return false;
2949 }
2950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002952 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002954 "or use the exported attribute for this activity. "
2955 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002957 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002959
Winson Chungbedf9232015-07-10 12:38:30 -07002960 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002961 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002962 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2963 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2964 return false;
2965 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002966 try {
2967 success = startActivity(v, intent, tag);
2968 } catch (ActivityNotFoundException e) {
2969 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2970 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2971 }
2972 return success;
2973 }
2974
Adam Cohen268c4752012-06-06 17:47:33 -07002975 /**
2976 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2977 * in the DragLayer in the exact absolute location of the original FolderIcon.
2978 */
2979 private void copyFolderIconToImage(FolderIcon fi) {
2980 final int width = fi.getMeasuredWidth();
2981 final int height = fi.getMeasuredHeight();
2982
2983 // Lazy load ImageView, Bitmap and Canvas
2984 if (mFolderIconImageView == null) {
2985 mFolderIconImageView = new ImageView(this);
2986 }
2987 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2988 mFolderIconBitmap.getHeight() != height) {
2989 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2990 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2991 }
2992
2993 DragLayer.LayoutParams lp;
2994 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2995 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2996 } else {
2997 lp = new DragLayer.LayoutParams(width, height);
2998 }
2999
Adam Cohen307fe232012-08-16 17:55:58 -07003000 // The layout from which the folder is being opened may be scaled, adjust the starting
3001 // view size by this scale factor.
3002 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003003 lp.customPosition = true;
3004 lp.x = mRectForFolderAnimation.left;
3005 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003006 lp.width = (int) (scale * width);
3007 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003008
3009 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3010 fi.draw(mFolderIconCanvas);
3011 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003012 if (fi.getFolder() != null) {
3013 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3014 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003015 }
Adam Cohen268c4752012-06-06 17:47:33 -07003016 // Just in case this image view is still in the drag layer from a previous animation,
3017 // we remove it and re-add it.
3018 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3019 mDragLayer.removeView(mFolderIconImageView);
3020 }
3021 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 if (fi.getFolder() != null) {
3023 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003024 }
Adam Cohen268c4752012-06-06 17:47:33 -07003025 }
3026
Sunny Goyal08442b82015-10-21 17:15:08 -07003027 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003028 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003029 FolderInfo info = (FolderInfo) fi.getTag();
3030 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3031 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003032 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3033 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003034 }
3035
Adam Cohen268c4752012-06-06 17:47:33 -07003036 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3037 copyFolderIconToImage(fi);
3038 fi.setVisibility(View.INVISIBLE);
3039
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003040 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3041 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003042 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003043 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3044 }
3045 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003047 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003048 oa.end();
3049 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 }
3051
Sunny Goyal935fca12015-10-13 10:19:01 -07003052 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003053 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003054 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003055
Adam Cohen268c4752012-06-06 17:47:33 -07003056 // We remove and re-draw the FolderIcon in-case it has changed
3057 mDragLayer.removeView(mFolderIconImageView);
3058 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003059
3060 if (cl != null) {
3061 cl.clearFolderLeaveBehind();
3062 }
3063
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003064 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003065 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003066 oa.addListener(new AnimatorListenerAdapter() {
3067 @Override
3068 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003069 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003070 // Remove the ImageView copy of the FolderIcon and make the original visible.
3071 mDragLayer.removeView(mFolderIconImageView);
3072 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003073 }
3074 }
3075 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003076 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 if (!animate) {
3078 oa.end();
3079 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003080 }
3081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003083 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 * is animated relative to the specified View. If the View is null, no animation
3085 * is played.
3086 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003087 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003089 public void openFolder(FolderIcon folderIcon, boolean animate) {
3090 animate &= !Utilities.isPowerSaverOn(this);
3091
Adam Cohenfb91f302012-06-11 15:45:18 -07003092 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003093 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3094 if (openFolder != null && openFolder != folder) {
3095 // Close any open folder before opening a folder.
3096 closeFolder();
3097 }
3098
Adam Cohena9cf38f2011-05-02 15:36:58 -07003099 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100
Adam Cohena9cf38f2011-05-02 15:36:58 -07003101 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102
Sunny Goyalf4066152015-04-15 09:42:19 -07003103 // While the folder is open, the position of the icon cannot change.
3104 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3105
Adam Cohen4554ee12011-08-03 16:13:21 -07003106 // Just verify that the folder hasn't already been added to the DragLayer.
3107 // There was a one-off crash where the folder had a parent already.
3108 if (folder.getParent() == null) {
3109 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003110 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003111 } else {
3112 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3113 folder.getParent() + ").");
3114 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003115 if (animate) {
3116 folder.animateOpen();
3117 } else {
3118 folder.open();
3119 }
3120 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003121
3122 // Notify the accessibility manager that this folder "window" has appeared and occluded
3123 // the workspace items
3124 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3125 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 }
3127
3128 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003129 closeFolder(true);
3130 }
3131
3132 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003133 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003134 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003135 if (folder.isEditingName()) {
3136 folder.dismissEditingName();
3137 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003138 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003139 }
3140 }
3141
Sunny Goyal935fca12015-10-13 10:19:01 -07003142 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003143 animate &= !Utilities.isPowerSaverOn(this);
3144
Adam Cohen4554ee12011-08-03 16:13:21 -07003145 folder.getInfo().opened = false;
3146
3147 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3148 if (parent != null) {
3149 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003150 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003151 if (fi != null) {
3152 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3153 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003154 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003155 if (animate) {
3156 folder.animateClosed();
3157 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003158 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003159 }
Winson Chung83ca4802013-04-12 15:10:52 -07003160
Sunny Goyal935fca12015-10-13 10:19:01 -07003161 // Notify the accessibility manager that this folder "window" has disappeared and no
3162 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003163 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165
Tony Wickhamdadb3042016-02-24 11:07:00 -08003166 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003168 if (!isDraggingEnabled()) return false;
3169 if (isWorkspaceLocked()) return false;
3170 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171
Winson Chung76648c52015-07-10 14:33:23 -07003172 if (v == mAllAppsButton) {
3173 onLongClickAllAppsButton(v);
3174 return true;
3175 }
3176
Adam Cohen1697b792013-09-17 19:08:21 -07003177 if (v instanceof Workspace) {
3178 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003179 if (!mWorkspace.isTouchActive()) {
3180 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003181 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3182 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3183 return true;
3184 } else {
3185 return false;
3186 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003187 } else {
3188 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003189 }
Adam Cohen1697b792013-09-17 19:08:21 -07003190 }
3191
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003192 CellLayout.CellInfo longClickCellInfo = null;
3193 View itemUnderLongClick = null;
3194 if (v.getTag() instanceof ItemInfo) {
3195 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003196 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003197 itemUnderLongClick = longClickCellInfo.cell;
3198 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 }
3200
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 // The hotseat touch handling does not go through Workspace, and we always allow long press
3202 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003203 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003204 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003205 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003206 // User long pressed on empty space
3207 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3208 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003209 if (mWorkspace.isInOverviewMode()) {
3210 mWorkspace.startReordering(v);
3211 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003212 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003215 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3216 mHotseat.getOrderInHotseat(
3217 longClickCellInfo.cellX,
3218 longClickCellInfo.cellY));
3219 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003221 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 }
3223 }
3224 }
3225 return true;
3226 }
3227
Winson Chung3d503fb2011-07-13 17:25:49 -07003228 boolean isHotseatLayout(View layout) {
3229 return mHotseat != null && layout != null &&
3230 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3231 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003232
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 /**
3234 * Returns the CellLayout of the specified container at the specified screen.
3235 */
Sunny Goyal92820592015-03-02 11:31:35 -08003236 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3238 if (mHotseat != null) {
3239 return mHotseat.getLayout();
3240 } else {
3241 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003242 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003243 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003244 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003245 }
3246 }
3247
Winson Chungb745afb2015-03-02 11:51:23 -08003248 /**
3249 * For overridden classes.
3250 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003251 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003252 return isAppsViewVisible();
3253 }
3254
3255 public boolean isAppsViewVisible() {
3256 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3257 }
3258
3259 public boolean isWidgetsViewVisible() {
3260 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003261 }
3262
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003263 private void setWorkspaceBackground(int background) {
3264 switch (background) {
3265 case WORKSPACE_BACKGROUND_TRANSPARENT:
3266 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3267 break;
3268 case WORKSPACE_BACKGROUND_BLACK:
3269 getWindow().setBackgroundDrawable(null);
3270 break;
3271 default:
3272 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3273 }
Craig Mautner360310b2012-10-26 15:13:08 -07003274 }
3275
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003276 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003277 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3278 int curflags = getWindow().getAttributes().flags
3279 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3280 if (wpflags != curflags) {
3281 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3282 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003283 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003284 }
3285
Michael Jurkae326f182011-11-21 14:05:46 -08003286 @Override
3287 public void onTrimMemory(int level) {
3288 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003289 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3290 // The widget preview db can result in holding onto over
3291 // 3MB of memory for caching which isn't necessary.
3292 SQLiteDatabase.releaseMemory();
3293
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003294 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003295 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003296 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003297 if (mLauncherCallbacks != null) {
3298 mLauncherCallbacks.onTrimMemory(level);
3299 }
Michael Jurkae326f182011-11-21 14:05:46 -08003300 }
3301
Winson5c6bdbb2015-09-03 11:36:19 -07003302 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003303 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003304 }
3305
Winson5c6bdbb2015-09-03 11:36:19 -07003306 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003307 boolean changed = mState != State.WORKSPACE ||
3308 mWorkspace.getState() != Workspace.State.NORMAL;
3309 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003310 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003311 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003312 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003313
Michael Jurkab3e22d92011-10-31 15:58:33 -07003314 // Set focus to the AppsCustomize button
3315 if (mAllAppsButton != null) {
3316 mAllAppsButton.requestFocus();
3317 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003318 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003319
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003320 // Change the state *after* we've called all the transition code
3321 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003322
Winson Chung5fb63472011-02-02 17:03:37 -08003323 // Resume the auto-advance of widgets
3324 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003325 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003326
Winson Chung0f785722015-04-08 10:27:49 -07003327 if (changed) {
3328 // Send an accessibility event to announce the context change
3329 getWindow().getDecorView()
3330 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003331 }
Winson5c6bdbb2015-09-03 11:36:19 -07003332 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003333 }
3334
Winsone9f27272015-10-13 10:47:51 -07003335 /**
3336 * Shows the overview button.
3337 */
Adam Cohened307df2013-10-02 09:37:31 -07003338 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003339 showOverviewMode(animated, false);
3340 }
3341
3342 /**
3343 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3344 * onto one of the overview panel buttons.
3345 */
3346 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3347 Runnable postAnimRunnable = null;
3348 if (requestButtonFocus) {
3349 postAnimRunnable = new Runnable() {
3350 @Override
3351 public void run() {
3352 // Hitting the menu button when in touch mode does not trigger touch mode to
3353 // be disabled, so if requested, force focus on one of the overview panel
3354 // buttons.
3355 mOverviewPanel.requestFocusFromTouch();
3356 }
3357 };
3358 }
Adam Cohened307df2013-10-02 09:37:31 -07003359 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003360 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003361 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003362 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003363 }
3364
Winson Chungb745afb2015-03-02 11:51:23 -08003365 /**
3366 * Shows the apps view.
3367 */
Winson Chung76648c52015-07-10 14:33:23 -07003368 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3369 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003370 if (resetListToTop) {
3371 mAppsView.scrollToTop();
3372 }
Winson Chung4ac30062015-05-08 17:34:17 -07003373 if (updatePredictedApps) {
3374 tryAndUpdatePredictedApps();
3375 }
Winson Chung76648c52015-07-10 14:33:23 -07003376 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003377 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378
Winson Chungb745afb2015-03-02 11:51:23 -08003379 /**
3380 * Shows the widgets view.
3381 */
3382 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003383 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003384 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003385 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003386 }
Winson Chung76648c52015-07-10 14:33:23 -07003387 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003388
3389 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003390 @Override
3391 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003393 }
3394 });
Winson Chungb745afb2015-03-02 11:51:23 -08003395 }
3396
3397 /**
3398 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003399 *
3400 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003401 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003402 // TODO: calling method should use the return value so that when {@code false} is returned
3403 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003404 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3406 mState != State.WIDGETS_SPRING_LOADED) {
3407 return false;
3408 }
3409 if (toState != State.APPS && toState != State.WIDGETS) {
3410 return false;
3411 }
Winson Chungb745afb2015-03-02 11:51:23 -08003412
3413 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003414 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3415 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003416 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003417 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003418 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003419
Michael Jurkab3e22d92011-10-31 15:58:33 -07003420 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003421 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003422
3423 // Pause the auto-advance of widgets until we are out of AllApps
3424 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003425 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003426 closeFolder();
3427
3428 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003429 getWindow().getDecorView()
3430 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003431 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003432 }
3433
Winson Chungcd99cd32015-04-29 11:03:24 -07003434 /**
3435 * Updates the workspace and interaction state on state change, and return the animation to this
3436 * new state.
3437 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003438 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003439 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003440 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003441 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003442 updateInteraction(fromState, toState);
3443 return anim;
3444 }
3445
Jun Mukaif0033da2015-08-04 18:34:30 -07003446 public void onLauncherClingShown() {
3447 // When a launcher cling appears, it should cover the underlying layers, so their focus
3448 // should be blocked.
3449 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3450 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3451 }
3452 }
3453
3454 public void onLauncherClingDismissed() {
3455 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3456 }
3457
Hyunyoung Song3f471442015-04-08 19:01:34 -07003458 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003459 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003460 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003461 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003462 }
Winson Chungb745afb2015-03-02 11:51:23 -08003463
Winson Chung006ee262015-08-03 14:40:11 -07003464 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003465 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003466 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003467
3468 if (isAppsViewVisible()) {
3469 mState = State.APPS_SPRING_LOADED;
3470 } else if (isWidgetsViewVisible()) {
3471 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003472 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003473 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003474 } else {
3475 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003476 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003477 }
Adam Cohen7777d962011-08-18 18:58:38 -07003478
Hyunyoung Song3f471442015-04-08 19:01:34 -07003479 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003480 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003481 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003482
Winson Chunge7a03942011-08-05 15:05:12 -07003483 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003484 @Override
3485 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003486 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003487 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3488 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003489 // Before we show workspace, hide all apps again because
3490 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3491 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003492 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003493 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003494 } else {
3495 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003496 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003497 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003498 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003499 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003500
Tony Wickhame0c33232016-02-08 11:37:04 -08003501 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003502 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3503 || mState == State.WIDGETS_SPRING_LOADED;
3504 }
3505
Michael Jurkad3ef3062010-11-23 16:23:58 -08003506 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003507 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003508 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003509 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003510 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003511 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003512 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3513 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003515 }
3516
Winson Chung4ac30062015-05-08 17:34:17 -07003517 /**
3518 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3519 * resumed.
3520 */
3521 private void tryAndUpdatePredictedApps() {
3522 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003523 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003524 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003525 mAppsView.setPredictedApps(apps);
3526 }
3527 }
3528 }
3529
Michael Jurkab3e22d92011-10-31 15:58:33 -07003530 void lockAllApps() {
3531 // TODO
3532 }
3533
3534 void unlockAllApps() {
3535 // TODO
3536 }
3537
Winsonf768d932015-09-25 16:12:35 -07003538 public boolean launcherCallbacksProvidesSearch() {
3539 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3540 }
3541
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003542 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003543 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003544 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003545 }
3546
Adam Cohen24ce0b32014-01-14 16:18:14 -08003547 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3549 if (searchProvider == null) {
3550 return null;
3551 }
3552
3553 Bundle opts = new Bundle();
3554 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003555 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003556
Tony Wickham3a3517f2015-10-06 11:27:04 -07003557 // Determine the min and max dimensions of the widget.
3558 LauncherAppState app = LauncherAppState.getInstance();
3559 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3560 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3561 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003562 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3563 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003564 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003565 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003566 int minWidth = maxWidth;
3567 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003568 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3569 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3570 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3571 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3572 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003573 }
3574 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3575 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3576 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3577 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003578 if (LOGD) {
3579 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3580 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3581 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003582
Tony Wickham775455c2015-10-16 09:49:32 -07003583 if (mLauncherCallbacks != null) {
3584 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3585 }
3586
Sunny Goyalf7258242015-10-19 16:59:07 -07003587 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003588 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003589 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003590 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003591 || (widgetInfo == null)
3592 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003593 // A valid widget is not already bound.
3594 if (widgetId > -1) {
3595 mAppWidgetHost.deleteAppWidgetId(widgetId);
3596 widgetId = -1;
3597 }
3598
3599 // Try to bind a new widget
3600 widgetId = mAppWidgetHost.allocateAppWidgetId();
3601
3602 if (!AppWidgetManagerCompat.getInstance(this)
3603 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3604 mAppWidgetHost.deleteAppWidgetId(widgetId);
3605 widgetId = -1;
3606 }
3607
Sunny Goyalf7258242015-10-19 16:59:07 -07003608 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003609 .putInt(QSB_WIDGET_ID, widgetId)
3610 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003611 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003612 }
3613
Sunny Goyal8d595092015-07-06 12:22:14 -07003614 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003615 if (widgetId != -1) {
3616 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003617 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003618 mQsb.updateAppWidgetOptions(opts);
3619 mQsb.setPadding(0, 0, 0, 0);
3620 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003621 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003622 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003623 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003624 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003625 }
3626
Sunny Goyal22235bc2015-04-03 09:23:43 -07003627 private void reinflateQSBIfNecessary() {
3628 if (mQsb instanceof LauncherAppWidgetHostView &&
3629 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3630 mSearchDropTargetBar.removeView(mQsb);
3631 mQsb = null;
3632 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3633 }
3634 }
3635
Michael Jurkad7c28052012-04-27 15:43:36 -07003636 @Override
3637 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003638 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003639 final List<CharSequence> text = event.getText();
3640 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003641 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003642 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003643 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003644 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003645 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003646 } else if (mWorkspace != null) {
3647 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003648 } else {
3649 text.add(getString(R.string.all_apps_home_button_label));
3650 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003651 return result;
3652 }
3653
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003654 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003655 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003656 */
Adam Cohen091440a2015-03-18 14:16:05 -07003657 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003658 @Override
3659 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003660 closeSystemDialogs();
3661 }
3662 }
3663
3664 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 * If the activity is currently paused, signal that we need to run the passed Runnable
3666 * in onResume.
3667 *
3668 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003669 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3670 * wrong when we're not running, and if the activity comes back to what the configuration was
3671 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 *
3673 * Implementation of the method from LauncherModel.Callbacks.
3674 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003675 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003677 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003678 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003679 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003681 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003682 }
3683 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 return true;
3686 } else {
3687 return false;
3688 }
3689 }
3690
Michael Jurkac402cd92013-05-20 15:49:32 +02003691 private boolean waitUntilResume(Runnable run) {
3692 return waitUntilResume(run, false);
3693 }
3694
Michael Jurka1e2f4652013-07-08 18:03:46 -07003695 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003696 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003697 }
3698
Michael Jurka7607c2f2013-04-03 14:33:19 -07003699 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003700 * If the activity is currently paused, signal that we need to re-run the loader
3701 * in onResume.
3702 *
3703 * This needs to be called from incoming places where resources might have been loaded
3704 * while we are paused. That is becaues the Configuration might be wrong
3705 * when we're not running, and if it comes back to what it was when we
3706 * were paused, we are not restarted.
3707 *
3708 * Implementation of the method from LauncherModel.Callbacks.
3709 *
3710 * @return true if we are currently paused. The caller might be able to
3711 * skip some work in that case since we will come back again.
3712 */
3713 public boolean setLoadOnResume() {
3714 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003715 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003716 mOnResumeNeedsLoad = true;
3717 return true;
3718 } else {
3719 return false;
3720 }
3721 }
3722
3723 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003725 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003727 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003728 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003729 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003730 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003733
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003735 * Clear any pending bind callbacks. This is called when is loader is planning to
3736 * perform a full rebind from scratch.
3737 */
3738 @Override
3739 public void clearPendingBinds() {
3740 mBindOnResumeCallbacks.clear();
3741 if (mPendingExecutor != null) {
3742 mPendingExecutor.markCompleted();
3743 mPendingExecutor = null;
3744 }
3745 }
3746
3747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 * Refreshes the shortcuts shown on the workspace.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
3751 */
3752 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003753 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003754
Winson Chungd64d1762013-08-20 14:37:16 -07003755 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003756 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003757 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003758
Michael Jurka05bf644e2011-11-30 20:28:53 -08003759 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003760 if (mHotseat != null) {
3761 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762 }
3763 }
3764
Adam Cohendcd297f2013-06-18 13:13:40 -07003765 @Override
3766 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003767 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003768
Adam Cohen5084cba2013-09-03 12:01:16 -07003769 // If there are no screens, we need to have an empty screen
3770 if (orderedScreenIds.size() == 0) {
3771 mWorkspace.addExtraEmptyScreen();
3772 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003773
3774 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003775 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003776 if (hasCustomContentToLeft()) {
3777 mWorkspace.createCustomContentContainer();
3778 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003779 }
Winson Chung64359a52013-07-08 17:17:08 -07003780 }
3781
3782 @Override
3783 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 int count = orderedScreenIds.size();
3785 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003786 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003787 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003788 }
3789
Winson Chungd64d1762013-08-20 14:37:16 -07003790 public void bindAppsAdded(final ArrayList<Long> newScreens,
3791 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003792 final ArrayList<ItemInfo> addAnimated,
3793 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003794 Runnable r = new Runnable() {
3795 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003796 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003797 }
3798 };
3799 if (waitUntilResume(r)) {
3800 return;
3801 }
3802
Winson Chungd64d1762013-08-20 14:37:16 -07003803 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003804 if (newScreens != null) {
3805 bindAddScreens(newScreens);
3806 }
Winson Chungd64d1762013-08-20 14:37:16 -07003807
3808 // We add the items without animation on non-visible pages, and with
3809 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003810 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003811 bindItems(addNotAnimated, 0,
3812 addNotAnimated.size(), false);
3813 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003814 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003815 bindItems(addAnimated, 0,
3816 addAnimated.size(), true);
3817 }
Winson Chungc58497e2013-09-03 17:48:37 -07003818
Winson Chung87412982013-10-03 18:34:14 -07003819 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003820 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003821
Winson Chungb745afb2015-03-02 11:51:23 -08003822 if (addedApps != null && mAppsView != null) {
3823 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003824 }
Winson Chungd64d1762013-08-20 14:37:16 -07003825 }
3826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003827 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003828 * Bind the items start-end from the list.
3829 *
3830 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003831 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003832 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003833 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3834 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003835 Runnable r = new Runnable() {
3836 public void run() {
3837 bindItems(shortcuts, start, end, forceAnimateIcons);
3838 }
3839 };
3840 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003841 return;
3842 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003843
Winson Chungf0c6ae02012-03-21 16:10:31 -07003844 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003845 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3846 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003847 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003848 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003849 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003850 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003852
3853 // Short circuit if we are loading dock items for a configuration which has no dock
3854 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3855 mHotseat == null) {
3856 continue;
3857 }
3858
Sunny Goyal639e9062015-08-19 19:17:06 -07003859 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 switch (item.itemType) {
3861 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3862 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003864 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003865
Winson Chung64359a52013-07-08 17:17:08 -07003866 /*
3867 * TODO: FIX collision case
3868 */
Winson Chungce376632013-07-11 16:12:41 -07003869 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3870 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3871 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003872 View v = cl.getChildAt(item.cellX, item.cellY);
3873 Object tag = v.getTag();
3874 String desc = "Collision while binding workspace item: " + item
3875 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003876 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003877 throw (new RuntimeException(desc));
3878 } else {
3879 Log.d(TAG, desc);
3880 }
Winson Chungce376632013-07-11 16:12:41 -07003881 }
Winson Chung64359a52013-07-08 17:17:08 -07003882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003883 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003884 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003885 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003886 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003887 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003889 default:
3890 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003891 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003892
3893 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3894 item.cellY, 1, 1);
3895 if (animateIcons) {
3896 // Animate all the applications up now
3897 view.setAlpha(0f);
3898 view.setScaleX(0f);
3899 view.setScaleY(0f);
3900 bounceAnims.add(createNewAppBounceAnimation(view, i));
3901 newShortcutsScreenId = item.screenId;
3902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003903 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003904
Winson Chung997a9232013-07-24 15:33:46 -07003905 if (animateIcons) {
3906 // Animate to the correct page
3907 if (newShortcutsScreenId > -1) {
3908 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003909 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003910 final Runnable startBounceAnimRunnable = new Runnable() {
3911 public void run() {
3912 anim.playTogether(bounceAnims);
3913 anim.start();
3914 }
3915 };
Winson Chung997a9232013-07-24 15:33:46 -07003916 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003917 // We post the animation slightly delayed to prevent slowdowns
3918 // when we are loading right after we return to launcher.
3919 mWorkspace.postDelayed(new Runnable() {
3920 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003921 if (mWorkspace != null) {
3922 mWorkspace.snapToPage(newScreenIndex);
3923 mWorkspace.postDelayed(startBounceAnimRunnable,
3924 NEW_APPS_ANIMATION_DELAY);
3925 }
Winson Chung94d67682013-09-25 16:29:40 -07003926 }
3927 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003928 } else {
3929 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003930 }
3931 }
Winson Chung64359a52013-07-08 17:17:08 -07003932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003934 }
3935
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 /**
3937 * Implementation of the method from LauncherModel.Callbacks.
3938 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003939 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003940 Runnable r = new Runnable() {
3941 public void run() {
3942 bindFolders(folders);
3943 }
3944 };
3945 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003946 return;
3947 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003948 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003951 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3952 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3953 view.updateIcon(mIconCache);
3954 item.hostView = view;
3955 item.hostView.updateAppWidget(null);
3956 item.hostView.setOnClickListener(this);
3957 addAppWidgetToWorkspace(item, null, false);
3958 mWorkspace.requestLayout();
3959 }
3960
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 /**
3962 * Add the views for a widget to the workspace.
3963 *
3964 * Implementation of the method from LauncherModel.Callbacks.
3965 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003966 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003967 Runnable r = new Runnable() {
3968 public void run() {
3969 bindAppWidget(item);
3970 }
3971 };
3972 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003973 return;
3974 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003975
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003976 if (mIsSafeModeEnabled) {
3977 bindSafeModeWidget(item);
3978 return;
3979 }
3980
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
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003986 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003987
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003988 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3989 // If the provider is not ready, bind as a pending widget.
3990 appWidgetInfo = null;
3991 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3992 // The widget id is not valid. Try to find the widget based on the provider info.
3993 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3994 } else {
3995 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3996 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003997
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003998 // If the provider is ready, but the width is not yet restored, try to restore it.
3999 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
4000 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004001 if (appWidgetInfo == null) {
4002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4004 + " belongs to component " + item.providerName
4005 + ", as the povider is null");
4006 }
4007 LauncherModel.deleteItemFromDatabase(this, item);
4008 return;
4009 }
Sunny Goyalff572272014-07-23 13:58:07 -07004010
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004011 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004012 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004013 // Note: This assumes that the id remap broadcast is received before this step.
4014 // If that is not the case, the id remap will be ignored and user may see the
4015 // click to setup view.
4016 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
4017 pendingInfo.spanX = item.spanX;
4018 pendingInfo.spanY = item.spanY;
4019 pendingInfo.minSpanX = item.minSpanX;
4020 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004021 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004022
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004023 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4024 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4025 newWidgetId, appWidgetInfo, options);
4026
4027 // TODO consider showing a permission dialog when the widget is clicked.
4028 if (!success) {
4029 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4030 if (DEBUG_WIDGETS) {
4031 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4032 + " belongs to component " + item.providerName
4033 + ", as the launcher is unable to bing a new widget id");
4034 }
4035 LauncherModel.deleteItemFromDatabase(this, item);
4036 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004037 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004038
4039 item.appWidgetId = newWidgetId;
4040
4041 // If the widget has a configure activity, it is still needs to set it up, otherwise
4042 // the widget is ready to go.
4043 item.restoreStatus = (appWidgetInfo.configure == null)
4044 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4045 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4046
4047 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004048 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004049 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004050 // The widget was marked as UI not ready, but there is no configure activity to
4051 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004052 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4053 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004054 }
Sunny Goyalff572272014-07-23 13:58:07 -07004055 }
4056
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004057 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004059 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4060 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004061 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004062
Sunny Goyal56c73602015-09-25 12:55:01 -07004063 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004064 if (appWidgetInfo == null) {
4065 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004066 deleteWidgetInfo(item);
4067 return;
4068 }
4069
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004070 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004071 item.minSpanX = appWidgetInfo.minSpanX;
4072 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004073 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004074 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004075 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4076 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004077 view.updateIcon(mIconCache);
4078 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004079 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004080 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004081 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004082 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004083 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084
Daniel Sandler843e8602010-06-07 14:59:01 -04004085 if (DEBUG_WIDGETS) {
4086 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4087 + (SystemClock.uptimeMillis()-start) + "ms");
4088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 }
4090
Sunny Goyalff572272014-07-23 13:58:07 -07004091 /**
4092 * Restores a pending widget.
4093 *
4094 * @param appWidgetId The app widget id
4095 * @param cellInfo The position on screen where to create the widget.
4096 */
4097 private void completeRestoreAppWidget(final int appWidgetId) {
4098 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4099 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4100 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4101 return;
4102 }
4103
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004104 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004105 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4106
4107 mWorkspace.reinflateWidgetsIfNecessary();
4108 LauncherModel.updateItemInDatabase(this, info);
4109 }
4110
Adam Cohen1462de32012-07-24 22:34:36 -07004111 public void onPageBoundSynchronously(int page) {
4112 mSynchronouslyBoundPages.add(page);
4113 }
4114
Sunny Goyal527c7d32015-08-28 15:19:36 -07004115 @Override
4116 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4117 if (mPendingExecutor != null) {
4118 mPendingExecutor.markCompleted();
4119 }
4120 mPendingExecutor = executor;
4121 executor.attachTo(this);
4122 }
4123
4124 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4125 if (mPendingExecutor == executor) {
4126 mPendingExecutor = null;
4127 }
4128 }
4129
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 /**
4131 * Callback saying that there aren't any more items to bind.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004135 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004136 Runnable r = new Runnable() {
4137 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004138 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004139 }
4140 };
4141 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004142 return;
4143 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 if (mSavedState != null) {
4145 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004146 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004148
Joe Onorato9c1289c2009-08-17 11:03:03 -04004149 mSavedState = null;
4150 }
4151
Adam Cohen1462de32012-07-24 22:34:36 -07004152 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004153
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004154 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004155 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004156
4157 // If we received the result of any pending adds while the loader was running (e.g. the
4158 // widget configuration forced an orientation change), process them now.
4159 if (sPendingAddItem != null) {
4160 final long screenId = completeAdd(sPendingAddItem);
4161
4162 // TODO: this moves the user to the page where the pending item was added. Ideally,
4163 // the screen would be guaranteed to exist after bind, and the page would be set through
4164 // the workspace restore process.
4165 mWorkspace.post(new Runnable() {
4166 @Override
4167 public void run() {
4168 mWorkspace.snapToScreenId(screenId);
4169 }
4170 });
4171 sPendingAddItem = null;
4172 }
4173
Sunny Goyal756adbc2015-04-16 15:20:51 -07004174 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004175
4176 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004177 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004178 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004179 }
4180
Adam Cohen3ed316a2014-07-23 18:21:20 -07004181 private void sendLoadingCompleteBroadcastIfNecessary() {
4182 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4183 String permission =
4184 getResources().getString(R.string.receive_first_load_broadcast_permission);
4185 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4186 sendBroadcast(intent, permission);
4187 SharedPreferences.Editor editor = mSharedPrefs.edit();
4188 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4189 editor.apply();
4190 }
4191 }
4192
Winson Chunga0b7e862013-09-05 16:03:15 -07004193 public boolean isAllAppsButtonRank(int rank) {
4194 if (mHotseat != null) {
4195 return mHotseat.isAllAppsButtonRank(rank);
4196 }
4197 return false;
4198 }
4199
Winson Chunga2413752012-04-03 14:22:34 -07004200 private boolean canRunNewAppsAnimation() {
4201 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004202 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4203 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004204 }
4205
Winson Chungc9168342013-06-26 14:54:55 -07004206 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004207 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004208 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4209 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004210 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004211 return bounceAnim;
4212 }
4213
Adam Cohen27772732013-11-11 14:00:56 +00004214 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004215 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004216 }
4217
Tony Wickham3a3517f2015-10-06 11:27:04 -07004218 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004219 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004220 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004221 }
4222
Tony Wickham55616cd2015-09-23 14:55:17 -07004223 public int getSearchBarHeight() {
4224 if (mLauncherCallbacks != null) {
4225 return mLauncherCallbacks.getSearchBarHeight();
4226 }
4227 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4228 }
4229
Winson Chung88fa7412015-08-03 14:25:28 -07004230 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004231 if (mSearchDropTargetBar == null) {
4232 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004233 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004234 if (mQsb != null) {
4235 mSearchDropTargetBar.removeView(mQsb);
4236 mQsb = null;
4237 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004238 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004239 }
4240
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004241 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004242 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4243 * multiple calls to bind the same list.)
4244 */
4245 @Thunk ArrayList<AppInfo> mTmpAppsList;
4246 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4247 public void run() {
4248 bindAllApplications(mTmpAppsList);
4249 mTmpAppsList = null;
4250 }
4251 };
4252
4253 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004254 * Add the icons for all apps.
4255 *
4256 * Implementation of the method from LauncherModel.Callbacks.
4257 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004258 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004259 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4260 mTmpAppsList = apps;
4261 return;
4262 }
4263
Winson Chungb745afb2015-03-02 11:51:23 -08004264 if (mAppsView != null) {
4265 mAppsView.setApps(apps);
4266 }
Adam Cohen9211d422014-10-07 18:14:53 -07004267 if (mLauncherCallbacks != null) {
4268 mLauncherCallbacks.bindAllApplications(apps);
4269 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004270 }
4271
4272 /**
4273 * A package was updated.
4274 *
4275 * Implementation of the method from LauncherModel.Callbacks.
4276 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004277 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004278 Runnable r = new Runnable() {
4279 public void run() {
4280 bindAppsUpdated(apps);
4281 }
4282 };
4283 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004284 return;
4285 }
4286
Winson Chungb745afb2015-03-02 11:51:23 -08004287 if (mAppsView != null) {
4288 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004289 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004290 }
4291
Sunny Goyal4390ace2014-10-13 11:33:11 -07004292 @Override
4293 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4294 Runnable r = new Runnable() {
4295 public void run() {
4296 bindWidgetsRestored(widgets);
4297 }
4298 };
4299 if (waitUntilResume(r)) {
4300 return;
4301 }
4302 mWorkspace.widgetsRestored(widgets);
4303 }
4304
Joe Onorato9c1289c2009-08-17 11:03:03 -04004305 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004306 * Some shortcuts were updated in the background.
4307 *
4308 * Implementation of the method from LauncherModel.Callbacks.
4309 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004310 @Override
4311 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4312 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004313 Runnable r = new Runnable() {
4314 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004315 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004316 }
4317 };
4318 if (waitUntilResume(r)) {
4319 return;
4320 }
4321
Sunny Goyal4390ace2014-10-13 11:33:11 -07004322 if (!updated.isEmpty()) {
4323 mWorkspace.updateShortcuts(updated);
4324 }
4325
4326 if (!removed.isEmpty()) {
4327 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4328 for (ShortcutInfo si : removed) {
4329 removedComponents.add(si.getTargetComponent());
4330 }
4331 mWorkspace.removeItemsByComponentName(removedComponents, user);
4332 // Notify the drag controller
4333 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004334 }
4335 }
4336
4337 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004338 * Update the state of a package, typically related to install state.
4339 *
4340 * Implementation of the method from LauncherModel.Callbacks.
4341 */
Sunny Goyale755d462014-07-22 13:48:29 -07004342 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004343 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4344 Runnable r = new Runnable() {
4345 public void run() {
4346 bindRestoreItemsChange(updates);
4347 }
4348 };
4349 if (waitUntilResume(r)) {
4350 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004351 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004352
Sunny Goyal756adbc2015-04-16 15:20:51 -07004353 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004354 }
4355
4356 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004357 * A package was uninstalled. We take both the super set of packageNames
4358 * in addition to specific applications to remove, the reason being that
4359 * this can be called when a package is updated as well. In that scenario,
4360 * we only remove specific components from the workspace, where as
4361 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004362 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004363 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004364 * Implementation of the method from LauncherModel.Callbacks.
4365 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004366 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004367 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004368 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004369 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004370 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004371 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004372 }
Winson Chungd64d1762013-08-20 14:37:16 -07004373 };
4374 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004375 return;
4376 }
4377
Sunny Goyal1a745e82014-10-02 15:58:31 -07004378 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004379 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4380 for (AppInfo info : appInfos) {
4381 removedComponents.add(info.componentName);
4382 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004383 if (!packageNames.isEmpty()) {
4384 mWorkspace.removeItemsByPackageName(packageNames, user);
4385 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004386 if (!removedComponents.isEmpty()) {
4387 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004388 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004389 // Notify the drag controller
4390 mDragController.onAppsRemoved(packageNames, removedComponents);
4391
Sunny Goyal1a745e82014-10-02 15:58:31 -07004392 } else {
4393 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004394 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004395
Winson Chungdf95eb12013-10-16 14:57:07 -07004396 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004397 if (mAppsView != null) {
4398 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004400 }
Joe Onoratobe386092009-11-17 17:32:16 -08004401
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004402 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004403 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004404 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004405 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004406 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004407
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004408 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004409 public void bindWidgetsModel(WidgetsModel model) {
4410 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004411 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004412 return;
4413 }
4414
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004415 if (mWidgetsView != null && model != null) {
4416 mWidgetsView.addWidgets(model);
4417 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004418 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004419 }
4420
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004421 @Override
4422 public void notifyWidgetProvidersChanged() {
4423 if (mWorkspace.getState().shouldUpdateWidget) {
4424 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4425 }
4426 }
4427
Winson Chung400438b2011-01-16 17:53:48 -08004428 private int mapConfigurationOriActivityInfoOri(int configOri) {
4429 final Display d = getWindowManager().getDefaultDisplay();
4430 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4431 switch (d.getRotation()) {
4432 case Surface.ROTATION_0:
4433 case Surface.ROTATION_180:
4434 // We are currently in the same basic orientation as the natural orientation
4435 naturalOri = configOri;
4436 break;
4437 case Surface.ROTATION_90:
4438 case Surface.ROTATION_270:
4439 // We are currently in the other basic orientation to the natural orientation
4440 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4441 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4442 break;
4443 }
4444
4445 int[] oriMap = {
4446 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4447 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4448 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4449 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4450 };
4451 // Since the map starts at portrait, we need to offset if this device's natural orientation
4452 // is landscape.
4453 int indexOffset = 0;
4454 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4455 indexOffset = 1;
4456 }
4457 return oriMap[(d.getRotation() + indexOffset) % 4];
4458 }
Adam Cohen446e9402011-09-15 18:21:21 -07004459
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004460 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004461 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004462 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004463 if (Utilities.ATLEAST_JB_MR2) {
4464 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4465 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004466 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4467 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004468 }
Winson Chung4b919f82012-05-01 10:44:08 -07004469 }
4470 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004471
Winson Chung4b919f82012-05-01 10:44:08 -07004472 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004473 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004474 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004475 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004476 } else {
4477 mHandler.postDelayed(new Runnable() {
4478 public void run() {
4479 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4480 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004481 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004482 }
Winson Chung4b919f82012-05-01 10:44:08 -07004483 }
Winson Chung400438b2011-01-16 17:53:48 -08004484 }
4485
Winson Chunge43a1e72014-01-15 10:33:02 -08004486 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004487 if (mLauncherCallbacks != null) {
4488 return mLauncherCallbacks.isLauncherPreinstalled();
4489 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004490 PackageManager pm = getPackageManager();
4491 try {
4492 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4493 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4494 return true;
4495 } else {
4496 return false;
4497 }
4498 } catch (NameNotFoundException e) {
4499 e.printStackTrace();
4500 return false;
4501 }
4502 }
4503
Adam Cohenea90f832014-05-21 15:03:34 -07004504 /**
4505 * This method indicates whether or not we should suggest default wallpaper dimensions
4506 * when our wallpaper cropper was not yet used to set a wallpaper.
4507 */
4508 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004509 if (mLauncherCallbacks != null) {
4510 return mLauncherCallbacks.overrideWallpaperDimensions();
4511 }
Adam Cohenea90f832014-05-21 15:03:34 -07004512 return true;
4513 }
4514
Adam Cohen432609a2014-03-13 17:03:22 -07004515 /**
4516 * To be overridden by subclasses to indicate that there is an activity to launch
4517 * before showing the standard launcher experience.
4518 */
4519 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004520 if (mLauncherCallbacks != null) {
4521 return mLauncherCallbacks.hasFirstRunActivity();
4522 }
Adam Cohen432609a2014-03-13 17:03:22 -07004523 return false;
4524 }
4525
4526 /**
4527 * To be overridden by subclasses to launch any first run activity
4528 */
4529 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004530 if (mLauncherCallbacks != null) {
4531 return mLauncherCallbacks.getFirstRunActivity();
4532 }
Adam Cohen432609a2014-03-13 17:03:22 -07004533 return null;
4534 }
4535
Winson Chunga6945242014-01-08 14:04:34 -08004536 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004537 return !ActivityManager.isRunningInTestHarness() &&
4538 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004539 }
4540
Adam Cohen4a9423d2014-03-28 14:22:31 -07004541 protected boolean hasRunFirstRunActivity() {
4542 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4543 }
4544
Adam Cohen432609a2014-03-13 17:03:22 -07004545 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004546 if (shouldRunFirstRunActivity() &&
4547 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004548 Intent firstRunIntent = getFirstRunActivity();
4549 if (firstRunIntent != null) {
4550 startActivity(firstRunIntent);
4551 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004552 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004553 }
4554 }
Adam Cohen432609a2014-03-13 17:03:22 -07004555 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004556 }
4557
4558 private void markFirstRunActivityShown() {
4559 SharedPreferences.Editor editor = mSharedPrefs.edit();
4560 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4561 editor.apply();
4562 }
4563
Adam Cohen432609a2014-03-13 17:03:22 -07004564 /**
4565 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4566 * screen that must be displayed and dismissed.
4567 */
4568 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004569 if (mLauncherCallbacks != null) {
4570 return mLauncherCallbacks.hasDismissableIntroScreen();
4571 }
Adam Cohen432609a2014-03-13 17:03:22 -07004572 return false;
4573 }
4574
4575 /**
4576 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4577 */
4578 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004579 if (mLauncherCallbacks != null) {
4580 return mLauncherCallbacks.getIntroScreen();
4581 }
Adam Cohen432609a2014-03-13 17:03:22 -07004582 return null;
4583 }
4584
4585 /**
4586 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4587 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4588 */
4589 private boolean shouldShowIntroScreen() {
4590 return hasDismissableIntroScreen() &&
4591 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4592 }
4593
4594 protected void showIntroScreen() {
4595 View introScreen = getIntroScreen();
4596 changeWallpaperVisiblity(false);
4597 if (introScreen != null) {
4598 mDragLayer.showOverlayView(introScreen);
4599 }
4600 }
4601
4602 public void dismissIntroScreen() {
4603 markIntroScreenDismissed();
4604 if (showFirstRunActivity()) {
4605 // We delay hiding the intro view until the first run activity is showing. This
4606 // avoids a blip.
4607 mWorkspace.postDelayed(new Runnable() {
4608 @Override
4609 public void run() {
4610 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004611 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004612 }
4613 }, ACTIVITY_START_DELAY);
4614 } else {
4615 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004616 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004617 }
4618 changeWallpaperVisiblity(true);
4619 }
4620
4621 private void markIntroScreenDismissed() {
4622 SharedPreferences.Editor editor = mSharedPrefs.edit();
4623 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4624 editor.apply();
4625 }
4626
Adam Cohen091440a2015-03-18 14:16:05 -07004627 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004628 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4629 // on the device, then we always show the first run cling experience (or if there is no
4630 // launcher2). Otherwise, we prompt the user upon started for migration
4631 LauncherClings launcherClings = new LauncherClings(this);
4632 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004633 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004634 if (mModel.canMigrateFromOldLauncherDb(this)) {
4635 launcherClings.showMigrationCling();
4636 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004637 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004638 }
4639 }
4640 }
4641
Winson Chunga6945242014-01-08 14:04:34 -08004642 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004643 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4644 if (mHotseat != null) mHotseat.setAlpha(1f);
4645 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004646 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4647 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004648 }
4649
4650 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004651 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4652 if (mHotseat != null) mHotseat.setAlpha(0f);
4653 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004654 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4655 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004656 }
4657
Winson Chung5ffd51d2015-06-25 11:36:50 -07004658 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004659 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004661 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004662 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004663 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004664 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4665 if (userHandle != null) {
4666 user = UserHandleCompat.fromUser(userHandle);
4667 }
4668 }
4669 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004670 }
4671
4672 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004673 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004674 if (user == null) {
4675 user = UserHandleCompat.myUserHandle();
4676 }
4677
4678 // Called from search suggestion, add the profile extra to the intent to ensure that we
4679 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004680 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004681 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004682 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004683 return null;
4684 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004685 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004686 }
4687
Winson Chung5ffd51d2015-06-25 11:36:50 -07004688 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004689 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4690 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004691 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004692 UserHandleCompat.myUserHandle());
4693 }
4694
Winson Chung5ffd51d2015-06-25 11:36:50 -07004695 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004696 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4697 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004698 mWorkspace.onExternalDragStartedWithItem(dragView);
4699 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004700 }
4701
Winson Chung5ffd51d2015-06-25 11:36:50 -07004702 protected void moveWorkspaceToDefaultScreen() {
4703 mWorkspace.moveToDefaultScreen(false);
4704 }
4705
Anjali Koppalf5d29132014-02-28 13:33:27 -08004706 @Override
4707 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004708 if (mLauncherCallbacks != null) {
4709 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4710 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004711 }
4712
Winson Chung80baf5a2010-08-09 16:03:15 -07004713 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004714 * Returns a FastBitmapDrawable with the icon, accurately sized.
4715 */
4716 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4717 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4718 d.setFilterBitmap(true);
4719 resizeIconDrawable(d);
4720 return d;
4721 }
4722
4723 /**
4724 * Resizes an icon drawable to the correct icon size.
4725 */
Winson5fbe0742015-09-30 15:33:00 -07004726 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004727 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004728 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004729 }
4730
4731 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004732 * Prints out out state for debugging.
4733 */
4734 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004735 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004736 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004737 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4738 Log.d(TAG, "mRestoring=" + mRestoring);
4739 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004740 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004741 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004742 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004743
Daniel Sandler325dc232013-06-05 22:57:57 -04004744 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004745 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004746
4747 @Override
4748 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4749 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004750 // Dump workspace
4751 writer.println(prefix + "Workspace Items");
4752 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4753 writer.println(prefix + " Homescreen " + i);
4754
4755 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4756 for (int j = 0; j < layout.getChildCount(); j++) {
4757 Object tag = layout.getChildAt(j).getTag();
4758 if (tag != null) {
4759 writer.println(prefix + " " + tag.toString());
4760 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004761 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004762 }
Sunny Goyala1365452015-10-01 15:46:24 -07004763
4764 writer.println(prefix + " Hotseat");
4765 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4766 for (int j = 0; j < layout.getChildCount(); j++) {
4767 Object tag = layout.getChildAt(j).getTag();
4768 if (tag != null) {
4769 writer.println(prefix + " " + tag.toString());
4770 }
4771 }
4772
4773 synchronized (sDumpLogs) {
4774 writer.println();
4775 writer.println(prefix + "Debug logs");
4776 for (String log : sDumpLogs) {
4777 writer.println(prefix + " " + log);
4778 }
4779 }
4780
Adam Cohen9211d422014-10-07 18:14:53 -07004781 if (mLauncherCallbacks != null) {
4782 mLauncherCallbacks.dump(prefix, fd, writer, args);
4783 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004784 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004785
Sunny Goyala1365452015-10-01 15:46:24 -07004786 public static void addDumpLog(String tag, String log) {
4787 Log.d(tag, log);
4788 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004789 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004790 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004791 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004792 }
4793
Adam Cohen59400422014-03-05 18:07:04 -08004794 public static CustomAppWidget getCustomAppWidget(String name) {
4795 return sCustomAppWidgets.get(name);
4796 }
4797
4798 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4799 return sCustomAppWidgets;
4800 }
4801
Sunny Goyal29538332016-02-18 09:10:19 -08004802 public static List<View> getFolderContents(View icon) {
4803 if (icon instanceof FolderIcon) {
4804 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4805 } else {
4806 return Collections.EMPTY_LIST;
4807 }
4808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004809}
Michael Jurka2763be32011-02-24 11:19:57 -08004810
Dan Sandlerd5024042014-01-09 15:01:33 -05004811interface DebugIntents {
4812 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4813 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004814}