blob: 2b03dc5883e817aaec141a47e722085af67618b7 [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 Song2bd3d7d2015-05-21 13:04:53 -0700111import com.android.launcher3.model.WidgetsModel;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800112import com.android.launcher3.userevent.Logger;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700113import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700114import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700115import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700116import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700117import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700118import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700119import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800121import com.android.wallpaperpicker.WallpaperUtils;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700122
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700123import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700127import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800128import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700129import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100137public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700138 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700139 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700140 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700141 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700144 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700145 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700147
Dan Sandlerd5024042014-01-09 15:01:33 -0500148 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Sunny Goyal28c6b962015-10-12 11:42:05 -0700158 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
159
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700160 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
161 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
162 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
163
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700164 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 // To turn on these properties, type
173 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Winson Chung2672ff92012-05-04 16:22:30 -0700176 // The Intent extra that defines whether to ignore the launch animation
177 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400178 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
181 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700182 // Type: int
183 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal08442b82015-10-21 17:15:08 -0700184 // Type: long
185 private static final String RUNTIME_STATE_OPEN_FOLDER_ID = "launcher.open_folder_id";
Sunny Goyal756cd262015-08-20 12:33:21 -0700186 // Type: Content Values / parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohen432609a2014-03-13 17:03:22 -0700193 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Adam Cohen3ed316a2014-07-23 18:21:20 -0700196 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
197 static final String ACTION_FIRST_LOAD_COMPLETE =
198 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
199
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 private static final String QSB_WIDGET_ID = "qsb_widget_id";
201 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800229 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700238 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800239 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800240 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700241
Michael Jurka0280c3b2010-09-17 15:00:07 -0700242 private int[] mTmpAddItemCellCoordinates = new int[2];
243
Sunny Goyal316490e2015-06-02 09:38:28 -0700244 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800245 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700248 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249
Winson Chungc51db6a2011-10-05 11:44:49 -0700250 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700251 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700256 // Main container view and the model for the widget tray screen.
257 @Thunk WidgetsContainerView mWidgetsView;
258 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
Sunny Goyal594d76d2014-11-06 10:12:54 -0800260 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700279 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700283 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700285 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Sunny Goyal639e9062015-08-19 19:17:06 -0700288 private LauncherClings mClings;
289
Sunny Goyale2df0622015-04-24 11:27:00 -0700290 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Adam Cohen61f560d2013-09-30 15:58:20 -0700292 private View.OnTouchListener mHapticFeedbackTouchListener;
293
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 // Related to the auto-advancing of widgets
295 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 private static final int ADVANCE_INTERVAL = 20000;
297 private static final int ADVANCE_STAGGER = 250;
298
299 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 private long mAutoAdvanceSentTime;
301 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700303
Winson Chung400438b2011-01-16 17:53:48 -0800304 // Determines how long to wait after a rotation before restoring the screen orientation to
305 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700312
Sunny Goyala1365452015-10-01 15:46:24 -0700313 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
314 private static final Date sDateStamp = new Date();
315 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700317
Winson Chung46353de2012-02-16 14:05:10 -0800318 // We only want to get the SharedPreferences once since it does an FS stat each time we get
319 // it from the context.
320 private SharedPreferences mSharedPrefs;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700329 private DeviceProfile mDeviceProfile;
330
Adam Cohen4b66bf32015-09-18 12:15:19 -0700331 private boolean mMoveToDefaultScreenFromNewIntent;
332
Winson Chung13eb5272015-05-11 16:30:13 -0700333 // This is set to the view that launched the activity that navigated the user away from
334 // launcher. Since there is no callback for when the activity has finished launching, enable
335 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 private BubbleTextView mWaitingForResume;
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
339 new HashMap<String, CustomAppWidget>();
340
Adam Cohen59400422014-03-05 18:07:04 -0800341 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700342 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
343 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800344 }
345 }
346
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700349 if (mWorkspace != null) {
350 mWorkspace.buildPageHardwareLayers();
351 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 }
353 };
354
Adam Cohendb364c32014-05-20 14:23:40 -0700355 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356
Adam Cohen091440a2015-03-18 14:16:05 -0700357 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int requestCode;
359 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700361 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int cellX;
363 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700364 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 }
366
Daniel Sandlerff02d492013-08-05 02:12:05 -0400367 private Stats mStats;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800368 private Logger mUserEventLogger;
369
Adam Cohenf9c184a2016-01-15 16:47:43 -0800370 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal7779d622015-06-11 16:18:39 -0700382 private Runnable mUpdateOrientationRunnable = new Runnable() {
383 public void run() {
384 setOrientation();
385 }
386 };
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 mDeviceProfile = getResources().getConfiguration().orientation
415 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700416 app.getInvariantDeviceProfile().landscapeProfile
417 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700418
Sunny Goyalf7258242015-10-19 16:59:07 -0700419 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700420 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800421 mModel = app.setLauncher(this);
422 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700425 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800427 mUserEventLogger = new Logger(this);
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
429
Sunny Goyalffe83f12014-08-14 17:39:34 -0700430 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
433 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700435 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
436 // this also ensures that any synchronous binding below doesn't re-trigger another
437 // LauncherModel load.
438 mPaused = false;
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200441 android.os.Debug.startMethodTracing(
442 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700446
Tony Wickhameef44322015-10-20 13:24:36 -0700447 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
448 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700450 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700485 }
486
487 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
488 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400489 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
493 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700494
495 if (shouldShowIntroScreen()) {
496 showIntroScreen();
497 } else {
498 showFirstRunActivity();
499 showFirstRunClings();
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
502
Sunny Goyal7779d622015-06-11 16:18:39 -0700503 @Override
504 public void onSettingsChanged(String settings, boolean value) {
505 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
506 mRotationEnabled = value;
507 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
508 mUpdateOrientationRunnable.run();
509 }
510 }
511 }
512
Adam Cohen9211d422014-10-07 18:14:53 -0700513 private LauncherCallbacks mLauncherCallbacks;
514
515 public void onPostCreate(Bundle savedInstanceState) {
516 super.onPostCreate(savedInstanceState);
517 if (mLauncherCallbacks != null) {
518 mLauncherCallbacks.onPostCreate(savedInstanceState);
519 }
520 }
521
Sunny Goyal32554d12015-12-03 15:31:25 -0800522 /**
523 * Call this after onCreate to set or clear overlay.
524 */
525 public void setLauncherOverlay(LauncherOverlay overlay) {
526 if (overlay != null) {
527 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
528 }
529 mWorkspace.setLauncherOverlay(overlay);
530 }
531
Adam Cohen9211d422014-10-07 18:14:53 -0700532 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
533 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 private boolean mWorkspaceImportanceStored = false;
536 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 private int mWorkspaceImportanceForAccessibility =
538 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
539 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540
541 @Override
542 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 return;
545 }
546 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700547 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspace != null) {
549 mWorkspaceImportanceForAccessibility =
550 mWorkspace.getImportantForAccessibility();
551 mWorkspace.setImportantForAccessibility(
552 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
553 mWorkspaceImportanceStored = true;
554 }
555 if (mHotseat != null) {
556 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
557 mHotseat.setImportantForAccessibility(
558 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
559 mHotseatImportanceStored = true;
560 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700561 }
562
563 @Override
564 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700565 if (mWorkspaceImportanceStored && mWorkspace != null) {
566 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
567 }
568 if (mHotseatImportanceStored && mHotseat != null) {
569 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
570 }
571 mWorkspaceImportanceStored = false;
572 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700573 }
574 });
Adam Cohen9211d422014-10-07 18:14:53 -0700575 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700576 }
577
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700579 public void onLauncherProviderChange() {
580 if (mLauncherCallbacks != null) {
581 mLauncherCallbacks.onLauncherProviderChange();
582 }
583 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584
Winson Chungef7f8742015-06-04 17:18:17 -0700585 /**
586 * Updates the bounds of all the overlays to match the new fixed bounds.
587 */
588 public void updateOverlayBounds(Rect newBounds) {
589 mAppsView.setSearchBarBounds(newBounds);
590 mWidgetsView.setSearchBarBounds(newBounds);
591 }
592
Adam Cohen9211d422014-10-07 18:14:53 -0700593 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700595 if (mLauncherCallbacks != null) {
596 return mLauncherCallbacks.hasCustomContentToLeft();
597 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700598 return false;
599 }
600
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 * {@link #addToCustomContentPage}. This will only be invoked if
604 * {@link #hasCustomContentToLeft()} is {@code true}.
605 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500606 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700607 if (mLauncherCallbacks != null) {
608 mLauncherCallbacks.populateCustomContentContainer();
609 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 }
611
612 /**
613 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
614 * ensure the custom content page is added or removed if necessary.
615 */
616 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600617 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 // Not bound yet, wait for bindScreens to be called.
619 return;
620 }
621
622 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
623 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500624 mWorkspace.createCustomContentContainer();
625 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
627 mWorkspace.removeCustomContentPage();
628 }
629 }
630
Anton Hanssona2f665f2013-09-26 12:18:32 +0100631 public Stats getStats() {
632 return mStats;
633 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800634 public Logger getLogger() {return mUserEventLogger; }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100635
Hyunyoung Song3f471442015-04-08 19:01:34 -0700636 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700637 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
638 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700639 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700640 }
641
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000642 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700643 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
644 // This cast is safe as long as the id < 0x00FFFFFF
645 // Since we jail all the dynamically generated views, there should be no clashes
646 // with any other views.
647 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000648 }
649
650 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700651 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
652 * a configuration step, this allows the proper animations to run after other transitions.
653 */
Adam Cohendb364c32014-05-20 14:23:40 -0700654 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700655 long screenId = args.screenId;
656 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
657 // When the screen id represents an actual screen (as opposed to a rank) we make sure
658 // that the drop page actually exists.
659 screenId = ensurePendingDropLayoutExists(args.screenId);
660 }
Adam Cohendb364c32014-05-20 14:23:40 -0700661
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700664 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700666 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700668 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700669 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700670 case REQUEST_RECONFIGURE_APPWIDGET:
671 completeRestoreAppWidget(args.appWidgetId);
672 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 }
Michael Jurka27614d22012-04-02 06:40:22 -0700674 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
675 // if you turned the screen off and then back while in All Apps, Launcher would not
676 // return to the workspace. Clearing mAddInfo.container here fixes this issue
677 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700678 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 }
680
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800681 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700682 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700683 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700684 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700685 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800686 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700687
Adam Cohenad4e15c2013-10-17 16:21:35 -0700688 Runnable exitSpringLoaded = new Runnable() {
689 @Override
690 public void run() {
691 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
692 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
693 }
694 };
695
Michael Jurka8b805b12012-04-18 14:23:14 -0700696 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700697 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700698 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
700 if (resultCode == RESULT_CANCELED) {
701 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700702 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700703 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700704 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800705 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700707
708 // When the user has granted permission to bind widgets, we should check to see if
709 // we can inflate the default search bar widget.
710 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 }
712 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200713 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
714 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700715 // User could have free-scrolled between pages before picking a wallpaper; make sure
716 // we move to the closest one now.
717 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700718 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200719 }
720 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200722
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700724 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700725
Adam Cohendb364c32014-05-20 14:23:40 -0700726 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 // We have special handling for widgets
728 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800729 final int appWidgetId;
730 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
731 : -1;
732 if (widgetId < 0) {
733 appWidgetId = pendingAddWidgetId;
734 } else {
735 appWidgetId = widgetId;
736 }
737
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800739 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700740 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
741 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700742 result = RESULT_CANCELED;
743 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 @Override
746 public void run() {
747 exitSpringLoadedDragModeDelayed(false, 0, null);
748 }
749 };
Adam Cohendb364c32014-05-20 14:23:40 -0700750 if (workspaceLocked) {
751 // No need to remove the empty screen if we're mid-binding, as the
752 // the bind will not add the empty screen.
753 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
754 } else {
755 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
756 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
757 }
Winson Chung5aaab772012-04-27 15:24:02 -0700758 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700759 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700760 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
761 // When the screen id represents an actual screen (as opposed to a rank)
762 // we make sure that the drop page actually exists.
763 mPendingAddInfo.screenId =
764 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
765 }
Adam Cohendb364c32014-05-20 14:23:40 -0700766 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
767
768 dropLayout.setDropPending(true);
769 final Runnable onComplete = new Runnable() {
770 @Override
771 public void run() {
772 completeTwoStageWidgetDrop(resultCode, appWidgetId);
773 dropLayout.setDropPending(false);
774 }
775 };
776 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
777 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
778 } else {
779 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
780 mPendingAddInfo);
781 sPendingAddItem = args;
782 }
Winson Chung5aaab772012-04-27 15:24:02 -0700783 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 return;
785 }
786
Sunny Goyalff572272014-07-23 13:58:07 -0700787 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
788 if (resultCode == RESULT_OK) {
789 // Update the widget view.
790 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
791 pendingAddWidgetId, mPendingAddInfo);
792 if (workspaceLocked) {
793 sPendingAddItem = args;
794 } else {
795 completeAdd(args);
796 }
797 }
798 // Leave the widget in the pending state if the user canceled the configure.
799 return;
800 }
801
Sunny Goyalaad90582015-07-09 14:22:50 -0700802 if (requestCode == REQUEST_CREATE_SHORTCUT) {
803 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
804 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
805 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
806 mPendingAddInfo);
807 if (isWorkspaceLocked()) {
808 sPendingAddItem = args;
809 } else {
810 completeAdd(args);
811 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813 }
814 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700815 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
816 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800820
821 }
822
823 @Override
824 protected void onActivityResult(
825 final int requestCode, final int resultCode, final Intent data) {
826 handleActivityResult(requestCode, resultCode, data);
827 if (mLauncherCallbacks != null) {
828 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
829 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800830 }
831
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600832 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700833 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600834 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
836 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
837 View v = null;
838 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
839 if (layout != null) {
840 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
841 }
842 Intent intent = sPendingAddItem.intent;
843 sPendingAddItem = null;
844 if (grantResults.length > 0
845 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
846 startActivity(v, intent, null);
847 } else {
848 // TODO: Show a snack bar with link to settings
849 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
850 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
851 }
852 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 if (mLauncherCallbacks != null) {
854 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
855 grantResults);
856 }
857 }
858
Adam Cohendb364c32014-05-20 14:23:40 -0700859 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
860 appWidgetId, ItemInfo info) {
861 PendingAddArguments args = new PendingAddArguments();
862 args.requestCode = requestCode;
863 args.intent = data;
864 args.container = info.container;
865 args.screenId = info.screenId;
866 args.cellX = info.cellX;
867 args.cellY = info.cellY;
868 args.appWidgetId = appWidgetId;
869 return args;
870 }
871
872 /**
873 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
874 *
875 * @param screenId the screen id to check
876 * @return the new screen, or screenId if it exists
877 */
878 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800879 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700880 if (dropLayout == null) {
881 // it's possible that the add screen was removed because it was
882 // empty and a re-bind occurred
883 mWorkspace.addExtraEmptyScreen();
884 return mWorkspace.commitExtraEmptyScreen();
885 } else {
886 return screenId;
887 }
888 }
889
Adam Cohen091440a2015-03-18 14:16:05 -0700890 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800891 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 Runnable onCompleteRunnable = null;
893 int animationType = 0;
894
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700895 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 if (resultCode == RESULT_OK) {
897 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
898 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 mPendingAddWidgetInfo);
900 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800901 onCompleteRunnable = new Runnable() {
902 @Override
903 public void run() {
904 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700906 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
907 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 }
909 };
910 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800911 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 if (mDragLayer.getAnimatedView() != null) {
915 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
916 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
917 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700918 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 // The animated view may be null in the case of a rotation during widget configuration
920 onCompleteRunnable.run();
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700925 protected void onStop() {
926 super.onStop();
927 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700928
929 if (mLauncherCallbacks != null) {
930 mLauncherCallbacks.onStop();
931 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700932 }
933
934 @Override
935 protected void onStart() {
936 super.onStart();
937 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700938
939 if (mLauncherCallbacks != null) {
940 mLauncherCallbacks.onStart();
941 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 }
943
944 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200946 long startTime = 0;
947 if (DEBUG_RESUME_TIME) {
948 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400949 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200950 }
Adam Cohen9211d422014-10-07 18:14:53 -0700951
952 if (mLauncherCallbacks != null) {
953 mLauncherCallbacks.preOnResume();
954 }
955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800957 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700958
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700960 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700961 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800962 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700963 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700964 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700965 // view after launching an app, as they may be depending on the UI to be static to
966 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700967 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700968 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800969 } else if (mOnResumeState == State.WIDGETS) {
970 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700971 }
972 mOnResumeState = State.NONE;
973
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700974 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700975 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
976 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700977
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800978 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700979 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700980 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700981 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700983 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700985 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200986 // We might have postponed some bind calls until onResume (see waitUntilResume) --
987 // execute them here
988 long startTimeCallbacks = 0;
989 if (DEBUG_RESUME_TIME) {
990 startTimeCallbacks = System.currentTimeMillis();
991 }
992
Michael Jurka1e2f4652013-07-08 18:03:46 -0700993 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
994 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700996 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200997 if (DEBUG_RESUME_TIME) {
998 Log.d(TAG, "Time spent processing callbacks in onResume: " +
999 (System.currentTimeMillis() - startTimeCallbacks));
1000 }
Michael Jurka447bf842013-05-15 14:52:15 +02001001 }
Michael Jurka54554252013-08-01 12:52:23 +02001002 if (mOnResumeCallbacks.size() > 0) {
1003 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1004 mOnResumeCallbacks.get(i).run();
1005 }
1006 mOnResumeCallbacks.clear();
1007 }
Winson Chunge4e50662012-01-23 14:45:13 -08001008
1009 // Reset the pressed state of icons that were locked in the press state while activities
1010 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001011 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001012 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001013 mWaitingForResume.setStayPressed(false);
1014 }
Winson Chung82963d52013-10-09 11:20:57 -07001015
Adam Cohen06dff352012-06-01 17:17:08 -07001016 // It is possible that widgets can receive updates while launcher is not in the foreground.
1017 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1018 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1019 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001020 if (!isWorkspaceLoading()) {
1021 getWorkspace().reinflateWidgetsIfNecessary();
1022 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001023 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001024
Michael Jurka447bf842013-05-15 14:52:15 +02001025 if (DEBUG_RESUME_TIME) {
1026 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1027 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001028
Adam Cohen4b66bf32015-09-18 12:15:19 -07001029 // We want to suppress callbacks about CustomContent being shown if we have just received
1030 // onNewIntent while the user was present within launcher. In that case, we post a call
1031 // to move the user to the main screen (which will occur after onResume). We don't want to
1032 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1033 // suppress here.
1034 if (mWorkspace.getCustomContentCallbacks() != null
1035 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001036 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001037 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001038 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1039 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001040 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001041 }
1042 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001043 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001044 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001045 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001046
Sunny Goyal756adbc2015-04-16 15:20:51 -07001047 if (!isWorkspaceLoading()) {
1048 // Process any items that were added while Launcher was away.
1049 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1050 }
Adam Cohen9211d422014-10-07 18:14:53 -07001051
1052 if (mLauncherCallbacks != null) {
1053 mLauncherCallbacks.onResume();
1054 }
Adam Cohen06dff352012-06-01 17:17:08 -07001055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001059 // Ensure that items added to Launcher are queued until Launcher returns
1060 InstallShortcutReceiver.enableInstallQueue();
1061
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001063 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001064 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001065 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
1067 // We call onHide() aggressively. The custom content callbacks should be able to
1068 // debounce excess onHide calls.
1069 if (mWorkspace.getCustomContentCallbacks() != null) {
1070 mWorkspace.getCustomContentCallbacks().onHide();
1071 }
Romain Guycbb89e42009-06-08 15:52:54 -07001072
Adam Cohen9211d422014-10-07 18:14:53 -07001073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001075 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
1077
1078 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001079 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1080 // by a onResume or by scrolling otherwise.
1081 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001082
1083 // Custom content is completely hidden
1084 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001085
1086 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1087 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001088
1089 // Indicates whether the user is allowed to scroll away from the custom content.
1090 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
1092
Adam Cohenc2d6e892014-10-16 09:49:24 -07001093 public interface LauncherOverlay {
1094
1095 /**
1096 * Touch interaction leading to overscroll has begun
1097 */
1098 public void onScrollInteractionBegin();
1099
1100 /**
1101 * Touch interaction related to overscroll has ended
1102 */
1103 public void onScrollInteractionEnd();
1104
1105 /**
1106 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1107 * screen (or in the case of RTL, the rightmost screen).
1108 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001109 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001110
1111 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001112 * Called when the launcher is ready to use the overlay
1113 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001115 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001116 }
1117
Jun Mukai8af0cd82015-05-12 12:32:12 -07001118 public interface LauncherSearchCallbacks {
1119 /**
1120 * Called when the search overlay is shown.
1121 */
1122 public void onSearchOverlayOpened();
1123
1124 /**
1125 * Called when the search overlay is dismissed.
1126 */
1127 public void onSearchOverlayClosed();
1128 }
1129
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001131 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 }
1133
1134 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1135
Sunny Goyalc86df472016-02-25 09:19:38 -08001136 public void onScrollChanged(float progress) {
1137 if (mWorkspace != null) {
1138 mWorkspace.onOverlayScrollChanged(progress);
1139 }
1140 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001141 }
1142
Jorim Jaggid017f882014-01-14 17:08:48 -08001143 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001144 if (mLauncherCallbacks != null) {
1145 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001146 } else {
1147 // On devices with a locked orientation, we will at least have the allow rotation
1148 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001149 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001150 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001151 }
1152
Adam Cohen9211d422014-10-07 18:14:53 -07001153 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001154 CustomContentCallbacks callbacks, String description) {
1155 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001156 }
1157
Adam Cohenedb40762013-07-18 16:45:45 -07001158 // The custom content needs to offset its content to account for the QSB
1159 public int getTopOffsetForCustomContent() {
1160 return mWorkspace.getPaddingTop();
1161 }
1162
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001163 @Override
1164 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001166 if (mModel.isCurrentCallbacks(this)) {
1167 mModel.stopLoader();
1168 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001169 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1170
Romain Guy13c2e7b2010-03-10 19:45:00 -08001171 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001172 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001174 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175 @Override
1176 public void onWindowFocusChanged(boolean hasFocus) {
1177 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001178 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001179
1180 if (mLauncherCallbacks != null) {
1181 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1182 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001183 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 private boolean acceptFilter() {
1186 final InputMethodManager inputManager = (InputMethodManager)
1187 getSystemService(Context.INPUT_METHOD_SERVICE);
1188 return !inputManager.isFullscreenMode();
1189 }
1190
1191 @Override
1192 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001193 final int uniChar = event.getUnicodeChar();
1194 final boolean handled = super.onKeyDown(keyCode, event);
1195 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1196 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1198 keyCode, event);
1199 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001200 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001201 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001202 // showSearchDialog()
1203 // If there are multiple keystrokes before the search dialog takes focus,
1204 // onSearchRequested() will be called for every keystroke,
1205 // but it is idempotent, so it's fine.
1206 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208 }
1209
Joe Onorato8a9625e2010-01-28 15:55:35 -08001210 // Eat the long press event so the keyboard doesn't come up.
1211 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1212 return true;
1213 }
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 return handled;
1216 }
1217
Winson46163472015-09-22 17:31:56 -07001218 @Override
1219 public boolean onKeyUp(int keyCode, KeyEvent event) {
1220 if (keyCode == KeyEvent.KEYCODE_MENU) {
1221 // Ignore the menu key if we are currently dragging or are on the custom content screen
1222 if (!isOnCustomContent() && !mDragController.isDragging()) {
1223 // Close any open folders
1224 closeFolder();
1225
1226 // Stop resizing any widgets
1227 mWorkspace.exitWidgetResizeMode();
1228
1229 // Show the overview mode if we are on the workspace
1230 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1231 !mWorkspace.isSwitchingState()) {
1232 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001233 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001234 }
1235 }
1236 return true;
1237 }
1238 return super.onKeyUp(keyCode, event);
1239 }
1240
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 private String getTypedText() {
1242 return mDefaultKeySsb.toString();
1243 }
1244
1245 private void clearTypedText() {
1246 mDefaultKeySsb.clear();
1247 mDefaultKeySsb.clearSpans();
1248 Selection.setSelection(mDefaultKeySsb, 0);
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001252 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1253 * State
1254 */
1255 private static State intToState(int stateOrdinal) {
1256 State state = State.WORKSPACE;
1257 final State[] stateValues = State.values();
1258 for (int i = 0; i < stateValues.length; i++) {
1259 if (stateValues[i].ordinal() == stateOrdinal) {
1260 state = stateValues[i];
1261 break;
1262 }
1263 }
1264 return state;
1265 }
1266
1267 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 * Restores the previous state, if it exists.
1269 *
1270 * @param savedState The previous state.
1271 */
1272 private void restoreState(Bundle savedState) {
1273 if (savedState == null) {
1274 return;
1275 }
1276
Michael Jurka883f55b2010-10-21 15:47:14 -07001277 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001278 if (state == State.APPS || state == State.WIDGETS) {
1279 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001280 }
1281
Adam Cohen21cd0022013-10-09 18:57:02 -07001282 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1283 PagedView.INVALID_RESTORE_PAGE);
1284 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001285 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
Sunny Goyal756cd262015-08-20 12:33:21 -07001288 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1289 if (itemValues != null) {
1290 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001291 AppWidgetProviderInfo info = savedState.getParcelable(
1292 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001293 mPendingAddWidgetInfo = info == null ?
1294 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1295
Adam Cohen4637b5a2013-11-04 18:21:24 -08001296 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001297 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 mRestoring = true;
1299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
1302 /**
1303 * Finds all the views we need and configure them properly.
1304 */
1305 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001306 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001307
Craig Mautner360310b2012-10-26 15:13:08 -07001308 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001309 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001310 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1311 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001312 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001313 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001314
John Spurlock77e1f472013-09-11 10:09:51 -04001315 mLauncherView.setSystemUiVisibility(
1316 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001317 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318
Winson Chung4c98d922011-05-31 16:50:48 -07001319 // Setup the drag layer
1320 mDragLayer.setup(this, dragController);
1321
Winson Chung3d503fb2011-07-13 17:25:49 -07001322 // Setup the hotseat
1323 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1324 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001325 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001326 }
1327
Winsone9f27272015-10-13 10:47:51 -07001328 // Setup the overview panel
1329 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330
Winson Chung4c98d922011-05-31 16:50:48 -07001331 // Setup the workspace
1332 mWorkspace.setHapticFeedbackEnabled(false);
1333 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001334 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001335 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001336
Tony Wickham34d2c912015-09-11 09:27:58 -07001337 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001338 mSearchDropTargetBar = (SearchDropTargetBar)
1339 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001340 // Get the app info bar
1341 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1342 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001343
Winson Chungef7f8742015-06-04 17:18:17 -07001344 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001345 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001346 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001347 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1348 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1349 } else {
1350 mAppsView.setSearchBarController(new DefaultAppSearchController());
1351 }
Craig Mautner360310b2012-10-26 15:13:08 -07001352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001354 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001355 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001356 dragController.setMoveTarget(mWorkspace);
1357 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001358 if (mSearchDropTargetBar != null) {
1359 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001360 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001361 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001362 if (mAppInfoDropTargetBar != null) {
1363 mAppInfoDropTargetBar.setup(this, dragController);
1364 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001365
Sunny Goyal322d5562015-06-25 19:35:49 -07001366 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1367 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
1370
Winsone9f27272015-10-13 10:47:51 -07001371 private void setupOverviewPanel() {
1372 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1373
1374 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1375 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1376 @Override
1377 public boolean onLongClick(View v) {
1378 return v.performClick();
1379 }
1380 };
1381
1382 // Bind wallpaper button actions
1383 View wallpaperButton = findViewById(R.id.wallpaper_button);
1384 wallpaperButton.setOnClickListener(new OnClickListener() {
1385 @Override
1386 public void onClick(View view) {
1387 if (!mWorkspace.isSwitchingState()) {
1388 onClickWallpaperPicker(view);
1389 }
1390 }
1391 });
1392 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1393 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1394
1395 // Bind widget button actions
1396 mWidgetsButton = findViewById(R.id.widget_button);
1397 mWidgetsButton.setOnClickListener(new OnClickListener() {
1398 @Override
1399 public void onClick(View view) {
1400 if (!mWorkspace.isSwitchingState()) {
1401 onClickAddWidgetButton(view);
1402 }
1403 }
1404 });
1405 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1406 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1407
1408 // Bind settings actions
1409 View settingsButton = findViewById(R.id.settings_button);
1410 boolean hasSettings = hasSettings();
1411 if (hasSettings) {
1412 settingsButton.setOnClickListener(new OnClickListener() {
1413 @Override
1414 public void onClick(View view) {
1415 if (!mWorkspace.isSwitchingState()) {
1416 onClickSettingsButton(view);
1417 }
1418 }
1419 });
1420 settingsButton.setOnLongClickListener(performClickOnLongClick);
1421 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1422 } else {
1423 settingsButton.setVisibility(View.GONE);
1424 }
1425
1426 mOverviewPanel.setAlpha(0f);
1427 }
1428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001430 * Sets the all apps button. This method is called from {@link Hotseat}.
1431 */
1432 public void setAllAppsButton(View allAppsButton) {
1433 mAllAppsButton = allAppsButton;
1434 }
1435
1436 public View getAllAppsButton() {
1437 return mAllAppsButton;
1438 }
1439
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001440 public View getWidgetsButton() {
1441 return mWidgetsButton;
1442 }
1443
Anjali Koppal5ad44842014-03-10 20:34:39 -07001444 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 * Creates a view representing a shortcut.
1446 *
1447 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001449 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001450 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
1453 /**
1454 * Creates a view representing a shortcut inflated from the specified resource.
1455 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 * @param parent The group the shortcut belongs to.
1457 * @param info The data structure describing the shortcut.
1458 *
1459 * @return A View inflated from layoutResId.
1460 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001461 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001462 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001463 parent, false);
1464 favorite.applyFromShortcutInfo(info, mIconCache);
1465 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001467 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 return favorite;
1469 }
1470
1471 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 * Add a shortcut to the workspace.
1473 *
1474 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001477 int cellY) {
1478 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001479 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Sunny Goyal5c97f512015-05-19 16:03:28 -07001481 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001482 if (info == null) {
1483 return;
1484 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001485 final View view = createShortcut(info);
1486
Sunny Goyal5c97f512015-05-19 16:03:28 -07001487 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 // First we check if we already know the exact location where we want to add this item.
1489 if (cellX >= 0 && cellY >= 0) {
1490 cellXY[0] = cellX;
1491 cellXY[1] = cellY;
1492 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001493
1494 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001495 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001496 true, null,null)) {
1497 return;
1498 }
1499 DragObject dragObject = new DragObject();
1500 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001501 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1502 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001503 return;
1504 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001505 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001507 }
1508
1509 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001510 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 return;
1512 }
1513
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001514 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
1516 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001518 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520 }
1521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001525 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 */
Adam Cohen091440a2015-03-18 14:16:05 -07001527 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001528 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1529
1530 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001531 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001532 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 }
Romain Guycbb89e42009-06-08 15:52:54 -07001534
Adam Cohen59400422014-03-05 18:07:04 -08001535 if (appWidgetInfo.isCustomWidget) {
1536 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001537 }
1538
Adam Cohen59400422014-03-05 18:07:04 -08001539 LauncherAppWidgetInfo launcherInfo;
1540 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1541 launcherInfo.spanX = info.spanX;
1542 launcherInfo.spanY = info.spanY;
1543 launcherInfo.minSpanX = info.minSpanX;
1544 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001545 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001548 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (hostView == null) {
1552 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001553 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1554 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 } else {
1556 // The AppWidgetHostView has already been inflated and instantiated
1557 launcherInfo.hostView = hostView;
1558 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001559 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001560 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001562 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Romain Guycbb89e42009-06-08 15:52:54 -07001564
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001565 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1566 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1567 item.hostView.setTag(item);
1568 item.onBindAppWidget(this);
1569
1570 item.hostView.setFocusable(true);
1571 item.hostView.setOnFocusChangeListener(mFocusHandler);
1572
1573 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1574 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1575
1576 if (!item.isCustomWidget()) {
1577 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1578 }
1579 }
1580
Adam Cohended9f8d2010-11-03 13:25:16 -07001581 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1582 @Override
1583 public void onReceive(Context context, Intent intent) {
1584 final String action = intent.getAction();
1585 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1586 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001587 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001588 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001589
Winson Chungc100e8e2011-08-09 16:02:43 -07001590 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001591 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001592 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001593 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001594 if (!showWorkspace(false)) {
1595 // If we are already on the workspace, then manually reset all apps
1596 mAppsView.reset();
1597 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001598 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1600 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001601 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001602 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1603 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001604 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001605 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1606 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1607 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001608 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001609 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1610 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 }
1612 }
1613 };
1614
1615 @Override
1616 public void onAttachedToWindow() {
1617 super.onAttachedToWindow();
1618
1619 // Listen for broadcasts related to user-presence
1620 final IntentFilter filter = new IntentFilter();
1621 filter.addAction(Intent.ACTION_SCREEN_OFF);
1622 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001623 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001624 if (ENABLE_DEBUG_INTENTS) {
1625 filter.addAction(DebugIntents.DELETE_DATABASE);
1626 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1627 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001629 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001630 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001632
1633 if (mLauncherCallbacks != null) {
1634 mLauncherCallbacks.onAttachedToWindow();
1635 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001636 }
1637
1638 @Override
1639 public void onDetachedFromWindow() {
1640 super.onDetachedFromWindow();
1641 mVisible = false;
1642
Adam Cohend113e0c2010-11-11 10:48:05 -08001643 if (mAttached) {
1644 unregisterReceiver(mReceiver);
1645 mAttached = false;
1646 }
Winson Chungb745afb2015-03-02 11:51:23 -08001647 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001648
1649 if (mLauncherCallbacks != null) {
1650 mLauncherCallbacks.onDetachedFromWindow();
1651 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653
1654 public void onWindowVisibilityChanged(int visibility) {
1655 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001657 // The following code used to be in onResume, but it turns out onResume is called when
1658 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1659 // is a more appropriate event to handle
1660 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001661 if (!mWorkspaceLoading) {
1662 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001663 // We want to let Launcher draw itself at least once before we force it to build
1664 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001665 // apps is nice and speedy.
1666 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001668 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001669 if (mStarted) return;
1670 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001671 // We delay the layer building a bit in order to give
1672 // other message processing a time to run. In particular
1673 // this avoids a delay in hiding the IME if it was
1674 // currently shown, because doing that may involve
1675 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001676 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001677 final ViewTreeObserver.OnDrawListener listener = this;
1678 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001679 public void run() {
1680 if (mWorkspace != null &&
1681 mWorkspace.getViewTreeObserver() != null) {
1682 mWorkspace.getViewTreeObserver().
1683 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001685 }
1686 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001687 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001688 }
1689 });
1690 }
1691 clearTypedText();
1692 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 }
1694
Adam Cohen091440a2015-03-18 14:16:05 -07001695 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 mHandler.removeMessages(ADVANCE_MSG);
1697 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1698 mHandler.sendMessageDelayed(msg, delay);
1699 mAutoAdvanceSentTime = System.currentTimeMillis();
1700 }
1701
Adam Cohen091440a2015-03-18 14:16:05 -07001702 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001703 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1704 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1705 mAutoAdvanceRunning = autoAdvanceRunning;
1706 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001707 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 sendAdvanceMessage(delay);
1709 } else {
1710 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001711 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1713 }
1714 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001715 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 }
1717 }
1718 }
1719
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001720 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1721
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001723 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 if (msg.what == ADVANCE_MSG) {
1725 int i = 0;
1726 for (View key: mWidgetsToAdvance.keySet()) {
1727 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001730 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 public void run() {
1732 ((Advanceable) v).advance();
1733 }
1734 }, delay);
1735 }
1736 i++;
1737 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001738 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001743
Winsonc0b52fe2015-09-09 16:38:15 -07001744 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001745 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1747 if (v instanceof Advanceable) {
1748 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001749 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001750 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752 }
1753
Winsonc0b52fe2015-09-09 16:38:15 -07001754 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 if (mWidgetsToAdvance.containsKey(hostView)) {
1756 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001757 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001759 }
1760
Hyunyoung Song3f471442015-04-08 19:01:34 -07001761 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001762 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1763 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 }
1765
Winson Chunga6945242014-01-08 14:04:34 -08001766 public DragLayer getDragLayer() {
1767 return mDragLayer;
1768 }
1769
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001770 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001771 return mAppsView;
1772 }
1773
Hyunyoung Song3f471442015-04-08 19:01:34 -07001774 public WidgetsContainerView getWidgetsView() {
1775 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001776 }
1777
Winson Chunga6945242014-01-08 14:04:34 -08001778 public Workspace getWorkspace() {
1779 return mWorkspace;
1780 }
1781
1782 public Hotseat getHotseat() {
1783 return mHotseat;
1784 }
1785
Jorim Jaggid017f882014-01-14 17:08:48 -08001786 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001787 return mOverviewPanel;
1788 }
1789
Winson Chung006ee262015-08-03 14:40:11 -07001790 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001791 return mSearchDropTargetBar;
1792 }
1793
Tony Wickham34d2c912015-09-11 09:27:58 -07001794 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1795 return mAppInfoDropTargetBar;
1796 }
1797
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001798 public LauncherAppWidgetHost getAppWidgetHost() {
1799 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Winson Chunga9abd0e2010-10-27 17:18:37 -07001802 public LauncherModel getModel() {
1803 return mModel;
1804 }
1805
Winson Chunga6945242014-01-08 14:04:34 -08001806 protected SharedPreferences getSharedPrefs() {
1807 return mSharedPrefs;
1808 }
1809
Adam Cohen2e6da152015-05-06 11:42:25 -07001810 public DeviceProfile getDeviceProfile() {
1811 return mDeviceProfile;
1812 }
1813
Bjorn Bringertc459e522013-06-07 19:36:01 +01001814 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001815 getWindow().closeAllPanels();
1816
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001817 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001818 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 @Override
1822 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001823 long startTime = 0;
1824 if (DEBUG_RESUME_TIME) {
1825 startTime = System.currentTimeMillis();
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 super.onNewIntent(intent);
1828
1829 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001830 Folder openFolder = mWorkspace.getOpenFolder();
1831 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1832 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1833 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1834 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1835 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001838
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 if (mWorkspace == null) {
1840 // Can be cases where mWorkspace is null, this prevents a NPE
1841 return;
1842 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001843 // In all these cases, only animate if we're already on home
1844 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001845
Sunny Goyal935fca12015-10-13 10:19:01 -07001846 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 exitSpringLoadedDragMode();
1848
1849 // If we are already on home, then just animate back to the workspace,
1850 // otherwise, just wait until onResume to set the state back to Workspace
1851 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001852 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001853 } else {
1854 mOnResumeState = State.WORKSPACE;
1855 }
1856
1857 final View v = getWindow().peekDecorView();
1858 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001859 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 INPUT_METHOD_SERVICE);
1861 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1862 }
1863
Winson Chungb745afb2015-03-02 11:51:23 -08001864 // Reset the apps view
1865 if (!alreadyOnHome && mAppsView != null) {
1866 mAppsView.scrollToTop();
1867 }
1868
Hyunyoung Song3f471442015-04-08 19:01:34 -07001869 // Reset the widgets view
1870 if (!alreadyOnHome && mWidgetsView != null) {
1871 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001873
Adam Cohen9211d422014-10-07 18:14:53 -07001874 if (mLauncherCallbacks != null) {
1875 mLauncherCallbacks.onHomeIntent();
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
Adam Cohened307df2013-10-02 09:37:31 -07001878
Adam Cohen9211d422014-10-07 18:14:53 -07001879 if (mLauncherCallbacks != null) {
1880 mLauncherCallbacks.onNewIntent(intent);
1881 }
Winson15f8b172015-08-19 11:02:39 -07001882
1883 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1884 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1885 // animation.
1886 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001887 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1888 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001889 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1890 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001891
1892 // We use this flag to suppress noisy callbacks above custom content state
1893 // from onResume.
1894 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001895 mWorkspace.post(new Runnable() {
1896 @Override
1897 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001898 if (mWorkspace != null) {
1899 mWorkspace.moveToDefaultScreen(true);
1900 }
Winson15f8b172015-08-19 11:02:39 -07001901 }
1902 });
1903 }
1904 }
1905
1906 if (DEBUG_RESUME_TIME) {
1907 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1908 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001912 public void onRestoreInstanceState(Bundle state) {
1913 super.onRestoreInstanceState(state);
1914 for (int page: mSynchronouslyBoundPages) {
1915 mWorkspace.restoreInstanceStateForChild(page);
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
1918
1919 @Override
1920 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001921 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001922 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1923 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001924 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001925 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
Michael Jurka883f55b2010-10-21 15:47:14 -07001927 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal08442b82015-10-21 17:15:08 -07001928 Folder openFolder = mWorkspace.getOpenFolder();
1929 if (openFolder != null) {
1930 outState.putLong(RUNTIME_STATE_OPEN_FOLDER_ID, openFolder.mInfo.id);
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001935 ContentValues itemValues = new ContentValues();
1936 mPendingAddInfo.writeToValues(itemValues);
1937 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001938 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001939 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941
Hyunyoung Song3f471442015-04-08 19:01:34 -07001942 // Save the current widgets tray?
1943 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001944
1945 if (mLauncherCallbacks != null) {
1946 mLauncherCallbacks.onSaveInstanceState(outState);
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
1950 @Override
1951 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001953
Winson Chunge7a03942011-08-05 15:05:12 -07001954 // Remove all pending runnables
1955 mHandler.removeMessages(ADVANCE_MSG);
1956 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001957 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001958
Winson Chungcd2b0142011-06-08 16:02:26 -07001959 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001960 // It's possible to receive onDestroy after a new Launcher activity has
1961 // been created. In this case, don't interfere with the new Launcher.
1962 if (mModel.isCurrentCallbacks(this)) {
1963 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001964 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001965 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001968 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001970 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001972 mAppWidgetHost = null;
1973
1974 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975
1976 TextKeyListener.getInstance().release();
1977
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001978 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001979
Michael Jurka2ecf9952012-06-18 12:52:28 -07001980 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001981
1982 if (mLauncherCallbacks != null) {
1983 mLauncherCallbacks.onDestroy();
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
Adam Cohena9cf38f2011-05-02 15:36:58 -07001987 public DragController getDragController() {
1988 return mDragController;
1989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 @Override
1992 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001993 onStartForResult(requestCode);
1994 super.startActivityForResult(intent, requestCode);
1995 }
1996
1997 @Override
1998 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1999 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2000 onStartForResult(requestCode);
2001 try {
2002 super.startIntentSenderForResult(intent, requestCode,
2003 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2004 } catch (IntentSender.SendIntentException e) {
2005 throw new ActivityNotFoundException();
2006 }
2007 }
2008
2009 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002010 if (requestCode >= 0) {
2011 setWaitingForResult(true);
2012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014
Winson Chung70d72102011-08-12 11:24:35 -07002015 /**
2016 * Indicates that we want global search for this activity by setting the globalSearch
2017 * argument for {@link #startSearch} to true.
2018 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002020 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002022
Karl Rosaen138a0412009-04-23 19:00:21 -07002023 if (initialQuery == null) {
2024 // Use any text typed in the launcher as the initial query
2025 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 if (appSearchData == null) {
2028 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002029 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
Winson Chungadf0c182012-08-23 14:59:07 -07002031 Rect sourceBounds = new Rect();
2032 if (mSearchDropTargetBar != null) {
2033 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2034 }
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Ian Parkinson047036e2014-09-01 15:40:53 +01002036 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002037 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002038 if (clearTextImmediately) {
2039 clearTypedText();
2040 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002041
2042 // We need to show the workspace after starting the search
2043 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002044 }
2045
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 /**
2047 * Start a text search.
2048 *
2049 * @return {@code true} if the search will start immediately, so any further keypresses
2050 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2051 * to buffer keypresses.
2052 */
2053 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002054 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002055 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2056 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2057 sourceBounds);
2058 }
2059
Michael Jurkaa33411c2012-06-14 16:18:21 -07002060 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002063 }
2064
2065 /**
2066 * Starts the global search activity. This code is a copied from SearchManager
2067 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002069 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2072 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2073 if (globalSearchActivity == null) {
2074 Log.w(TAG, "No global search activity found.");
2075 return;
2076 }
2077 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2078 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2079 intent.setComponent(globalSearchActivity);
2080 // Make sure that we have a Bundle to put source in
2081 if (appSearchData == null) {
2082 appSearchData = new Bundle();
2083 } else {
2084 appSearchData = new Bundle(appSearchData);
2085 }
Adam Cohen9211d422014-10-07 18:14:53 -07002086 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 if (!appSearchData.containsKey("source")) {
2088 appSearchData.putString("source", getPackageName());
2089 }
2090 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2091 if (!TextUtils.isEmpty(initialQuery)) {
2092 intent.putExtra(SearchManager.QUERY, initialQuery);
2093 }
2094 if (selectInitialQuery) {
2095 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2096 }
2097 intent.setSourceBounds(sourceBounds);
2098 try {
2099 startActivity(intent);
2100 } catch (ActivityNotFoundException ex) {
2101 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104
Yura4f93ec62014-02-04 14:15:21 +00002105 public boolean isOnCustomContent() {
2106 return mWorkspace.isOnOrMovingToCustomContent();
2107 }
2108
Winson Chung70d72102011-08-12 11:24:35 -07002109 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002110 public boolean onPrepareOptionsMenu(Menu menu) {
2111 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002112 if (mLauncherCallbacks != null) {
2113 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2114 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002115 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002118 @Override
2119 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002120 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002121 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002122 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002123 }
2124
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125 public boolean isWorkspaceLocked() {
2126 return mWorkspaceLoading || mWaitingForResult;
2127 }
2128
Adam Cohen517a7f52014-03-01 12:12:59 -08002129 public boolean isWorkspaceLoading() {
2130 return mWorkspaceLoading;
2131 }
2132
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002133 private void setWorkspaceLoading(boolean value) {
2134 boolean isLocked = isWorkspaceLocked();
2135 mWorkspaceLoading = value;
2136 if (isLocked != isWorkspaceLocked()) {
2137 onWorkspaceLockedChanged();
2138 }
2139 }
2140
2141 private void setWaitingForResult(boolean value) {
2142 boolean isLocked = isWorkspaceLocked();
2143 mWaitingForResult = value;
2144 if (isLocked != isWorkspaceLocked()) {
2145 onWorkspaceLockedChanged();
2146 }
2147 }
2148
Adam Cohen9211d422014-10-07 18:14:53 -07002149 protected void onWorkspaceLockedChanged() {
2150 if (mLauncherCallbacks != null) {
2151 mLauncherCallbacks.onWorkspaceLockedChanged();
2152 }
2153 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002154
Michael Jurka0280c3b2010-09-17 15:00:07 -07002155 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002156 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002157 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2159 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002160 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002161 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002162
Tony Wickhama0628cc2015-10-14 15:23:04 -07002163 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002164 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002165 if (LOGD) {
2166 Log.d(TAG, "Adding widget from drop");
2167 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002168 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2169 }
2170
Sunny Goyale6b63a32015-01-16 16:14:29 -08002171 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002172 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2173 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002174 if (appWidgetInfo.configure != null) {
2175 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002176 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002177
Bjorn Bringert7984c942009-12-09 15:38:25 +00002178 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002179 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2180 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002183 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002184 Runnable onComplete = new Runnable() {
2185 @Override
2186 public void run() {
2187 // Exit spring loaded mode if necessary after adding the widget
2188 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2189 null);
2190 }
2191 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002193 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002194 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
2196 }
Romain Guycbb89e42009-06-08 15:52:54 -07002197
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002198 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002199 // Close any folders that may be open.
2200 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002201 mWorkspace.moveToCustomContentScreen(animate);
2202 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002203
2204 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2205 int[] cell, int spanX, int spanY) {
2206 switch (info.itemType) {
2207 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2208 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2209 int span[] = new int[2];
2210 span[0] = spanX;
2211 span[1] = spanY;
2212 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2213 container, screenId, cell, span);
2214 break;
2215 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2216 processShortcutFromDrop(info.componentName, container, screenId, cell);
2217 break;
2218 default:
2219 throw new IllegalStateException("Unknown item type: " + info.itemType);
2220 }
2221 }
2222
Adam Cohenfbba09b2011-07-18 21:43:05 -07002223 /**
2224 * Process a shortcut drop.
2225 *
2226 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002228 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002230 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2231 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235
2236 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 mPendingAddInfo.cellX = cell[0];
2238 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240
2241 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2242 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002243 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 }
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 /**
2247 * Process a widget drop.
2248 *
2249 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002253 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2254 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002258 mPendingAddInfo.minSpanX = info.minSpanX;
2259 mPendingAddInfo.minSpanY = info.minSpanY;
2260
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.cellX = cell[0];
2263 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002265 if (span != null) {
2266 mPendingAddInfo.spanX = span[0];
2267 mPendingAddInfo.spanY = span[1];
2268 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269
Adam Cohened66b2b2012-01-23 17:28:51 -08002270 AppWidgetHostView hostView = info.boundWidget;
2271 int appWidgetId;
2272 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002273 // In the case where we've prebound the widget, we remove it from the DragLayer
2274 if (LOGD) {
2275 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2276 }
2277 getDragLayer().removeView(hostView);
2278
Adam Cohened66b2b2012-01-23 17:28:51 -08002279 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002280 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002281
2282 // Clear the boundWidget so that it doesn't get destroyed.
2283 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002284 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002285 // In this case, we either need to start an activity to get permission to bind
2286 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002287 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002288 Bundle options = info.bindOptions;
2289
Sunny Goyalffe83f12014-08-14 17:39:34 -07002290 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2291 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002292 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002293 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2297 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2298 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2300 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 // TODO: we need to make sure that this accounts for the options bundle.
2302 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2304 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 }
2307
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002310 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 folderInfo.title = getText(R.string.folder_name);
2312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002314 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2315 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002316 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317
2318 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 FolderIcon newFolder =
2320 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002323 // Force measure the new folder icon
2324 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2325 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002326 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 }
Romain Guycbb89e42009-06-08 15:52:54 -07002328
Winsonc0b52fe2015-09-09 16:38:15 -07002329 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002330 * Unbinds the view for the specified item, and removes the item and all its children.
2331 *
2332 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002333 * @param itemInfo the {@link ItemInfo} for this view.
2334 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002335 */
Winson2949fb52015-09-24 09:56:11 -07002336 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002337 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002338 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002339 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2340 if (folderInfo != null) {
2341 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002342 } else {
2343 mWorkspace.removeWorkspaceItem(v);
2344 }
Winsonc0b52fe2015-09-09 16:38:15 -07002345 if (deleteFromDb) {
2346 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2347 }
2348 } else if (itemInfo instanceof FolderInfo) {
2349 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2350 unbindFolder(folderInfo);
2351 mWorkspace.removeWorkspaceItem(v);
2352 if (deleteFromDb) {
2353 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2354 }
2355 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2356 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002357 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002358 removeWidgetToAutoAdvance(widgetInfo.hostView);
2359 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002361 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002362 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002363
Winsonc0b52fe2015-09-09 16:38:15 -07002364 } else {
2365 return false;
2366 }
2367 return true;
2368 }
2369
2370 /**
2371 * Unbinds any launcher references to the folder.
2372 */
2373 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002374 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 }
2376
Winsonc0b52fe2015-09-09 16:38:15 -07002377 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002378 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002379 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002381 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002383 // Deleting an app widget ID is a void call but writes to disk before returning
2384 // to the caller...
2385 new AsyncTask<Void, Void, Void>() {
2386 public Void doInBackground(Void ... args) {
2387 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2388 return null;
2389 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002390 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002391 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002393 }
2394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 @Override
2396 public boolean dispatchKeyEvent(KeyEvent event) {
2397 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2398 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002400 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002401 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002402 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002403 dumpState();
2404 return true;
2405 }
2406 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002407 }
2408 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2409 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002410 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 return true;
2412 }
2413 }
2414
2415 return super.dispatchKeyEvent(event);
2416 }
2417
Joe Onorato88ec0992009-11-19 13:16:06 -08002418 @Override
2419 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002420 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2421 return;
2422 }
2423
Sunny Goyal45478022015-06-08 16:52:41 -07002424 if (mDragController.isDragging()) {
2425 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002426 return;
2427 }
2428
Winson Chungb745afb2015-03-02 11:51:23 -08002429 if (isAppsViewVisible()) {
2430 showWorkspace(true);
2431 } else if (isWidgetsViewVisible()) {
2432 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002433 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002434 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002435 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002436 Folder openFolder = mWorkspace.getOpenFolder();
2437 if (openFolder.isEditingName()) {
2438 openFolder.dismissEditingName();
2439 } else {
2440 closeFolder();
2441 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002442 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002443 mWorkspace.exitWidgetResizeMode();
2444
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002445 // Back button is a no-op here, but give at least some feedback for the button press
2446 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002447 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002448 }
2449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002451 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002452 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002453 @Override
2454 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002455 if (mAppWidgetHost != null) {
2456 mAppWidgetHost.startListening();
2457 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002458
2459 // Recreate the QSB, as the widget has been reset.
2460 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002461 }
2462
2463 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 * Launches the intent referred by the clicked shortcut.
2465 *
2466 * @param v The view representing the clicked shortcut.
2467 */
2468 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002469 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2470 // view has detached (it's possible for this to happen if the view is removed mid touch).
2471 if (v.getWindowToken() == null) {
2472 return;
2473 }
2474
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002475 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002476 return;
2477 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002478
Adam Cohen1697b792013-09-17 19:08:21 -07002479 if (v instanceof Workspace) {
2480 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002481 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 }
2483 return;
2484 }
2485
2486 if (v instanceof CellLayout) {
2487 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002488 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2489 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002490 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002491 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002495 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002501 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002503 } else if (tag instanceof AppInfo) {
2504 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 } else if (tag instanceof LauncherAppWidgetInfo) {
2506 if (v instanceof PendingAppWidgetHostView) {
2507 onClickPendingWidget((PendingAppWidgetHostView) v);
2508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 }
2510 }
2511
Sunny Goyal70660032015-05-14 00:07:08 -07002512 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002513 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002514 return false;
2515 }
2516
Michael Jurkaaf442092010-06-10 17:01:57 -07002517 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002518 * Event handler for the app widget view which has not fully restored.
2519 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002520 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002521 if (mIsSafeModeEnabled) {
2522 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2523 return;
2524 }
2525
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002527 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002528 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002529 LauncherAppWidgetProviderInfo appWidgetInfo =
2530 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002531 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002532 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002533 mPendingAddInfo.copyFrom(info);
2534 mPendingAddWidgetId = widgetId;
2535
Sunny Goyalffe83f12014-08-14 17:39:34 -07002536 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2537 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002538 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002539 } else if (info.installProgress < 0) {
2540 // The install has not been queued
2541 final String packageName = info.providerName.getPackageName();
2542 showBrokenAppInstallDialog(packageName,
2543 new DialogInterface.OnClickListener() {
2544 public void onClick(DialogInterface dialog, int id) {
2545 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2546 }
2547 });
2548 } else {
2549 // Download has started.
2550 final String packageName = info.providerName.getPackageName();
2551 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002552 }
2553 }
2554
2555 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002556 * Event handler for the "grid" button that appears on the home screen, which
2557 * enters all apps mode.
2558 *
2559 * @param v The view that was clicked.
2560 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002561 protected void onClickAllAppsButton(View v) {
2562 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002563 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002564 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002565 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002566
2567 if (mLauncherCallbacks != null) {
2568 mLauncherCallbacks.onClickAllAppsButton(v);
2569 }
Winson Chung76648c52015-07-10 14:33:23 -07002570 }
2571 }
2572
2573 protected void onLongClickAllAppsButton(View v) {
2574 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2575 if (!isAppsViewVisible()) {
2576 showAppsView(true /* animated */, false /* resetListToTop */,
2577 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002578 }
2579 }
2580
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 private void showBrokenAppInstallDialog(final String packageName,
2582 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002583 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 .setTitle(R.string.abandoned_promises_title)
2585 .setMessage(R.string.abandoned_promise_explanation)
2586 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2587 .setNeutralButton(R.string.abandoned_clean_this,
2588 new DialogInterface.OnClickListener() {
2589 public void onClick(DialogInterface dialog, int id) {
2590 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2591 mWorkspace.removeAbandonedPromise(packageName, user);
2592 }
2593 })
2594 .create().show();
2595 return;
2596 }
2597
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 /**
2599 * Event handler for an app shortcut click.
2600 *
2601 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2602 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002603 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2605 Object tag = v.getTag();
2606 if (!(tag instanceof ShortcutInfo)) {
2607 throw new IllegalArgumentException("Input must be a Shortcut");
2608 }
2609
2610 // Open shortcut
2611 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002612
2613 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002614 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2615 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002616 // Launch activity anyway, framework will tell the user why the app is suspended.
2617 } else {
2618 int error = R.string.activity_not_available;
2619 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2620 error = R.string.safemode_shortcut_error;
2621 }
2622 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2623 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002624 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002625 }
2626
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002628 if ((v instanceof BubbleTextView)
2629 && shortcut.isPromise()
2630 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002631 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002632 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 new DialogInterface.OnClickListener() {
2634 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002635 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 return;
2639 }
2640
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002642 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002643
2644 if (mLauncherCallbacks != null) {
2645 mLauncherCallbacks.onClickAppShortcut(v);
2646 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 }
2648
Adam Cohen091440a2015-03-18 14:16:05 -07002649 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002651 final ShortcutInfo shortcut;
2652 final Intent intent;
2653 if (tag instanceof ShortcutInfo) {
2654 shortcut = (ShortcutInfo) tag;
2655 intent = shortcut.intent;
2656 int[] pos = new int[2];
2657 v.getLocationOnScreen(pos);
2658 intent.setSourceBounds(new Rect(pos[0], pos[1],
2659 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002660
Sunny Goyal508da152014-08-14 10:53:27 -07002661 } else if (tag instanceof AppInfo) {
2662 shortcut = null;
2663 intent = ((AppInfo) tag).intent;
2664 } else {
2665 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2666 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667
2668 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002669 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670
2671 if (success && v instanceof BubbleTextView) {
2672 mWaitingForResume = (BubbleTextView) v;
2673 mWaitingForResume.setStayPressed(true);
2674 }
2675 }
2676
2677 /**
2678 * Event handler for a folder icon click.
2679 *
2680 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2681 */
2682 protected void onClickFolderIcon(View v) {
2683 if (LOGD) Log.d(TAG, "onClickFolder");
2684 if (!(v instanceof FolderIcon)){
2685 throw new IllegalArgumentException("Input must be a FolderIcon");
2686 }
2687
2688 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002689 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002691 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 }
Adam Cohen9211d422014-10-07 18:14:53 -07002693
2694 if (mLauncherCallbacks != null) {
2695 mLauncherCallbacks.onClickFolderIcon(v);
2696 }
Michael Jurka5130e402011-10-13 04:55:35 -07002697 }
2698
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 /**
2700 * Event handler for the (Add) Widgets button that appears after a long press
2701 * on the home screen.
2702 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002703 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002705 if (mIsSafeModeEnabled) {
2706 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2707 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002708 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002709 if (mLauncherCallbacks != null) {
2710 mLauncherCallbacks.onClickAddWidgetButton(view);
2711 }
Adam Cohen9211d422014-10-07 18:14:53 -07002712 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 /**
2716 * Event handler for the wallpaper picker button that appears after a long press
2717 * on the home screen.
2718 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002719 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002721 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2722 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2723 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002724 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002725 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002726
2727 if (mLauncherCallbacks != null) {
2728 mLauncherCallbacks.onClickWallpaperPicker(v);
2729 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 }
2731
2732 /**
2733 * Event handler for a click on the settings button that appears after a long press
2734 * on the home screen.
2735 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002736 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002738 if (mLauncherCallbacks != null) {
2739 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002740 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002741 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002742 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 }
2744
Adam Cohen61f560d2013-09-30 15:58:20 -07002745 public View.OnTouchListener getHapticFeedbackTouchListener() {
2746 if (mHapticFeedbackTouchListener == null) {
2747 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002748 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002749 @Override
2750 public boolean onTouch(View v, MotionEvent event) {
2751 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2752 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2753 }
2754 return false;
2755 }
2756 };
2757 }
2758 return mHapticFeedbackTouchListener;
2759 }
2760
Adam Cohen9211d422014-10-07 18:14:53 -07002761 public void onDragStarted(View view) {
2762 if (isOnCustomContent()) {
2763 // Custom content screen doesn't participate in drag and drop. If on custom
2764 // content screen, move to default.
2765 moveWorkspaceToDefaultScreen();
2766 }
2767
2768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onDragStarted(view);
2770 }
2771 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002772
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002773 /**
2774 * Called when the user stops interacting with the launcher.
2775 * This implies that the user is now on the homescreen and is not doing housekeeping.
2776 */
Adam Cohen9211d422014-10-07 18:14:53 -07002777 protected void onInteractionEnd() {
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onInteractionEnd();
2780 }
2781 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002782
2783 /**
2784 * Called when the user starts interacting with the launcher.
2785 * The possible interactions are:
2786 * - open all apps
2787 * - reorder an app shortcut, or a widget
2788 * - open the overview mode.
2789 * This is a good time to stop doing things that only make sense
2790 * when the user is on the homescreen and not doing housekeeping.
2791 */
Adam Cohen9211d422014-10-07 18:14:53 -07002792 protected void onInteractionBegin() {
2793 if (mLauncherCallbacks != null) {
2794 mLauncherCallbacks.onInteractionBegin();
2795 }
2796 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002797
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 /** Updates the interaction state. */
2799 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002800 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002801 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002802 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2803 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002804 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002805 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionEnd();
2808 }
2809 }
2810
Kenny Guyf07af7b2014-07-31 11:39:16 +01002811 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002812 try {
2813 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002814 launcherApps.showAppDetailsForProfile(componentName, user);
2815 } catch (SecurityException e) {
2816 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2817 Log.e(TAG, "Launcher does not have permission to launch settings");
2818 } catch (ActivityNotFoundException e) {
2819 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2820 Log.e(TAG, "Unable to launch settings");
2821 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002822 }
2823
Michael Jurka1e2f4652013-07-08 18:03:46 -07002824 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002825 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2826 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002827 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002828 // System applications cannot be installed. For now, show a toast explaining that.
2829 // We may give them the option of disabling apps this way.
2830 int messageId = R.string.uninstall_system_app_text;
2831 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002832 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002833 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002834 String packageName = componentName.getPackageName();
2835 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002836 Intent intent = new Intent(
2837 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002838 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2839 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002840 if (user != null) {
2841 user.addToIntent(intent, Intent.EXTRA_USER);
2842 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002843 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002844 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002845 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002846 }
2847
Winson Chung8f1eff72015-05-28 17:33:40 -07002848 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002849 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002851 // Only launch using the new animation if the shortcut has not opted out (this is a
2852 // private contract between launcher and may be ignored in the future).
2853 boolean useLaunchAnimation = (v != null) &&
2854 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002855 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2856 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002857
Kenny Guy1317e2d2014-05-08 18:52:50 +01002858 UserHandleCompat user = null;
2859 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2860 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2861 user = userManager.getUserForSerialNumber(serialNumber);
2862 }
2863
2864 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002865 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002866 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002867 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002868 int left = 0, top = 0;
2869 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2870 if (v instanceof TextView) {
2871 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002872 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2873 if (icon != null) {
2874 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002875 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002876 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002877 width = bounds.width();
2878 height = bounds.height();
2879 }
2880 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002881 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2882 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002883 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002884 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002885 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002886 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002887 // On L devices, we use the device default slide-up transition.
2888 // On L MR1 devices, we a custom version of the slide-up transition which
2889 // doesn't have the delay present in the device default.
2890 opts = ActivityOptions.makeCustomAnimation(this,
2891 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002892 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002893 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002894 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002895
2896 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2897 // Could be launching some bookkeeping activity
2898 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002899 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002900 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002901 launcherApps.startActivityForProfile(intent.getComponent(), user,
2902 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002903 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002904 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002906 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2907 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2908 // corresponding permission. Show the appropriate permission prompt if that
2909 // is the case.
2910 if (intent.getComponent() == null
2911 && Intent.ACTION_CALL.equals(intent.getAction())
2912 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2913 PackageManager.PERMISSION_GRANTED) {
2914 // TODO: Rename sPendingAddItem to a generic name.
2915 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2916 0, (ItemInfo) tag);
2917 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2918 REQUEST_PERMISSION_CALL_PHONE);
2919 return false;
2920 }
2921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002923 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002925 "or use the exported attribute for this activity. "
2926 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002928 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002930
Winson Chungbedf9232015-07-10 12:38:30 -07002931 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002932 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002933 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2934 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2935 return false;
2936 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002937 try {
2938 success = startActivity(v, intent, tag);
2939 } catch (ActivityNotFoundException e) {
2940 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2941 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2942 }
2943 return success;
2944 }
2945
Adam Cohen268c4752012-06-06 17:47:33 -07002946 /**
2947 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2948 * in the DragLayer in the exact absolute location of the original FolderIcon.
2949 */
2950 private void copyFolderIconToImage(FolderIcon fi) {
2951 final int width = fi.getMeasuredWidth();
2952 final int height = fi.getMeasuredHeight();
2953
2954 // Lazy load ImageView, Bitmap and Canvas
2955 if (mFolderIconImageView == null) {
2956 mFolderIconImageView = new ImageView(this);
2957 }
2958 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2959 mFolderIconBitmap.getHeight() != height) {
2960 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2961 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2962 }
2963
2964 DragLayer.LayoutParams lp;
2965 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2966 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2967 } else {
2968 lp = new DragLayer.LayoutParams(width, height);
2969 }
2970
Adam Cohen307fe232012-08-16 17:55:58 -07002971 // The layout from which the folder is being opened may be scaled, adjust the starting
2972 // view size by this scale factor.
2973 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002974 lp.customPosition = true;
2975 lp.x = mRectForFolderAnimation.left;
2976 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002977 lp.width = (int) (scale * width);
2978 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002979
2980 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2981 fi.draw(mFolderIconCanvas);
2982 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002983 if (fi.getFolder() != null) {
2984 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2985 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002986 }
Adam Cohen268c4752012-06-06 17:47:33 -07002987 // Just in case this image view is still in the drag layer from a previous animation,
2988 // we remove it and re-add it.
2989 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2990 mDragLayer.removeView(mFolderIconImageView);
2991 }
2992 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002993 if (fi.getFolder() != null) {
2994 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002995 }
Adam Cohen268c4752012-06-06 17:47:33 -07002996 }
2997
Sunny Goyal08442b82015-10-21 17:15:08 -07002998 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002999 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 FolderInfo info = (FolderInfo) fi.getTag();
3001 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3002 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003003 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3004 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003005 }
3006
Adam Cohen268c4752012-06-06 17:47:33 -07003007 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3008 copyFolderIconToImage(fi);
3009 fi.setVisibility(View.INVISIBLE);
3010
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003011 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3012 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003013 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003014 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3015 }
3016 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003017 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003018 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003019 oa.end();
3020 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003021 }
3022
Sunny Goyal935fca12015-10-13 10:19:01 -07003023 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003024 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003025 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003026
Adam Cohen268c4752012-06-06 17:47:33 -07003027 // We remove and re-draw the FolderIcon in-case it has changed
3028 mDragLayer.removeView(mFolderIconImageView);
3029 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003030
3031 if (cl != null) {
3032 cl.clearFolderLeaveBehind();
3033 }
3034
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003035 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003036 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003037 oa.addListener(new AnimatorListenerAdapter() {
3038 @Override
3039 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003040 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003041 // Remove the ImageView copy of the FolderIcon and make the original visible.
3042 mDragLayer.removeView(mFolderIconImageView);
3043 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003044 }
3045 }
3046 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003047 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003048 if (!animate) {
3049 oa.end();
3050 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003051 }
3052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003054 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055 * is animated relative to the specified View. If the View is null, no animation
3056 * is played.
3057 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003058 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003060 public void openFolder(FolderIcon folderIcon, boolean animate) {
3061 animate &= !Utilities.isPowerSaverOn(this);
3062
Adam Cohenfb91f302012-06-11 15:45:18 -07003063 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003064 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3065 if (openFolder != null && openFolder != folder) {
3066 // Close any open folder before opening a folder.
3067 closeFolder();
3068 }
3069
Adam Cohena9cf38f2011-05-02 15:36:58 -07003070 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071
Adam Cohena9cf38f2011-05-02 15:36:58 -07003072 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073
Sunny Goyalf4066152015-04-15 09:42:19 -07003074 // While the folder is open, the position of the icon cannot change.
3075 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3076
Adam Cohen4554ee12011-08-03 16:13:21 -07003077 // Just verify that the folder hasn't already been added to the DragLayer.
3078 // There was a one-off crash where the folder had a parent already.
3079 if (folder.getParent() == null) {
3080 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003081 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 } else {
3083 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3084 folder.getParent() + ").");
3085 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003086 if (animate) {
3087 folder.animateOpen();
3088 } else {
3089 folder.open();
3090 }
3091 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003092
3093 // Notify the accessibility manager that this folder "window" has appeared and occluded
3094 // the workspace items
3095 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3096 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003097 }
3098
3099 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003100 closeFolder(true);
3101 }
3102
3103 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003104 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003106 if (folder.isEditingName()) {
3107 folder.dismissEditingName();
3108 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003109 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003110 }
3111 }
3112
Sunny Goyal935fca12015-10-13 10:19:01 -07003113 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003114 animate &= !Utilities.isPowerSaverOn(this);
3115
Adam Cohen4554ee12011-08-03 16:13:21 -07003116 folder.getInfo().opened = false;
3117
3118 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3119 if (parent != null) {
3120 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003122 if (fi != null) {
3123 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3124 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003126 if (animate) {
3127 folder.animateClosed();
3128 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003129 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003130 }
Winson Chung83ca4802013-04-12 15:10:52 -07003131
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 // Notify the accessibility manager that this folder "window" has disappeared and no
3133 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003134 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Tony Wickhamdadb3042016-02-24 11:07:00 -08003137 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003139 if (!isDraggingEnabled()) return false;
3140 if (isWorkspaceLocked()) return false;
3141 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142
Winson Chung76648c52015-07-10 14:33:23 -07003143 if (v == mAllAppsButton) {
3144 onLongClickAllAppsButton(v);
3145 return true;
3146 }
3147
Adam Cohen1697b792013-09-17 19:08:21 -07003148 if (v instanceof Workspace) {
3149 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003150 if (!mWorkspace.isTouchActive()) {
3151 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003152 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3153 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3154 return true;
3155 } else {
3156 return false;
3157 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003158 } else {
3159 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003160 }
Adam Cohen1697b792013-09-17 19:08:21 -07003161 }
3162
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003163 CellLayout.CellInfo longClickCellInfo = null;
3164 View itemUnderLongClick = null;
3165 if (v.getTag() instanceof ItemInfo) {
3166 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003167 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003168 itemUnderLongClick = longClickCellInfo.cell;
3169 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 }
3171
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 // The hotseat touch handling does not go through Workspace, and we always allow long press
3173 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003174 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003175 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003176 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003177 // User long pressed on empty space
3178 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3179 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003180 if (mWorkspace.isInOverviewMode()) {
3181 mWorkspace.startReordering(v);
3182 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003183 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003186 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3187 mHotseat.getOrderInHotseat(
3188 longClickCellInfo.cellX,
3189 longClickCellInfo.cellY));
3190 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003192 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 }
3194 }
3195 }
3196 return true;
3197 }
3198
Winson Chung3d503fb2011-07-13 17:25:49 -07003199 boolean isHotseatLayout(View layout) {
3200 return mHotseat != null && layout != null &&
3201 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3202 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003203
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 /**
3205 * Returns the CellLayout of the specified container at the specified screen.
3206 */
Sunny Goyal92820592015-03-02 11:31:35 -08003207 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3209 if (mHotseat != null) {
3210 return mHotseat.getLayout();
3211 } else {
3212 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003213 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003215 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003216 }
3217 }
3218
Winson Chungb745afb2015-03-02 11:51:23 -08003219 /**
3220 * For overridden classes.
3221 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003222 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003223 return isAppsViewVisible();
3224 }
3225
3226 public boolean isAppsViewVisible() {
3227 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3228 }
3229
3230 public boolean isWidgetsViewVisible() {
3231 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003232 }
3233
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003234 private void setWorkspaceBackground(int background) {
3235 switch (background) {
3236 case WORKSPACE_BACKGROUND_TRANSPARENT:
3237 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3238 break;
3239 case WORKSPACE_BACKGROUND_BLACK:
3240 getWindow().setBackgroundDrawable(null);
3241 break;
3242 default:
3243 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3244 }
Craig Mautner360310b2012-10-26 15:13:08 -07003245 }
3246
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003247 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003248 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3249 int curflags = getWindow().getAttributes().flags
3250 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3251 if (wpflags != curflags) {
3252 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3253 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003254 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003255 }
3256
Michael Jurkae326f182011-11-21 14:05:46 -08003257 @Override
3258 public void onTrimMemory(int level) {
3259 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003260 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3261 // The widget preview db can result in holding onto over
3262 // 3MB of memory for caching which isn't necessary.
3263 SQLiteDatabase.releaseMemory();
3264
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003265 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003266 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003267 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003268 if (mLauncherCallbacks != null) {
3269 mLauncherCallbacks.onTrimMemory(level);
3270 }
Michael Jurkae326f182011-11-21 14:05:46 -08003271 }
3272
Winson5c6bdbb2015-09-03 11:36:19 -07003273 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003274 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003275 }
3276
Winson5c6bdbb2015-09-03 11:36:19 -07003277 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003278 boolean changed = mState != State.WORKSPACE ||
3279 mWorkspace.getState() != Workspace.State.NORMAL;
3280 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003281 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003282 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003283 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003284
Michael Jurkab3e22d92011-10-31 15:58:33 -07003285 // Set focus to the AppsCustomize button
3286 if (mAllAppsButton != null) {
3287 mAllAppsButton.requestFocus();
3288 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003289 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003290
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003291 // Change the state *after* we've called all the transition code
3292 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003293
Winson Chung5fb63472011-02-02 17:03:37 -08003294 // Resume the auto-advance of widgets
3295 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003296 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003297
Winson Chung0f785722015-04-08 10:27:49 -07003298 if (changed) {
3299 // Send an accessibility event to announce the context change
3300 getWindow().getDecorView()
3301 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003302 }
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -08003303 mUserEventLogger.resetElapsedContainerMillis();
Winson5c6bdbb2015-09-03 11:36:19 -07003304 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003305 }
3306
Winsone9f27272015-10-13 10:47:51 -07003307 /**
3308 * Shows the overview button.
3309 */
Adam Cohened307df2013-10-02 09:37:31 -07003310 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003311 showOverviewMode(animated, false);
3312 }
3313
3314 /**
3315 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3316 * onto one of the overview panel buttons.
3317 */
3318 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3319 Runnable postAnimRunnable = null;
3320 if (requestButtonFocus) {
3321 postAnimRunnable = new Runnable() {
3322 @Override
3323 public void run() {
3324 // Hitting the menu button when in touch mode does not trigger touch mode to
3325 // be disabled, so if requested, force focus on one of the overview panel
3326 // buttons.
3327 mOverviewPanel.requestFocusFromTouch();
3328 }
3329 };
3330 }
Adam Cohened307df2013-10-02 09:37:31 -07003331 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003332 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003333 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003334 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003335 }
3336
Winson Chungb745afb2015-03-02 11:51:23 -08003337 /**
3338 * Shows the apps view.
3339 */
Winson Chung76648c52015-07-10 14:33:23 -07003340 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3341 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003342 if (resetListToTop) {
3343 mAppsView.scrollToTop();
3344 }
Winson Chung4ac30062015-05-08 17:34:17 -07003345 if (updatePredictedApps) {
3346 tryAndUpdatePredictedApps();
3347 }
Winson Chung76648c52015-07-10 14:33:23 -07003348 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003349 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350
Winson Chungb745afb2015-03-02 11:51:23 -08003351 /**
3352 * Shows the widgets view.
3353 */
3354 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003355 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003356 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003357 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003358 }
Winson Chung76648c52015-07-10 14:33:23 -07003359 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360
3361 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003362 @Override
3363 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003364 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003365 }
3366 });
Winson Chungb745afb2015-03-02 11:51:23 -08003367 }
3368
3369 /**
3370 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003371 *
3372 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003373 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003374 // TODO: calling method should use the return value so that when {@code false} is returned
3375 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003376 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003377 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3378 mState != State.WIDGETS_SPRING_LOADED) {
3379 return false;
3380 }
3381 if (toState != State.APPS && toState != State.WIDGETS) {
3382 return false;
3383 }
Winson Chungb745afb2015-03-02 11:51:23 -08003384
3385 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003386 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3387 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003388 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003389 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003390 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003391
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003393 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003394
3395 // Pause the auto-advance of widgets until we are out of AllApps
3396 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003397 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003398 closeFolder();
3399
3400 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003401 getWindow().getDecorView()
3402 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003403 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404 }
3405
Winson Chungcd99cd32015-04-29 11:03:24 -07003406 /**
3407 * Updates the workspace and interaction state on state change, and return the animation to this
3408 * new state.
3409 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003410 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003411 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003412 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003413 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003414 updateInteraction(fromState, toState);
3415 return anim;
3416 }
3417
Jun Mukaif0033da2015-08-04 18:34:30 -07003418 public void onLauncherClingShown() {
3419 // When a launcher cling appears, it should cover the underlying layers, so their focus
3420 // should be blocked.
3421 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3422 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3423 }
3424 }
3425
3426 public void onLauncherClingDismissed() {
3427 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3428 }
3429
Hyunyoung Song3f471442015-04-08 19:01:34 -07003430 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003431 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003432 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003433 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003434 }
Winson Chungb745afb2015-03-02 11:51:23 -08003435
Winson Chung006ee262015-08-03 14:40:11 -07003436 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003437 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003438 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003439
3440 if (isAppsViewVisible()) {
3441 mState = State.APPS_SPRING_LOADED;
3442 } else if (isWidgetsViewVisible()) {
3443 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003444 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003445 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003446 } else {
3447 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003448 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003449 }
Adam Cohen7777d962011-08-18 18:58:38 -07003450
Hyunyoung Song3f471442015-04-08 19:01:34 -07003451 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003452 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003453 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003454
Winson Chunge7a03942011-08-05 15:05:12 -07003455 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003456 @Override
3457 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003458 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003459 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3460 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003461 // Before we show workspace, hide all apps again because
3462 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3463 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003464 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003465 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003466 } else {
3467 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003468 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003469 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003470 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003471 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003472
Tony Wickhame0c33232016-02-08 11:37:04 -08003473 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003474 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3475 || mState == State.WIDGETS_SPRING_LOADED;
3476 }
3477
Michael Jurkad3ef3062010-11-23 16:23:58 -08003478 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003479 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003480 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003481 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003482 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003483 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3485 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003486 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003487 }
3488
Winson Chung4ac30062015-05-08 17:34:17 -07003489 /**
3490 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3491 * resumed.
3492 */
3493 private void tryAndUpdatePredictedApps() {
3494 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003495 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003496 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003497 mAppsView.setPredictedApps(apps);
3498 }
3499 }
3500 }
3501
Michael Jurkab3e22d92011-10-31 15:58:33 -07003502 void lockAllApps() {
3503 // TODO
3504 }
3505
3506 void unlockAllApps() {
3507 // TODO
3508 }
3509
Winsonf768d932015-09-25 16:12:35 -07003510 public boolean launcherCallbacksProvidesSearch() {
3511 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3512 }
3513
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003514 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003515 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003516 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003517 }
3518
Adam Cohen24ce0b32014-01-14 16:18:14 -08003519 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003520 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3521 if (searchProvider == null) {
3522 return null;
3523 }
3524
3525 Bundle opts = new Bundle();
3526 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003527 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003528
Tony Wickham3a3517f2015-10-06 11:27:04 -07003529 // Determine the min and max dimensions of the widget.
3530 LauncherAppState app = LauncherAppState.getInstance();
3531 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3532 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3533 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003534 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3535 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003536 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003537 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003538 int minWidth = maxWidth;
3539 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003540 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3541 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3542 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3543 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3544 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003545 }
3546 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3547 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3548 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3549 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003550 if (LOGD) {
3551 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3552 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3553 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003554
Tony Wickham775455c2015-10-16 09:49:32 -07003555 if (mLauncherCallbacks != null) {
3556 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3557 }
3558
Sunny Goyalf7258242015-10-19 16:59:07 -07003559 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003560 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003561 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003562 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003563 || (widgetInfo == null)
3564 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003565 // A valid widget is not already bound.
3566 if (widgetId > -1) {
3567 mAppWidgetHost.deleteAppWidgetId(widgetId);
3568 widgetId = -1;
3569 }
3570
3571 // Try to bind a new widget
3572 widgetId = mAppWidgetHost.allocateAppWidgetId();
3573
3574 if (!AppWidgetManagerCompat.getInstance(this)
3575 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3576 mAppWidgetHost.deleteAppWidgetId(widgetId);
3577 widgetId = -1;
3578 }
3579
Sunny Goyalf7258242015-10-19 16:59:07 -07003580 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003581 .putInt(QSB_WIDGET_ID, widgetId)
3582 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003583 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003584 }
3585
Sunny Goyal8d595092015-07-06 12:22:14 -07003586 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003587 if (widgetId != -1) {
3588 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003589 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003590 mQsb.updateAppWidgetOptions(opts);
3591 mQsb.setPadding(0, 0, 0, 0);
3592 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003593 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003594 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003595 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003596 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003597 }
3598
Sunny Goyal22235bc2015-04-03 09:23:43 -07003599 private void reinflateQSBIfNecessary() {
3600 if (mQsb instanceof LauncherAppWidgetHostView &&
3601 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3602 mSearchDropTargetBar.removeView(mQsb);
3603 mQsb = null;
3604 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3605 }
3606 }
3607
Michael Jurkad7c28052012-04-27 15:43:36 -07003608 @Override
3609 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003610 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003611 final List<CharSequence> text = event.getText();
3612 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003613 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003614 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003615 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003616 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003617 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003618 } else if (mWorkspace != null) {
3619 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003620 } else {
3621 text.add(getString(R.string.all_apps_home_button_label));
3622 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003623 return result;
3624 }
3625
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003626 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003627 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003628 */
Adam Cohen091440a2015-03-18 14:16:05 -07003629 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003630 @Override
3631 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003632 closeSystemDialogs();
3633 }
3634 }
3635
3636 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003637 * If the activity is currently paused, signal that we need to run the passed Runnable
3638 * in onResume.
3639 *
3640 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003641 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3642 * wrong when we're not running, and if the activity comes back to what the configuration was
3643 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003644 *
3645 * Implementation of the method from LauncherModel.Callbacks.
3646 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003647 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003649 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003650 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003651 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003652 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003653 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003654 }
3655 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003656 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003657 return true;
3658 } else {
3659 return false;
3660 }
3661 }
3662
Michael Jurkac402cd92013-05-20 15:49:32 +02003663 private boolean waitUntilResume(Runnable run) {
3664 return waitUntilResume(run, false);
3665 }
3666
Michael Jurka1e2f4652013-07-08 18:03:46 -07003667 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003668 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003669 }
3670
Michael Jurka7607c2f2013-04-03 14:33:19 -07003671 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003672 * If the activity is currently paused, signal that we need to re-run the loader
3673 * in onResume.
3674 *
3675 * This needs to be called from incoming places where resources might have been loaded
3676 * while we are paused. That is becaues the Configuration might be wrong
3677 * when we're not running, and if it comes back to what it was when we
3678 * were paused, we are not restarted.
3679 *
3680 * Implementation of the method from LauncherModel.Callbacks.
3681 *
3682 * @return true if we are currently paused. The caller might be able to
3683 * skip some work in that case since we will come back again.
3684 */
3685 public boolean setLoadOnResume() {
3686 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003687 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003688 mOnResumeNeedsLoad = true;
3689 return true;
3690 } else {
3691 return false;
3692 }
3693 }
3694
3695 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003697 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003699 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003700 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003701 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003702 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003705
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003707 * Clear any pending bind callbacks. This is called when is loader is planning to
3708 * perform a full rebind from scratch.
3709 */
3710 @Override
3711 public void clearPendingBinds() {
3712 mBindOnResumeCallbacks.clear();
3713 if (mPendingExecutor != null) {
3714 mPendingExecutor.markCompleted();
3715 mPendingExecutor = null;
3716 }
3717 }
3718
3719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 * Refreshes the shortcuts shown on the workspace.
3721 *
3722 * Implementation of the method from LauncherModel.Callbacks.
3723 */
3724 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003725 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003726
Winson Chungd64d1762013-08-20 14:37:16 -07003727 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003728 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003729 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003730
Michael Jurka05bf644e2011-11-30 20:28:53 -08003731 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003732 if (mHotseat != null) {
3733 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003734 }
3735 }
3736
Adam Cohendcd297f2013-06-18 13:13:40 -07003737 @Override
3738 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003739 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003740
Adam Cohen5084cba2013-09-03 12:01:16 -07003741 // If there are no screens, we need to have an empty screen
3742 if (orderedScreenIds.size() == 0) {
3743 mWorkspace.addExtraEmptyScreen();
3744 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003745
3746 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003747 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003748 if (hasCustomContentToLeft()) {
3749 mWorkspace.createCustomContentContainer();
3750 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003751 }
Winson Chung64359a52013-07-08 17:17:08 -07003752 }
3753
3754 @Override
3755 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003756 int count = orderedScreenIds.size();
3757 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003758 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003759 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003760 }
3761
Winson Chungd64d1762013-08-20 14:37:16 -07003762 public void bindAppsAdded(final ArrayList<Long> newScreens,
3763 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003764 final ArrayList<ItemInfo> addAnimated,
3765 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 Runnable r = new Runnable() {
3767 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003768 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003769 }
3770 };
3771 if (waitUntilResume(r)) {
3772 return;
3773 }
3774
Winson Chungd64d1762013-08-20 14:37:16 -07003775 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003776 if (newScreens != null) {
3777 bindAddScreens(newScreens);
3778 }
Winson Chungd64d1762013-08-20 14:37:16 -07003779
3780 // We add the items without animation on non-visible pages, and with
3781 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003782 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003783 bindItems(addNotAnimated, 0,
3784 addNotAnimated.size(), false);
3785 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003786 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003787 bindItems(addAnimated, 0,
3788 addAnimated.size(), true);
3789 }
Winson Chungc58497e2013-09-03 17:48:37 -07003790
Winson Chung87412982013-10-03 18:34:14 -07003791 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003792 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003793
Winson Chungb745afb2015-03-02 11:51:23 -08003794 if (addedApps != null && mAppsView != null) {
3795 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003796 }
Winson Chungd64d1762013-08-20 14:37:16 -07003797 }
3798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 * Bind the items start-end from the list.
3801 *
3802 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003803 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003804 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003805 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3806 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 Runnable r = new Runnable() {
3808 public void run() {
3809 bindItems(shortcuts, start, end, forceAnimateIcons);
3810 }
3811 };
3812 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003813 return;
3814 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003815
Winson Chungf0c6ae02012-03-21 16:10:31 -07003816 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003817 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3818 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003819 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003821 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003822 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003824
3825 // Short circuit if we are loading dock items for a configuration which has no dock
3826 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3827 mHotseat == null) {
3828 continue;
3829 }
3830
Sunny Goyal639e9062015-08-19 19:17:06 -07003831 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 switch (item.itemType) {
3833 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3834 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003836 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003837
Winson Chung64359a52013-07-08 17:17:08 -07003838 /*
3839 * TODO: FIX collision case
3840 */
Winson Chungce376632013-07-11 16:12:41 -07003841 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3842 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3843 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003844 View v = cl.getChildAt(item.cellX, item.cellY);
3845 Object tag = v.getTag();
3846 String desc = "Collision while binding workspace item: " + item
3847 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003848 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003849 throw (new RuntimeException(desc));
3850 } else {
3851 Log.d(TAG, desc);
3852 }
Winson Chungce376632013-07-11 16:12:41 -07003853 }
Winson Chung64359a52013-07-08 17:17:08 -07003854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003856 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003857 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003858 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003859 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003861 default:
3862 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003863 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003864
3865 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3866 item.cellY, 1, 1);
3867 if (animateIcons) {
3868 // Animate all the applications up now
3869 view.setAlpha(0f);
3870 view.setScaleX(0f);
3871 view.setScaleY(0f);
3872 bounceAnims.add(createNewAppBounceAnimation(view, i));
3873 newShortcutsScreenId = item.screenId;
3874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003876
Winson Chung997a9232013-07-24 15:33:46 -07003877 if (animateIcons) {
3878 // Animate to the correct page
3879 if (newShortcutsScreenId > -1) {
3880 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003881 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003882 final Runnable startBounceAnimRunnable = new Runnable() {
3883 public void run() {
3884 anim.playTogether(bounceAnims);
3885 anim.start();
3886 }
3887 };
Winson Chung997a9232013-07-24 15:33:46 -07003888 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003889 // We post the animation slightly delayed to prevent slowdowns
3890 // when we are loading right after we return to launcher.
3891 mWorkspace.postDelayed(new Runnable() {
3892 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003893 if (mWorkspace != null) {
3894 mWorkspace.snapToPage(newScreenIndex);
3895 mWorkspace.postDelayed(startBounceAnimRunnable,
3896 NEW_APPS_ANIMATION_DELAY);
3897 }
Winson Chung94d67682013-09-25 16:29:40 -07003898 }
3899 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003900 } else {
3901 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003902 }
3903 }
Winson Chung64359a52013-07-08 17:17:08 -07003904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003906 }
3907
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 /**
3909 * Implementation of the method from LauncherModel.Callbacks.
3910 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003911 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003912 Runnable r = new Runnable() {
3913 public void run() {
3914 bindFolders(folders);
3915 }
3916 };
3917 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003918 return;
3919 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003920 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003921 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003923 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3924 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3925 view.updateIcon(mIconCache);
3926 item.hostView = view;
3927 item.hostView.updateAppWidget(null);
3928 item.hostView.setOnClickListener(this);
3929 addAppWidgetToWorkspace(item, null, false);
3930 mWorkspace.requestLayout();
3931 }
3932
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 /**
3934 * Add the views for a widget to the workspace.
3935 *
3936 * Implementation of the method from LauncherModel.Callbacks.
3937 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003938 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003939 Runnable r = new Runnable() {
3940 public void run() {
3941 bindAppWidget(item);
3942 }
3943 };
3944 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003945 return;
3946 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003947
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003948 if (mIsSafeModeEnabled) {
3949 bindSafeModeWidget(item);
3950 return;
3951 }
3952
Daniel Sandler843e8602010-06-07 14:59:01 -04003953 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3954 if (DEBUG_WIDGETS) {
3955 Log.d(TAG, "bindAppWidget: " + item);
3956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003958 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003959
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003960 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3961 // If the provider is not ready, bind as a pending widget.
3962 appWidgetInfo = null;
3963 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3964 // The widget id is not valid. Try to find the widget based on the provider info.
3965 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3966 } else {
3967 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3968 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003969
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003970 // If the provider is ready, but the width is not yet restored, try to restore it.
3971 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3972 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003973 if (appWidgetInfo == null) {
3974 if (DEBUG_WIDGETS) {
3975 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3976 + " belongs to component " + item.providerName
3977 + ", as the povider is null");
3978 }
3979 LauncherModel.deleteItemFromDatabase(this, item);
3980 return;
3981 }
Sunny Goyalff572272014-07-23 13:58:07 -07003982
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003983 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003984 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003985 // Note: This assumes that the id remap broadcast is received before this step.
3986 // If that is not the case, the id remap will be ignored and user may see the
3987 // click to setup view.
3988 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3989 pendingInfo.spanX = item.spanX;
3990 pendingInfo.spanY = item.spanY;
3991 pendingInfo.minSpanX = item.minSpanX;
3992 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003993 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003994
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003995 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3996 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3997 newWidgetId, appWidgetInfo, options);
3998
3999 // TODO consider showing a permission dialog when the widget is clicked.
4000 if (!success) {
4001 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4004 + " belongs to component " + item.providerName
4005 + ", as the launcher is unable to bing a new widget id");
4006 }
4007 LauncherModel.deleteItemFromDatabase(this, item);
4008 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004009 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004010
4011 item.appWidgetId = newWidgetId;
4012
4013 // If the widget has a configure activity, it is still needs to set it up, otherwise
4014 // the widget is ready to go.
4015 item.restoreStatus = (appWidgetInfo.configure == null)
4016 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4017 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4018
4019 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004020 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004021 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004022 // The widget was marked as UI not ready, but there is no configure activity to
4023 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004024 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4025 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004026 }
Sunny Goyalff572272014-07-23 13:58:07 -07004027 }
4028
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004029 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004030 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004031 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4032 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004034
Sunny Goyal56c73602015-09-25 12:55:01 -07004035 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004036 if (appWidgetInfo == null) {
4037 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004038 deleteWidgetInfo(item);
4039 return;
4040 }
4041
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004042 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004043 item.minSpanX = appWidgetInfo.minSpanX;
4044 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004045 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004047 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4048 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004049 view.updateIcon(mIconCache);
4050 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004051 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004052 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004053 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004054 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004055 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056
Daniel Sandler843e8602010-06-07 14:59:01 -04004057 if (DEBUG_WIDGETS) {
4058 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4059 + (SystemClock.uptimeMillis()-start) + "ms");
4060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 }
4062
Sunny Goyalff572272014-07-23 13:58:07 -07004063 /**
4064 * Restores a pending widget.
4065 *
4066 * @param appWidgetId The app widget id
4067 * @param cellInfo The position on screen where to create the widget.
4068 */
4069 private void completeRestoreAppWidget(final int appWidgetId) {
4070 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4071 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4072 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4073 return;
4074 }
4075
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004076 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004077 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4078
4079 mWorkspace.reinflateWidgetsIfNecessary();
4080 LauncherModel.updateItemInDatabase(this, info);
4081 }
4082
Adam Cohen1462de32012-07-24 22:34:36 -07004083 public void onPageBoundSynchronously(int page) {
4084 mSynchronouslyBoundPages.add(page);
4085 }
4086
Sunny Goyal527c7d32015-08-28 15:19:36 -07004087 @Override
4088 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4089 if (mPendingExecutor != null) {
4090 mPendingExecutor.markCompleted();
4091 }
4092 mPendingExecutor = executor;
4093 executor.attachTo(this);
4094 }
4095
4096 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4097 if (mPendingExecutor == executor) {
4098 mPendingExecutor = null;
4099 }
4100 }
4101
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 /**
4103 * Callback saying that there aren't any more items to bind.
4104 *
4105 * Implementation of the method from LauncherModel.Callbacks.
4106 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004107 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004108 Runnable r = new Runnable() {
4109 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004110 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004111 }
4112 };
4113 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004114 return;
4115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 if (mSavedState != null) {
4117 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004118 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004120
4121 long folderId = mSavedState.getLong(RUNTIME_STATE_OPEN_FOLDER_ID);
4122 if (folderId != 0) {
4123 View view = mWorkspace.getHomescreenIconByItemId(folderId);
4124 if (view instanceof FolderIcon) {
4125 FolderIcon icon = (FolderIcon) view;
4126 FolderInfo info = icon.getFolderInfo();
4127 long currentScreenId = mWorkspace.getScreenIdForPageIndex(
4128 mWorkspace.getNextPage());
4129 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
4130 || info.screenId == currentScreenId) {
4131 // We can show the folder
4132 openFolder(icon, false);
4133 } else {
4134 Launcher.addDumpLog(TAG, "Saved state contains folder " + info +
4135 " but current screen is " + currentScreenId);
4136 }
4137 }
4138 }
4139
Joe Onorato9c1289c2009-08-17 11:03:03 -04004140 mSavedState = null;
4141 }
4142
Adam Cohen1462de32012-07-24 22:34:36 -07004143 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004145 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004146 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004147
4148 // If we received the result of any pending adds while the loader was running (e.g. the
4149 // widget configuration forced an orientation change), process them now.
4150 if (sPendingAddItem != null) {
4151 final long screenId = completeAdd(sPendingAddItem);
4152
4153 // TODO: this moves the user to the page where the pending item was added. Ideally,
4154 // the screen would be guaranteed to exist after bind, and the page would be set through
4155 // the workspace restore process.
4156 mWorkspace.post(new Runnable() {
4157 @Override
4158 public void run() {
4159 mWorkspace.snapToScreenId(screenId);
4160 }
4161 });
4162 sPendingAddItem = null;
4163 }
4164
Sunny Goyal756adbc2015-04-16 15:20:51 -07004165 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004166
4167 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004168 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004169 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004170 }
4171
Adam Cohen3ed316a2014-07-23 18:21:20 -07004172 private void sendLoadingCompleteBroadcastIfNecessary() {
4173 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4174 String permission =
4175 getResources().getString(R.string.receive_first_load_broadcast_permission);
4176 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4177 sendBroadcast(intent, permission);
4178 SharedPreferences.Editor editor = mSharedPrefs.edit();
4179 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4180 editor.apply();
4181 }
4182 }
4183
Winson Chunga0b7e862013-09-05 16:03:15 -07004184 public boolean isAllAppsButtonRank(int rank) {
4185 if (mHotseat != null) {
4186 return mHotseat.isAllAppsButtonRank(rank);
4187 }
4188 return false;
4189 }
4190
Winson Chunga2413752012-04-03 14:22:34 -07004191 private boolean canRunNewAppsAnimation() {
4192 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004193 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4194 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004195 }
4196
Winson Chungc9168342013-06-26 14:54:55 -07004197 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004198 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004199 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4200 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004201 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004202 return bounceAnim;
4203 }
4204
Adam Cohen27772732013-11-11 14:00:56 +00004205 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004206 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004207 }
4208
Tony Wickham3a3517f2015-10-06 11:27:04 -07004209 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004210 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004211 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004212 }
4213
Tony Wickham55616cd2015-09-23 14:55:17 -07004214 public int getSearchBarHeight() {
4215 if (mLauncherCallbacks != null) {
4216 return mLauncherCallbacks.getSearchBarHeight();
4217 }
4218 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4219 }
4220
Winson Chung88fa7412015-08-03 14:25:28 -07004221 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004222 if (mSearchDropTargetBar == null) {
4223 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004224 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004225 if (mQsb != null) {
4226 mSearchDropTargetBar.removeView(mQsb);
4227 mQsb = null;
4228 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004229 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004230 }
4231
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004232 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004233 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4234 * multiple calls to bind the same list.)
4235 */
4236 @Thunk ArrayList<AppInfo> mTmpAppsList;
4237 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4238 public void run() {
4239 bindAllApplications(mTmpAppsList);
4240 mTmpAppsList = null;
4241 }
4242 };
4243
4244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 * Add the icons for all apps.
4246 *
4247 * Implementation of the method from LauncherModel.Callbacks.
4248 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004249 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004250 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4251 mTmpAppsList = apps;
4252 return;
4253 }
4254
Winson Chungb745afb2015-03-02 11:51:23 -08004255 if (mAppsView != null) {
4256 mAppsView.setApps(apps);
4257 }
Adam Cohen9211d422014-10-07 18:14:53 -07004258 if (mLauncherCallbacks != null) {
4259 mLauncherCallbacks.bindAllApplications(apps);
4260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 }
4262
4263 /**
4264 * A package was updated.
4265 *
4266 * Implementation of the method from LauncherModel.Callbacks.
4267 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004268 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004269 Runnable r = new Runnable() {
4270 public void run() {
4271 bindAppsUpdated(apps);
4272 }
4273 };
4274 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004275 return;
4276 }
4277
Winson Chungb745afb2015-03-02 11:51:23 -08004278 if (mAppsView != null) {
4279 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004281 }
4282
Sunny Goyal4390ace2014-10-13 11:33:11 -07004283 @Override
4284 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4285 Runnable r = new Runnable() {
4286 public void run() {
4287 bindWidgetsRestored(widgets);
4288 }
4289 };
4290 if (waitUntilResume(r)) {
4291 return;
4292 }
4293 mWorkspace.widgetsRestored(widgets);
4294 }
4295
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004297 * Some shortcuts were updated in the background.
4298 *
4299 * Implementation of the method from LauncherModel.Callbacks.
4300 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004301 @Override
4302 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4303 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004304 Runnable r = new Runnable() {
4305 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004306 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004307 }
4308 };
4309 if (waitUntilResume(r)) {
4310 return;
4311 }
4312
Sunny Goyal4390ace2014-10-13 11:33:11 -07004313 if (!updated.isEmpty()) {
4314 mWorkspace.updateShortcuts(updated);
4315 }
4316
4317 if (!removed.isEmpty()) {
4318 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4319 for (ShortcutInfo si : removed) {
4320 removedComponents.add(si.getTargetComponent());
4321 }
4322 mWorkspace.removeItemsByComponentName(removedComponents, user);
4323 // Notify the drag controller
4324 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004325 }
4326 }
4327
4328 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004329 * Update the state of a package, typically related to install state.
4330 *
4331 * Implementation of the method from LauncherModel.Callbacks.
4332 */
Sunny Goyale755d462014-07-22 13:48:29 -07004333 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004334 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4335 Runnable r = new Runnable() {
4336 public void run() {
4337 bindRestoreItemsChange(updates);
4338 }
4339 };
4340 if (waitUntilResume(r)) {
4341 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004342 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004343
Sunny Goyal756adbc2015-04-16 15:20:51 -07004344 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004345 }
4346
4347 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004348 * A package was uninstalled. We take both the super set of packageNames
4349 * in addition to specific applications to remove, the reason being that
4350 * this can be called when a package is updated as well. In that scenario,
4351 * we only remove specific components from the workspace, where as
4352 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004354 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004355 * Implementation of the method from LauncherModel.Callbacks.
4356 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004357 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004358 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004359 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004360 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004361 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004363 }
Winson Chungd64d1762013-08-20 14:37:16 -07004364 };
4365 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004366 return;
4367 }
4368
Sunny Goyal1a745e82014-10-02 15:58:31 -07004369 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004370 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4371 for (AppInfo info : appInfos) {
4372 removedComponents.add(info.componentName);
4373 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004374 if (!packageNames.isEmpty()) {
4375 mWorkspace.removeItemsByPackageName(packageNames, user);
4376 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004377 if (!removedComponents.isEmpty()) {
4378 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004379 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004380 // Notify the drag controller
4381 mDragController.onAppsRemoved(packageNames, removedComponents);
4382
Sunny Goyal1a745e82014-10-02 15:58:31 -07004383 } else {
4384 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004385 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004386
Winson Chungdf95eb12013-10-16 14:57:07 -07004387 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004388 if (mAppsView != null) {
4389 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004391 }
Joe Onoratobe386092009-11-17 17:32:16 -08004392
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004393 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004394 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004395 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004396 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004397 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004398
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004399 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004400 public void bindWidgetsModel(WidgetsModel model) {
4401 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004402 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004403 return;
4404 }
4405
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004406 if (mWidgetsView != null && model != null) {
4407 mWidgetsView.addWidgets(model);
4408 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004409 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004410 }
4411
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004412 @Override
4413 public void notifyWidgetProvidersChanged() {
4414 if (mWorkspace.getState().shouldUpdateWidget) {
4415 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4416 }
4417 }
4418
Winson Chung400438b2011-01-16 17:53:48 -08004419 private int mapConfigurationOriActivityInfoOri(int configOri) {
4420 final Display d = getWindowManager().getDefaultDisplay();
4421 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4422 switch (d.getRotation()) {
4423 case Surface.ROTATION_0:
4424 case Surface.ROTATION_180:
4425 // We are currently in the same basic orientation as the natural orientation
4426 naturalOri = configOri;
4427 break;
4428 case Surface.ROTATION_90:
4429 case Surface.ROTATION_270:
4430 // We are currently in the other basic orientation to the natural orientation
4431 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4432 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4433 break;
4434 }
4435
4436 int[] oriMap = {
4437 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4438 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4439 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4440 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4441 };
4442 // Since the map starts at portrait, we need to offset if this device's natural orientation
4443 // is landscape.
4444 int indexOffset = 0;
4445 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4446 indexOffset = 1;
4447 }
4448 return oriMap[(d.getRotation() + indexOffset) % 4];
4449 }
Adam Cohen446e9402011-09-15 18:21:21 -07004450
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004451 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004452 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004453 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004454 if (Utilities.ATLEAST_JB_MR2) {
4455 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4456 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004457 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4458 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004459 }
Winson Chung4b919f82012-05-01 10:44:08 -07004460 }
4461 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004462
Winson Chung4b919f82012-05-01 10:44:08 -07004463 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004464 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004465 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004466 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004467 } else {
4468 mHandler.postDelayed(new Runnable() {
4469 public void run() {
4470 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4471 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004472 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004473 }
Winson Chung4b919f82012-05-01 10:44:08 -07004474 }
Winson Chung400438b2011-01-16 17:53:48 -08004475 }
4476
Winson Chunge43a1e72014-01-15 10:33:02 -08004477 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.isLauncherPreinstalled();
4480 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004481 PackageManager pm = getPackageManager();
4482 try {
4483 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4484 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4485 return true;
4486 } else {
4487 return false;
4488 }
4489 } catch (NameNotFoundException e) {
4490 e.printStackTrace();
4491 return false;
4492 }
4493 }
4494
Adam Cohenea90f832014-05-21 15:03:34 -07004495 /**
4496 * This method indicates whether or not we should suggest default wallpaper dimensions
4497 * when our wallpaper cropper was not yet used to set a wallpaper.
4498 */
4499 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004500 if (mLauncherCallbacks != null) {
4501 return mLauncherCallbacks.overrideWallpaperDimensions();
4502 }
Adam Cohenea90f832014-05-21 15:03:34 -07004503 return true;
4504 }
4505
Adam Cohen432609a2014-03-13 17:03:22 -07004506 /**
4507 * To be overridden by subclasses to indicate that there is an activity to launch
4508 * before showing the standard launcher experience.
4509 */
4510 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004511 if (mLauncherCallbacks != null) {
4512 return mLauncherCallbacks.hasFirstRunActivity();
4513 }
Adam Cohen432609a2014-03-13 17:03:22 -07004514 return false;
4515 }
4516
4517 /**
4518 * To be overridden by subclasses to launch any first run activity
4519 */
4520 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004521 if (mLauncherCallbacks != null) {
4522 return mLauncherCallbacks.getFirstRunActivity();
4523 }
Adam Cohen432609a2014-03-13 17:03:22 -07004524 return null;
4525 }
4526
Winson Chunga6945242014-01-08 14:04:34 -08004527 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004528 return !ActivityManager.isRunningInTestHarness() &&
4529 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004530 }
4531
Adam Cohen4a9423d2014-03-28 14:22:31 -07004532 protected boolean hasRunFirstRunActivity() {
4533 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4534 }
4535
Adam Cohen432609a2014-03-13 17:03:22 -07004536 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004537 if (shouldRunFirstRunActivity() &&
4538 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004539 Intent firstRunIntent = getFirstRunActivity();
4540 if (firstRunIntent != null) {
4541 startActivity(firstRunIntent);
4542 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004543 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004544 }
4545 }
Adam Cohen432609a2014-03-13 17:03:22 -07004546 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004547 }
4548
4549 private void markFirstRunActivityShown() {
4550 SharedPreferences.Editor editor = mSharedPrefs.edit();
4551 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4552 editor.apply();
4553 }
4554
Adam Cohen432609a2014-03-13 17:03:22 -07004555 /**
4556 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4557 * screen that must be displayed and dismissed.
4558 */
4559 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004560 if (mLauncherCallbacks != null) {
4561 return mLauncherCallbacks.hasDismissableIntroScreen();
4562 }
Adam Cohen432609a2014-03-13 17:03:22 -07004563 return false;
4564 }
4565
4566 /**
4567 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4568 */
4569 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004570 if (mLauncherCallbacks != null) {
4571 return mLauncherCallbacks.getIntroScreen();
4572 }
Adam Cohen432609a2014-03-13 17:03:22 -07004573 return null;
4574 }
4575
4576 /**
4577 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4578 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4579 */
4580 private boolean shouldShowIntroScreen() {
4581 return hasDismissableIntroScreen() &&
4582 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4583 }
4584
4585 protected void showIntroScreen() {
4586 View introScreen = getIntroScreen();
4587 changeWallpaperVisiblity(false);
4588 if (introScreen != null) {
4589 mDragLayer.showOverlayView(introScreen);
4590 }
4591 }
4592
4593 public void dismissIntroScreen() {
4594 markIntroScreenDismissed();
4595 if (showFirstRunActivity()) {
4596 // We delay hiding the intro view until the first run activity is showing. This
4597 // avoids a blip.
4598 mWorkspace.postDelayed(new Runnable() {
4599 @Override
4600 public void run() {
4601 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004602 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004603 }
4604 }, ACTIVITY_START_DELAY);
4605 } else {
4606 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004607 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004608 }
4609 changeWallpaperVisiblity(true);
4610 }
4611
4612 private void markIntroScreenDismissed() {
4613 SharedPreferences.Editor editor = mSharedPrefs.edit();
4614 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4615 editor.apply();
4616 }
4617
Adam Cohen091440a2015-03-18 14:16:05 -07004618 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004619 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4620 // on the device, then we always show the first run cling experience (or if there is no
4621 // launcher2). Otherwise, we prompt the user upon started for migration
4622 LauncherClings launcherClings = new LauncherClings(this);
4623 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004624 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004625 if (mModel.canMigrateFromOldLauncherDb(this)) {
4626 launcherClings.showMigrationCling();
4627 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004628 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004629 }
4630 }
4631 }
4632
Winson Chunga6945242014-01-08 14:04:34 -08004633 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004634 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4635 if (mHotseat != null) mHotseat.setAlpha(1f);
4636 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004637 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4638 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004639 }
4640
4641 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004642 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4643 if (mHotseat != null) mHotseat.setAlpha(0f);
4644 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004645 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4646 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004647 }
4648
Winson Chung5ffd51d2015-06-25 11:36:50 -07004649 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004650 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004651 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004652 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004653 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004654 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004655 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4656 if (userHandle != null) {
4657 user = UserHandleCompat.fromUser(userHandle);
4658 }
4659 }
4660 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004661 }
4662
4663 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004664 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004665 if (user == null) {
4666 user = UserHandleCompat.myUserHandle();
4667 }
4668
4669 // Called from search suggestion, add the profile extra to the intent to ensure that we
4670 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004671 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004672 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004673 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004674 return null;
4675 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004676 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004677 }
4678
Winson Chung5ffd51d2015-06-25 11:36:50 -07004679 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004680 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4681 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004682 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004683 UserHandleCompat.myUserHandle());
4684 }
4685
Winson Chung5ffd51d2015-06-25 11:36:50 -07004686 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004687 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4688 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004689 mWorkspace.onExternalDragStartedWithItem(dragView);
4690 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004691 }
4692
Winson Chung5ffd51d2015-06-25 11:36:50 -07004693 protected void moveWorkspaceToDefaultScreen() {
4694 mWorkspace.moveToDefaultScreen(false);
4695 }
4696
Anjali Koppalf5d29132014-02-28 13:33:27 -08004697 @Override
4698 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004699 if (mLauncherCallbacks != null) {
4700 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4701 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004702 }
4703
Winson Chung80baf5a2010-08-09 16:03:15 -07004704 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004705 * Returns a FastBitmapDrawable with the icon, accurately sized.
4706 */
4707 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4708 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4709 d.setFilterBitmap(true);
4710 resizeIconDrawable(d);
4711 return d;
4712 }
4713
4714 /**
4715 * Resizes an icon drawable to the correct icon size.
4716 */
Winson5fbe0742015-09-30 15:33:00 -07004717 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004718 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004719 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004720 }
4721
4722 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004723 * Prints out out state for debugging.
4724 */
4725 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004726 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004727 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004728 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4729 Log.d(TAG, "mRestoring=" + mRestoring);
4730 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004731 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004732 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004733 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004734
Daniel Sandler325dc232013-06-05 22:57:57 -04004735 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004736 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004737
4738 @Override
4739 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4740 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004741 // Dump workspace
4742 writer.println(prefix + "Workspace Items");
4743 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4744 writer.println(prefix + " Homescreen " + i);
4745
4746 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4747 for (int j = 0; j < layout.getChildCount(); j++) {
4748 Object tag = layout.getChildAt(j).getTag();
4749 if (tag != null) {
4750 writer.println(prefix + " " + tag.toString());
4751 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004752 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004753 }
Sunny Goyala1365452015-10-01 15:46:24 -07004754
4755 writer.println(prefix + " Hotseat");
4756 ViewGroup layout = mHotseat.getLayout().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 }
4762 }
4763
4764 synchronized (sDumpLogs) {
4765 writer.println();
4766 writer.println(prefix + "Debug logs");
4767 for (String log : sDumpLogs) {
4768 writer.println(prefix + " " + log);
4769 }
4770 }
4771
Adam Cohen9211d422014-10-07 18:14:53 -07004772 if (mLauncherCallbacks != null) {
4773 mLauncherCallbacks.dump(prefix, fd, writer, args);
4774 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004775 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004776
Sunny Goyala1365452015-10-01 15:46:24 -07004777 public static void addDumpLog(String tag, String log) {
4778 Log.d(tag, log);
4779 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004780 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004781 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004782 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004783 }
4784
Adam Cohen59400422014-03-05 18:07:04 -08004785 public static CustomAppWidget getCustomAppWidget(String name) {
4786 return sCustomAppWidgets.get(name);
4787 }
4788
4789 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4790 return sCustomAppWidgets;
4791 }
4792
Sunny Goyal29538332016-02-18 09:10:19 -08004793 public static List<View> getFolderContents(View icon) {
4794 if (icon instanceof FolderIcon) {
4795 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4796 } else {
4797 return Collections.EMPTY_LIST;
4798 }
4799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004800}
Michael Jurka2763be32011-02-24 11:19:57 -08004801
Dan Sandlerd5024042014-01-09 15:01:33 -05004802interface DebugIntents {
4803 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4804 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004805}