blob: 752567b59a1d82b41074ef0817fad271c5589fe2 [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 Goyal08442b82015-10-21 17:15:08 -0700187 // Type: long
188 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700189 // Type: Content Values / parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Adam Cohen3ed316a2014-07-23 18:21:20 -0700199 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
200 static final String ACTION_FIRST_LOAD_COMPLETE =
201 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
202
Sunny Goyal594d76d2014-11-06 10:12:54 -0800203 private static final String QSB_WIDGET_ID = "qsb_widget_id";
204 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
205
Winson Chunga6945242014-01-08 14:04:34 -0800206 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
207
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700209 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
210 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700211
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700215 private boolean mIsSafeModeEnabled;
216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800218 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700219 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700220 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Winson Chunga2413752012-04-03 14:22:34 -0700222 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700223 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
224 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700226
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800227 private final BroadcastReceiver mCloseSystemDialogsReceiver
228 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800229
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700231 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800232 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800234 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700235
236 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Sunny Goyalffe83f12014-08-14 17:39:34 -0700238 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700241 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800242 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800243 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700244
Michael Jurka0280c3b2010-09-17 15:00:07 -0700245 private int[] mTmpAddItemCellCoordinates = new int[2];
246
Sunny Goyal316490e2015-06-02 09:38:28 -0700247 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800248 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700249
Michael Jurka838a4ca2011-02-07 13:33:06 -0800250 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700251 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700252
Winson Chungc51db6a2011-10-05 11:44:49 -0700253 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700254 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
256 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700257 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700259 // Main container view and the model for the widget tray screen.
260 @Thunk WidgetsContainerView mWidgetsView;
261 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Sunny Goyal594d76d2014-11-06 10:12:54 -0800263 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
267 // scroll issues (because the workspace may not have been measured yet) and extra work.
268 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
269 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
271 private SpannableStringBuilder mDefaultKeySsb = null;
272
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800275 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private boolean mRestoring;
277 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700278 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700282 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Sunny Goyal639e9062015-08-19 19:17:06 -0700291 private LauncherClings mClings;
292
Sunny Goyale2df0622015-04-24 11:27:00 -0700293 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 private static final int ADVANCE_INTERVAL = 20000;
300 private static final int ADVANCE_STAGGER = 250;
301
302 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700309 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700312
Adam Cohen1462de32012-07-24 22:34:36 -0700313 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700314 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700315
Sunny Goyala1365452015-10-01 15:46:24 -0700316 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
317 private static final Date sDateStamp = new Date();
318 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700319 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700320
Winson Chung46353de2012-02-16 14:05:10 -0800321 // We only want to get the SharedPreferences once since it does an FS stat each time we get
322 // it from the context.
323 private SharedPreferences mSharedPrefs;
324
Winson Chungf0c6ae02012-03-21 16:10:31 -0700325 // Holds the page that we need to animate to, and the icon views that we need to animate up
326 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700327 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700328 private Bitmap mFolderIconBitmap;
329 private Canvas mFolderIconCanvas;
330 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700332 private DeviceProfile mDeviceProfile;
333
Adam Cohen4b66bf32015-09-18 12:15:19 -0700334 private boolean mMoveToDefaultScreenFromNewIntent;
335
Winson Chung13eb5272015-05-11 16:30:13 -0700336 // This is set to the view that launched the activity that navigated the user away from
337 // launcher. Since there is no callback for when the activity has finished launching, enable
338 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800339 private BubbleTextView mWaitingForResume;
340
Adam Cohen59400422014-03-05 18:07:04 -0800341 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
342 new HashMap<String, CustomAppWidget>();
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700345 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
346 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800347 }
348 }
349
Adam Cohen091440a2015-03-18 14:16:05 -0700350 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700352 if (mWorkspace != null) {
353 mWorkspace.buildPageHardwareLayers();
354 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 }
356 };
357
Adam Cohendb364c32014-05-20 14:23:40 -0700358 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359
Adam Cohen091440a2015-03-18 14:16:05 -0700360 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int requestCode;
362 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700363 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 int cellX;
366 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700367 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800371 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800372
Adam Cohenf9c184a2016-01-15 16:47:43 -0800373 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375
376 @Thunk void setOrientation() {
377 if (mRotationEnabled) {
378 unlockScreenOrientation(true);
379 } else {
380 setRequestedOrientation(
381 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384
Sunny Goyal7779d622015-06-11 16:18:39 -0700385 private Runnable mUpdateOrientationRunnable = new Runnable() {
386 public void run() {
387 setOrientation();
388 }
389 };
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 @Override
392 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700393 if (DEBUG_STRICT_MODE) {
394 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
395 .detectDiskReads()
396 .detectDiskWrites()
397 .detectNetwork() // or .detectAll() for all detectable problems
398 .penaltyLog()
399 .build());
400 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
401 .detectLeakedSqlLiteObjects()
402 .detectLeakedClosableObjects()
403 .penaltyLog()
404 .penaltyDeath()
405 .build());
406 }
407
Adam Cohen9211d422014-10-07 18:14:53 -0700408 if (mLauncherCallbacks != null) {
409 mLauncherCallbacks.preOnCreate();
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400413
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Adam Cohen2e6da152015-05-06 11:42:25 -0700416 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700417 mDeviceProfile = getResources().getConfiguration().orientation
418 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700419 app.getInvariantDeviceProfile().landscapeProfile
420 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700421
Sunny Goyalf7258242015-10-19 16:59:07 -0700422 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700423 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800424 mModel = app.setLauncher(this);
425 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700428 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800431 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400432
Sunny Goyalffe83f12014-08-14 17:39:34 -0700433 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
436 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700438 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
439 // this also ensures that any synchronous binding below doesn't re-trigger another
440 // LauncherModel load.
441 mPaused = false;
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200444 android.os.Debug.startMethodTracing(
445 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
Tony Wickhameef44322015-10-20 13:24:36 -0700450 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
451 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700453 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454
Joe Onorato7c312c12009-08-13 21:36:53 -0700455 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mSavedState = savedInstanceState;
458 restoreState(mSavedState);
459
460 if (PROFILE_STARTUP) {
461 android.os.Debug.stopMethodTracing();
462 }
463
464 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700465 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 // If the user leaves launcher, then we should just load items asynchronously when
467 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 } else {
470 // We only load the page synchronously if the user rotates (or triggers a
471 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700472 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
481 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800483 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 // In case we are on a device with locked rotation, we should look at preferences to check
485 // if the user has specifically allowed rotation.
486 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700487 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700488 }
489
490 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
491 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400492 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700493
Adam Cohen9211d422014-10-07 18:14:53 -0700494 if (mLauncherCallbacks != null) {
495 mLauncherCallbacks.onCreate(savedInstanceState);
496 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700497
498 if (shouldShowIntroScreen()) {
499 showIntroScreen();
500 } else {
501 showFirstRunActivity();
502 showFirstRunClings();
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
505
Sunny Goyal7779d622015-06-11 16:18:39 -0700506 @Override
507 public void onSettingsChanged(String settings, boolean value) {
508 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
509 mRotationEnabled = value;
510 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
511 mUpdateOrientationRunnable.run();
512 }
513 }
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Winson Chungef7f8742015-06-04 17:18:17 -0700588 /**
589 * Updates the bounds of all the overlays to match the new fixed bounds.
590 */
591 public void updateOverlayBounds(Rect newBounds) {
592 mAppsView.setSearchBarBounds(newBounds);
593 mWidgetsView.setSearchBarBounds(newBounds);
594 }
595
Adam Cohen9211d422014-10-07 18:14:53 -0700596 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700597 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700598 if (mLauncherCallbacks != null) {
599 return mLauncherCallbacks.hasCustomContentToLeft();
600 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 return false;
602 }
603
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500605 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 * {@link #addToCustomContentPage}. This will only be invoked if
607 * {@link #hasCustomContentToLeft()} is {@code true}.
608 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700610 if (mLauncherCallbacks != null) {
611 mLauncherCallbacks.populateCustomContentContainer();
612 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 }
614
615 /**
616 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
617 * ensure the custom content page is added or removed if necessary.
618 */
619 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600620 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 // Not bound yet, wait for bindScreens to be called.
622 return;
623 }
624
625 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
626 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500627 mWorkspace.createCustomContentContainer();
628 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600629 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
630 mWorkspace.removeCustomContentPage();
631 }
632 }
633
Anton Hanssona2f665f2013-09-26 12:18:32 +0100634 public Stats getStats() {
635 return mStats;
636 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800637
638 /**
639 * Logger object is a singleton and does not have to be coupled with the foreground activity.
640 * Since most user event logging is done on the UI, the object is retrieved from the
641 * callback for convenience.
642 */
643 private void initLogger() {
644 if (mLauncherCallbacks != null) {
645 mUserEventLogger = mLauncherCallbacks.getLogger();
646 }
647 if (mUserEventLogger == null) {
648 mUserEventLogger = new UserEventLogger() {
649 @Override
650 public void processEvent(LauncherLogProto.LauncherEvent ev) {
651 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
652 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
653 LoggerUtils.getActionStr(ev.action),
654 LoggerUtils.getTargetStr(ev.srcTarget),
655 ev.elapsedContainerMillis,
656 ev.elapsedSessionMillis));
657 }
658 }
659 };
660 }
661 }
662
663 public UserEventLogger getLogger() {
664 return mUserEventLogger;
665 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666
Hyunyoung Song3f471442015-04-08 19:01:34 -0700667 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700668 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
669 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700670 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700671 }
672
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700674 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
675 // This cast is safe as long as the id < 0x00FFFFFF
676 // Since we jail all the dynamically generated views, there should be no clashes
677 // with any other views.
678 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000679 }
680
681 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700682 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
683 * a configuration step, this allows the proper animations to run after other transitions.
684 */
Adam Cohendb364c32014-05-20 14:23:40 -0700685 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700686 long screenId = args.screenId;
687 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
688 // When the screen id represents an actual screen (as opposed to a rank) we make sure
689 // that the drop page actually exists.
690 screenId = ensurePendingDropLayoutExists(args.screenId);
691 }
Adam Cohendb364c32014-05-20 14:23:40 -0700692
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700695 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700696 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700700 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700701 case REQUEST_RECONFIGURE_APPWIDGET:
702 completeRestoreAppWidget(args.appWidgetId);
703 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
Michael Jurka27614d22012-04-02 06:40:22 -0700705 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
706 // if you turned the screen off and then back while in All Apps, Launcher would not
707 // return to the workspace. Clearing mAddInfo.container here fixes this issue
708 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700709 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800710 }
711
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800712 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700714 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700715 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700716 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800717 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700718
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 Runnable exitSpringLoaded = new Runnable() {
720 @Override
721 public void run() {
722 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
723 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
724 }
725 };
726
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700728 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
731 if (resultCode == RESULT_CANCELED) {
732 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700733 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800736 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700738
739 // When the user has granted permission to bind widgets, we should check to see if
740 // we can inflate the default search bar widget.
741 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
743 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200744 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
745 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700746 // User could have free-scrolled between pages before picking a wallpaper; make sure
747 // we move to the closest one now.
748 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700749 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200750 }
751 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200753
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700755 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700756
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 // We have special handling for widgets
759 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800760 final int appWidgetId;
761 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
762 : -1;
763 if (widgetId < 0) {
764 appWidgetId = pendingAddWidgetId;
765 } else {
766 appWidgetId = widgetId;
767 }
768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800770 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700771 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
772 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 result = RESULT_CANCELED;
774 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700775 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 @Override
777 public void run() {
778 exitSpringLoadedDragModeDelayed(false, 0, null);
779 }
780 };
Adam Cohendb364c32014-05-20 14:23:40 -0700781 if (workspaceLocked) {
782 // No need to remove the empty screen if we're mid-binding, as the
783 // the bind will not add the empty screen.
784 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
785 } else {
786 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
788 }
Winson Chung5aaab772012-04-27 15:24:02 -0700789 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700790 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700791 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
792 // When the screen id represents an actual screen (as opposed to a rank)
793 // we make sure that the drop page actually exists.
794 mPendingAddInfo.screenId =
795 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
796 }
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
798
799 dropLayout.setDropPending(true);
800 final Runnable onComplete = new Runnable() {
801 @Override
802 public void run() {
803 completeTwoStageWidgetDrop(resultCode, appWidgetId);
804 dropLayout.setDropPending(false);
805 }
806 };
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
809 } else {
810 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
811 mPendingAddInfo);
812 sPendingAddItem = args;
813 }
Winson Chung5aaab772012-04-27 15:24:02 -0700814 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 return;
816 }
817
Sunny Goyalff572272014-07-23 13:58:07 -0700818 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
819 if (resultCode == RESULT_OK) {
820 // Update the widget view.
821 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
822 pendingAddWidgetId, mPendingAddInfo);
823 if (workspaceLocked) {
824 sPendingAddItem = args;
825 } else {
826 completeAdd(args);
827 }
828 }
829 // Leave the widget in the pending state if the user canceled the configure.
830 return;
831 }
832
Sunny Goyalaad90582015-07-09 14:22:50 -0700833 if (requestCode == REQUEST_CREATE_SHORTCUT) {
834 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
835 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
836 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
837 mPendingAddInfo);
838 if (isWorkspaceLocked()) {
839 sPendingAddItem = args;
840 } else {
841 completeAdd(args);
842 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
843 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
844 }
845 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700846 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800851
852 }
853
854 @Override
855 protected void onActivityResult(
856 final int requestCode, final int resultCode, final Intent data) {
857 handleActivityResult(requestCode, resultCode, data);
858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 }
862
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600863 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700864 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600865 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
867 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
868 View v = null;
869 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
870 if (layout != null) {
871 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
872 }
873 Intent intent = sPendingAddItem.intent;
874 sPendingAddItem = null;
875 if (grantResults.length > 0
876 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
877 startActivity(v, intent, null);
878 } else {
879 // TODO: Show a snack bar with link to settings
880 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
881 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
882 }
883 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
886 grantResults);
887 }
888 }
889
Adam Cohendb364c32014-05-20 14:23:40 -0700890 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
891 appWidgetId, ItemInfo info) {
892 PendingAddArguments args = new PendingAddArguments();
893 args.requestCode = requestCode;
894 args.intent = data;
895 args.container = info.container;
896 args.screenId = info.screenId;
897 args.cellX = info.cellX;
898 args.cellY = info.cellY;
899 args.appWidgetId = appWidgetId;
900 return args;
901 }
902
903 /**
904 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
905 *
906 * @param screenId the screen id to check
907 * @return the new screen, or screenId if it exists
908 */
909 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800910 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700911 if (dropLayout == null) {
912 // it's possible that the add screen was removed because it was
913 // empty and a re-bind occurred
914 mWorkspace.addExtraEmptyScreen();
915 return mWorkspace.commitExtraEmptyScreen();
916 } else {
917 return screenId;
918 }
919 }
920
Adam Cohen091440a2015-03-18 14:16:05 -0700921 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800922 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
925
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 mPendingAddWidgetInfo);
931 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 onCompleteRunnable = new Runnable() {
933 @Override
934 public void run() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
940 };
941 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 protected void onStop() {
957 super.onStop();
958 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStop();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
966 protected void onStart() {
967 super.onStart();
968 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.onStart();
972 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700973 }
974
975 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200977 long startTime = 0;
978 if (DEBUG_RESUME_TIME) {
979 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400980 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200981 }
Adam Cohen9211d422014-10-07 18:14:53 -0700982
983 if (mLauncherCallbacks != null) {
984 mLauncherCallbacks.preOnResume();
985 }
986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800988 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700989
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700991 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800993 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700994 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700995 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700996 // view after launching an app, as they may be depending on the UI to be static to
997 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700998 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700999 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001000 } else if (mOnResumeState == State.WIDGETS) {
1001 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001002 }
1003 mOnResumeState = State.NONE;
1004
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001005 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001006 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1007 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001008
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001009 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001010 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001011 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001012 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001014 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1018 // execute them here
1019 long startTimeCallbacks = 0;
1020 if (DEBUG_RESUME_TIME) {
1021 startTimeCallbacks = System.currentTimeMillis();
1022 }
1023
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1025 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001027 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 if (DEBUG_RESUME_TIME) {
1029 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1030 (System.currentTimeMillis() - startTimeCallbacks));
1031 }
Michael Jurka447bf842013-05-15 14:52:15 +02001032 }
Michael Jurka54554252013-08-01 12:52:23 +02001033 if (mOnResumeCallbacks.size() > 0) {
1034 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1035 mOnResumeCallbacks.get(i).run();
1036 }
1037 mOnResumeCallbacks.clear();
1038 }
Winson Chunge4e50662012-01-23 14:45:13 -08001039
1040 // Reset the pressed state of icons that were locked in the press state while activities
1041 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001043 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001044 mWaitingForResume.setStayPressed(false);
1045 }
Winson Chung82963d52013-10-09 11:20:57 -07001046
Adam Cohen06dff352012-06-01 17:17:08 -07001047 // It is possible that widgets can receive updates while launcher is not in the foreground.
1048 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1049 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1050 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001051 if (!isWorkspaceLoading()) {
1052 getWorkspace().reinflateWidgetsIfNecessary();
1053 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001054 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001055
Michael Jurka447bf842013-05-15 14:52:15 +02001056 if (DEBUG_RESUME_TIME) {
1057 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1058 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001059
Adam Cohen4b66bf32015-09-18 12:15:19 -07001060 // We want to suppress callbacks about CustomContent being shown if we have just received
1061 // onNewIntent while the user was present within launcher. In that case, we post a call
1062 // to move the user to the main screen (which will occur after onResume). We don't want to
1063 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1064 // suppress here.
1065 if (mWorkspace.getCustomContentCallbacks() != null
1066 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001067 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001068 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001069 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1070 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001071 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 }
1073 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001074 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001075 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001076 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001077
Sunny Goyal756adbc2015-04-16 15:20:51 -07001078 if (!isWorkspaceLoading()) {
1079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1081 }
Adam Cohen9211d422014-10-07 18:14:53 -07001082
1083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onResume();
1085 }
Adam Cohen06dff352012-06-01 17:17:08 -07001086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001090 // Ensure that items added to Launcher are queued until Launcher returns
1091 InstallShortcutReceiver.enableInstallQueue();
1092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001094 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001095 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001096 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001097
1098 // We call onHide() aggressively. The custom content callbacks should be able to
1099 // debounce excess onHide calls.
1100 if (mWorkspace.getCustomContentCallbacks() != null) {
1101 mWorkspace.getCustomContentCallbacks().onHide();
1102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Adam Cohen9211d422014-10-07 18:14:53 -07001104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
1109 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001110 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1111 // by a onResume or by scrolling otherwise.
1112 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001113
1114 // Custom content is completely hidden
1115 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001116
1117 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1118 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001119
1120 // Indicates whether the user is allowed to scroll away from the custom content.
1121 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 }
1123
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 public interface LauncherOverlay {
1125
1126 /**
1127 * Touch interaction leading to overscroll has begun
1128 */
1129 public void onScrollInteractionBegin();
1130
1131 /**
1132 * Touch interaction related to overscroll has ended
1133 */
1134 public void onScrollInteractionEnd();
1135
1136 /**
1137 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1138 * screen (or in the case of RTL, the rightmost screen).
1139 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001140 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001141
1142 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001143 * Called when the launcher is ready to use the overlay
1144 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001146 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 }
1148
Jun Mukai8af0cd82015-05-12 12:32:12 -07001149 public interface LauncherSearchCallbacks {
1150 /**
1151 * Called when the search overlay is shown.
1152 */
1153 public void onSearchOverlayOpened();
1154
1155 /**
1156 * Called when the search overlay is dismissed.
1157 */
1158 public void onSearchOverlayClosed();
1159 }
1160
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001162 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001163 }
1164
1165 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1166
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 public void onScrollChanged(float progress) {
1168 if (mWorkspace != null) {
1169 mWorkspace.onOverlayScrollChanged(progress);
1170 }
1171 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 }
1173
Jorim Jaggid017f882014-01-14 17:08:48 -08001174 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001175 if (mLauncherCallbacks != null) {
1176 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001177 } else {
1178 // On devices with a locked orientation, we will at least have the allow rotation
1179 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001180 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001181 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001182 }
1183
Adam Cohen9211d422014-10-07 18:14:53 -07001184 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001185 CustomContentCallbacks callbacks, String description) {
1186 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001187 }
1188
Adam Cohenedb40762013-07-18 16:45:45 -07001189 // The custom content needs to offset its content to account for the QSB
1190 public int getTopOffsetForCustomContent() {
1191 return mWorkspace.getPaddingTop();
1192 }
1193
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001194 @Override
1195 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001197 if (mModel.isCurrentCallbacks(this)) {
1198 mModel.stopLoader();
1199 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001200 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1201
Romain Guy13c2e7b2010-03-10 19:45:00 -08001202 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001203 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001206 @Override
1207 public void onWindowFocusChanged(boolean hasFocus) {
1208 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001209 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001210
1211 if (mLauncherCallbacks != null) {
1212 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1213 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 private boolean acceptFilter() {
1217 final InputMethodManager inputManager = (InputMethodManager)
1218 getSystemService(Context.INPUT_METHOD_SERVICE);
1219 return !inputManager.isFullscreenMode();
1220 }
1221
1222 @Override
1223 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001224 final int uniChar = event.getUnicodeChar();
1225 final boolean handled = super.onKeyDown(keyCode, event);
1226 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1227 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1229 keyCode, event);
1230 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001232 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 // showSearchDialog()
1234 // If there are multiple keystrokes before the search dialog takes focus,
1235 // onSearchRequested() will be called for every keystroke,
1236 // but it is idempotent, so it's fine.
1237 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 }
1239 }
1240
Joe Onorato8a9625e2010-01-28 15:55:35 -08001241 // Eat the long press event so the keyboard doesn't come up.
1242 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1243 return true;
1244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 return handled;
1247 }
1248
Winson46163472015-09-22 17:31:56 -07001249 @Override
1250 public boolean onKeyUp(int keyCode, KeyEvent event) {
1251 if (keyCode == KeyEvent.KEYCODE_MENU) {
1252 // Ignore the menu key if we are currently dragging or are on the custom content screen
1253 if (!isOnCustomContent() && !mDragController.isDragging()) {
1254 // Close any open folders
1255 closeFolder();
1256
1257 // Stop resizing any widgets
1258 mWorkspace.exitWidgetResizeMode();
1259
1260 // Show the overview mode if we are on the workspace
1261 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1262 !mWorkspace.isSwitchingState()) {
1263 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001264 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001265 }
1266 }
1267 return true;
1268 }
1269 return super.onKeyUp(keyCode, event);
1270 }
1271
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 private String getTypedText() {
1273 return mDefaultKeySsb.toString();
1274 }
1275
1276 private void clearTypedText() {
1277 mDefaultKeySsb.clear();
1278 mDefaultKeySsb.clearSpans();
1279 Selection.setSelection(mDefaultKeySsb, 0);
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001283 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1284 * State
1285 */
1286 private static State intToState(int stateOrdinal) {
1287 State state = State.WORKSPACE;
1288 final State[] stateValues = State.values();
1289 for (int i = 0; i < stateValues.length; i++) {
1290 if (stateValues[i].ordinal() == stateOrdinal) {
1291 state = stateValues[i];
1292 break;
1293 }
1294 }
1295 return state;
1296 }
1297
1298 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 * Restores the previous state, if it exists.
1300 *
1301 * @param savedState The previous state.
1302 */
1303 private void restoreState(Bundle savedState) {
1304 if (savedState == null) {
1305 return;
1306 }
1307
Michael Jurka883f55b2010-10-21 15:47:14 -07001308 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001309 if (state == State.APPS || state == State.WIDGETS) {
1310 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001311 }
1312
Adam Cohen21cd0022013-10-09 18:57:02 -07001313 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1314 PagedView.INVALID_RESTORE_PAGE);
1315 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001316 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
Sunny Goyal756cd262015-08-20 12:33:21 -07001319 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1320 if (itemValues != null) {
1321 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001322 AppWidgetProviderInfo info = savedState.getParcelable(
1323 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001324 mPendingAddWidgetInfo = info == null ?
1325 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1326
Adam Cohen4637b5a2013-11-04 18:21:24 -08001327 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001328 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 /**
1334 * Finds all the views we need and configure them properly.
1335 */
1336 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001337 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001338
Craig Mautner360310b2012-10-26 15:13:08 -07001339 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001340 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001341 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1342 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001343 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001344 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001345
John Spurlock77e1f472013-09-11 10:09:51 -04001346 mLauncherView.setSystemUiVisibility(
1347 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
Winson Chung4c98d922011-05-31 16:50:48 -07001350 // Setup the drag layer
1351 mDragLayer.setup(this, dragController);
1352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 // Setup the hotseat
1354 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1355 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001356 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 }
1358
Winsone9f27272015-10-13 10:47:51 -07001359 // Setup the overview panel
1360 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001361
Winson Chung4c98d922011-05-31 16:50:48 -07001362 // Setup the workspace
1363 mWorkspace.setHapticFeedbackEnabled(false);
1364 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001365 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001366 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001367
Tony Wickham34d2c912015-09-11 09:27:58 -07001368 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001369 mSearchDropTargetBar = (SearchDropTargetBar)
1370 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001371 // Get the app info bar
1372 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1373 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001374
Winson Chungef7f8742015-06-04 17:18:17 -07001375 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001376 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001377 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001378 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1379 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1380 } else {
1381 mAppsView.setSearchBarController(new DefaultAppSearchController());
1382 }
Craig Mautner360310b2012-10-26 15:13:08 -07001383
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001385 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001386 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001387 dragController.setMoveTarget(mWorkspace);
1388 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001389 if (mSearchDropTargetBar != null) {
1390 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001391 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001392 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001393 if (mAppInfoDropTargetBar != null) {
1394 mAppInfoDropTargetBar.setup(this, dragController);
1395 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001396
Sunny Goyal322d5562015-06-25 19:35:49 -07001397 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1398 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
Winsone9f27272015-10-13 10:47:51 -07001402 private void setupOverviewPanel() {
1403 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1404
1405 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1406 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1407 @Override
1408 public boolean onLongClick(View v) {
1409 return v.performClick();
1410 }
1411 };
1412
1413 // Bind wallpaper button actions
1414 View wallpaperButton = findViewById(R.id.wallpaper_button);
1415 wallpaperButton.setOnClickListener(new OnClickListener() {
1416 @Override
1417 public void onClick(View view) {
1418 if (!mWorkspace.isSwitchingState()) {
1419 onClickWallpaperPicker(view);
1420 }
1421 }
1422 });
1423 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1424 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425
1426 // Bind widget button actions
1427 mWidgetsButton = findViewById(R.id.widget_button);
1428 mWidgetsButton.setOnClickListener(new OnClickListener() {
1429 @Override
1430 public void onClick(View view) {
1431 if (!mWorkspace.isSwitchingState()) {
1432 onClickAddWidgetButton(view);
1433 }
1434 }
1435 });
1436 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1437 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 // Bind settings actions
1440 View settingsButton = findViewById(R.id.settings_button);
1441 boolean hasSettings = hasSettings();
1442 if (hasSettings) {
1443 settingsButton.setOnClickListener(new OnClickListener() {
1444 @Override
1445 public void onClick(View view) {
1446 if (!mWorkspace.isSwitchingState()) {
1447 onClickSettingsButton(view);
1448 }
1449 }
1450 });
1451 settingsButton.setOnLongClickListener(performClickOnLongClick);
1452 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1453 } else {
1454 settingsButton.setVisibility(View.GONE);
1455 }
1456
1457 mOverviewPanel.setAlpha(0f);
1458 }
1459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001461 * Sets the all apps button. This method is called from {@link Hotseat}.
1462 */
1463 public void setAllAppsButton(View allAppsButton) {
1464 mAllAppsButton = allAppsButton;
1465 }
1466
1467 public View getAllAppsButton() {
1468 return mAllAppsButton;
1469 }
1470
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001471 public View getWidgetsButton() {
1472 return mWidgetsButton;
1473 }
1474
Anjali Koppal5ad44842014-03-10 20:34:39 -07001475 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * Creates a view representing a shortcut.
1477 *
1478 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001480 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
1484 /**
1485 * Creates a view representing a shortcut inflated from the specified resource.
1486 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 * @param parent The group the shortcut belongs to.
1488 * @param info The data structure describing the shortcut.
1489 *
1490 * @return A View inflated from layoutResId.
1491 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001493 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001494 parent, false);
1495 favorite.applyFromShortcutInfo(info, mIconCache);
1496 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001498 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 return favorite;
1500 }
1501
1502 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 * Add a shortcut to the workspace.
1504 *
1505 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001508 int cellY) {
1509 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Sunny Goyal5c97f512015-05-19 16:03:28 -07001512 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001513 if (info == null) {
1514 return;
1515 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 final View view = createShortcut(info);
1517
Sunny Goyal5c97f512015-05-19 16:03:28 -07001518 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 // First we check if we already know the exact location where we want to add this item.
1520 if (cellX >= 0 && cellY >= 0) {
1521 cellXY[0] = cellX;
1522 cellXY[1] = cellY;
1523 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001524
1525 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001526 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001527 true, null,null)) {
1528 return;
1529 }
1530 DragObject dragObject = new DragObject();
1531 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001532 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1533 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 return;
1535 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001537 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 }
1539
1540 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001541 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 return;
1543 }
1544
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001545 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
1547 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
1552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001554 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001556 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 */
Adam Cohen091440a2015-03-18 14:16:05 -07001558 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001559 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1560
1561 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001563 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1564 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Adam Cohen59400422014-03-05 18:07:04 -08001567 if (appWidgetInfo.isCustomWidget) {
1568 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001569 }
1570
Adam Cohen59400422014-03-05 18:07:04 -08001571 LauncherAppWidgetInfo launcherInfo;
1572 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1573 launcherInfo.spanX = info.spanX;
1574 launcherInfo.spanY = info.spanY;
1575 launcherInfo.minSpanX = info.minSpanX;
1576 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001577 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001580 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581
1582 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 if (hostView == null) {
1584 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001585 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1586 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 } else {
1588 // The AppWidgetHostView has already been inflated and instantiated
1589 launcherInfo.hostView = hostView;
1590 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001594 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1598 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1599 item.hostView.setTag(item);
1600 item.onBindAppWidget(this);
1601
1602 item.hostView.setFocusable(true);
1603 item.hostView.setOnFocusChangeListener(mFocusHandler);
1604
1605 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1606 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1607
1608 if (!item.isCustomWidget()) {
1609 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1610 }
1611 }
1612
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1614 @Override
1615 public void onReceive(Context context, Intent intent) {
1616 final String action = intent.getAction();
1617 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1618 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001619 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001620 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001621
Winson Chungc100e8e2011-08-09 16:02:43 -07001622 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001623 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001624 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001625 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001626 if (!showWorkspace(false)) {
1627 // If we are already on the workspace, then manually reset all apps
1628 mAppsView.reset();
1629 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1632 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001633 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001634 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1635 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001636 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001637 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1638 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1639 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001640 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001641 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1642 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 }
1644 }
1645 };
1646
1647 @Override
1648 public void onAttachedToWindow() {
1649 super.onAttachedToWindow();
1650
1651 // Listen for broadcasts related to user-presence
1652 final IntentFilter filter = new IntentFilter();
1653 filter.addAction(Intent.ACTION_SCREEN_OFF);
1654 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001655 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001656 if (ENABLE_DEBUG_INTENTS) {
1657 filter.addAction(DebugIntents.DELETE_DATABASE);
1658 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1659 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001662 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001664
1665 if (mLauncherCallbacks != null) {
1666 mLauncherCallbacks.onAttachedToWindow();
1667 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 }
1669
1670 @Override
1671 public void onDetachedFromWindow() {
1672 super.onDetachedFromWindow();
1673 mVisible = false;
1674
Adam Cohend113e0c2010-11-11 10:48:05 -08001675 if (mAttached) {
1676 unregisterReceiver(mReceiver);
1677 mAttached = false;
1678 }
Winson Chungb745afb2015-03-02 11:51:23 -08001679 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001680
1681 if (mLauncherCallbacks != null) {
1682 mLauncherCallbacks.onDetachedFromWindow();
1683 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001684 }
1685
1686 public void onWindowVisibilityChanged(int visibility) {
1687 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001688 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001689 // The following code used to be in onResume, but it turns out onResume is called when
1690 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1691 // is a more appropriate event to handle
1692 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 if (!mWorkspaceLoading) {
1694 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001695 // We want to let Launcher draw itself at least once before we force it to build
1696 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001697 // apps is nice and speedy.
1698 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001699 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001700 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 if (mStarted) return;
1702 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001703 // We delay the layer building a bit in order to give
1704 // other message processing a time to run. In particular
1705 // this avoids a delay in hiding the IME if it was
1706 // currently shown, because doing that may involve
1707 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001708 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001709 final ViewTreeObserver.OnDrawListener listener = this;
1710 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001711 public void run() {
1712 if (mWorkspace != null &&
1713 mWorkspace.getViewTreeObserver() != null) {
1714 mWorkspace.getViewTreeObserver().
1715 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001716 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001717 }
1718 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001719 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001720 }
1721 });
1722 }
1723 clearTypedText();
1724 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 }
1726
Adam Cohen091440a2015-03-18 14:16:05 -07001727 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 mHandler.removeMessages(ADVANCE_MSG);
1729 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1730 mHandler.sendMessageDelayed(msg, delay);
1731 mAutoAdvanceSentTime = System.currentTimeMillis();
1732 }
1733
Adam Cohen091440a2015-03-18 14:16:05 -07001734 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1736 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1737 mAutoAdvanceRunning = autoAdvanceRunning;
1738 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001739 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 sendAdvanceMessage(delay);
1741 } else {
1742 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001743 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1745 }
1746 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001747 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
1749 }
1750 }
1751
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1753
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001755 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 if (msg.what == ADVANCE_MSG) {
1757 int i = 0;
1758 for (View key: mWidgetsToAdvance.keySet()) {
1759 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001760 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001762 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 public void run() {
1764 ((Advanceable) v).advance();
1765 }
1766 }, delay);
1767 }
1768 i++;
1769 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001770 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001772 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001774 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001775
Winsonc0b52fe2015-09-09 16:38:15 -07001776 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001777 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1779 if (v instanceof Advanceable) {
1780 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001781 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001782 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 }
1784 }
1785
Winsonc0b52fe2015-09-09 16:38:15 -07001786 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 if (mWidgetsToAdvance.containsKey(hostView)) {
1788 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001789 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001791 }
1792
Hyunyoung Song3f471442015-04-08 19:01:34 -07001793 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001794 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1795 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 }
1797
Winson Chunga6945242014-01-08 14:04:34 -08001798 public DragLayer getDragLayer() {
1799 return mDragLayer;
1800 }
1801
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001802 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001803 return mAppsView;
1804 }
1805
Hyunyoung Song3f471442015-04-08 19:01:34 -07001806 public WidgetsContainerView getWidgetsView() {
1807 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001808 }
1809
Winson Chunga6945242014-01-08 14:04:34 -08001810 public Workspace getWorkspace() {
1811 return mWorkspace;
1812 }
1813
1814 public Hotseat getHotseat() {
1815 return mHotseat;
1816 }
1817
Jorim Jaggid017f882014-01-14 17:08:48 -08001818 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001819 return mOverviewPanel;
1820 }
1821
Winson Chung006ee262015-08-03 14:40:11 -07001822 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001823 return mSearchDropTargetBar;
1824 }
1825
Tony Wickham34d2c912015-09-11 09:27:58 -07001826 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1827 return mAppInfoDropTargetBar;
1828 }
1829
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001830 public LauncherAppWidgetHost getAppWidgetHost() {
1831 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
Romain Guycbb89e42009-06-08 15:52:54 -07001833
Winson Chunga9abd0e2010-10-27 17:18:37 -07001834 public LauncherModel getModel() {
1835 return mModel;
1836 }
1837
Winson Chunga6945242014-01-08 14:04:34 -08001838 protected SharedPreferences getSharedPrefs() {
1839 return mSharedPrefs;
1840 }
1841
Adam Cohen2e6da152015-05-06 11:42:25 -07001842 public DeviceProfile getDeviceProfile() {
1843 return mDeviceProfile;
1844 }
1845
Bjorn Bringertc459e522013-06-07 19:36:01 +01001846 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001847 getWindow().closeAllPanels();
1848
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001849 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001850 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001851 }
1852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 @Override
1854 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001855 long startTime = 0;
1856 if (DEBUG_RESUME_TIME) {
1857 startTime = System.currentTimeMillis();
1858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 super.onNewIntent(intent);
1860
1861 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001862 Folder openFolder = mWorkspace.getOpenFolder();
1863 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1864 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1865 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1866 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1867 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001868 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001869 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001870
Adam Cohen6fecd412013-10-02 17:41:50 -07001871 if (mWorkspace == null) {
1872 // Can be cases where mWorkspace is null, this prevents a NPE
1873 return;
1874 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 // In all these cases, only animate if we're already on home
1876 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001877
Sunny Goyal935fca12015-10-13 10:19:01 -07001878 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001879 exitSpringLoadedDragMode();
1880
1881 // If we are already on home, then just animate back to the workspace,
1882 // otherwise, just wait until onResume to set the state back to Workspace
1883 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001884 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 } else {
1886 mOnResumeState = State.WORKSPACE;
1887 }
1888
1889 final View v = getWindow().peekDecorView();
1890 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001891 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001892 INPUT_METHOD_SERVICE);
1893 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1894 }
1895
Winson Chungb745afb2015-03-02 11:51:23 -08001896 // Reset the apps view
1897 if (!alreadyOnHome && mAppsView != null) {
1898 mAppsView.scrollToTop();
1899 }
1900
Hyunyoung Song3f471442015-04-08 19:01:34 -07001901 // Reset the widgets view
1902 if (!alreadyOnHome && mWidgetsView != null) {
1903 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001904 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001905
Adam Cohen9211d422014-10-07 18:14:53 -07001906 if (mLauncherCallbacks != null) {
1907 mLauncherCallbacks.onHomeIntent();
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
Adam Cohened307df2013-10-02 09:37:31 -07001910
Adam Cohen9211d422014-10-07 18:14:53 -07001911 if (mLauncherCallbacks != null) {
1912 mLauncherCallbacks.onNewIntent(intent);
1913 }
Winson15f8b172015-08-19 11:02:39 -07001914
1915 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1916 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1917 // animation.
1918 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001919 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1920 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001921 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1922 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001923
1924 // We use this flag to suppress noisy callbacks above custom content state
1925 // from onResume.
1926 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001927 mWorkspace.post(new Runnable() {
1928 @Override
1929 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001930 if (mWorkspace != null) {
1931 mWorkspace.moveToDefaultScreen(true);
1932 }
Winson15f8b172015-08-19 11:02:39 -07001933 }
1934 });
1935 }
1936 }
1937
1938 if (DEBUG_RESUME_TIME) {
1939 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1940 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001944 public void onRestoreInstanceState(Bundle state) {
1945 super.onRestoreInstanceState(state);
1946 for (int page: mSynchronouslyBoundPages) {
1947 mWorkspace.restoreInstanceStateForChild(page);
1948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950
1951 @Override
1952 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001954 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1955 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001956 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001957 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Michael Jurka883f55b2010-10-21 15:47:14 -07001959 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001960 Folder openFolder = mWorkspace.getOpenFolder();
1961 if (openFolder != null) {
1962 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001967 ContentValues itemValues = new ContentValues();
1968 mPendingAddInfo.writeToValues(itemValues);
1969 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001971 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
Hyunyoung Song3f471442015-04-08 19:01:34 -07001974 // Save the current widgets tray?
1975 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001976
1977 if (mLauncherCallbacks != null) {
1978 mLauncherCallbacks.onSaveInstanceState(outState);
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981
1982 @Override
1983 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001985
Winson Chunge7a03942011-08-05 15:05:12 -07001986 // Remove all pending runnables
1987 mHandler.removeMessages(ADVANCE_MSG);
1988 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001989 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001990
Winson Chungcd2b0142011-06-08 16:02:26 -07001991 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001992 // It's possible to receive onDestroy after a new Launcher activity has
1993 // been created. In this case, don't interfere with the new Launcher.
1994 if (mModel.isCurrentCallbacks(this)) {
1995 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001996 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001997 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002000 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002002 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002004 mAppWidgetHost = null;
2005
2006 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007
2008 TextKeyListener.getInstance().release();
2009
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002010 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002011
Michael Jurka2ecf9952012-06-18 12:52:28 -07002012 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onDestroy();
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 public DragController getDragController() {
2020 return mDragController;
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 @Override
2024 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002025 onStartForResult(requestCode);
2026 super.startActivityForResult(intent, requestCode);
2027 }
2028
2029 @Override
2030 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2031 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2032 onStartForResult(requestCode);
2033 try {
2034 super.startIntentSenderForResult(intent, requestCode,
2035 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2036 } catch (IntentSender.SendIntentException e) {
2037 throw new ActivityNotFoundException();
2038 }
2039 }
2040
2041 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002042 if (requestCode >= 0) {
2043 setWaitingForResult(true);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Winson Chung70d72102011-08-12 11:24:35 -07002047 /**
2048 * Indicates that we want global search for this activity by setting the globalSearch
2049 * argument for {@link #startSearch} to true.
2050 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002052 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002054
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 if (initialQuery == null) {
2056 // Use any text typed in the launcher as the initial query
2057 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 if (appSearchData == null) {
2060 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002061 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
Winson Chungadf0c182012-08-23 14:59:07 -07002063 Rect sourceBounds = new Rect();
2064 if (mSearchDropTargetBar != null) {
2065 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2066 }
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 if (clearTextImmediately) {
2071 clearTypedText();
2072 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002073
2074 // We need to show the workspace after starting the search
2075 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 }
2077
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 /**
2079 * Start a text search.
2080 *
2081 * @return {@code true} if the search will start immediately, so any further keypresses
2082 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2083 * to buffer keypresses.
2084 */
2085 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002087 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2088 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2089 sourceBounds);
2090 }
2091
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002094 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 }
2096
2097 /**
2098 * Starts the global search activity. This code is a copied from SearchManager
2099 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2105 if (globalSearchActivity == null) {
2106 Log.w(TAG, "No global search activity found.");
2107 return;
2108 }
2109 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2110 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2111 intent.setComponent(globalSearchActivity);
2112 // Make sure that we have a Bundle to put source in
2113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
2115 } else {
2116 appSearchData = new Bundle(appSearchData);
2117 }
Adam Cohen9211d422014-10-07 18:14:53 -07002118 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002119 if (!appSearchData.containsKey("source")) {
2120 appSearchData.putString("source", getPackageName());
2121 }
2122 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2123 if (!TextUtils.isEmpty(initialQuery)) {
2124 intent.putExtra(SearchManager.QUERY, initialQuery);
2125 }
2126 if (selectInitialQuery) {
2127 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2128 }
2129 intent.setSourceBounds(sourceBounds);
2130 try {
2131 startActivity(intent);
2132 } catch (ActivityNotFoundException ex) {
2133 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136
Yura4f93ec62014-02-04 14:15:21 +00002137 public boolean isOnCustomContent() {
2138 return mWorkspace.isOnOrMovingToCustomContent();
2139 }
2140
Winson Chung70d72102011-08-12 11:24:35 -07002141 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002142 public boolean onPrepareOptionsMenu(Menu menu) {
2143 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002144 if (mLauncherCallbacks != null) {
2145 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2146 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002147 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002150 @Override
2151 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002152 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002153 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002154 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002155 }
2156
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 public boolean isWorkspaceLocked() {
2158 return mWorkspaceLoading || mWaitingForResult;
2159 }
2160
Adam Cohen517a7f52014-03-01 12:12:59 -08002161 public boolean isWorkspaceLoading() {
2162 return mWorkspaceLoading;
2163 }
2164
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002165 private void setWorkspaceLoading(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWorkspaceLoading = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
2173 private void setWaitingForResult(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWaitingForResult = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
Adam Cohen9211d422014-10-07 18:14:53 -07002181 protected void onWorkspaceLockedChanged() {
2182 if (mLauncherCallbacks != null) {
2183 mLauncherCallbacks.onWorkspaceLockedChanged();
2184 }
2185 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002186
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002188 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002189 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002190 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2191 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002192 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002194
Tony Wickhama0628cc2015-10-14 15:23:04 -07002195 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002196 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002197 if (LOGD) {
2198 Log.d(TAG, "Adding widget from drop");
2199 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002200 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2201 }
2202
Sunny Goyale6b63a32015-01-16 16:14:29 -08002203 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002204 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2205 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002206 if (appWidgetInfo.configure != null) {
2207 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002208 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002209
Bjorn Bringert7984c942009-12-09 15:38:25 +00002210 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002211 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2212 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002215 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002216 Runnable onComplete = new Runnable() {
2217 @Override
2218 public void run() {
2219 // Exit spring loaded mode if necessary after adding the widget
2220 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2221 null);
2222 }
2223 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002225 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002226 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228 }
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002231 // Close any folders that may be open.
2232 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 mWorkspace.moveToCustomContentScreen(animate);
2234 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002235
2236 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2237 int[] cell, int spanX, int spanY) {
2238 switch (info.itemType) {
2239 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2240 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2241 int span[] = new int[2];
2242 span[0] = spanX;
2243 span[1] = spanY;
2244 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2245 container, screenId, cell, span);
2246 break;
2247 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2248 processShortcutFromDrop(info.componentName, container, screenId, cell);
2249 break;
2250 default:
2251 throw new IllegalStateException("Unknown item type: " + info.itemType);
2252 }
2253 }
2254
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 /**
2256 * Process a shortcut drop.
2257 *
2258 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002262 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2263 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267
2268 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.cellX = cell[0];
2270 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272
2273 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2274 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002275 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 }
2277
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 /**
2279 * Process a widget drop.
2280 *
2281 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002285 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2286 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002290 mPendingAddInfo.minSpanX = info.minSpanX;
2291 mPendingAddInfo.minSpanY = info.minSpanY;
2292
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 mPendingAddInfo.cellX = cell[0];
2295 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002297 if (span != null) {
2298 mPendingAddInfo.spanX = span[0];
2299 mPendingAddInfo.spanY = span[1];
2300 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 AppWidgetHostView hostView = info.boundWidget;
2303 int appWidgetId;
2304 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 // In the case where we've prebound the widget, we remove it from the DragLayer
2306 if (LOGD) {
2307 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2308 }
2309 getDragLayer().removeView(hostView);
2310
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002312 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002313
2314 // Clear the boundWidget so that it doesn't get destroyed.
2315 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002316 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002317 // In this case, we either need to start an activity to get permission to bind
2318 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002320 Bundle options = info.bindOptions;
2321
Sunny Goyalffe83f12014-08-14 17:39:34 -07002322 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2323 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002324 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002325 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002326 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002327 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002328 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2329 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2330 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002331 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2332 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002333 // TODO: we need to make sure that this accounts for the options bundle.
2334 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002335 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2336 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002337 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 }
2339
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002342 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 folderInfo.title = getText(R.string.folder_name);
2344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002346 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2347 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002348 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349
2350 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 FolderIcon newFolder =
2352 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002355 // Force measure the new folder icon
2356 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2357 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002358 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 }
Romain Guycbb89e42009-06-08 15:52:54 -07002360
Winsonc0b52fe2015-09-09 16:38:15 -07002361 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002362 * Unbinds the view for the specified item, and removes the item and all its children.
2363 *
2364 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002365 * @param itemInfo the {@link ItemInfo} for this view.
2366 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002367 */
Winson2949fb52015-09-24 09:56:11 -07002368 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002369 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002370 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002371 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2372 if (folderInfo != null) {
2373 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002374 } else {
2375 mWorkspace.removeWorkspaceItem(v);
2376 }
Winsonc0b52fe2015-09-09 16:38:15 -07002377 if (deleteFromDb) {
2378 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2379 }
2380 } else if (itemInfo instanceof FolderInfo) {
2381 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2382 unbindFolder(folderInfo);
2383 mWorkspace.removeWorkspaceItem(v);
2384 if (deleteFromDb) {
2385 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2386 }
2387 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2388 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002389 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002390 removeWidgetToAutoAdvance(widgetInfo.hostView);
2391 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002392 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002394 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002395
Winsonc0b52fe2015-09-09 16:38:15 -07002396 } else {
2397 return false;
2398 }
2399 return true;
2400 }
2401
2402 /**
2403 * Unbinds any launcher references to the folder.
2404 */
2405 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002406 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407 }
2408
Winsonc0b52fe2015-09-09 16:38:15 -07002409 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002410 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002411 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002412 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002413 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002414 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002415 // Deleting an app widget ID is a void call but writes to disk before returning
2416 // to the caller...
2417 new AsyncTask<Void, Void, Void>() {
2418 public Void doInBackground(Void ... args) {
2419 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2420 return null;
2421 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002422 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002423 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002424 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002433 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002435 dumpState();
2436 return true;
2437 }
2438 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 }
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002442 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 return true;
2444 }
2445 }
2446
2447 return super.dispatchKeyEvent(event);
2448 }
2449
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 @Override
2451 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2453 return;
2454 }
2455
Sunny Goyal45478022015-06-08 16:52:41 -07002456 if (mDragController.isDragging()) {
2457 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002458 return;
2459 }
2460
Winson Chungb745afb2015-03-02 11:51:23 -08002461 if (isAppsViewVisible()) {
2462 showWorkspace(true);
2463 } else if (isWidgetsViewVisible()) {
2464 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002465 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002466 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002468 Folder openFolder = mWorkspace.getOpenFolder();
2469 if (openFolder.isEditingName()) {
2470 openFolder.dismissEditingName();
2471 } else {
2472 closeFolder();
2473 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002475 mWorkspace.exitWidgetResizeMode();
2476
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 // Back button is a no-op here, but give at least some feedback for the button press
2478 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002479 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002483 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002484 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002485 @Override
2486 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002487 if (mAppWidgetHost != null) {
2488 mAppWidgetHost.startListening();
2489 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002490
2491 // Recreate the QSB, as the widget has been reset.
2492 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002493 }
2494
2495 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 * Launches the intent referred by the clicked shortcut.
2497 *
2498 * @param v The view representing the clicked shortcut.
2499 */
2500 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002501 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2502 // view has detached (it's possible for this to happen if the view is removed mid touch).
2503 if (v.getWindowToken() == null) {
2504 return;
2505 }
2506
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002507 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002508 return;
2509 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002510
Adam Cohen1697b792013-09-17 19:08:21 -07002511 if (v instanceof Workspace) {
2512 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002513 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002514 }
2515 return;
2516 }
2517
2518 if (v instanceof CellLayout) {
2519 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002520 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2521 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002522 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002523 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002524 }
2525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002527 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002530 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002532 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002533 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002534 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002535 } else if (tag instanceof AppInfo) {
2536 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002537 } else if (tag instanceof LauncherAppWidgetInfo) {
2538 if (v instanceof PendingAppWidgetHostView) {
2539 onClickPendingWidget((PendingAppWidgetHostView) v);
2540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 }
2542 }
2543
Sunny Goyal70660032015-05-14 00:07:08 -07002544 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002545 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002546 return false;
2547 }
2548
Michael Jurkaaf442092010-06-10 17:01:57 -07002549 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002550 * Event handler for the app widget view which has not fully restored.
2551 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002552 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002553 if (mIsSafeModeEnabled) {
2554 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2555 return;
2556 }
2557
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002558 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002559 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002560 int widgetId = info.appWidgetId;
2561 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2562 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002563 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2564 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002565 mPendingAddInfo.copyFrom(info);
2566 mPendingAddWidgetId = widgetId;
2567
Sunny Goyalffe83f12014-08-14 17:39:34 -07002568 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2569 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002570 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 } else if (info.installProgress < 0) {
2572 // The install has not been queued
2573 final String packageName = info.providerName.getPackageName();
2574 showBrokenAppInstallDialog(packageName,
2575 new DialogInterface.OnClickListener() {
2576 public void onClick(DialogInterface dialog, int id) {
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2578 }
2579 });
2580 } else {
2581 // Download has started.
2582 final String packageName = info.providerName.getPackageName();
2583 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002584 }
2585 }
2586
2587 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002588 * Event handler for the "grid" button that appears on the home screen, which
2589 * enters all apps mode.
2590 *
2591 * @param v The view that was clicked.
2592 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002593 protected void onClickAllAppsButton(View v) {
2594 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002595 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002596 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002597 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002598
2599 if (mLauncherCallbacks != null) {
2600 mLauncherCallbacks.onClickAllAppsButton(v);
2601 }
Winson Chung76648c52015-07-10 14:33:23 -07002602 }
2603 }
2604
2605 protected void onLongClickAllAppsButton(View v) {
2606 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2607 if (!isAppsViewVisible()) {
2608 showAppsView(true /* animated */, false /* resetListToTop */,
2609 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 }
2611 }
2612
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002613 private void showBrokenAppInstallDialog(final String packageName,
2614 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002615 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002616 .setTitle(R.string.abandoned_promises_title)
2617 .setMessage(R.string.abandoned_promise_explanation)
2618 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2619 .setNeutralButton(R.string.abandoned_clean_this,
2620 new DialogInterface.OnClickListener() {
2621 public void onClick(DialogInterface dialog, int id) {
2622 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2623 mWorkspace.removeAbandonedPromise(packageName, user);
2624 }
2625 })
2626 .create().show();
2627 return;
2628 }
2629
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 /**
2631 * Event handler for an app shortcut click.
2632 *
2633 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2634 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2637 Object tag = v.getTag();
2638 if (!(tag instanceof ShortcutInfo)) {
2639 throw new IllegalArgumentException("Input must be a Shortcut");
2640 }
2641
2642 // Open shortcut
2643 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002644
2645 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002646 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2647 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002648 // Launch activity anyway, framework will tell the user why the app is suspended.
2649 } else {
2650 int error = R.string.activity_not_available;
2651 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2652 error = R.string.safemode_shortcut_error;
2653 }
2654 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2655 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002656 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002657 }
2658
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002660 if ((v instanceof BubbleTextView)
2661 && shortcut.isPromise()
2662 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002663 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002664 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 new DialogInterface.OnClickListener() {
2666 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002667 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 return;
2671 }
2672
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002674 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002675
2676 if (mLauncherCallbacks != null) {
2677 mLauncherCallbacks.onClickAppShortcut(v);
2678 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 }
2680
Adam Cohen091440a2015-03-18 14:16:05 -07002681 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002683 final ShortcutInfo shortcut;
2684 final Intent intent;
2685 if (tag instanceof ShortcutInfo) {
2686 shortcut = (ShortcutInfo) tag;
2687 intent = shortcut.intent;
2688 int[] pos = new int[2];
2689 v.getLocationOnScreen(pos);
2690 intent.setSourceBounds(new Rect(pos[0], pos[1],
2691 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002692
Sunny Goyal508da152014-08-14 10:53:27 -07002693 } else if (tag instanceof AppInfo) {
2694 shortcut = null;
2695 intent = ((AppInfo) tag).intent;
2696 } else {
2697 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2698 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699
2700 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002701 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002702
2703 if (success && v instanceof BubbleTextView) {
2704 mWaitingForResume = (BubbleTextView) v;
2705 mWaitingForResume.setStayPressed(true);
2706 }
2707 }
2708
2709 /**
2710 * Event handler for a folder icon click.
2711 *
2712 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2713 */
2714 protected void onClickFolderIcon(View v) {
2715 if (LOGD) Log.d(TAG, "onClickFolder");
2716 if (!(v instanceof FolderIcon)){
2717 throw new IllegalArgumentException("Input must be a FolderIcon");
2718 }
2719
2720 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002721 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002723 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 }
Adam Cohen9211d422014-10-07 18:14:53 -07002725
2726 if (mLauncherCallbacks != null) {
2727 mLauncherCallbacks.onClickFolderIcon(v);
2728 }
Michael Jurka5130e402011-10-13 04:55:35 -07002729 }
2730
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002731 /**
2732 * Event handler for the (Add) Widgets button that appears after a long press
2733 * on the home screen.
2734 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002735 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002737 if (mIsSafeModeEnabled) {
2738 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2739 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002740 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002741 if (mLauncherCallbacks != null) {
2742 mLauncherCallbacks.onClickAddWidgetButton(view);
2743 }
Adam Cohen9211d422014-10-07 18:14:53 -07002744 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002745 }
2746
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002747 /**
2748 * Event handler for the wallpaper picker button that appears after a long press
2749 * on the home screen.
2750 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002751 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002752 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002753 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2754 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2755 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002756 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002757 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002758
2759 if (mLauncherCallbacks != null) {
2760 mLauncherCallbacks.onClickWallpaperPicker(v);
2761 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 }
2763
2764 /**
2765 * Event handler for a click on the settings button that appears after a long press
2766 * on the home screen.
2767 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002768 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002772 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002773 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002774 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002775 }
2776
Adam Cohen61f560d2013-09-30 15:58:20 -07002777 public View.OnTouchListener getHapticFeedbackTouchListener() {
2778 if (mHapticFeedbackTouchListener == null) {
2779 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002780 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002781 @Override
2782 public boolean onTouch(View v, MotionEvent event) {
2783 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2784 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2785 }
2786 return false;
2787 }
2788 };
2789 }
2790 return mHapticFeedbackTouchListener;
2791 }
2792
Adam Cohen9211d422014-10-07 18:14:53 -07002793 public void onDragStarted(View view) {
2794 if (isOnCustomContent()) {
2795 // Custom content screen doesn't participate in drag and drop. If on custom
2796 // content screen, move to default.
2797 moveWorkspaceToDefaultScreen();
2798 }
2799
2800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onDragStarted(view);
2802 }
2803 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002804
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002805 /**
2806 * Called when the user stops interacting with the launcher.
2807 * This implies that the user is now on the homescreen and is not doing housekeeping.
2808 */
Adam Cohen9211d422014-10-07 18:14:53 -07002809 protected void onInteractionEnd() {
2810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onInteractionEnd();
2812 }
2813 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002814
2815 /**
2816 * Called when the user starts interacting with the launcher.
2817 * The possible interactions are:
2818 * - open all apps
2819 * - reorder an app shortcut, or a widget
2820 * - open the overview mode.
2821 * This is a good time to stop doing things that only make sense
2822 * when the user is on the homescreen and not doing housekeeping.
2823 */
Adam Cohen9211d422014-10-07 18:14:53 -07002824 protected void onInteractionBegin() {
2825 if (mLauncherCallbacks != null) {
2826 mLauncherCallbacks.onInteractionBegin();
2827 }
2828 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002829
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 /** Updates the interaction state. */
2831 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002832 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002833 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002834 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2835 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002836 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002838 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002839 onInteractionEnd();
2840 }
2841 }
2842
Kenny Guyf07af7b2014-07-31 11:39:16 +01002843 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002844 try {
2845 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002846 launcherApps.showAppDetailsForProfile(componentName, user);
2847 } catch (SecurityException e) {
2848 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2849 Log.e(TAG, "Launcher does not have permission to launch settings");
2850 } catch (ActivityNotFoundException e) {
2851 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2852 Log.e(TAG, "Unable to launch settings");
2853 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002854 }
2855
Michael Jurka1e2f4652013-07-08 18:03:46 -07002856 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002857 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2858 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002859 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002860 // System applications cannot be installed. For now, show a toast explaining that.
2861 // We may give them the option of disabling apps this way.
2862 int messageId = R.string.uninstall_system_app_text;
2863 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002864 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002865 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002866 String packageName = componentName.getPackageName();
2867 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002868 Intent intent = new Intent(
2869 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002870 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2871 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002872 if (user != null) {
2873 user.addToIntent(intent, Intent.EXTRA_USER);
2874 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002876 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002877 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002878 }
2879
Winson Chung8f1eff72015-05-28 17:33:40 -07002880 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002881 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002883 // Only launch using the new animation if the shortcut has not opted out (this is a
2884 // private contract between launcher and may be ignored in the future).
2885 boolean useLaunchAnimation = (v != null) &&
2886 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002887 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2888 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002889
Kenny Guy1317e2d2014-05-08 18:52:50 +01002890 UserHandleCompat user = null;
2891 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2892 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2893 user = userManager.getUserForSerialNumber(serialNumber);
2894 }
2895
2896 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002897 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002898 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002899 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002900 int left = 0, top = 0;
2901 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2902 if (v instanceof TextView) {
2903 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002904 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2905 if (icon != null) {
2906 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002907 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002908 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002909 width = bounds.width();
2910 height = bounds.height();
2911 }
2912 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002913 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2914 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002915 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002916 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002917 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002918 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002919 // On L devices, we use the device default slide-up transition.
2920 // On L MR1 devices, we a custom version of the slide-up transition which
2921 // doesn't have the delay present in the device default.
2922 opts = ActivityOptions.makeCustomAnimation(this,
2923 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002924 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002925 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002926 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002927
2928 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2929 // Could be launching some bookkeeping activity
2930 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002931 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002932 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002933 launcherApps.startActivityForProfile(intent.getComponent(), user,
2934 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002935 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002936 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002938 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2939 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2940 // corresponding permission. Show the appropriate permission prompt if that
2941 // is the case.
2942 if (intent.getComponent() == null
2943 && Intent.ACTION_CALL.equals(intent.getAction())
2944 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2945 PackageManager.PERMISSION_GRANTED) {
2946 // TODO: Rename sPendingAddItem to a generic name.
2947 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2948 0, (ItemInfo) tag);
2949 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2950 REQUEST_PERMISSION_CALL_PHONE);
2951 return false;
2952 }
2953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002955 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002957 "or use the exported attribute for this activity. "
2958 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002960 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002962
Winson Chungbedf9232015-07-10 12:38:30 -07002963 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002964 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002965 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2966 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2967 return false;
2968 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002969 try {
2970 success = startActivity(v, intent, tag);
2971 } catch (ActivityNotFoundException e) {
2972 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2973 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2974 }
2975 return success;
2976 }
2977
Adam Cohen268c4752012-06-06 17:47:33 -07002978 /**
2979 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2980 * in the DragLayer in the exact absolute location of the original FolderIcon.
2981 */
2982 private void copyFolderIconToImage(FolderIcon fi) {
2983 final int width = fi.getMeasuredWidth();
2984 final int height = fi.getMeasuredHeight();
2985
2986 // Lazy load ImageView, Bitmap and Canvas
2987 if (mFolderIconImageView == null) {
2988 mFolderIconImageView = new ImageView(this);
2989 }
2990 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2991 mFolderIconBitmap.getHeight() != height) {
2992 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2993 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2994 }
2995
2996 DragLayer.LayoutParams lp;
2997 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2998 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2999 } else {
3000 lp = new DragLayer.LayoutParams(width, height);
3001 }
3002
Adam Cohen307fe232012-08-16 17:55:58 -07003003 // The layout from which the folder is being opened may be scaled, adjust the starting
3004 // view size by this scale factor.
3005 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003006 lp.customPosition = true;
3007 lp.x = mRectForFolderAnimation.left;
3008 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003009 lp.width = (int) (scale * width);
3010 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003011
3012 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3013 fi.draw(mFolderIconCanvas);
3014 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003015 if (fi.getFolder() != null) {
3016 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3017 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003018 }
Adam Cohen268c4752012-06-06 17:47:33 -07003019 // Just in case this image view is still in the drag layer from a previous animation,
3020 // we remove it and re-add it.
3021 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3022 mDragLayer.removeView(mFolderIconImageView);
3023 }
3024 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003025 if (fi.getFolder() != null) {
3026 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003027 }
Adam Cohen268c4752012-06-06 17:47:33 -07003028 }
3029
Sunny Goyal08442b82015-10-21 17:15:08 -07003030 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003031 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003032 FolderInfo info = (FolderInfo) fi.getTag();
3033 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3034 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003035 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3036 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003037 }
3038
Adam Cohen268c4752012-06-06 17:47:33 -07003039 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3040 copyFolderIconToImage(fi);
3041 fi.setVisibility(View.INVISIBLE);
3042
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003043 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3044 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003045 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003046 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3047 }
3048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003049 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003050 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003051 oa.end();
3052 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 }
3054
Sunny Goyal935fca12015-10-13 10:19:01 -07003055 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003056 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003057 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003058
Adam Cohen268c4752012-06-06 17:47:33 -07003059 // We remove and re-draw the FolderIcon in-case it has changed
3060 mDragLayer.removeView(mFolderIconImageView);
3061 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003062
3063 if (cl != null) {
3064 cl.clearFolderLeaveBehind();
3065 }
3066
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003067 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003068 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003069 oa.addListener(new AnimatorListenerAdapter() {
3070 @Override
3071 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003072 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003073 // Remove the ImageView copy of the FolderIcon and make the original visible.
3074 mDragLayer.removeView(mFolderIconImageView);
3075 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003076 }
3077 }
3078 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003079 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003080 if (!animate) {
3081 oa.end();
3082 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003083 }
3084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003086 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 * is animated relative to the specified View. If the View is null, no animation
3088 * is played.
3089 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003090 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003092 public void openFolder(FolderIcon folderIcon, boolean animate) {
3093 animate &= !Utilities.isPowerSaverOn(this);
3094
Adam Cohenfb91f302012-06-11 15:45:18 -07003095 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003096 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3097 if (openFolder != null && openFolder != folder) {
3098 // Close any open folder before opening a folder.
3099 closeFolder();
3100 }
3101
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Adam Cohena9cf38f2011-05-02 15:36:58 -07003104 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105
Sunny Goyalf4066152015-04-15 09:42:19 -07003106 // While the folder is open, the position of the icon cannot change.
3107 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3108
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 // Just verify that the folder hasn't already been added to the DragLayer.
3110 // There was a one-off crash where the folder had a parent already.
3111 if (folder.getParent() == null) {
3112 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003113 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003114 } else {
3115 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3116 folder.getParent() + ").");
3117 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003118 if (animate) {
3119 folder.animateOpen();
3120 } else {
3121 folder.open();
3122 }
3123 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003124
3125 // Notify the accessibility manager that this folder "window" has appeared and occluded
3126 // the workspace items
3127 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3128 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003129 }
3130
3131 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 closeFolder(true);
3133 }
3134
3135 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003136 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003137 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003138 if (folder.isEditingName()) {
3139 folder.dismissEditingName();
3140 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003141 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 }
3143 }
3144
Sunny Goyal935fca12015-10-13 10:19:01 -07003145 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003146 animate &= !Utilities.isPowerSaverOn(this);
3147
Adam Cohen4554ee12011-08-03 16:13:21 -07003148 folder.getInfo().opened = false;
3149
3150 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3151 if (parent != null) {
3152 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003153 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003154 if (fi != null) {
3155 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3156 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003157 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003158 if (animate) {
3159 folder.animateClosed();
3160 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003161 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003162 }
Winson Chung83ca4802013-04-12 15:10:52 -07003163
Sunny Goyal935fca12015-10-13 10:19:01 -07003164 // Notify the accessibility manager that this folder "window" has disappeared and no
3165 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003166 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 }
3168
Tony Wickhamdadb3042016-02-24 11:07:00 -08003169 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003171 if (!isDraggingEnabled()) return false;
3172 if (isWorkspaceLocked()) return false;
3173 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174
Winson Chung76648c52015-07-10 14:33:23 -07003175 if (v == mAllAppsButton) {
3176 onLongClickAllAppsButton(v);
3177 return true;
3178 }
3179
Adam Cohen1697b792013-09-17 19:08:21 -07003180 if (v instanceof Workspace) {
3181 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003182 if (!mWorkspace.isTouchActive()) {
3183 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003184 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3185 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3186 return true;
3187 } else {
3188 return false;
3189 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003190 } else {
3191 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003192 }
Adam Cohen1697b792013-09-17 19:08:21 -07003193 }
3194
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003195 CellLayout.CellInfo longClickCellInfo = null;
3196 View itemUnderLongClick = null;
3197 if (v.getTag() instanceof ItemInfo) {
3198 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003199 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003200 itemUnderLongClick = longClickCellInfo.cell;
3201 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 }
3203
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 // The hotseat touch handling does not go through Workspace, and we always allow long press
3205 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003206 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003207 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003208 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003209 // User long pressed on empty space
3210 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3211 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003212 if (mWorkspace.isInOverviewMode()) {
3213 mWorkspace.startReordering(v);
3214 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003215 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003218 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3219 mHotseat.getOrderInHotseat(
3220 longClickCellInfo.cellX,
3221 longClickCellInfo.cellY));
3222 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003224 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 }
3226 }
3227 }
3228 return true;
3229 }
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 boolean isHotseatLayout(View layout) {
3232 return mHotseat != null && layout != null &&
3233 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3234 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003235
Winson Chung3d503fb2011-07-13 17:25:49 -07003236 /**
3237 * Returns the CellLayout of the specified container at the specified screen.
3238 */
Sunny Goyal92820592015-03-02 11:31:35 -08003239 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3241 if (mHotseat != null) {
3242 return mHotseat.getLayout();
3243 } else {
3244 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003245 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003247 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003248 }
3249 }
3250
Winson Chungb745afb2015-03-02 11:51:23 -08003251 /**
3252 * For overridden classes.
3253 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003254 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003255 return isAppsViewVisible();
3256 }
3257
3258 public boolean isAppsViewVisible() {
3259 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3260 }
3261
3262 public boolean isWidgetsViewVisible() {
3263 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003264 }
3265
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003266 private void setWorkspaceBackground(int background) {
3267 switch (background) {
3268 case WORKSPACE_BACKGROUND_TRANSPARENT:
3269 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3270 break;
3271 case WORKSPACE_BACKGROUND_BLACK:
3272 getWindow().setBackgroundDrawable(null);
3273 break;
3274 default:
3275 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3276 }
Craig Mautner360310b2012-10-26 15:13:08 -07003277 }
3278
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003279 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003280 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3281 int curflags = getWindow().getAttributes().flags
3282 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3283 if (wpflags != curflags) {
3284 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3285 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003286 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003287 }
3288
Michael Jurkae326f182011-11-21 14:05:46 -08003289 @Override
3290 public void onTrimMemory(int level) {
3291 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003292 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3293 // The widget preview db can result in holding onto over
3294 // 3MB of memory for caching which isn't necessary.
3295 SQLiteDatabase.releaseMemory();
3296
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003297 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003298 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003299 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003300 if (mLauncherCallbacks != null) {
3301 mLauncherCallbacks.onTrimMemory(level);
3302 }
Michael Jurkae326f182011-11-21 14:05:46 -08003303 }
3304
Winson5c6bdbb2015-09-03 11:36:19 -07003305 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003306 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003307 }
3308
Winson5c6bdbb2015-09-03 11:36:19 -07003309 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003310 boolean changed = mState != State.WORKSPACE ||
3311 mWorkspace.getState() != Workspace.State.NORMAL;
3312 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003314 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003315 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003316
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 // Set focus to the AppsCustomize button
3318 if (mAllAppsButton != null) {
3319 mAllAppsButton.requestFocus();
3320 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003321 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003322
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003323 // Change the state *after* we've called all the transition code
3324 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003325
Winson Chung5fb63472011-02-02 17:03:37 -08003326 // Resume the auto-advance of widgets
3327 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003328 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003329
Winson Chung0f785722015-04-08 10:27:49 -07003330 if (changed) {
3331 // Send an accessibility event to announce the context change
3332 getWindow().getDecorView()
3333 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003334 }
Winson5c6bdbb2015-09-03 11:36:19 -07003335 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003336 }
3337
Winsone9f27272015-10-13 10:47:51 -07003338 /**
3339 * Shows the overview button.
3340 */
Adam Cohened307df2013-10-02 09:37:31 -07003341 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003342 showOverviewMode(animated, false);
3343 }
3344
3345 /**
3346 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3347 * onto one of the overview panel buttons.
3348 */
3349 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3350 Runnable postAnimRunnable = null;
3351 if (requestButtonFocus) {
3352 postAnimRunnable = new Runnable() {
3353 @Override
3354 public void run() {
3355 // Hitting the menu button when in touch mode does not trigger touch mode to
3356 // be disabled, so if requested, force focus on one of the overview panel
3357 // buttons.
3358 mOverviewPanel.requestFocusFromTouch();
3359 }
3360 };
3361 }
Adam Cohened307df2013-10-02 09:37:31 -07003362 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003363 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003364 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003365 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003366 }
3367
Winson Chungb745afb2015-03-02 11:51:23 -08003368 /**
3369 * Shows the apps view.
3370 */
Winson Chung76648c52015-07-10 14:33:23 -07003371 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3372 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003373 if (resetListToTop) {
3374 mAppsView.scrollToTop();
3375 }
Winson Chung4ac30062015-05-08 17:34:17 -07003376 if (updatePredictedApps) {
3377 tryAndUpdatePredictedApps();
3378 }
Winson Chung76648c52015-07-10 14:33:23 -07003379 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003380 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003381
Winson Chungb745afb2015-03-02 11:51:23 -08003382 /**
3383 * Shows the widgets view.
3384 */
3385 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003386 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003387 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003388 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003389 }
Winson Chung76648c52015-07-10 14:33:23 -07003390 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003391
3392 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003393 @Override
3394 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003395 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003396 }
3397 });
Winson Chungb745afb2015-03-02 11:51:23 -08003398 }
3399
3400 /**
3401 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003402 *
3403 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003404 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 // TODO: calling method should use the return value so that when {@code false} is returned
3406 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003407 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003408 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3409 mState != State.WIDGETS_SPRING_LOADED) {
3410 return false;
3411 }
3412 if (toState != State.APPS && toState != State.WIDGETS) {
3413 return false;
3414 }
Winson Chungb745afb2015-03-02 11:51:23 -08003415
3416 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003417 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3418 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003419 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003420 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003421 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003422
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003424 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
3426 // Pause the auto-advance of widgets until we are out of AllApps
3427 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003428 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429 closeFolder();
3430
3431 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003432 getWindow().getDecorView()
3433 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003434 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003435 }
3436
Winson Chungcd99cd32015-04-29 11:03:24 -07003437 /**
3438 * Updates the workspace and interaction state on state change, and return the animation to this
3439 * new state.
3440 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003441 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003442 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003443 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003444 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003445 updateInteraction(fromState, toState);
3446 return anim;
3447 }
3448
Jun Mukaif0033da2015-08-04 18:34:30 -07003449 public void onLauncherClingShown() {
3450 // When a launcher cling appears, it should cover the underlying layers, so their focus
3451 // should be blocked.
3452 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3453 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3454 }
3455 }
3456
3457 public void onLauncherClingDismissed() {
3458 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3459 }
3460
Hyunyoung Song3f471442015-04-08 19:01:34 -07003461 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003462 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003463 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003464 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003465 }
Winson Chungb745afb2015-03-02 11:51:23 -08003466
Winson Chung006ee262015-08-03 14:40:11 -07003467 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003468 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003469 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003470
3471 if (isAppsViewVisible()) {
3472 mState = State.APPS_SPRING_LOADED;
3473 } else if (isWidgetsViewVisible()) {
3474 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003475 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003476 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003477 } else {
3478 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003479 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003480 }
Adam Cohen7777d962011-08-18 18:58:38 -07003481
Hyunyoung Song3f471442015-04-08 19:01:34 -07003482 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003483 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003485
Winson Chunge7a03942011-08-05 15:05:12 -07003486 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003487 @Override
3488 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003489 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003490 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3491 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003492 // Before we show workspace, hide all apps again because
3493 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3494 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003495 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003496 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003497 } else {
3498 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003499 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003500 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003501 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003502 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003503
Tony Wickhame0c33232016-02-08 11:37:04 -08003504 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003505 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3506 || mState == State.WIDGETS_SPRING_LOADED;
3507 }
3508
Michael Jurkad3ef3062010-11-23 16:23:58 -08003509 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003510 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003511 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003512 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003513 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003514 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003515 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3516 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003518 }
3519
Winson Chung4ac30062015-05-08 17:34:17 -07003520 /**
3521 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3522 * resumed.
3523 */
3524 private void tryAndUpdatePredictedApps() {
3525 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003526 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003527 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003528 mAppsView.setPredictedApps(apps);
3529 }
3530 }
3531 }
3532
Michael Jurkab3e22d92011-10-31 15:58:33 -07003533 void lockAllApps() {
3534 // TODO
3535 }
3536
3537 void unlockAllApps() {
3538 // TODO
3539 }
3540
Sunny Goyal594d76d2014-11-06 10:12:54 -08003541 protected void disableVoiceButtonProxy(boolean disable) {
3542 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003543 }
3544
Winsonf768d932015-09-25 16:12:35 -07003545 public boolean launcherCallbacksProvidesSearch() {
3546 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3547 }
3548
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003549 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003550 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003551 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003552 }
3553
Adam Cohen24ce0b32014-01-14 16:18:14 -08003554 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3556 if (searchProvider == null) {
3557 return null;
3558 }
3559
3560 Bundle opts = new Bundle();
3561 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003562 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003563
Tony Wickham3a3517f2015-10-06 11:27:04 -07003564 // Determine the min and max dimensions of the widget.
3565 LauncherAppState app = LauncherAppState.getInstance();
3566 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3567 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3568 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003569 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3570 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003571 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003572 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003573 int minWidth = maxWidth;
3574 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003575 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3576 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3577 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3578 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3579 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003580 }
3581 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3582 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3583 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3584 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003585 if (LOGD) {
3586 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3587 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3588 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003589
Tony Wickham775455c2015-10-16 09:49:32 -07003590 if (mLauncherCallbacks != null) {
3591 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3592 }
3593
Sunny Goyalf7258242015-10-19 16:59:07 -07003594 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003595 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003596 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003597 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003598 || (widgetInfo == null)
3599 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003600 // A valid widget is not already bound.
3601 if (widgetId > -1) {
3602 mAppWidgetHost.deleteAppWidgetId(widgetId);
3603 widgetId = -1;
3604 }
3605
3606 // Try to bind a new widget
3607 widgetId = mAppWidgetHost.allocateAppWidgetId();
3608
3609 if (!AppWidgetManagerCompat.getInstance(this)
3610 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3611 mAppWidgetHost.deleteAppWidgetId(widgetId);
3612 widgetId = -1;
3613 }
3614
Sunny Goyalf7258242015-10-19 16:59:07 -07003615 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003616 .putInt(QSB_WIDGET_ID, widgetId)
3617 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003618 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003619 }
3620
Sunny Goyal8d595092015-07-06 12:22:14 -07003621 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003622 if (widgetId != -1) {
3623 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003624 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003625 mQsb.updateAppWidgetOptions(opts);
3626 mQsb.setPadding(0, 0, 0, 0);
3627 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003628 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003629 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003630 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003631 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003632 }
3633
Sunny Goyal22235bc2015-04-03 09:23:43 -07003634 private void reinflateQSBIfNecessary() {
3635 if (mQsb instanceof LauncherAppWidgetHostView &&
3636 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3637 mSearchDropTargetBar.removeView(mQsb);
3638 mQsb = null;
3639 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3640 }
3641 }
3642
Michael Jurkad7c28052012-04-27 15:43:36 -07003643 @Override
3644 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003645 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003646 final List<CharSequence> text = event.getText();
3647 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003648 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003649 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003650 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003651 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003652 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003653 } else if (mWorkspace != null) {
3654 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003655 } else {
3656 text.add(getString(R.string.all_apps_home_button_label));
3657 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003658 return result;
3659 }
3660
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003661 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003662 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003663 */
Adam Cohen091440a2015-03-18 14:16:05 -07003664 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003665 @Override
3666 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003667 closeSystemDialogs();
3668 }
3669 }
3670
3671 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 * If the activity is currently paused, signal that we need to run the passed Runnable
3673 * in onResume.
3674 *
3675 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003676 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3677 * wrong when we're not running, and if the activity comes back to what the configuration was
3678 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 *
3680 * Implementation of the method from LauncherModel.Callbacks.
3681 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003682 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003684 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003686 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003687 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003688 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003689 }
3690 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003691 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003692 return true;
3693 } else {
3694 return false;
3695 }
3696 }
3697
Michael Jurkac402cd92013-05-20 15:49:32 +02003698 private boolean waitUntilResume(Runnable run) {
3699 return waitUntilResume(run, false);
3700 }
3701
Michael Jurka1e2f4652013-07-08 18:03:46 -07003702 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003703 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003704 }
3705
Michael Jurka7607c2f2013-04-03 14:33:19 -07003706 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003707 * If the activity is currently paused, signal that we need to re-run the loader
3708 * in onResume.
3709 *
3710 * This needs to be called from incoming places where resources might have been loaded
3711 * while we are paused. That is becaues the Configuration might be wrong
3712 * when we're not running, and if it comes back to what it was when we
3713 * were paused, we are not restarted.
3714 *
3715 * Implementation of the method from LauncherModel.Callbacks.
3716 *
3717 * @return true if we are currently paused. The caller might be able to
3718 * skip some work in that case since we will come back again.
3719 */
3720 public boolean setLoadOnResume() {
3721 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003722 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003723 mOnResumeNeedsLoad = true;
3724 return true;
3725 } else {
3726 return false;
3727 }
3728 }
3729
3730 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003734 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003735 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003736 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003737 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003740
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003742 * Clear any pending bind callbacks. This is called when is loader is planning to
3743 * perform a full rebind from scratch.
3744 */
3745 @Override
3746 public void clearPendingBinds() {
3747 mBindOnResumeCallbacks.clear();
3748 if (mPendingExecutor != null) {
3749 mPendingExecutor.markCompleted();
3750 mPendingExecutor = null;
3751 }
3752 }
3753
3754 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003755 * Refreshes the shortcuts shown on the workspace.
3756 *
3757 * Implementation of the method from LauncherModel.Callbacks.
3758 */
3759 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003760 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003761
Winson Chungd64d1762013-08-20 14:37:16 -07003762 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003763 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003764 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003765
Michael Jurka05bf644e2011-11-30 20:28:53 -08003766 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003767 if (mHotseat != null) {
3768 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003769 }
3770 }
3771
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 @Override
3773 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003774 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003775
Adam Cohen5084cba2013-09-03 12:01:16 -07003776 // If there are no screens, we need to have an empty screen
3777 if (orderedScreenIds.size() == 0) {
3778 mWorkspace.addExtraEmptyScreen();
3779 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003780
3781 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003782 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003783 if (hasCustomContentToLeft()) {
3784 mWorkspace.createCustomContentContainer();
3785 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003786 }
Winson Chung64359a52013-07-08 17:17:08 -07003787 }
3788
3789 @Override
3790 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003791 int count = orderedScreenIds.size();
3792 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003793 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003794 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003795 }
3796
Winson Chungd64d1762013-08-20 14:37:16 -07003797 public void bindAppsAdded(final ArrayList<Long> newScreens,
3798 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003799 final ArrayList<ItemInfo> addAnimated,
3800 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003801 Runnable r = new Runnable() {
3802 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003803 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003804 }
3805 };
3806 if (waitUntilResume(r)) {
3807 return;
3808 }
3809
Winson Chungd64d1762013-08-20 14:37:16 -07003810 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003811 if (newScreens != null) {
3812 bindAddScreens(newScreens);
3813 }
Winson Chungd64d1762013-08-20 14:37:16 -07003814
3815 // We add the items without animation on non-visible pages, and with
3816 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003817 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003818 bindItems(addNotAnimated, 0,
3819 addNotAnimated.size(), false);
3820 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003821 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003822 bindItems(addAnimated, 0,
3823 addAnimated.size(), true);
3824 }
Winson Chungc58497e2013-09-03 17:48:37 -07003825
Winson Chung87412982013-10-03 18:34:14 -07003826 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003827 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003828
Winson Chungb745afb2015-03-02 11:51:23 -08003829 if (addedApps != null && mAppsView != null) {
3830 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003831 }
Winson Chungd64d1762013-08-20 14:37:16 -07003832 }
3833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003834 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 * Bind the items start-end from the list.
3836 *
3837 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003838 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003839 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003840 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3841 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003842 Runnable r = new Runnable() {
3843 public void run() {
3844 bindItems(shortcuts, start, end, forceAnimateIcons);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003848 return;
3849 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003850
Winson Chungf0c6ae02012-03-21 16:10:31 -07003851 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003852 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3853 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003854 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003856 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003857 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003859
3860 // Short circuit if we are loading dock items for a configuration which has no dock
3861 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3862 mHotseat == null) {
3863 continue;
3864 }
3865
Sunny Goyal639e9062015-08-19 19:17:06 -07003866 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 switch (item.itemType) {
3868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3869 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003871 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003872
Winson Chung64359a52013-07-08 17:17:08 -07003873 /*
3874 * TODO: FIX collision case
3875 */
Winson Chungce376632013-07-11 16:12:41 -07003876 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3877 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3878 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003879 View v = cl.getChildAt(item.cellX, item.cellY);
3880 Object tag = v.getTag();
3881 String desc = "Collision while binding workspace item: " + item
3882 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003883 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003884 throw (new RuntimeException(desc));
3885 } else {
3886 Log.d(TAG, desc);
3887 }
Winson Chungce376632013-07-11 16:12:41 -07003888 }
Winson Chung64359a52013-07-08 17:17:08 -07003889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003890 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003891 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003892 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003893 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003894 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003896 default:
3897 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003899
3900 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3901 item.cellY, 1, 1);
3902 if (animateIcons) {
3903 // Animate all the applications up now
3904 view.setAlpha(0f);
3905 view.setScaleX(0f);
3906 view.setScaleY(0f);
3907 bounceAnims.add(createNewAppBounceAnimation(view, i));
3908 newShortcutsScreenId = item.screenId;
3909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003910 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003911
Winson Chung997a9232013-07-24 15:33:46 -07003912 if (animateIcons) {
3913 // Animate to the correct page
3914 if (newShortcutsScreenId > -1) {
3915 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003916 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003917 final Runnable startBounceAnimRunnable = new Runnable() {
3918 public void run() {
3919 anim.playTogether(bounceAnims);
3920 anim.start();
3921 }
3922 };
Winson Chung997a9232013-07-24 15:33:46 -07003923 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003924 // We post the animation slightly delayed to prevent slowdowns
3925 // when we are loading right after we return to launcher.
3926 mWorkspace.postDelayed(new Runnable() {
3927 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003928 if (mWorkspace != null) {
3929 mWorkspace.snapToPage(newScreenIndex);
3930 mWorkspace.postDelayed(startBounceAnimRunnable,
3931 NEW_APPS_ANIMATION_DELAY);
3932 }
Winson Chung94d67682013-09-25 16:29:40 -07003933 }
3934 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003935 } else {
3936 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003937 }
3938 }
Winson Chung64359a52013-07-08 17:17:08 -07003939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003940 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003941 }
3942
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 /**
3944 * Implementation of the method from LauncherModel.Callbacks.
3945 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003946 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003947 Runnable r = new Runnable() {
3948 public void run() {
3949 bindFolders(folders);
3950 }
3951 };
3952 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003953 return;
3954 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003955 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957
3958 /**
3959 * Add the views for a widget to the workspace.
3960 *
3961 * Implementation of the method from LauncherModel.Callbacks.
3962 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003963 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003964 Runnable r = new Runnable() {
3965 public void run() {
3966 bindAppWidget(item);
3967 }
3968 };
3969 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003970 return;
3971 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003972
Daniel Sandler843e8602010-06-07 14:59:01 -04003973 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3974 if (DEBUG_WIDGETS) {
3975 Log.d(TAG, "bindAppWidget: " + item);
3976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 final Workspace workspace = mWorkspace;
3978
Adam Cohen59400422014-03-05 18:07:04 -08003979 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003980 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003981
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003982 if (!mIsSafeModeEnabled
3983 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003984 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3985
Sunny Goyalff572272014-07-23 13:58:07 -07003986 if (appWidgetInfo == null) {
3987 if (DEBUG_WIDGETS) {
3988 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3989 + " belongs to component " + item.providerName
3990 + ", as the povider is null");
3991 }
3992 LauncherModel.deleteItemFromDatabase(this, item);
3993 return;
3994 }
Sunny Goyalff572272014-07-23 13:58:07 -07003995
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003996 // If we do not have a valid id, try to bind an id.
3997 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3998 // Note: This assumes that the id remap broadcast is received before this step.
3999 // If that is not the case, the id remap will be ignored and user may see the
4000 // click to setup view.
4001 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
4002 pendingInfo.spanX = item.spanX;
4003 pendingInfo.spanY = item.spanY;
4004 pendingInfo.minSpanX = item.minSpanX;
4005 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004006 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004007
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004008 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4009 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4010 newWidgetId, appWidgetInfo, options);
4011
4012 // TODO consider showing a permission dialog when the widget is clicked.
4013 if (!success) {
4014 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4015 if (DEBUG_WIDGETS) {
4016 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4017 + " belongs to component " + item.providerName
4018 + ", as the launcher is unable to bing a new widget id");
4019 }
4020 LauncherModel.deleteItemFromDatabase(this, item);
4021 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004022 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004023
4024 item.appWidgetId = newWidgetId;
4025
4026 // If the widget has a configure activity, it is still needs to set it up, otherwise
4027 // the widget is ready to go.
4028 item.restoreStatus = (appWidgetInfo.configure == null)
4029 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4030 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4031
4032 LauncherModel.updateItemInDatabase(this, item);
4033 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4034 && (appWidgetInfo.configure == null)) {
4035 // If the ID is already valid, verify if we need to configure or not.
4036 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4037 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004038 }
Sunny Goyalff572272014-07-23 13:58:07 -07004039 }
4040
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004041 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004042 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004043 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004044 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4045 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004047
Sunny Goyal56c73602015-09-25 12:55:01 -07004048 // Verify that we own the widget
4049 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4050 if (info == null || appWidgetInfo == null ||
4051 !info.provider.equals(appWidgetInfo.provider)) {
Hyunyoung Song70264a92016-02-03 15:28:47 -08004052 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId + " info=" + info
4053 + " appWidgetInfo=" + appWidgetInfo);
Sunny Goyal56c73602015-09-25 12:55:01 -07004054 deleteWidgetInfo(item);
4055 return;
4056 }
4057
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004059 item.minSpanX = appWidgetInfo.minSpanX;
4060 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004061 } else {
4062 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004063 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4064 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004065 view.updateIcon(mIconCache);
4066 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004067 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004068 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004069 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07004071 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 workspace.requestLayout();
4073
Daniel Sandler843e8602010-06-07 14:59:01 -04004074 if (DEBUG_WIDGETS) {
4075 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4076 + (SystemClock.uptimeMillis()-start) + "ms");
4077 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 }
4079
Sunny Goyalff572272014-07-23 13:58:07 -07004080 /**
4081 * Restores a pending widget.
4082 *
4083 * @param appWidgetId The app widget id
4084 * @param cellInfo The position on screen where to create the widget.
4085 */
4086 private void completeRestoreAppWidget(final int appWidgetId) {
4087 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4088 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4089 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4090 return;
4091 }
4092
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004093 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004094 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4095
4096 mWorkspace.reinflateWidgetsIfNecessary();
4097 LauncherModel.updateItemInDatabase(this, info);
4098 }
4099
Adam Cohen1462de32012-07-24 22:34:36 -07004100 public void onPageBoundSynchronously(int page) {
4101 mSynchronouslyBoundPages.add(page);
4102 }
4103
Sunny Goyal527c7d32015-08-28 15:19:36 -07004104 @Override
4105 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4106 if (mPendingExecutor != null) {
4107 mPendingExecutor.markCompleted();
4108 }
4109 mPendingExecutor = executor;
4110 executor.attachTo(this);
4111 }
4112
4113 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4114 if (mPendingExecutor == executor) {
4115 mPendingExecutor = null;
4116 }
4117 }
4118
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 /**
4120 * Callback saying that there aren't any more items to bind.
4121 *
4122 * Implementation of the method from LauncherModel.Callbacks.
4123 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004124 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004125 Runnable r = new Runnable() {
4126 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004127 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004128 }
4129 };
4130 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004131 return;
4132 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 if (mSavedState != null) {
4134 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004135 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004136 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004137
4138 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4139 if (folderId != 0) {
4140 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4141 if (view instanceof FolderIcon) {
4142 FolderIcon icon = (FolderIcon) view;
4143 FolderInfo info = icon.getFolderInfo();
4144 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4145 mWorkspace.getNextPage());
4146 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4147 || info.screenId == currentScreenId) {
4148 // We can show the folder
4149 openFolder(icon, false);
4150 } else {
4151 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4152 " but current screen is " + currentScreenId);
4153 }
4154 }
4155 }
4156
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 mSavedState = null;
4158 }
4159
Adam Cohen1462de32012-07-24 22:34:36 -07004160 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004162 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004163 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004164
4165 // If we received the result of any pending adds while the loader was running (e.g. the
4166 // widget configuration forced an orientation change), process them now.
4167 if (sPendingAddItem != null) {
4168 final long screenId = completeAdd(sPendingAddItem);
4169
4170 // TODO: this moves the user to the page where the pending item was added. Ideally,
4171 // the screen would be guaranteed to exist after bind, and the page would be set through
4172 // the workspace restore process.
4173 mWorkspace.post(new Runnable() {
4174 @Override
4175 public void run() {
4176 mWorkspace.snapToScreenId(screenId);
4177 }
4178 });
4179 sPendingAddItem = null;
4180 }
4181
Sunny Goyal756adbc2015-04-16 15:20:51 -07004182 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004183
4184 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004185 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004186 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004187 }
4188
Adam Cohen3ed316a2014-07-23 18:21:20 -07004189 private void sendLoadingCompleteBroadcastIfNecessary() {
4190 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4191 String permission =
4192 getResources().getString(R.string.receive_first_load_broadcast_permission);
4193 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4194 sendBroadcast(intent, permission);
4195 SharedPreferences.Editor editor = mSharedPrefs.edit();
4196 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4197 editor.apply();
4198 }
4199 }
4200
Winson Chunga0b7e862013-09-05 16:03:15 -07004201 public boolean isAllAppsButtonRank(int rank) {
4202 if (mHotseat != null) {
4203 return mHotseat.isAllAppsButtonRank(rank);
4204 }
4205 return false;
4206 }
4207
Winson Chunga2413752012-04-03 14:22:34 -07004208 private boolean canRunNewAppsAnimation() {
4209 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004210 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4211 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004212 }
4213
Winson Chungc9168342013-06-26 14:54:55 -07004214 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004215 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004216 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4217 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004218 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004219 return bounceAnim;
4220 }
4221
Adam Cohen27772732013-11-11 14:00:56 +00004222 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004223 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004224 }
4225
Tony Wickham3a3517f2015-10-06 11:27:04 -07004226 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004227 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004228 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004229 }
4230
Tony Wickham55616cd2015-09-23 14:55:17 -07004231 public int getSearchBarHeight() {
4232 if (mLauncherCallbacks != null) {
4233 return mLauncherCallbacks.getSearchBarHeight();
4234 }
4235 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4236 }
4237
Winson Chung88fa7412015-08-03 14:25:28 -07004238 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004239 if (mSearchDropTargetBar == null) {
4240 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004241 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004242 if (mQsb != null) {
4243 mSearchDropTargetBar.removeView(mQsb);
4244 mQsb = null;
4245 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004246 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004247 }
4248
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004249 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004250 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4251 * multiple calls to bind the same list.)
4252 */
4253 @Thunk ArrayList<AppInfo> mTmpAppsList;
4254 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4255 public void run() {
4256 bindAllApplications(mTmpAppsList);
4257 mTmpAppsList = null;
4258 }
4259 };
4260
4261 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004262 * Add the icons for all apps.
4263 *
4264 * Implementation of the method from LauncherModel.Callbacks.
4265 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004266 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004267 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4268 mTmpAppsList = apps;
4269 return;
4270 }
4271
Winson Chungb745afb2015-03-02 11:51:23 -08004272 if (mAppsView != null) {
4273 mAppsView.setApps(apps);
4274 }
Adam Cohen9211d422014-10-07 18:14:53 -07004275 if (mLauncherCallbacks != null) {
4276 mLauncherCallbacks.bindAllApplications(apps);
4277 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004278 }
4279
4280 /**
4281 * A package was updated.
4282 *
4283 * Implementation of the method from LauncherModel.Callbacks.
4284 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004285 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004286 Runnable r = new Runnable() {
4287 public void run() {
4288 bindAppsUpdated(apps);
4289 }
4290 };
4291 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004292 return;
4293 }
4294
Winson Chungb745afb2015-03-02 11:51:23 -08004295 if (mAppsView != null) {
4296 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004298 }
4299
Sunny Goyal4390ace2014-10-13 11:33:11 -07004300 @Override
4301 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4302 Runnable r = new Runnable() {
4303 public void run() {
4304 bindWidgetsRestored(widgets);
4305 }
4306 };
4307 if (waitUntilResume(r)) {
4308 return;
4309 }
4310 mWorkspace.widgetsRestored(widgets);
4311 }
4312
Joe Onorato9c1289c2009-08-17 11:03:03 -04004313 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004314 * Some shortcuts were updated in the background.
4315 *
4316 * Implementation of the method from LauncherModel.Callbacks.
4317 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004318 @Override
4319 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4320 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004321 Runnable r = new Runnable() {
4322 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004323 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004324 }
4325 };
4326 if (waitUntilResume(r)) {
4327 return;
4328 }
4329
Sunny Goyal4390ace2014-10-13 11:33:11 -07004330 if (!updated.isEmpty()) {
4331 mWorkspace.updateShortcuts(updated);
4332 }
4333
4334 if (!removed.isEmpty()) {
4335 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4336 for (ShortcutInfo si : removed) {
4337 removedComponents.add(si.getTargetComponent());
4338 }
4339 mWorkspace.removeItemsByComponentName(removedComponents, user);
4340 // Notify the drag controller
4341 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004342 }
4343 }
4344
4345 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004346 * Update the state of a package, typically related to install state.
4347 *
4348 * Implementation of the method from LauncherModel.Callbacks.
4349 */
Sunny Goyale755d462014-07-22 13:48:29 -07004350 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004351 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4352 Runnable r = new Runnable() {
4353 public void run() {
4354 bindRestoreItemsChange(updates);
4355 }
4356 };
4357 if (waitUntilResume(r)) {
4358 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004359 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004360
Sunny Goyal756adbc2015-04-16 15:20:51 -07004361 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004362 }
4363
4364 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004365 * A package was uninstalled. We take both the super set of packageNames
4366 * in addition to specific applications to remove, the reason being that
4367 * this can be called when a package is updated as well. In that scenario,
4368 * we only remove specific components from the workspace, where as
4369 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004371 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004372 * Implementation of the method from LauncherModel.Callbacks.
4373 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004374 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004375 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004376 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004377 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004378 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004379 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004380 }
Winson Chungd64d1762013-08-20 14:37:16 -07004381 };
4382 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004383 return;
4384 }
4385
Sunny Goyal1a745e82014-10-02 15:58:31 -07004386 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004387 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4388 for (AppInfo info : appInfos) {
4389 removedComponents.add(info.componentName);
4390 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004391 if (!packageNames.isEmpty()) {
4392 mWorkspace.removeItemsByPackageName(packageNames, user);
4393 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004394 if (!removedComponents.isEmpty()) {
4395 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004396 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004397 // Notify the drag controller
4398 mDragController.onAppsRemoved(packageNames, removedComponents);
4399
Sunny Goyal1a745e82014-10-02 15:58:31 -07004400 } else {
4401 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004402 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004403
Winson Chungdf95eb12013-10-16 14:57:07 -07004404 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004405 if (mAppsView != null) {
4406 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004408 }
Joe Onoratobe386092009-11-17 17:32:16 -08004409
Michael Jurkac402cd92013-05-20 15:49:32 +02004410 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004411 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004412 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004413 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004414 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004415
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004416 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004417 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004418 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004419 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004420 return;
4421 }
4422
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004423 if (mWidgetsView != null && model != null) {
4424 mWidgetsView.addWidgets(model);
4425 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004426 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004427 }
4428
Winson Chung400438b2011-01-16 17:53:48 -08004429 private int mapConfigurationOriActivityInfoOri(int configOri) {
4430 final Display d = getWindowManager().getDefaultDisplay();
4431 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4432 switch (d.getRotation()) {
4433 case Surface.ROTATION_0:
4434 case Surface.ROTATION_180:
4435 // We are currently in the same basic orientation as the natural orientation
4436 naturalOri = configOri;
4437 break;
4438 case Surface.ROTATION_90:
4439 case Surface.ROTATION_270:
4440 // We are currently in the other basic orientation to the natural orientation
4441 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4442 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4443 break;
4444 }
4445
4446 int[] oriMap = {
4447 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4448 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4449 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4450 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4451 };
4452 // Since the map starts at portrait, we need to offset if this device's natural orientation
4453 // is landscape.
4454 int indexOffset = 0;
4455 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4456 indexOffset = 1;
4457 }
4458 return oriMap[(d.getRotation() + indexOffset) % 4];
4459 }
Adam Cohen446e9402011-09-15 18:21:21 -07004460
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004461 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004462 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004463 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004464 if (Utilities.ATLEAST_JB_MR2) {
4465 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4466 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004467 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4468 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004469 }
Winson Chung4b919f82012-05-01 10:44:08 -07004470 }
4471 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004472
Winson Chung4b919f82012-05-01 10:44:08 -07004473 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004474 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004475 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004476 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004477 } else {
4478 mHandler.postDelayed(new Runnable() {
4479 public void run() {
4480 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4481 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004482 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004483 }
Winson Chung4b919f82012-05-01 10:44:08 -07004484 }
Winson Chung400438b2011-01-16 17:53:48 -08004485 }
4486
Winson Chunge43a1e72014-01-15 10:33:02 -08004487 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004488 if (mLauncherCallbacks != null) {
4489 return mLauncherCallbacks.isLauncherPreinstalled();
4490 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004491 PackageManager pm = getPackageManager();
4492 try {
4493 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4494 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4495 return true;
4496 } else {
4497 return false;
4498 }
4499 } catch (NameNotFoundException e) {
4500 e.printStackTrace();
4501 return false;
4502 }
4503 }
4504
Adam Cohenea90f832014-05-21 15:03:34 -07004505 /**
4506 * This method indicates whether or not we should suggest default wallpaper dimensions
4507 * when our wallpaper cropper was not yet used to set a wallpaper.
4508 */
4509 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004510 if (mLauncherCallbacks != null) {
4511 return mLauncherCallbacks.overrideWallpaperDimensions();
4512 }
Adam Cohenea90f832014-05-21 15:03:34 -07004513 return true;
4514 }
4515
Adam Cohen432609a2014-03-13 17:03:22 -07004516 /**
4517 * To be overridden by subclasses to indicate that there is an activity to launch
4518 * before showing the standard launcher experience.
4519 */
4520 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004521 if (mLauncherCallbacks != null) {
4522 return mLauncherCallbacks.hasFirstRunActivity();
4523 }
Adam Cohen432609a2014-03-13 17:03:22 -07004524 return false;
4525 }
4526
4527 /**
4528 * To be overridden by subclasses to launch any first run activity
4529 */
4530 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004531 if (mLauncherCallbacks != null) {
4532 return mLauncherCallbacks.getFirstRunActivity();
4533 }
Adam Cohen432609a2014-03-13 17:03:22 -07004534 return null;
4535 }
4536
Winson Chunga6945242014-01-08 14:04:34 -08004537 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004538 return !ActivityManager.isRunningInTestHarness() &&
4539 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004540 }
4541
Adam Cohen4a9423d2014-03-28 14:22:31 -07004542 protected boolean hasRunFirstRunActivity() {
4543 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4544 }
4545
Adam Cohen432609a2014-03-13 17:03:22 -07004546 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004547 if (shouldRunFirstRunActivity() &&
4548 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004549 Intent firstRunIntent = getFirstRunActivity();
4550 if (firstRunIntent != null) {
4551 startActivity(firstRunIntent);
4552 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004553 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004554 }
4555 }
Adam Cohen432609a2014-03-13 17:03:22 -07004556 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004557 }
4558
4559 private void markFirstRunActivityShown() {
4560 SharedPreferences.Editor editor = mSharedPrefs.edit();
4561 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4562 editor.apply();
4563 }
4564
Adam Cohen432609a2014-03-13 17:03:22 -07004565 /**
4566 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4567 * screen that must be displayed and dismissed.
4568 */
4569 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004570 if (mLauncherCallbacks != null) {
4571 return mLauncherCallbacks.hasDismissableIntroScreen();
4572 }
Adam Cohen432609a2014-03-13 17:03:22 -07004573 return false;
4574 }
4575
4576 /**
4577 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4578 */
4579 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004580 if (mLauncherCallbacks != null) {
4581 return mLauncherCallbacks.getIntroScreen();
4582 }
Adam Cohen432609a2014-03-13 17:03:22 -07004583 return null;
4584 }
4585
4586 /**
4587 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4588 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4589 */
4590 private boolean shouldShowIntroScreen() {
4591 return hasDismissableIntroScreen() &&
4592 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4593 }
4594
4595 protected void showIntroScreen() {
4596 View introScreen = getIntroScreen();
4597 changeWallpaperVisiblity(false);
4598 if (introScreen != null) {
4599 mDragLayer.showOverlayView(introScreen);
4600 }
4601 }
4602
4603 public void dismissIntroScreen() {
4604 markIntroScreenDismissed();
4605 if (showFirstRunActivity()) {
4606 // We delay hiding the intro view until the first run activity is showing. This
4607 // avoids a blip.
4608 mWorkspace.postDelayed(new Runnable() {
4609 @Override
4610 public void run() {
4611 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004612 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004613 }
4614 }, ACTIVITY_START_DELAY);
4615 } else {
4616 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004617 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004618 }
4619 changeWallpaperVisiblity(true);
4620 }
4621
4622 private void markIntroScreenDismissed() {
4623 SharedPreferences.Editor editor = mSharedPrefs.edit();
4624 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4625 editor.apply();
4626 }
4627
Adam Cohen091440a2015-03-18 14:16:05 -07004628 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004629 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4630 // on the device, then we always show the first run cling experience (or if there is no
4631 // launcher2). Otherwise, we prompt the user upon started for migration
4632 LauncherClings launcherClings = new LauncherClings(this);
4633 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004634 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004635 if (mModel.canMigrateFromOldLauncherDb(this)) {
4636 launcherClings.showMigrationCling();
4637 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004638 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004639 }
4640 }
4641 }
4642
Winson Chunga6945242014-01-08 14:04:34 -08004643 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004644 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4645 if (mHotseat != null) mHotseat.setAlpha(1f);
4646 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004647 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4648 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004649 }
4650
4651 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004652 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4653 if (mHotseat != null) mHotseat.setAlpha(0f);
4654 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004655 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4656 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004657 }
4658
Winson Chung5ffd51d2015-06-25 11:36:50 -07004659 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004660 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004661 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004662 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004663 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004664 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004665 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4666 if (userHandle != null) {
4667 user = UserHandleCompat.fromUser(userHandle);
4668 }
4669 }
4670 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004671 }
4672
4673 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004674 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004675 if (user == null) {
4676 user = UserHandleCompat.myUserHandle();
4677 }
4678
4679 // Called from search suggestion, add the profile extra to the intent to ensure that we
4680 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004681 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004682 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004683 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004684 return null;
4685 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004686 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004687 }
4688
Winson Chung5ffd51d2015-06-25 11:36:50 -07004689 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004690 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4691 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004692 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004693 UserHandleCompat.myUserHandle());
4694 }
4695
Winson Chung5ffd51d2015-06-25 11:36:50 -07004696 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004697 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4698 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004699 mWorkspace.onExternalDragStartedWithItem(dragView);
4700 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004701 }
4702
Winson Chung5ffd51d2015-06-25 11:36:50 -07004703 protected void moveWorkspaceToDefaultScreen() {
4704 mWorkspace.moveToDefaultScreen(false);
4705 }
4706
Anjali Koppalf5d29132014-02-28 13:33:27 -08004707 @Override
4708 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004709 if (mLauncherCallbacks != null) {
4710 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4711 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004712 }
4713
Winson Chung80baf5a2010-08-09 16:03:15 -07004714 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004715 * Returns a FastBitmapDrawable with the icon, accurately sized.
4716 */
4717 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4718 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4719 d.setFilterBitmap(true);
4720 resizeIconDrawable(d);
4721 return d;
4722 }
4723
4724 /**
4725 * Resizes an icon drawable to the correct icon size.
4726 */
Winson5fbe0742015-09-30 15:33:00 -07004727 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004728 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004729 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004730 }
4731
4732 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004733 * Prints out out state for debugging.
4734 */
4735 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004736 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004737 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004738 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4739 Log.d(TAG, "mRestoring=" + mRestoring);
4740 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004741 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004742 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004743 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004744
Daniel Sandler325dc232013-06-05 22:57:57 -04004745 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004746 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004747
4748 @Override
4749 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4750 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004751 // Dump workspace
4752 writer.println(prefix + "Workspace Items");
4753 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4754 writer.println(prefix + " Homescreen " + i);
4755
4756 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4757 for (int j = 0; j < layout.getChildCount(); j++) {
4758 Object tag = layout.getChildAt(j).getTag();
4759 if (tag != null) {
4760 writer.println(prefix + " " + tag.toString());
4761 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004762 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004763 }
Sunny Goyala1365452015-10-01 15:46:24 -07004764
4765 writer.println(prefix + " Hotseat");
4766 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4767 for (int j = 0; j < layout.getChildCount(); j++) {
4768 Object tag = layout.getChildAt(j).getTag();
4769 if (tag != null) {
4770 writer.println(prefix + " " + tag.toString());
4771 }
4772 }
4773
4774 synchronized (sDumpLogs) {
4775 writer.println();
4776 writer.println(prefix + "Debug logs");
4777 for (String log : sDumpLogs) {
4778 writer.println(prefix + " " + log);
4779 }
4780 }
4781
Adam Cohen9211d422014-10-07 18:14:53 -07004782 if (mLauncherCallbacks != null) {
4783 mLauncherCallbacks.dump(prefix, fd, writer, args);
4784 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004785 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004786
Sunny Goyala1365452015-10-01 15:46:24 -07004787 public static void addDumpLog(String tag, String log) {
4788 Log.d(tag, log);
4789 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004790 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004791 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004792 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004793 }
4794
Adam Cohen59400422014-03-05 18:07:04 -08004795 public static CustomAppWidget getCustomAppWidget(String name) {
4796 return sCustomAppWidgets.get(name);
4797 }
4798
4799 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4800 return sCustomAppWidgets;
4801 }
4802
Sunny Goyal29538332016-02-18 09:10:19 -08004803 public static List<View> getFolderContents(View icon) {
4804 if (icon instanceof FolderIcon) {
4805 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4806 } else {
4807 return Collections.EMPTY_LIST;
4808 }
4809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004810}
Michael Jurka2763be32011-02-24 11:19:57 -08004811
Dan Sandlerd5024042014-01-09 15:01:33 -05004812interface DebugIntents {
4813 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4814 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004815}