blob: 97954986c1894d8b96187b0203a4fd6c857a57a1 [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;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070087import android.view.accessibility.AccessibilityManager;
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;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800112import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800113import com.android.launcher3.logging.UserEventLogger;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700114import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700117import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700118import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700120import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700131import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800135import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700141 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
143 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700144 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700145 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
177 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700188 // Type: Content Values / parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Adam Cohen3ed316a2014-07-23 18:21:20 -0700198 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
199 static final String ACTION_FIRST_LOAD_COMPLETE =
200 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
201
Sunny Goyal594d76d2014-11-06 10:12:54 -0800202 private static final String QSB_WIDGET_ID = "qsb_widget_id";
203 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
204
Winson Chunga6945242014-01-08 14:04:34 -0800205 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700208 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
209 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700210
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700213
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700214 private boolean mIsSafeModeEnabled;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700218 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700219 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Winson Chunga2413752012-04-03 14:22:34 -0700221 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700222 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
223 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700225
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800226 private final BroadcastReceiver mCloseSystemDialogsReceiver
227 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700230 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800231 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800233 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700234
235 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalffe83f12014-08-14 17:39:34 -0700237 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700240 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800241 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800242 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243
Michael Jurka0280c3b2010-09-17 15:00:07 -0700244 private int[] mTmpAddItemCellCoordinates = new int[2];
245
Sunny Goyal316490e2015-06-02 09:38:28 -0700246 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800247 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700248
Michael Jurka838a4ca2011-02-07 13:33:06 -0800249 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700250 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700251
Winson Chungc51db6a2011-10-05 11:44:49 -0700252 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700253 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700254
255 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700256 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700258 // Main container view and the model for the widget tray screen.
259 @Thunk WidgetsContainerView mWidgetsView;
260 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
Sunny Goyal594d76d2014-11-06 10:12:54 -0800262 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Adam Cohen091440a2015-03-18 14:16:05 -0700272 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700281 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700285 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Sunny Goyal639e9062015-08-19 19:17:06 -0700291 private LauncherClings mClings;
292
Sunny Goyale2df0622015-04-24 11:27:00 -0700293 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 private static final int ADVANCE_INTERVAL = 20000;
300 private static final int ADVANCE_STAGGER = 250;
301
302 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700309 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700312
Adam Cohen1462de32012-07-24 22:34:36 -0700313 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700314 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700315
Sunny Goyala1365452015-10-01 15:46:24 -0700316 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
317 private static final Date sDateStamp = new Date();
318 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700319 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700320
Winson Chung46353de2012-02-16 14:05:10 -0800321 // We only want to get the SharedPreferences once since it does an FS stat each time we get
322 // it from the context.
323 private SharedPreferences mSharedPrefs;
324
Winson Chungf0c6ae02012-03-21 16:10:31 -0700325 // Holds the page that we need to animate to, and the icon views that we need to animate up
326 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700327 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700328 private Bitmap mFolderIconBitmap;
329 private Canvas mFolderIconCanvas;
330 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700332 private DeviceProfile mDeviceProfile;
333
Adam Cohen4b66bf32015-09-18 12:15:19 -0700334 private boolean mMoveToDefaultScreenFromNewIntent;
335
Winson Chung13eb5272015-05-11 16:30:13 -0700336 // This is set to the view that launched the activity that navigated the user away from
337 // launcher. Since there is no callback for when the activity has finished launching, enable
338 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800339 private BubbleTextView mWaitingForResume;
340
Adam Cohen59400422014-03-05 18:07:04 -0800341 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
342 new HashMap<String, CustomAppWidget>();
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700345 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
346 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800347 }
348 }
349
Adam Cohen091440a2015-03-18 14:16:05 -0700350 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700352 if (mWorkspace != null) {
353 mWorkspace.buildPageHardwareLayers();
354 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 }
356 };
357
Adam Cohendb364c32014-05-20 14:23:40 -0700358 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359
Adam Cohen091440a2015-03-18 14:16:05 -0700360 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int requestCode;
362 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700363 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 int cellX;
366 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700367 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800371 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800372
Adam Cohenf9c184a2016-01-15 16:47:43 -0800373 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375
376 @Thunk void setOrientation() {
377 if (mRotationEnabled) {
378 unlockScreenOrientation(true);
379 } else {
380 setRequestedOrientation(
381 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384
Sunny Goyal7779d622015-06-11 16:18:39 -0700385 private Runnable mUpdateOrientationRunnable = new Runnable() {
386 public void run() {
387 setOrientation();
388 }
389 };
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 @Override
392 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700393 if (DEBUG_STRICT_MODE) {
394 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
395 .detectDiskReads()
396 .detectDiskWrites()
397 .detectNetwork() // or .detectAll() for all detectable problems
398 .penaltyLog()
399 .build());
400 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
401 .detectLeakedSqlLiteObjects()
402 .detectLeakedClosableObjects()
403 .penaltyLog()
404 .penaltyDeath()
405 .build());
406 }
407
Adam Cohen9211d422014-10-07 18:14:53 -0700408 if (mLauncherCallbacks != null) {
409 mLauncherCallbacks.preOnCreate();
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400413
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Adam Cohen2e6da152015-05-06 11:42:25 -0700416 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700417 mDeviceProfile = getResources().getConfiguration().orientation
418 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700419 app.getInvariantDeviceProfile().landscapeProfile
420 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700421
Sunny Goyalf7258242015-10-19 16:59:07 -0700422 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700423 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800424 mModel = app.setLauncher(this);
425 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700428 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800431 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400432
Sunny Goyalffe83f12014-08-14 17:39:34 -0700433 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
436 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700438 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
439 // this also ensures that any synchronous binding below doesn't re-trigger another
440 // LauncherModel load.
441 mPaused = false;
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200444 android.os.Debug.startMethodTracing(
445 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
Tony Wickhameef44322015-10-20 13:24:36 -0700450 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
451 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700453 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700454 mExtractedColors = new ExtractedColors();
455 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
Tony Wickhame2217252016-03-22 16:34:23 -0700457 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
458 .addAccessibilityStateChangeListener(this);
459
Joe Onorato7c312c12009-08-13 21:36:53 -0700460 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 mSavedState = savedInstanceState;
463 restoreState(mSavedState);
464
465 if (PROFILE_STARTUP) {
466 android.os.Debug.stopMethodTracing();
467 }
468
469 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700470 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 // If the user leaves launcher, then we should just load items asynchronously when
472 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700473 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 } else {
475 // We only load the page synchronously if the user rotates (or triggers a
476 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700477 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
486 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800487
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800488 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700489 // In case we are on a device with locked rotation, we should look at preferences to check
490 // if the user has specifically allowed rotation.
491 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700492 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700493 }
494
495 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
496 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400497 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700498
Adam Cohen9211d422014-10-07 18:14:53 -0700499 if (mLauncherCallbacks != null) {
500 mLauncherCallbacks.onCreate(savedInstanceState);
501 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700502
503 if (shouldShowIntroScreen()) {
504 showIntroScreen();
505 } else {
506 showFirstRunActivity();
507 showFirstRunClings();
508 }
Adam Cohen9211d422014-10-07 18:14:53 -0700509 }
510
Sunny Goyal7779d622015-06-11 16:18:39 -0700511 @Override
512 public void onSettingsChanged(String settings, boolean value) {
513 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
514 mRotationEnabled = value;
515 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
516 mUpdateOrientationRunnable.run();
517 }
518 }
519 }
520
Tony Wickham827cef22016-03-17 15:39:39 -0700521 @Override
522 public void onExtractedColorsChanged() {
523 loadExtractedColorsAndColorItems();
524 }
525
526 private void loadExtractedColorsAndColorItems() {
527 if (mExtractedColors != null) {
528 mExtractedColors.load(this);
529 // TODO: pass mExtractedColors to interested items such as hotseat.
Tony Wickham827cef22016-03-17 15:39:39 -0700530 }
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 private LauncherCallbacks mLauncherCallbacks;
534
535 public void onPostCreate(Bundle savedInstanceState) {
536 super.onPostCreate(savedInstanceState);
537 if (mLauncherCallbacks != null) {
538 mLauncherCallbacks.onPostCreate(savedInstanceState);
539 }
540 }
541
Sunny Goyal32554d12015-12-03 15:31:25 -0800542 /**
543 * Call this after onCreate to set or clear overlay.
544 */
545 public void setLauncherOverlay(LauncherOverlay overlay) {
546 if (overlay != null) {
547 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
548 }
549 mWorkspace.setLauncherOverlay(overlay);
550 }
551
Adam Cohen9211d422014-10-07 18:14:53 -0700552 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
553 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 private boolean mWorkspaceImportanceStored = false;
556 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700557 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800558 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700559 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
560
561 @Override
562 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700563 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 return;
565 }
566 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700567 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspace != null) {
569 mWorkspaceImportanceForAccessibility =
570 mWorkspace.getImportantForAccessibility();
571 mWorkspace.setImportantForAccessibility(
572 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
573 mWorkspaceImportanceStored = true;
574 }
575 if (mHotseat != null) {
576 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
577 mHotseat.setImportantForAccessibility(
578 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
579 mHotseatImportanceStored = true;
580 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700581 }
582
583 @Override
584 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700585 if (mWorkspaceImportanceStored && mWorkspace != null) {
586 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
587 }
588 if (mHotseatImportanceStored && mHotseat != null) {
589 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
590 }
591 mWorkspaceImportanceStored = false;
592 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700593 }
594 });
Adam Cohen9211d422014-10-07 18:14:53 -0700595 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700596 }
597
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700598 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700599 public void onLauncherProviderChange() {
600 if (mLauncherCallbacks != null) {
601 mLauncherCallbacks.onLauncherProviderChange();
602 }
603 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700604
Adam Cohen9211d422014-10-07 18:14:53 -0700605 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700606 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700607 if (mLauncherCallbacks != null) {
608 return mLauncherCallbacks.hasCustomContentToLeft();
609 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700610 return false;
611 }
612
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500614 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 * {@link #addToCustomContentPage}. This will only be invoked if
616 * {@link #hasCustomContentToLeft()} is {@code true}.
617 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700619 if (mLauncherCallbacks != null) {
620 mLauncherCallbacks.populateCustomContentContainer();
621 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 }
623
624 /**
625 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
626 * ensure the custom content page is added or removed if necessary.
627 */
628 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600629 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600630 // Not bound yet, wait for bindScreens to be called.
631 return;
632 }
633
634 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
635 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500636 mWorkspace.createCustomContentContainer();
637 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600638 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
639 mWorkspace.removeCustomContentPage();
640 }
641 }
642
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643 public Stats getStats() {
644 return mStats;
645 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800646
647 /**
648 * Logger object is a singleton and does not have to be coupled with the foreground activity.
649 * Since most user event logging is done on the UI, the object is retrieved from the
650 * callback for convenience.
651 */
652 private void initLogger() {
653 if (mLauncherCallbacks != null) {
654 mUserEventLogger = mLauncherCallbacks.getLogger();
655 }
656 if (mUserEventLogger == null) {
657 mUserEventLogger = new UserEventLogger() {
658 @Override
659 public void processEvent(LauncherLogProto.LauncherEvent ev) {
660 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
661 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
662 LoggerUtils.getActionStr(ev.action),
663 LoggerUtils.getTargetStr(ev.srcTarget),
664 ev.elapsedContainerMillis,
665 ev.elapsedSessionMillis));
666 }
667 }
668 };
669 }
670 }
671
672 public UserEventLogger getLogger() {
673 return mUserEventLogger;
674 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100675
Hyunyoung Song3f471442015-04-08 19:01:34 -0700676 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700677 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
678 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700679 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700680 }
681
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000682 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700683 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
684 // This cast is safe as long as the id < 0x00FFFFFF
685 // Since we jail all the dynamically generated views, there should be no clashes
686 // with any other views.
687 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000688 }
689
690 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700691 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
692 * a configuration step, this allows the proper animations to run after other transitions.
693 */
Adam Cohendb364c32014-05-20 14:23:40 -0700694 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700695 long screenId = args.screenId;
696 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
697 // When the screen id represents an actual screen (as opposed to a rank) we make sure
698 // that the drop page actually exists.
699 screenId = ensurePendingDropLayoutExists(args.screenId);
700 }
Adam Cohendb364c32014-05-20 14:23:40 -0700701
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700704 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700705 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700706 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800707 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700708 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700709 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700710 case REQUEST_RECONFIGURE_APPWIDGET:
711 completeRestoreAppWidget(args.appWidgetId);
712 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 }
Michael Jurka27614d22012-04-02 06:40:22 -0700714 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
715 // if you turned the screen off and then back while in All Apps, Launcher would not
716 // return to the workspace. Clearing mAddInfo.container here fixes this issue
717 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700718 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 }
720
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800721 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700723 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700724 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700725 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800726 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700727
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 Runnable exitSpringLoaded = new Runnable() {
729 @Override
730 public void run() {
731 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
732 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
733 }
734 };
735
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700737 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
740 if (resultCode == RESULT_CANCELED) {
741 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700742 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800745 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700747
748 // When the user has granted permission to bind widgets, we should check to see if
749 // we can inflate the default search bar widget.
750 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 }
752 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200753 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
754 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700755 // User could have free-scrolled between pages before picking a wallpaper; make sure
756 // we move to the closest one now.
757 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700758 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200759 }
760 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200762
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700764 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700765
Adam Cohendb364c32014-05-20 14:23:40 -0700766 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 // We have special handling for widgets
768 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800769 final int appWidgetId;
770 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
771 : -1;
772 if (widgetId < 0) {
773 appWidgetId = pendingAddWidgetId;
774 } else {
775 appWidgetId = widgetId;
776 }
777
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800779 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700780 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
781 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 result = RESULT_CANCELED;
783 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 @Override
786 public void run() {
787 exitSpringLoadedDragModeDelayed(false, 0, null);
788 }
789 };
Adam Cohendb364c32014-05-20 14:23:40 -0700790 if (workspaceLocked) {
791 // No need to remove the empty screen if we're mid-binding, as the
792 // the bind will not add the empty screen.
793 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
794 } else {
795 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
796 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
797 }
Winson Chung5aaab772012-04-27 15:24:02 -0700798 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700799 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700800 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
801 // When the screen id represents an actual screen (as opposed to a rank)
802 // we make sure that the drop page actually exists.
803 mPendingAddInfo.screenId =
804 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
805 }
Adam Cohendb364c32014-05-20 14:23:40 -0700806 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
807
808 dropLayout.setDropPending(true);
809 final Runnable onComplete = new Runnable() {
810 @Override
811 public void run() {
812 completeTwoStageWidgetDrop(resultCode, appWidgetId);
813 dropLayout.setDropPending(false);
814 }
815 };
816 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
817 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
818 } else {
819 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
820 mPendingAddInfo);
821 sPendingAddItem = args;
822 }
Winson Chung5aaab772012-04-27 15:24:02 -0700823 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 return;
825 }
826
Sunny Goyalff572272014-07-23 13:58:07 -0700827 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
828 if (resultCode == RESULT_OK) {
829 // Update the widget view.
830 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
831 pendingAddWidgetId, mPendingAddInfo);
832 if (workspaceLocked) {
833 sPendingAddItem = args;
834 } else {
835 completeAdd(args);
836 }
837 }
838 // Leave the widget in the pending state if the user canceled the configure.
839 return;
840 }
841
Sunny Goyalaad90582015-07-09 14:22:50 -0700842 if (requestCode == REQUEST_CREATE_SHORTCUT) {
843 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
844 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
845 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
846 mPendingAddInfo);
847 if (isWorkspaceLocked()) {
848 sPendingAddItem = args;
849 } else {
850 completeAdd(args);
851 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
852 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
853 }
854 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800860
861 }
862
863 @Override
864 protected void onActivityResult(
865 final int requestCode, final int resultCode, final Intent data) {
866 handleActivityResult(requestCode, resultCode, data);
867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
869 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600872 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700873 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700875 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
876 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
877 View v = null;
878 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
879 if (layout != null) {
880 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
881 }
882 Intent intent = sPendingAddItem.intent;
883 sPendingAddItem = null;
884 if (grantResults.length > 0
885 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
886 startActivity(v, intent, null);
887 } else {
888 // TODO: Show a snack bar with link to settings
889 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
890 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
891 }
892 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600893 if (mLauncherCallbacks != null) {
894 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
895 grantResults);
896 }
897 }
898
Adam Cohendb364c32014-05-20 14:23:40 -0700899 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
900 appWidgetId, ItemInfo info) {
901 PendingAddArguments args = new PendingAddArguments();
902 args.requestCode = requestCode;
903 args.intent = data;
904 args.container = info.container;
905 args.screenId = info.screenId;
906 args.cellX = info.cellX;
907 args.cellY = info.cellY;
908 args.appWidgetId = appWidgetId;
909 return args;
910 }
911
912 /**
913 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
914 *
915 * @param screenId the screen id to check
916 * @return the new screen, or screenId if it exists
917 */
918 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800919 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700920 if (dropLayout == null) {
921 // it's possible that the add screen was removed because it was
922 // empty and a re-bind occurred
923 mWorkspace.addExtraEmptyScreen();
924 return mWorkspace.commitExtraEmptyScreen();
925 } else {
926 return screenId;
927 }
928 }
929
Adam Cohen091440a2015-03-18 14:16:05 -0700930 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800931 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 Runnable onCompleteRunnable = null;
933 int animationType = 0;
934
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 if (resultCode == RESULT_OK) {
937 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
938 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 mPendingAddWidgetInfo);
940 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 onCompleteRunnable = new Runnable() {
942 @Override
943 public void run() {
944 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
947 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 }
949 };
950 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800951 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 if (mDragLayer.getAnimatedView() != null) {
955 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
956 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
957 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700958 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 // The animated view may be null in the case of a rotation during widget configuration
960 onCompleteRunnable.run();
961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
963
964 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700965 protected void onStop() {
966 super.onStop();
967 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700968
969 if (mLauncherCallbacks != null) {
970 mLauncherCallbacks.onStop();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
975 protected void onStart() {
976 super.onStart();
977 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700978
979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.onStart();
981 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700982 }
983
984 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200986 long startTime = 0;
987 if (DEBUG_RESUME_TIME) {
988 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400989 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200990 }
Adam Cohen9211d422014-10-07 18:14:53 -0700991
992 if (mLauncherCallbacks != null) {
993 mLauncherCallbacks.preOnResume();
994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800997 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700998
Winson Chung4a2afa32012-07-19 14:53:05 -0700999 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001000 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001002 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001003 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001004 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001005 // view after launching an app, as they may be depending on the UI to be static to
1006 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001007 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001008 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001009 } else if (mOnResumeState == State.WIDGETS) {
1010 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001011 }
1012 mOnResumeState = State.NONE;
1013
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001014 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001015 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1016 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001018 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001020 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001021 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1031 }
1032
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1040 }
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Michael Jurka54554252013-08-01 12:52:23 +02001042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1045 }
1046 mOnResumeCallbacks.clear();
1047 }
Winson Chunge4e50662012-01-23 14:45:13 -08001048
1049 // Reset the pressed state of icons that were locked in the press state while activities
1050 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001052 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 mWaitingForResume.setStayPressed(false);
1054 }
Winson Chung82963d52013-10-09 11:20:57 -07001055
Adam Cohen06dff352012-06-01 17:17:08 -07001056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1059 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001060 if (!isWorkspaceLoading()) {
1061 getWorkspace().reinflateWidgetsIfNecessary();
1062 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001063 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001064
Michael Jurka447bf842013-05-15 14:52:15 +02001065 if (DEBUG_RESUME_TIME) {
1066 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1067 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001068
Adam Cohen4b66bf32015-09-18 12:15:19 -07001069 // We want to suppress callbacks about CustomContent being shown if we have just received
1070 // onNewIntent while the user was present within launcher. In that case, we post a call
1071 // to move the user to the main screen (which will occur after onResume). We don't want to
1072 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1073 // suppress here.
1074 if (mWorkspace.getCustomContentCallbacks() != null
1075 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001077 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001078 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1079 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001080 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001081 }
1082 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001083 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001084 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001085 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001086
Sunny Goyal756adbc2015-04-16 15:20:51 -07001087 if (!isWorkspaceLoading()) {
1088 // Process any items that were added while Launcher was away.
1089 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1090 }
Adam Cohen9211d422014-10-07 18:14:53 -07001091
1092 if (mLauncherCallbacks != null) {
1093 mLauncherCallbacks.onResume();
1094 }
Adam Cohen06dff352012-06-01 17:17:08 -07001095 }
1096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001099 // Ensure that items added to Launcher are queued until Launcher returns
1100 InstallShortcutReceiver.enableInstallQueue();
1101
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001102 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001103 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001104 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001105 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001106
1107 // We call onHide() aggressively. The custom content callbacks should be able to
1108 // debounce excess onHide calls.
1109 if (mWorkspace.getCustomContentCallbacks() != null) {
1110 mWorkspace.getCustomContentCallbacks().onHide();
1111 }
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Adam Cohen9211d422014-10-07 18:14:53 -07001113 if (mLauncherCallbacks != null) {
1114 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001116 }
1117
1118 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001119 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1120 // by a onResume or by scrolling otherwise.
1121 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001122
1123 // Custom content is completely hidden
1124 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001125
1126 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1127 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001128
1129 // Indicates whether the user is allowed to scroll away from the custom content.
1130 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001131 }
1132
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 public interface LauncherOverlay {
1134
1135 /**
1136 * Touch interaction leading to overscroll has begun
1137 */
1138 public void onScrollInteractionBegin();
1139
1140 /**
1141 * Touch interaction related to overscroll has ended
1142 */
1143 public void onScrollInteractionEnd();
1144
1145 /**
1146 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1147 * screen (or in the case of RTL, the rightmost screen).
1148 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150
1151 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001152 * Called when the launcher is ready to use the overlay
1153 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001155 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 }
1157
Jun Mukai8af0cd82015-05-12 12:32:12 -07001158 public interface LauncherSearchCallbacks {
1159 /**
1160 * Called when the search overlay is shown.
1161 */
1162 public void onSearchOverlayOpened();
1163
1164 /**
1165 * Called when the search overlay is dismissed.
1166 */
1167 public void onSearchOverlayClosed();
1168 }
1169
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001171 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 }
1173
1174 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1175
Sunny Goyalc86df472016-02-25 09:19:38 -08001176 public void onScrollChanged(float progress) {
1177 if (mWorkspace != null) {
1178 mWorkspace.onOverlayScrollChanged(progress);
1179 }
1180 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001181 }
1182
Jorim Jaggid017f882014-01-14 17:08:48 -08001183 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001184 if (mLauncherCallbacks != null) {
1185 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001186 } else {
1187 // On devices with a locked orientation, we will at least have the allow rotation
1188 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001189 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001190 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001191 }
1192
Adam Cohen9211d422014-10-07 18:14:53 -07001193 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001194 CustomContentCallbacks callbacks, String description) {
1195 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001196 }
1197
Adam Cohenedb40762013-07-18 16:45:45 -07001198 // The custom content needs to offset its content to account for the QSB
1199 public int getTopOffsetForCustomContent() {
1200 return mWorkspace.getPaddingTop();
1201 }
1202
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001203 @Override
1204 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001206 if (mModel.isCurrentCallbacks(this)) {
1207 mModel.stopLoader();
1208 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001209 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1210
Romain Guy13c2e7b2010-03-10 19:45:00 -08001211 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001212 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001213
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 @Override
1216 public void onWindowFocusChanged(boolean hasFocus) {
1217 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001218 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001219
1220 if (mLauncherCallbacks != null) {
1221 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1222 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 private boolean acceptFilter() {
1226 final InputMethodManager inputManager = (InputMethodManager)
1227 getSystemService(Context.INPUT_METHOD_SERVICE);
1228 return !inputManager.isFullscreenMode();
1229 }
1230
1231 @Override
1232 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001233 final int uniChar = event.getUnicodeChar();
1234 final boolean handled = super.onKeyDown(keyCode, event);
1235 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1236 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1238 keyCode, event);
1239 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001241 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 // showSearchDialog()
1243 // If there are multiple keystrokes before the search dialog takes focus,
1244 // onSearchRequested() will be called for every keystroke,
1245 // but it is idempotent, so it's fine.
1246 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
1249
Joe Onorato8a9625e2010-01-28 15:55:35 -08001250 // Eat the long press event so the keyboard doesn't come up.
1251 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1252 return true;
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 return handled;
1256 }
1257
Winson46163472015-09-22 17:31:56 -07001258 @Override
1259 public boolean onKeyUp(int keyCode, KeyEvent event) {
1260 if (keyCode == KeyEvent.KEYCODE_MENU) {
1261 // Ignore the menu key if we are currently dragging or are on the custom content screen
1262 if (!isOnCustomContent() && !mDragController.isDragging()) {
1263 // Close any open folders
1264 closeFolder();
1265
1266 // Stop resizing any widgets
1267 mWorkspace.exitWidgetResizeMode();
1268
1269 // Show the overview mode if we are on the workspace
1270 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1271 !mWorkspace.isSwitchingState()) {
1272 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001273 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001274 }
1275 }
1276 return true;
1277 }
1278 return super.onKeyUp(keyCode, event);
1279 }
1280
Karl Rosaen138a0412009-04-23 19:00:21 -07001281 private String getTypedText() {
1282 return mDefaultKeySsb.toString();
1283 }
1284
1285 private void clearTypedText() {
1286 mDefaultKeySsb.clear();
1287 mDefaultKeySsb.clearSpans();
1288 Selection.setSelection(mDefaultKeySsb, 0);
1289 }
1290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001292 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1293 * State
1294 */
1295 private static State intToState(int stateOrdinal) {
1296 State state = State.WORKSPACE;
1297 final State[] stateValues = State.values();
1298 for (int i = 0; i < stateValues.length; i++) {
1299 if (stateValues[i].ordinal() == stateOrdinal) {
1300 state = stateValues[i];
1301 break;
1302 }
1303 }
1304 return state;
1305 }
1306
1307 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 * Restores the previous state, if it exists.
1309 *
1310 * @param savedState The previous state.
1311 */
1312 private void restoreState(Bundle savedState) {
1313 if (savedState == null) {
1314 return;
1315 }
1316
Michael Jurka883f55b2010-10-21 15:47:14 -07001317 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001318 if (state == State.APPS || state == State.WIDGETS) {
1319 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001320 }
1321
Adam Cohen21cd0022013-10-09 18:57:02 -07001322 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1323 PagedView.INVALID_RESTORE_PAGE);
1324 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001325 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
Sunny Goyal756cd262015-08-20 12:33:21 -07001328 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1329 if (itemValues != null) {
1330 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001331 AppWidgetProviderInfo info = savedState.getParcelable(
1332 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001333 mPendingAddWidgetInfo = info == null ?
1334 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1335
Adam Cohen4637b5a2013-11-04 18:21:24 -08001336 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001337 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 mRestoring = true;
1339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341
1342 /**
1343 * Finds all the views we need and configure them properly.
1344 */
1345 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001346 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001347
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001349 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001350 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1351 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001352 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001353 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001354
Sunny Goyal784f9c32016-03-23 16:56:54 -07001355 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1356 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1357 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001358 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359
Winson Chung4c98d922011-05-31 16:50:48 -07001360 // Setup the drag layer
1361 mDragLayer.setup(this, dragController);
1362
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 // Setup the hotseat
1364 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1365 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001366 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 }
1368
Winsone9f27272015-10-13 10:47:51 -07001369 // Setup the overview panel
1370 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001371
Winson Chung4c98d922011-05-31 16:50:48 -07001372 // Setup the workspace
1373 mWorkspace.setHapticFeedbackEnabled(false);
1374 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001375 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001376 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001377
Tony Wickham34d2c912015-09-11 09:27:58 -07001378 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001379 mSearchDropTargetBar = (SearchDropTargetBar)
1380 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001381 // Get the app info bar
1382 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1383 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001384
Winson Chungef7f8742015-06-04 17:18:17 -07001385 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001386 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001387 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001388 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1389 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1390 } else {
1391 mAppsView.setSearchBarController(new DefaultAppSearchController());
1392 }
Craig Mautner360310b2012-10-26 15:13:08 -07001393
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001395 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001396 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001397 dragController.setMoveTarget(mWorkspace);
1398 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001399 if (mSearchDropTargetBar != null) {
1400 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001401 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001402 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001403 if (mAppInfoDropTargetBar != null) {
1404 mAppInfoDropTargetBar.setup(this, dragController);
1405 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001406
Sunny Goyal322d5562015-06-25 19:35:49 -07001407 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1408 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411
Winsone9f27272015-10-13 10:47:51 -07001412 private void setupOverviewPanel() {
1413 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1414
1415 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1416 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1417 @Override
1418 public boolean onLongClick(View v) {
1419 return v.performClick();
1420 }
1421 };
1422
1423 // Bind wallpaper button actions
1424 View wallpaperButton = findViewById(R.id.wallpaper_button);
1425 wallpaperButton.setOnClickListener(new OnClickListener() {
1426 @Override
1427 public void onClick(View view) {
1428 if (!mWorkspace.isSwitchingState()) {
1429 onClickWallpaperPicker(view);
1430 }
1431 }
1432 });
1433 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1434 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 // Bind widget button actions
1437 mWidgetsButton = findViewById(R.id.widget_button);
1438 mWidgetsButton.setOnClickListener(new OnClickListener() {
1439 @Override
1440 public void onClick(View view) {
1441 if (!mWorkspace.isSwitchingState()) {
1442 onClickAddWidgetButton(view);
1443 }
1444 }
1445 });
1446 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1447 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1448
1449 // Bind settings actions
1450 View settingsButton = findViewById(R.id.settings_button);
1451 boolean hasSettings = hasSettings();
1452 if (hasSettings) {
1453 settingsButton.setOnClickListener(new OnClickListener() {
1454 @Override
1455 public void onClick(View view) {
1456 if (!mWorkspace.isSwitchingState()) {
1457 onClickSettingsButton(view);
1458 }
1459 }
1460 });
1461 settingsButton.setOnLongClickListener(performClickOnLongClick);
1462 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1463 } else {
1464 settingsButton.setVisibility(View.GONE);
1465 }
1466
1467 mOverviewPanel.setAlpha(0f);
1468 }
1469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001471 * Sets the all apps button. This method is called from {@link Hotseat}.
1472 */
1473 public void setAllAppsButton(View allAppsButton) {
1474 mAllAppsButton = allAppsButton;
1475 }
1476
1477 public View getAllAppsButton() {
1478 return mAllAppsButton;
1479 }
1480
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001481 public View getWidgetsButton() {
1482 return mWidgetsButton;
1483 }
1484
Anjali Koppal5ad44842014-03-10 20:34:39 -07001485 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 * Creates a view representing a shortcut.
1487 *
1488 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001490 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001491 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 /**
1495 * Creates a view representing a shortcut inflated from the specified resource.
1496 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 * @param parent The group the shortcut belongs to.
1498 * @param info The data structure describing the shortcut.
1499 *
1500 * @return A View inflated from layoutResId.
1501 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001502 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001503 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001504 parent, false);
1505 favorite.applyFromShortcutInfo(info, mIconCache);
1506 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001508 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 return favorite;
1510 }
1511
1512 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * Add a shortcut to the workspace.
1514 *
1515 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001518 int cellY) {
1519 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001520 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Sunny Goyal5c97f512015-05-19 16:03:28 -07001522 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001523 if (info == null) {
1524 return;
1525 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 final View view = createShortcut(info);
1527
Sunny Goyal5c97f512015-05-19 16:03:28 -07001528 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529 // First we check if we already know the exact location where we want to add this item.
1530 if (cellX >= 0 && cellY >= 0) {
1531 cellXY[0] = cellX;
1532 cellXY[1] = cellY;
1533 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001534
1535 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001536 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001537 true, null,null)) {
1538 return;
1539 }
1540 DragObject dragObject = new DragObject();
1541 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001542 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1543 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001544 return;
1545 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001547 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001548 }
1549
1550 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001551 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 return;
1553 }
1554
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001555 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
1557 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001559 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561 }
1562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001564 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001566 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 */
Adam Cohen091440a2015-03-18 14:16:05 -07001568 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001569 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1570
1571 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001573 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Adam Cohen59400422014-03-05 18:07:04 -08001576 if (appWidgetInfo.isCustomWidget) {
1577 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001578 }
1579
Adam Cohen59400422014-03-05 18:07:04 -08001580 LauncherAppWidgetInfo launcherInfo;
1581 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1582 launcherInfo.spanX = info.spanX;
1583 launcherInfo.spanY = info.spanY;
1584 launcherInfo.minSpanX = info.minSpanX;
1585 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001586 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001589 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
1591 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 if (hostView == null) {
1593 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001594 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1595 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001596 } else {
1597 // The AppWidgetHostView has already been inflated and instantiated
1598 launcherInfo.hostView = hostView;
1599 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001600 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001603 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001606 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1607 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1608 item.hostView.setTag(item);
1609 item.onBindAppWidget(this);
1610
1611 item.hostView.setFocusable(true);
1612 item.hostView.setOnFocusChangeListener(mFocusHandler);
1613
1614 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1615 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1616
1617 if (!item.isCustomWidget()) {
1618 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1619 }
1620 }
1621
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1623 @Override
1624 public void onReceive(Context context, Intent intent) {
1625 final String action = intent.getAction();
1626 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1627 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001628 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001629 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001630
Winson Chungc100e8e2011-08-09 16:02:43 -07001631 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001632 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001633 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001634 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001635 if (!showWorkspace(false)) {
1636 // If we are already on the workspace, then manually reset all apps
1637 mAppsView.reset();
1638 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001639 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1641 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001642 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001643 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1644 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001645 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001646 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1647 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1648 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001649 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001650 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1651 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653 }
1654 };
1655
1656 @Override
1657 public void onAttachedToWindow() {
1658 super.onAttachedToWindow();
1659
1660 // Listen for broadcasts related to user-presence
1661 final IntentFilter filter = new IntentFilter();
1662 filter.addAction(Intent.ACTION_SCREEN_OFF);
1663 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001664 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001665 if (ENABLE_DEBUG_INTENTS) {
1666 filter.addAction(DebugIntents.DELETE_DATABASE);
1667 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1668 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001670 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001671 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001673
1674 if (mLauncherCallbacks != null) {
1675 mLauncherCallbacks.onAttachedToWindow();
1676 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 }
1678
1679 @Override
1680 public void onDetachedFromWindow() {
1681 super.onDetachedFromWindow();
1682 mVisible = false;
1683
Adam Cohend113e0c2010-11-11 10:48:05 -08001684 if (mAttached) {
1685 unregisterReceiver(mReceiver);
1686 mAttached = false;
1687 }
Winson Chungb745afb2015-03-02 11:51:23 -08001688 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001689
1690 if (mLauncherCallbacks != null) {
1691 mLauncherCallbacks.onDetachedFromWindow();
1692 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 }
1694
1695 public void onWindowVisibilityChanged(int visibility) {
1696 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001697 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001698 // The following code used to be in onResume, but it turns out onResume is called when
1699 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1700 // is a more appropriate event to handle
1701 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001702 if (!mWorkspaceLoading) {
1703 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 // We want to let Launcher draw itself at least once before we force it to build
1705 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001706 // apps is nice and speedy.
1707 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001708 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001709 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001710 if (mStarted) return;
1711 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001712 // We delay the layer building a bit in order to give
1713 // other message processing a time to run. In particular
1714 // this avoids a delay in hiding the IME if it was
1715 // currently shown, because doing that may involve
1716 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001717 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001718 final ViewTreeObserver.OnDrawListener listener = this;
1719 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001720 public void run() {
1721 if (mWorkspace != null &&
1722 mWorkspace.getViewTreeObserver() != null) {
1723 mWorkspace.getViewTreeObserver().
1724 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001725 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001726 }
1727 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001728 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001729 }
1730 });
1731 }
1732 clearTypedText();
1733 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 }
1735
Adam Cohen091440a2015-03-18 14:16:05 -07001736 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 mHandler.removeMessages(ADVANCE_MSG);
1738 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1739 mHandler.sendMessageDelayed(msg, delay);
1740 mAutoAdvanceSentTime = System.currentTimeMillis();
1741 }
1742
Adam Cohen091440a2015-03-18 14:16:05 -07001743 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1745 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1746 mAutoAdvanceRunning = autoAdvanceRunning;
1747 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001748 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 sendAdvanceMessage(delay);
1750 } else {
1751 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001752 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1754 }
1755 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001756 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
1758 }
1759 }
1760
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001761 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1762
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001764 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 if (msg.what == ADVANCE_MSG) {
1766 int i = 0;
1767 for (View key: mWidgetsToAdvance.keySet()) {
1768 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001769 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001771 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 public void run() {
1773 ((Advanceable) v).advance();
1774 }
1775 }, delay);
1776 }
1777 i++;
1778 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001779 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001781 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001783 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001784
Winsonc0b52fe2015-09-09 16:38:15 -07001785 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001786 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1788 if (v instanceof Advanceable) {
1789 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001790 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001791 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793 }
1794
Winsonc0b52fe2015-09-09 16:38:15 -07001795 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 if (mWidgetsToAdvance.containsKey(hostView)) {
1797 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001798 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 }
1801
Hyunyoung Song3f471442015-04-08 19:01:34 -07001802 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001803 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1804 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 }
1806
Winson Chunga6945242014-01-08 14:04:34 -08001807 public DragLayer getDragLayer() {
1808 return mDragLayer;
1809 }
1810
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001811 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001812 return mAppsView;
1813 }
1814
Hyunyoung Song3f471442015-04-08 19:01:34 -07001815 public WidgetsContainerView getWidgetsView() {
1816 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001817 }
1818
Winson Chunga6945242014-01-08 14:04:34 -08001819 public Workspace getWorkspace() {
1820 return mWorkspace;
1821 }
1822
1823 public Hotseat getHotseat() {
1824 return mHotseat;
1825 }
1826
Jorim Jaggid017f882014-01-14 17:08:48 -08001827 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001828 return mOverviewPanel;
1829 }
1830
Winson Chung006ee262015-08-03 14:40:11 -07001831 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001832 return mSearchDropTargetBar;
1833 }
1834
Tony Wickham34d2c912015-09-11 09:27:58 -07001835 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1836 return mAppInfoDropTargetBar;
1837 }
1838
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001839 public LauncherAppWidgetHost getAppWidgetHost() {
1840 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
Romain Guycbb89e42009-06-08 15:52:54 -07001842
Winson Chunga9abd0e2010-10-27 17:18:37 -07001843 public LauncherModel getModel() {
1844 return mModel;
1845 }
1846
Winson Chunga6945242014-01-08 14:04:34 -08001847 protected SharedPreferences getSharedPrefs() {
1848 return mSharedPrefs;
1849 }
1850
Adam Cohen2e6da152015-05-06 11:42:25 -07001851 public DeviceProfile getDeviceProfile() {
1852 return mDeviceProfile;
1853 }
1854
Bjorn Bringertc459e522013-06-07 19:36:01 +01001855 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 getWindow().closeAllPanels();
1857
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001858 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001859 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 @Override
1863 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001864 long startTime = 0;
1865 if (DEBUG_RESUME_TIME) {
1866 startTime = System.currentTimeMillis();
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 super.onNewIntent(intent);
1869
1870 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001871 Folder openFolder = mWorkspace.getOpenFolder();
1872 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1873 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1874 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1875 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1876 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001877 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001878 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001879
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 if (mWorkspace == null) {
1881 // Can be cases where mWorkspace is null, this prevents a NPE
1882 return;
1883 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 // In all these cases, only animate if we're already on home
1885 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001886
Sunny Goyal935fca12015-10-13 10:19:01 -07001887 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 exitSpringLoadedDragMode();
1889
1890 // If we are already on home, then just animate back to the workspace,
1891 // otherwise, just wait until onResume to set the state back to Workspace
1892 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001893 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 } else {
1895 mOnResumeState = State.WORKSPACE;
1896 }
1897
1898 final View v = getWindow().peekDecorView();
1899 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001900 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 INPUT_METHOD_SERVICE);
1902 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1903 }
1904
Winson Chungb745afb2015-03-02 11:51:23 -08001905 // Reset the apps view
1906 if (!alreadyOnHome && mAppsView != null) {
1907 mAppsView.scrollToTop();
1908 }
1909
Hyunyoung Song3f471442015-04-08 19:01:34 -07001910 // Reset the widgets view
1911 if (!alreadyOnHome && mWidgetsView != null) {
1912 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001913 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001914
Adam Cohen9211d422014-10-07 18:14:53 -07001915 if (mLauncherCallbacks != null) {
1916 mLauncherCallbacks.onHomeIntent();
1917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
Adam Cohened307df2013-10-02 09:37:31 -07001919
Adam Cohen9211d422014-10-07 18:14:53 -07001920 if (mLauncherCallbacks != null) {
1921 mLauncherCallbacks.onNewIntent(intent);
1922 }
Winson15f8b172015-08-19 11:02:39 -07001923
1924 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1925 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1926 // animation.
1927 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001928 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1929 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001930 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1931 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001932
1933 // We use this flag to suppress noisy callbacks above custom content state
1934 // from onResume.
1935 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001936 mWorkspace.post(new Runnable() {
1937 @Override
1938 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001939 if (mWorkspace != null) {
1940 mWorkspace.moveToDefaultScreen(true);
1941 }
Winson15f8b172015-08-19 11:02:39 -07001942 }
1943 });
1944 }
1945 }
1946
1947 if (DEBUG_RESUME_TIME) {
1948 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1949 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001950 }
1951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001953 public void onRestoreInstanceState(Bundle state) {
1954 super.onRestoreInstanceState(state);
1955 for (int page: mSynchronouslyBoundPages) {
1956 mWorkspace.restoreInstanceStateForChild(page);
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
1960 @Override
1961 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001962 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001963 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1964 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001965 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001966 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Michael Jurka883f55b2010-10-21 15:47:14 -07001968 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001969 // We close any open folder since it will not be re-opened, and we need to make sure
1970 // this state is reflected.
1971 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1972 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973
Adam Cohendcd297f2013-06-18 13:13:40 -07001974 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001976 ContentValues itemValues = new ContentValues();
1977 mPendingAddInfo.writeToValues(itemValues);
1978 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001979 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001980 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
Hyunyoung Song3f471442015-04-08 19:01:34 -07001983 // Save the current widgets tray?
1984 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001985
1986 if (mLauncherCallbacks != null) {
1987 mLauncherCallbacks.onSaveInstanceState(outState);
1988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
1991 @Override
1992 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Winson Chunge7a03942011-08-05 15:05:12 -07001995 // Remove all pending runnables
1996 mHandler.removeMessages(ADVANCE_MSG);
1997 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001998 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001999
Winson Chungcd2b0142011-06-08 16:02:26 -07002000 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07002001 // It's possible to receive onDestroy after a new Launcher activity has
2002 // been created. In this case, don't interfere with the new Launcher.
2003 if (mModel.isCurrentCallbacks(this)) {
2004 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08002005 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07002006 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002009 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002011 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002013 mAppWidgetHost = null;
2014
2015 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
2017 TextKeyListener.getInstance().release();
2018
Tony Wickhame2217252016-03-22 16:34:23 -07002019 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2020 .removeAccessibilityStateChangeListener(this);
2021
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002022 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002023
Michael Jurka2ecf9952012-06-18 12:52:28 -07002024 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002025
2026 if (mLauncherCallbacks != null) {
2027 mLauncherCallbacks.onDestroy();
2028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
Adam Cohena9cf38f2011-05-02 15:36:58 -07002031 public DragController getDragController() {
2032 return mDragController;
2033 }
2034
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 @Override
2036 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002037 onStartForResult(requestCode);
2038 super.startActivityForResult(intent, requestCode);
2039 }
2040
2041 @Override
2042 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2043 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2044 onStartForResult(requestCode);
2045 try {
2046 super.startIntentSenderForResult(intent, requestCode,
2047 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2048 } catch (IntentSender.SendIntentException e) {
2049 throw new ActivityNotFoundException();
2050 }
2051 }
2052
2053 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002054 if (requestCode >= 0) {
2055 setWaitingForResult(true);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058
Winson Chung70d72102011-08-12 11:24:35 -07002059 /**
2060 * Indicates that we want global search for this activity by setting the globalSearch
2061 * argument for {@link #startSearch} to true.
2062 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002064 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002066
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 if (initialQuery == null) {
2068 // Use any text typed in the launcher as the initial query
2069 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 if (appSearchData == null) {
2072 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002073 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
Winson Chungadf0c182012-08-23 14:59:07 -07002075 Rect sourceBounds = new Rect();
2076 if (mSearchDropTargetBar != null) {
2077 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2078 }
Romain Guycbb89e42009-06-08 15:52:54 -07002079
Ian Parkinson047036e2014-09-01 15:40:53 +01002080 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 if (clearTextImmediately) {
2083 clearTypedText();
2084 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002085
2086 // We need to show the workspace after starting the search
2087 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 }
2089
Ian Parkinson047036e2014-09-01 15:40:53 +01002090 /**
2091 * Start a text search.
2092 *
2093 * @return {@code true} if the search will start immediately, so any further keypresses
2094 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2095 * to buffer keypresses.
2096 */
2097 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002098 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002099 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2100 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2101 sourceBounds);
2102 }
2103
Michael Jurkaa33411c2012-06-14 16:18:21 -07002104 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002105 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 }
2108
2109 /**
2110 * Starts the global search activity. This code is a copied from SearchManager
2111 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002112 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002113 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002114 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002115 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2116 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2117 if (globalSearchActivity == null) {
2118 Log.w(TAG, "No global search activity found.");
2119 return;
2120 }
2121 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2122 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2123 intent.setComponent(globalSearchActivity);
2124 // Make sure that we have a Bundle to put source in
2125 if (appSearchData == null) {
2126 appSearchData = new Bundle();
2127 } else {
2128 appSearchData = new Bundle(appSearchData);
2129 }
Adam Cohen9211d422014-10-07 18:14:53 -07002130 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002131 if (!appSearchData.containsKey("source")) {
2132 appSearchData.putString("source", getPackageName());
2133 }
2134 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2135 if (!TextUtils.isEmpty(initialQuery)) {
2136 intent.putExtra(SearchManager.QUERY, initialQuery);
2137 }
2138 if (selectInitialQuery) {
2139 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2140 }
2141 intent.setSourceBounds(sourceBounds);
2142 try {
2143 startActivity(intent);
2144 } catch (ActivityNotFoundException ex) {
2145 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
2148
Yura4f93ec62014-02-04 14:15:21 +00002149 public boolean isOnCustomContent() {
2150 return mWorkspace.isOnOrMovingToCustomContent();
2151 }
2152
Winson Chung70d72102011-08-12 11:24:35 -07002153 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002154 public boolean onPrepareOptionsMenu(Menu menu) {
2155 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002156 if (mLauncherCallbacks != null) {
2157 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2158 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002159 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002162 @Override
2163 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002164 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002165 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002166 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002167 }
2168
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 public boolean isWorkspaceLocked() {
2170 return mWorkspaceLoading || mWaitingForResult;
2171 }
2172
Adam Cohen517a7f52014-03-01 12:12:59 -08002173 public boolean isWorkspaceLoading() {
2174 return mWorkspaceLoading;
2175 }
2176
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002177 private void setWorkspaceLoading(boolean value) {
2178 boolean isLocked = isWorkspaceLocked();
2179 mWorkspaceLoading = value;
2180 if (isLocked != isWorkspaceLocked()) {
2181 onWorkspaceLockedChanged();
2182 }
2183 }
2184
2185 private void setWaitingForResult(boolean value) {
2186 boolean isLocked = isWorkspaceLocked();
2187 mWaitingForResult = value;
2188 if (isLocked != isWorkspaceLocked()) {
2189 onWorkspaceLockedChanged();
2190 }
2191 }
2192
Adam Cohen9211d422014-10-07 18:14:53 -07002193 protected void onWorkspaceLockedChanged() {
2194 if (mLauncherCallbacks != null) {
2195 mLauncherCallbacks.onWorkspaceLockedChanged();
2196 }
2197 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002198
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002200 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002202 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2203 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002204 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002205 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002206
Tony Wickhama0628cc2015-10-14 15:23:04 -07002207 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002208 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002209 if (LOGD) {
2210 Log.d(TAG, "Adding widget from drop");
2211 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002212 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2213 }
2214
Sunny Goyale6b63a32015-01-16 16:14:29 -08002215 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002216 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2217 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002218 if (appWidgetInfo.configure != null) {
2219 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002220 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002221
Bjorn Bringert7984c942009-12-09 15:38:25 +00002222 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002223 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2224 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002227 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002228 Runnable onComplete = new Runnable() {
2229 @Override
2230 public void run() {
2231 // Exit spring loaded mode if necessary after adding the widget
2232 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2233 null);
2234 }
2235 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002237 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002238 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
2240 }
Romain Guycbb89e42009-06-08 15:52:54 -07002241
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002242 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002243 // Close any folders that may be open.
2244 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002245 mWorkspace.moveToCustomContentScreen(animate);
2246 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002247
2248 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2249 int[] cell, int spanX, int spanY) {
2250 switch (info.itemType) {
2251 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2252 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2253 int span[] = new int[2];
2254 span[0] = spanX;
2255 span[1] = spanY;
2256 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2257 container, screenId, cell, span);
2258 break;
2259 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2260 processShortcutFromDrop(info.componentName, container, screenId, cell);
2261 break;
2262 default:
2263 throw new IllegalStateException("Unknown item type: " + info.itemType);
2264 }
2265 }
2266
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 /**
2268 * Process a shortcut drop.
2269 *
2270 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002274 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2275 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279
2280 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = cell[0];
2282 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284
2285 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2286 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002287 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002288 }
2289
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 /**
2291 * Process a widget drop.
2292 *
2293 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002297 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2298 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002301 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002302 mPendingAddInfo.minSpanX = info.minSpanX;
2303 mPendingAddInfo.minSpanY = info.minSpanY;
2304
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002306 mPendingAddInfo.cellX = cell[0];
2307 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002309 if (span != null) {
2310 mPendingAddInfo.spanX = span[0];
2311 mPendingAddInfo.spanY = span[1];
2312 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313
Adam Cohened66b2b2012-01-23 17:28:51 -08002314 AppWidgetHostView hostView = info.boundWidget;
2315 int appWidgetId;
2316 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002317 // In the case where we've prebound the widget, we remove it from the DragLayer
2318 if (LOGD) {
2319 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2320 }
2321 getDragLayer().removeView(hostView);
2322
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002324 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002325
2326 // Clear the boundWidget so that it doesn't get destroyed.
2327 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002329 // In this case, we either need to start an activity to get permission to bind
2330 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002331 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002332 Bundle options = info.bindOptions;
2333
Sunny Goyalffe83f12014-08-14 17:39:34 -07002334 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2335 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002336 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002337 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002338 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002339 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002340 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2341 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2342 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002343 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2344 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002345 // TODO: we need to make sure that this accounts for the options bundle.
2346 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002347 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2348 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002349 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 }
2351
Adam Cohendcd297f2013-06-18 13:13:40 -07002352 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002354 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 folderInfo.title = getText(R.string.folder_name);
2356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002358 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2359 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002360 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361
2362 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 FolderIcon newFolder =
2364 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002366 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002367 // Force measure the new folder icon
2368 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2369 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002370 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 }
Romain Guycbb89e42009-06-08 15:52:54 -07002372
Winsonc0b52fe2015-09-09 16:38:15 -07002373 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002374 * Unbinds the view for the specified item, and removes the item and all its children.
2375 *
2376 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002377 * @param itemInfo the {@link ItemInfo} for this view.
2378 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002379 */
Winson2949fb52015-09-24 09:56:11 -07002380 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002381 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002382 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002383 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2384 if (folderInfo != null) {
2385 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002386 } else {
2387 mWorkspace.removeWorkspaceItem(v);
2388 }
Winsonc0b52fe2015-09-09 16:38:15 -07002389 if (deleteFromDb) {
2390 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2391 }
2392 } else if (itemInfo instanceof FolderInfo) {
2393 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2394 unbindFolder(folderInfo);
2395 mWorkspace.removeWorkspaceItem(v);
2396 if (deleteFromDb) {
2397 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2398 }
2399 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2400 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002401 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002402 removeWidgetToAutoAdvance(widgetInfo.hostView);
2403 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002404 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002405 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002406 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002407
Winsonc0b52fe2015-09-09 16:38:15 -07002408 } else {
2409 return false;
2410 }
2411 return true;
2412 }
2413
2414 /**
2415 * Unbinds any launcher references to the folder.
2416 */
2417 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002418 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002419 }
2420
Winsonc0b52fe2015-09-09 16:38:15 -07002421 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002422 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002423 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002424 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002425 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002426 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002427 // Deleting an app widget ID is a void call but writes to disk before returning
2428 // to the caller...
2429 new AsyncTask<Void, Void, Void>() {
2430 public Void doInBackground(Void ... args) {
2431 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2432 return null;
2433 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002434 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002435 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002436 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002437 }
2438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 @Override
2440 public boolean dispatchKeyEvent(KeyEvent event) {
2441 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2442 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002444 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002445 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002446 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002447 dumpState();
2448 return true;
2449 }
2450 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002451 }
2452 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2453 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 return true;
2456 }
2457 }
2458
2459 return super.dispatchKeyEvent(event);
2460 }
2461
Joe Onorato88ec0992009-11-19 13:16:06 -08002462 @Override
2463 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002464 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2465 return;
2466 }
2467
Sunny Goyal45478022015-06-08 16:52:41 -07002468 if (mDragController.isDragging()) {
2469 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002470 return;
2471 }
2472
Winson Chungb745afb2015-03-02 11:51:23 -08002473 if (isAppsViewVisible()) {
2474 showWorkspace(true);
2475 } else if (isWidgetsViewVisible()) {
2476 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002477 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002478 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002480 Folder openFolder = mWorkspace.getOpenFolder();
2481 if (openFolder.isEditingName()) {
2482 openFolder.dismissEditingName();
2483 } else {
2484 closeFolder();
2485 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002486 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002487 mWorkspace.exitWidgetResizeMode();
2488
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002489 // Back button is a no-op here, but give at least some feedback for the button press
2490 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002491 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002495 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002496 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002497 @Override
2498 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002499 if (mAppWidgetHost != null) {
2500 mAppWidgetHost.startListening();
2501 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002502
2503 // Recreate the QSB, as the widget has been reset.
2504 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 }
2506
2507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * Launches the intent referred by the clicked shortcut.
2509 *
2510 * @param v The view representing the clicked shortcut.
2511 */
2512 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2514 // view has detached (it's possible for this to happen if the view is removed mid touch).
2515 if (v.getWindowToken() == null) {
2516 return;
2517 }
2518
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002519 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 return;
2521 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002522
Adam Cohen1697b792013-09-17 19:08:21 -07002523 if (v instanceof Workspace) {
2524 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002525 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 return;
2528 }
2529
2530 if (v instanceof CellLayout) {
2531 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002532 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2533 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002535 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002536 }
2537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002539 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002540 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002544 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002545 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002547 } else if (tag instanceof AppInfo) {
2548 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002549 } else if (tag instanceof LauncherAppWidgetInfo) {
2550 if (v instanceof PendingAppWidgetHostView) {
2551 onClickPendingWidget((PendingAppWidgetHostView) v);
2552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 }
2554 }
2555
Sunny Goyal70660032015-05-14 00:07:08 -07002556 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002557 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002558 return false;
2559 }
2560
Michael Jurkaaf442092010-06-10 17:01:57 -07002561 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002562 * Event handler for the app widget view which has not fully restored.
2563 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002564 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002565 if (mIsSafeModeEnabled) {
2566 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2567 return;
2568 }
2569
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002570 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002571 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002572 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002573 LauncherAppWidgetProviderInfo appWidgetInfo =
2574 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002575 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002576 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002577 mPendingAddInfo.copyFrom(info);
2578 mPendingAddWidgetId = widgetId;
2579
Sunny Goyalffe83f12014-08-14 17:39:34 -07002580 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2581 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002582 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 } else if (info.installProgress < 0) {
2584 // The install has not been queued
2585 final String packageName = info.providerName.getPackageName();
2586 showBrokenAppInstallDialog(packageName,
2587 new DialogInterface.OnClickListener() {
2588 public void onClick(DialogInterface dialog, int id) {
2589 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2590 }
2591 });
2592 } else {
2593 // Download has started.
2594 final String packageName = info.providerName.getPackageName();
2595 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002596 }
2597 }
2598
2599 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002600 * Event handler for the "grid" button that appears on the home screen, which
2601 * enters all apps mode.
2602 *
2603 * @param v The view that was clicked.
2604 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 protected void onClickAllAppsButton(View v) {
2606 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002607 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002608 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002609 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002610
2611 if (mLauncherCallbacks != null) {
2612 mLauncherCallbacks.onClickAllAppsButton(v);
2613 }
Winson Chung76648c52015-07-10 14:33:23 -07002614 }
2615 }
2616
2617 protected void onLongClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2619 if (!isAppsViewVisible()) {
2620 showAppsView(true /* animated */, false /* resetListToTop */,
2621 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 }
2623 }
2624
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 private void showBrokenAppInstallDialog(final String packageName,
2626 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002627 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 .setTitle(R.string.abandoned_promises_title)
2629 .setMessage(R.string.abandoned_promise_explanation)
2630 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2631 .setNeutralButton(R.string.abandoned_clean_this,
2632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
2634 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2635 mWorkspace.removeAbandonedPromise(packageName, user);
2636 }
2637 })
2638 .create().show();
2639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 /**
2643 * Event handler for an app shortcut click.
2644 *
2645 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2646 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2649 Object tag = v.getTag();
2650 if (!(tag instanceof ShortcutInfo)) {
2651 throw new IllegalArgumentException("Input must be a Shortcut");
2652 }
2653
2654 // Open shortcut
2655 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002656
2657 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002658 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2659 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002660 // Launch activity anyway, framework will tell the user why the app is suspended.
2661 } else {
2662 int error = R.string.activity_not_available;
2663 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2664 error = R.string.safemode_shortcut_error;
2665 }
2666 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2667 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002668 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002669 }
2670
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002672 if ((v instanceof BubbleTextView)
2673 && shortcut.isPromise()
2674 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002675 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002676 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 new DialogInterface.OnClickListener() {
2678 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 return;
2683 }
2684
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002686 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002687
2688 if (mLauncherCallbacks != null) {
2689 mLauncherCallbacks.onClickAppShortcut(v);
2690 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 }
2692
Adam Cohen091440a2015-03-18 14:16:05 -07002693 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002695 final ShortcutInfo shortcut;
2696 final Intent intent;
2697 if (tag instanceof ShortcutInfo) {
2698 shortcut = (ShortcutInfo) tag;
2699 intent = shortcut.intent;
2700 int[] pos = new int[2];
2701 v.getLocationOnScreen(pos);
2702 intent.setSourceBounds(new Rect(pos[0], pos[1],
2703 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002704
Sunny Goyal508da152014-08-14 10:53:27 -07002705 } else if (tag instanceof AppInfo) {
2706 shortcut = null;
2707 intent = ((AppInfo) tag).intent;
2708 } else {
2709 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2710 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711
2712 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002713 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714
2715 if (success && v instanceof BubbleTextView) {
2716 mWaitingForResume = (BubbleTextView) v;
2717 mWaitingForResume.setStayPressed(true);
2718 }
2719 }
2720
2721 /**
2722 * Event handler for a folder icon click.
2723 *
2724 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2725 */
2726 protected void onClickFolderIcon(View v) {
2727 if (LOGD) Log.d(TAG, "onClickFolder");
2728 if (!(v instanceof FolderIcon)){
2729 throw new IllegalArgumentException("Input must be a FolderIcon");
2730 }
2731
2732 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002733 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002735 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 }
Adam Cohen9211d422014-10-07 18:14:53 -07002737
2738 if (mLauncherCallbacks != null) {
2739 mLauncherCallbacks.onClickFolderIcon(v);
2740 }
Michael Jurka5130e402011-10-13 04:55:35 -07002741 }
2742
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002743 /**
2744 * Event handler for the (Add) Widgets button that appears after a long press
2745 * on the home screen.
2746 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002747 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002748 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002749 if (mIsSafeModeEnabled) {
2750 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2751 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002752 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002753 if (mLauncherCallbacks != null) {
2754 mLauncherCallbacks.onClickAddWidgetButton(view);
2755 }
Adam Cohen9211d422014-10-07 18:14:53 -07002756 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002757 }
2758
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 /**
2760 * Event handler for the wallpaper picker button that appears after a long press
2761 * on the home screen.
2762 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002763 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002764 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002765 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2766 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2767 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal6f866092016-03-17 17:04:15 -07002768 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002769 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002770
2771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onClickWallpaperPicker(v);
2773 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 }
2775
2776 /**
2777 * Event handler for a click on the settings button that appears after a long press
2778 * on the home screen.
2779 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002780 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002784 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002785 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002786 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 }
2788
Adam Cohen61f560d2013-09-30 15:58:20 -07002789 public View.OnTouchListener getHapticFeedbackTouchListener() {
2790 if (mHapticFeedbackTouchListener == null) {
2791 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002792 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002793 @Override
2794 public boolean onTouch(View v, MotionEvent event) {
2795 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2796 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2797 }
2798 return false;
2799 }
2800 };
2801 }
2802 return mHapticFeedbackTouchListener;
2803 }
2804
Tony Wickhame2217252016-03-22 16:34:23 -07002805 @Override
2806 public void onAccessibilityStateChanged(boolean enabled) {
2807 mDragLayer.onAccessibilityStateChanged(enabled);
2808 }
2809
Adam Cohen9211d422014-10-07 18:14:53 -07002810 public void onDragStarted(View view) {
2811 if (isOnCustomContent()) {
2812 // Custom content screen doesn't participate in drag and drop. If on custom
2813 // content screen, move to default.
2814 moveWorkspaceToDefaultScreen();
2815 }
2816
2817 if (mLauncherCallbacks != null) {
2818 mLauncherCallbacks.onDragStarted(view);
2819 }
2820 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002821
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002822 /**
2823 * Called when the user stops interacting with the launcher.
2824 * This implies that the user is now on the homescreen and is not doing housekeeping.
2825 */
Adam Cohen9211d422014-10-07 18:14:53 -07002826 protected void onInteractionEnd() {
2827 if (mLauncherCallbacks != null) {
2828 mLauncherCallbacks.onInteractionEnd();
2829 }
2830 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002831
2832 /**
2833 * Called when the user starts interacting with the launcher.
2834 * The possible interactions are:
2835 * - open all apps
2836 * - reorder an app shortcut, or a widget
2837 * - open the overview mode.
2838 * This is a good time to stop doing things that only make sense
2839 * when the user is on the homescreen and not doing housekeeping.
2840 */
Adam Cohen9211d422014-10-07 18:14:53 -07002841 protected void onInteractionBegin() {
2842 if (mLauncherCallbacks != null) {
2843 mLauncherCallbacks.onInteractionBegin();
2844 }
2845 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002846
Winson Chungcd99cd32015-04-29 11:03:24 -07002847 /** Updates the interaction state. */
2848 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002849 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002850 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002851 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2852 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002853 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002854 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002855 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002856 onInteractionEnd();
2857 }
2858 }
2859
Winson Chung8f1eff72015-05-28 17:33:40 -07002860 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002861 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002863 // Only launch using the new animation if the shortcut has not opted out (this is a
2864 // private contract between launcher and may be ignored in the future).
2865 boolean useLaunchAnimation = (v != null) &&
2866 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002867 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2868 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002869
Kenny Guy1317e2d2014-05-08 18:52:50 +01002870 UserHandleCompat user = null;
2871 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2872 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2873 user = userManager.getUserForSerialNumber(serialNumber);
2874 }
2875
2876 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002877 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002878 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002879 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002880 int left = 0, top = 0;
2881 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2882 if (v instanceof TextView) {
2883 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002884 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2885 if (icon != null) {
2886 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002887 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002888 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002889 width = bounds.width();
2890 height = bounds.height();
2891 }
2892 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002893 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2894 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002895 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002896 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002897 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002898 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002899 // On L devices, we use the device default slide-up transition.
2900 // On L MR1 devices, we a custom version of the slide-up transition which
2901 // doesn't have the delay present in the device default.
2902 opts = ActivityOptions.makeCustomAnimation(this,
2903 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002904 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002905 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002906 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002907
2908 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2909 // Could be launching some bookkeeping activity
2910 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002911 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002912 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002913 launcherApps.startActivityForProfile(intent.getComponent(), user,
2914 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002915 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002916 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002918 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2919 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2920 // corresponding permission. Show the appropriate permission prompt if that
2921 // is the case.
2922 if (intent.getComponent() == null
2923 && Intent.ACTION_CALL.equals(intent.getAction())
2924 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2925 PackageManager.PERMISSION_GRANTED) {
2926 // TODO: Rename sPendingAddItem to a generic name.
2927 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2928 0, (ItemInfo) tag);
2929 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2930 REQUEST_PERMISSION_CALL_PHONE);
2931 return false;
2932 }
2933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002935 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002937 "or use the exported attribute for this activity. "
2938 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002940 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002942
Winson Chungbedf9232015-07-10 12:38:30 -07002943 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002944 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002945 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2946 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2947 return false;
2948 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002949 try {
2950 success = startActivity(v, intent, tag);
2951 } catch (ActivityNotFoundException e) {
2952 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2953 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2954 }
2955 return success;
2956 }
2957
Adam Cohen268c4752012-06-06 17:47:33 -07002958 /**
2959 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2960 * in the DragLayer in the exact absolute location of the original FolderIcon.
2961 */
2962 private void copyFolderIconToImage(FolderIcon fi) {
2963 final int width = fi.getMeasuredWidth();
2964 final int height = fi.getMeasuredHeight();
2965
2966 // Lazy load ImageView, Bitmap and Canvas
2967 if (mFolderIconImageView == null) {
2968 mFolderIconImageView = new ImageView(this);
2969 }
2970 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2971 mFolderIconBitmap.getHeight() != height) {
2972 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2973 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2974 }
2975
2976 DragLayer.LayoutParams lp;
2977 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2978 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2979 } else {
2980 lp = new DragLayer.LayoutParams(width, height);
2981 }
2982
Adam Cohen307fe232012-08-16 17:55:58 -07002983 // The layout from which the folder is being opened may be scaled, adjust the starting
2984 // view size by this scale factor.
2985 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002986 lp.customPosition = true;
2987 lp.x = mRectForFolderAnimation.left;
2988 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002989 lp.width = (int) (scale * width);
2990 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002991
2992 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2993 fi.draw(mFolderIconCanvas);
2994 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002995 if (fi.getFolder() != null) {
2996 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2997 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002998 }
Adam Cohen268c4752012-06-06 17:47:33 -07002999 // Just in case this image view is still in the drag layer from a previous animation,
3000 // we remove it and re-add it.
3001 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3002 mDragLayer.removeView(mFolderIconImageView);
3003 }
3004 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003005 if (fi.getFolder() != null) {
3006 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003007 }
Adam Cohen268c4752012-06-06 17:47:33 -07003008 }
3009
Sunny Goyal08442b82015-10-21 17:15:08 -07003010 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003011 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003012 FolderInfo info = (FolderInfo) fi.getTag();
3013 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3014 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003015 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3016 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003017 }
3018
Adam Cohen268c4752012-06-06 17:47:33 -07003019 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3020 copyFolderIconToImage(fi);
3021 fi.setVisibility(View.INVISIBLE);
3022
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003023 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3024 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003025 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003026 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3027 }
3028 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003029 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003030 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003031 oa.end();
3032 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003033 }
3034
Sunny Goyal935fca12015-10-13 10:19:01 -07003035 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003036 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003037 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003038
Adam Cohen268c4752012-06-06 17:47:33 -07003039 // We remove and re-draw the FolderIcon in-case it has changed
3040 mDragLayer.removeView(mFolderIconImageView);
3041 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003042
3043 if (cl != null) {
3044 cl.clearFolderLeaveBehind();
3045 }
3046
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003047 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 oa.addListener(new AnimatorListenerAdapter() {
3050 @Override
3051 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003052 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003053 // Remove the ImageView copy of the FolderIcon and make the original visible.
3054 mDragLayer.removeView(mFolderIconImageView);
3055 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003056 }
3057 }
3058 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003059 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003060 if (!animate) {
3061 oa.end();
3062 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003063 }
3064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003066 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067 * is animated relative to the specified View. If the View is null, no animation
3068 * is played.
3069 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003070 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003072 public void openFolder(FolderIcon folderIcon, boolean animate) {
3073 animate &= !Utilities.isPowerSaverOn(this);
3074
Adam Cohenfb91f302012-06-11 15:45:18 -07003075 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003076 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3077 if (openFolder != null && openFolder != folder) {
3078 // Close any open folder before opening a folder.
3079 closeFolder();
3080 }
3081
Adam Cohena9cf38f2011-05-02 15:36:58 -07003082 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083
Adam Cohena9cf38f2011-05-02 15:36:58 -07003084 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085
Sunny Goyalf4066152015-04-15 09:42:19 -07003086 // While the folder is open, the position of the icon cannot change.
3087 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3088
Adam Cohen4554ee12011-08-03 16:13:21 -07003089 // Just verify that the folder hasn't already been added to the DragLayer.
3090 // There was a one-off crash where the folder had a parent already.
3091 if (folder.getParent() == null) {
3092 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003093 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003094 } else {
3095 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3096 folder.getParent() + ").");
3097 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003098 if (animate) {
3099 folder.animateOpen();
3100 } else {
3101 folder.open();
3102 }
3103 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003104
3105 // Notify the accessibility manager that this folder "window" has appeared and occluded
3106 // the workspace items
3107 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3108 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 }
3110
3111 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003112 closeFolder(true);
3113 }
3114
3115 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003116 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003118 if (folder.isEditingName()) {
3119 folder.dismissEditingName();
3120 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003122 }
3123 }
3124
Sunny Goyal935fca12015-10-13 10:19:01 -07003125 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003126 animate &= !Utilities.isPowerSaverOn(this);
3127
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 folder.getInfo().opened = false;
3129
3130 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3131 if (parent != null) {
3132 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003133 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003134 if (fi != null) {
3135 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3136 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003137 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003138 if (animate) {
3139 folder.animateClosed();
3140 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003141 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003142 }
Winson Chung83ca4802013-04-12 15:10:52 -07003143
Sunny Goyal935fca12015-10-13 10:19:01 -07003144 // Notify the accessibility manager that this folder "window" has disappeared and no
3145 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003146 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 }
3148
Tony Wickhamdadb3042016-02-24 11:07:00 -08003149 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003151 if (!isDraggingEnabled()) return false;
3152 if (isWorkspaceLocked()) return false;
3153 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154
Winson Chung76648c52015-07-10 14:33:23 -07003155 if (v == mAllAppsButton) {
3156 onLongClickAllAppsButton(v);
3157 return true;
3158 }
3159
Adam Cohen1697b792013-09-17 19:08:21 -07003160 if (v instanceof Workspace) {
3161 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003162 if (!mWorkspace.isTouchActive()) {
3163 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003164 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3165 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3166 return true;
3167 } else {
3168 return false;
3169 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003170 } else {
3171 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003172 }
Adam Cohen1697b792013-09-17 19:08:21 -07003173 }
3174
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003175 CellLayout.CellInfo longClickCellInfo = null;
3176 View itemUnderLongClick = null;
3177 if (v.getTag() instanceof ItemInfo) {
3178 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003179 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003180 itemUnderLongClick = longClickCellInfo.cell;
3181 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182 }
3183
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 // The hotseat touch handling does not go through Workspace, and we always allow long press
3185 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003186 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003187 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003188 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003189 // User long pressed on empty space
3190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003192 if (mWorkspace.isInOverviewMode()) {
3193 mWorkspace.startReordering(v);
3194 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003195 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003198 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3199 mHotseat.getOrderInHotseat(
3200 longClickCellInfo.cellX,
3201 longClickCellInfo.cellY));
3202 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003204 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
3206 }
3207 }
3208 return true;
3209 }
3210
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 boolean isHotseatLayout(View layout) {
3212 return mHotseat != null && layout != null &&
3213 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3214 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003215
Winson Chung3d503fb2011-07-13 17:25:49 -07003216 /**
3217 * Returns the CellLayout of the specified container at the specified screen.
3218 */
Sunny Goyal92820592015-03-02 11:31:35 -08003219 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3221 if (mHotseat != null) {
3222 return mHotseat.getLayout();
3223 } else {
3224 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003225 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003227 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003228 }
3229 }
3230
Winson Chungb745afb2015-03-02 11:51:23 -08003231 /**
3232 * For overridden classes.
3233 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003234 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003235 return isAppsViewVisible();
3236 }
3237
3238 public boolean isAppsViewVisible() {
3239 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3240 }
3241
3242 public boolean isWidgetsViewVisible() {
3243 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003244 }
3245
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003246 private void setWorkspaceBackground(int background) {
3247 switch (background) {
3248 case WORKSPACE_BACKGROUND_TRANSPARENT:
3249 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3250 break;
3251 case WORKSPACE_BACKGROUND_BLACK:
3252 getWindow().setBackgroundDrawable(null);
3253 break;
3254 default:
3255 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3256 }
Craig Mautner360310b2012-10-26 15:13:08 -07003257 }
3258
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003259 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003260 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3261 int curflags = getWindow().getAttributes().flags
3262 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3263 if (wpflags != curflags) {
3264 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3265 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003266 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003267 }
3268
Michael Jurkae326f182011-11-21 14:05:46 -08003269 @Override
3270 public void onTrimMemory(int level) {
3271 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003272 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3273 // The widget preview db can result in holding onto over
3274 // 3MB of memory for caching which isn't necessary.
3275 SQLiteDatabase.releaseMemory();
3276
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003277 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003278 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003279 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003280 if (mLauncherCallbacks != null) {
3281 mLauncherCallbacks.onTrimMemory(level);
3282 }
Michael Jurkae326f182011-11-21 14:05:46 -08003283 }
3284
Winson5c6bdbb2015-09-03 11:36:19 -07003285 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003286 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003287 }
3288
Winson5c6bdbb2015-09-03 11:36:19 -07003289 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003290 boolean changed = mState != State.WORKSPACE ||
3291 mWorkspace.getState() != Workspace.State.NORMAL;
3292 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003294 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003295 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003296
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297 // Set focus to the AppsCustomize button
3298 if (mAllAppsButton != null) {
3299 mAllAppsButton.requestFocus();
3300 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003301 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003302
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003303 // Change the state *after* we've called all the transition code
3304 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003305
Winson Chung5fb63472011-02-02 17:03:37 -08003306 // Resume the auto-advance of widgets
3307 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003308 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003309
Winson Chung0f785722015-04-08 10:27:49 -07003310 if (changed) {
3311 // Send an accessibility event to announce the context change
3312 getWindow().getDecorView()
3313 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003314 }
Winson5c6bdbb2015-09-03 11:36:19 -07003315 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003316 }
3317
Winsone9f27272015-10-13 10:47:51 -07003318 /**
3319 * Shows the overview button.
3320 */
Adam Cohened307df2013-10-02 09:37:31 -07003321 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003322 showOverviewMode(animated, false);
3323 }
3324
3325 /**
3326 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3327 * onto one of the overview panel buttons.
3328 */
3329 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3330 Runnable postAnimRunnable = null;
3331 if (requestButtonFocus) {
3332 postAnimRunnable = new Runnable() {
3333 @Override
3334 public void run() {
3335 // Hitting the menu button when in touch mode does not trigger touch mode to
3336 // be disabled, so if requested, force focus on one of the overview panel
3337 // buttons.
3338 mOverviewPanel.requestFocusFromTouch();
3339 }
3340 };
3341 }
Adam Cohened307df2013-10-02 09:37:31 -07003342 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003343 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003344 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003345 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003346 }
3347
Winson Chungb745afb2015-03-02 11:51:23 -08003348 /**
3349 * Shows the apps view.
3350 */
Winson Chung76648c52015-07-10 14:33:23 -07003351 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3352 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003353 if (resetListToTop) {
3354 mAppsView.scrollToTop();
3355 }
Winson Chung4ac30062015-05-08 17:34:17 -07003356 if (updatePredictedApps) {
3357 tryAndUpdatePredictedApps();
3358 }
Winson Chung76648c52015-07-10 14:33:23 -07003359 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003360 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361
Winson Chungb745afb2015-03-02 11:51:23 -08003362 /**
3363 * Shows the widgets view.
3364 */
3365 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003366 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003367 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003368 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003369 }
Winson Chung76648c52015-07-10 14:33:23 -07003370 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003371
3372 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003373 @Override
3374 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003376 }
3377 });
Winson Chungb745afb2015-03-02 11:51:23 -08003378 }
3379
3380 /**
3381 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003382 *
3383 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003384 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003385 // TODO: calling method should use the return value so that when {@code false} is returned
3386 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003387 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003388 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3389 mState != State.WIDGETS_SPRING_LOADED) {
3390 return false;
3391 }
3392 if (toState != State.APPS && toState != State.WIDGETS) {
3393 return false;
3394 }
Winson Chungb745afb2015-03-02 11:51:23 -08003395
3396 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003397 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3398 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003399 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003400 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003401 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402
Michael Jurkab3e22d92011-10-31 15:58:33 -07003403 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003404 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405
3406 // Pause the auto-advance of widgets until we are out of AllApps
3407 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003408 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003409 closeFolder();
3410
3411 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003412 getWindow().getDecorView()
3413 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003414 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415 }
3416
Winson Chungcd99cd32015-04-29 11:03:24 -07003417 /**
3418 * Updates the workspace and interaction state on state change, and return the animation to this
3419 * new state.
3420 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003421 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003422 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003423 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003424 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003425 updateInteraction(fromState, toState);
3426 return anim;
3427 }
3428
Jun Mukaif0033da2015-08-04 18:34:30 -07003429 public void onLauncherClingShown() {
3430 // When a launcher cling appears, it should cover the underlying layers, so their focus
3431 // should be blocked.
3432 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3433 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3434 }
3435 }
3436
3437 public void onLauncherClingDismissed() {
3438 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3439 }
3440
Hyunyoung Song3f471442015-04-08 19:01:34 -07003441 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003442 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003443 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003444 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003445 }
Winson Chungb745afb2015-03-02 11:51:23 -08003446
Winson Chung006ee262015-08-03 14:40:11 -07003447 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003448 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003449 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003450
3451 if (isAppsViewVisible()) {
3452 mState = State.APPS_SPRING_LOADED;
3453 } else if (isWidgetsViewVisible()) {
3454 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003455 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003456 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003457 } else {
3458 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003459 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003460 }
Adam Cohen7777d962011-08-18 18:58:38 -07003461
Hyunyoung Song3f471442015-04-08 19:01:34 -07003462 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003463 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003464 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003465
Winson Chunge7a03942011-08-05 15:05:12 -07003466 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003467 @Override
3468 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003469 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003470 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3471 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003472 // Before we show workspace, hide all apps again because
3473 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3474 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003475 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003476 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003477 } else {
3478 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003479 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003480 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003481 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003482 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003483
Tony Wickhame0c33232016-02-08 11:37:04 -08003484 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003485 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3486 || mState == State.WIDGETS_SPRING_LOADED;
3487 }
3488
Michael Jurkad3ef3062010-11-23 16:23:58 -08003489 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003490 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003491 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003492 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003493 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003494 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003495 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3496 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003497 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003498 }
3499
Winson Chung4ac30062015-05-08 17:34:17 -07003500 /**
3501 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3502 * resumed.
3503 */
3504 private void tryAndUpdatePredictedApps() {
3505 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003506 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003507 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003508 mAppsView.setPredictedApps(apps);
3509 }
3510 }
3511 }
3512
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513 void lockAllApps() {
3514 // TODO
3515 }
3516
3517 void unlockAllApps() {
3518 // TODO
3519 }
3520
Winsonf768d932015-09-25 16:12:35 -07003521 public boolean launcherCallbacksProvidesSearch() {
3522 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3523 }
3524
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003525 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003526 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003527 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003528 }
3529
Adam Cohen24ce0b32014-01-14 16:18:14 -08003530 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003531 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3532 if (searchProvider == null) {
3533 return null;
3534 }
3535
3536 Bundle opts = new Bundle();
3537 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003538 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003539
Tony Wickham3a3517f2015-10-06 11:27:04 -07003540 // Determine the min and max dimensions of the widget.
3541 LauncherAppState app = LauncherAppState.getInstance();
3542 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3543 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3544 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003545 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3546 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003547 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003548 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003549 int minWidth = maxWidth;
3550 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003551 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3552 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3553 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3554 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3555 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003556 }
3557 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3558 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3559 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3560 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003561 if (LOGD) {
3562 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3563 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3564 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003565
Tony Wickham775455c2015-10-16 09:49:32 -07003566 if (mLauncherCallbacks != null) {
3567 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3568 }
3569
Sunny Goyalf7258242015-10-19 16:59:07 -07003570 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003571 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003572 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003573 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003574 || (widgetInfo == null)
3575 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003576 // A valid widget is not already bound.
3577 if (widgetId > -1) {
3578 mAppWidgetHost.deleteAppWidgetId(widgetId);
3579 widgetId = -1;
3580 }
3581
3582 // Try to bind a new widget
3583 widgetId = mAppWidgetHost.allocateAppWidgetId();
3584
3585 if (!AppWidgetManagerCompat.getInstance(this)
3586 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3587 mAppWidgetHost.deleteAppWidgetId(widgetId);
3588 widgetId = -1;
3589 }
3590
Sunny Goyalf7258242015-10-19 16:59:07 -07003591 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003592 .putInt(QSB_WIDGET_ID, widgetId)
3593 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003594 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003595 }
3596
Sunny Goyal8d595092015-07-06 12:22:14 -07003597 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003598 if (widgetId != -1) {
3599 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003600 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003601 mQsb.updateAppWidgetOptions(opts);
3602 mQsb.setPadding(0, 0, 0, 0);
3603 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003604 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003605 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003606 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003607 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003608 }
3609
Sunny Goyal22235bc2015-04-03 09:23:43 -07003610 private void reinflateQSBIfNecessary() {
3611 if (mQsb instanceof LauncherAppWidgetHostView &&
3612 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3613 mSearchDropTargetBar.removeView(mQsb);
3614 mQsb = null;
3615 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3616 }
3617 }
3618
Michael Jurkad7c28052012-04-27 15:43:36 -07003619 @Override
3620 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003621 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003622 final List<CharSequence> text = event.getText();
3623 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003624 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003625 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003626 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003627 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003628 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003629 } else if (mWorkspace != null) {
3630 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003631 } else {
3632 text.add(getString(R.string.all_apps_home_button_label));
3633 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003634 return result;
3635 }
3636
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003637 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003638 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003639 */
Adam Cohen091440a2015-03-18 14:16:05 -07003640 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003641 @Override
3642 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003643 closeSystemDialogs();
3644 }
3645 }
3646
3647 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 * If the activity is currently paused, signal that we need to run the passed Runnable
3649 * in onResume.
3650 *
3651 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003652 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3653 * wrong when we're not running, and if the activity comes back to what the configuration was
3654 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 *
3656 * Implementation of the method from LauncherModel.Callbacks.
3657 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003658 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003660 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003662 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003663 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003664 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003665 }
3666 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003667 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 return true;
3669 } else {
3670 return false;
3671 }
3672 }
3673
Michael Jurkac402cd92013-05-20 15:49:32 +02003674 private boolean waitUntilResume(Runnable run) {
3675 return waitUntilResume(run, false);
3676 }
3677
Michael Jurka1e2f4652013-07-08 18:03:46 -07003678 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003679 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003680 }
3681
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003683 * If the activity is currently paused, signal that we need to re-run the loader
3684 * in onResume.
3685 *
3686 * This needs to be called from incoming places where resources might have been loaded
3687 * while we are paused. That is becaues the Configuration might be wrong
3688 * when we're not running, and if it comes back to what it was when we
3689 * were paused, we are not restarted.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
3692 *
3693 * @return true if we are currently paused. The caller might be able to
3694 * skip some work in that case since we will come back again.
3695 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003696 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003697 public boolean setLoadOnResume() {
3698 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003699 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003700 mOnResumeNeedsLoad = true;
3701 return true;
3702 } else {
3703 return false;
3704 }
3705 }
3706
3707 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003709 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003710 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003712 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003713 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003714 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003715 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003716 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003718
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003720 * Clear any pending bind callbacks. This is called when is loader is planning to
3721 * perform a full rebind from scratch.
3722 */
3723 @Override
3724 public void clearPendingBinds() {
3725 mBindOnResumeCallbacks.clear();
3726 if (mPendingExecutor != null) {
3727 mPendingExecutor.markCompleted();
3728 mPendingExecutor = null;
3729 }
3730 }
3731
3732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 * Refreshes the shortcuts shown on the workspace.
3734 *
3735 * Implementation of the method from LauncherModel.Callbacks.
3736 */
3737 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003738 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003739
Winson Chungd64d1762013-08-20 14:37:16 -07003740 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003741 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003742 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003743
Michael Jurka05bf644e2011-11-30 20:28:53 -08003744 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003745 if (mHotseat != null) {
3746 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003747 }
3748 }
3749
Adam Cohendcd297f2013-06-18 13:13:40 -07003750 @Override
3751 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003752 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003753
Adam Cohen5084cba2013-09-03 12:01:16 -07003754 // If there are no screens, we need to have an empty screen
3755 if (orderedScreenIds.size() == 0) {
3756 mWorkspace.addExtraEmptyScreen();
3757 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003758
3759 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003760 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003761 if (hasCustomContentToLeft()) {
3762 mWorkspace.createCustomContentContainer();
3763 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003764 }
Winson Chung64359a52013-07-08 17:17:08 -07003765 }
3766
3767 @Override
3768 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 int count = orderedScreenIds.size();
3770 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003771 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003773 }
3774
Winson Chungd64d1762013-08-20 14:37:16 -07003775 public void bindAppsAdded(final ArrayList<Long> newScreens,
3776 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003777 final ArrayList<ItemInfo> addAnimated,
3778 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003779 Runnable r = new Runnable() {
3780 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003781 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003782 }
3783 };
3784 if (waitUntilResume(r)) {
3785 return;
3786 }
3787
Winson Chungd64d1762013-08-20 14:37:16 -07003788 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003789 if (newScreens != null) {
3790 bindAddScreens(newScreens);
3791 }
Winson Chungd64d1762013-08-20 14:37:16 -07003792
3793 // We add the items without animation on non-visible pages, and with
3794 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003795 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003796 bindItems(addNotAnimated, 0,
3797 addNotAnimated.size(), false);
3798 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003799 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 bindItems(addAnimated, 0,
3801 addAnimated.size(), true);
3802 }
Winson Chungc58497e2013-09-03 17:48:37 -07003803
Winson Chung87412982013-10-03 18:34:14 -07003804 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003805 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003806
Winson Chungb745afb2015-03-02 11:51:23 -08003807 if (addedApps != null && mAppsView != null) {
3808 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003809 }
Winson Chungd64d1762013-08-20 14:37:16 -07003810 }
3811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 * Bind the items start-end from the list.
3814 *
3815 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003816 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003817 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003818 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3819 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 Runnable r = new Runnable() {
3821 public void run() {
3822 bindItems(shortcuts, start, end, forceAnimateIcons);
3823 }
3824 };
3825 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003826 return;
3827 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003828
Winson Chungf0c6ae02012-03-21 16:10:31 -07003829 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003830 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3831 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003832 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003833 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003834 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003837
3838 // Short circuit if we are loading dock items for a configuration which has no dock
3839 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3840 mHotseat == null) {
3841 continue;
3842 }
3843
Sunny Goyal639e9062015-08-19 19:17:06 -07003844 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845 switch (item.itemType) {
3846 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3847 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003848 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003849 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003850
Winson Chung64359a52013-07-08 17:17:08 -07003851 /*
3852 * TODO: FIX collision case
3853 */
Winson Chungce376632013-07-11 16:12:41 -07003854 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3855 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3856 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003857 View v = cl.getChildAt(item.cellX, item.cellY);
3858 Object tag = v.getTag();
3859 String desc = "Collision while binding workspace item: " + item
3860 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003861 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003862 throw (new RuntimeException(desc));
3863 } else {
3864 Log.d(TAG, desc);
3865 }
Winson Chungce376632013-07-11 16:12:41 -07003866 }
Winson Chung64359a52013-07-08 17:17:08 -07003867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003869 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003870 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003871 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003872 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003874 default:
3875 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003877
3878 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3879 item.cellY, 1, 1);
3880 if (animateIcons) {
3881 // Animate all the applications up now
3882 view.setAlpha(0f);
3883 view.setScaleX(0f);
3884 view.setScaleY(0f);
3885 bounceAnims.add(createNewAppBounceAnimation(view, i));
3886 newShortcutsScreenId = item.screenId;
3887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003888 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003889
Winson Chung997a9232013-07-24 15:33:46 -07003890 if (animateIcons) {
3891 // Animate to the correct page
3892 if (newShortcutsScreenId > -1) {
3893 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003894 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003895 final Runnable startBounceAnimRunnable = new Runnable() {
3896 public void run() {
3897 anim.playTogether(bounceAnims);
3898 anim.start();
3899 }
3900 };
Winson Chung997a9232013-07-24 15:33:46 -07003901 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003902 // We post the animation slightly delayed to prevent slowdowns
3903 // when we are loading right after we return to launcher.
3904 mWorkspace.postDelayed(new Runnable() {
3905 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003906 if (mWorkspace != null) {
3907 mWorkspace.snapToPage(newScreenIndex);
3908 mWorkspace.postDelayed(startBounceAnimRunnable,
3909 NEW_APPS_ANIMATION_DELAY);
3910 }
Winson Chung94d67682013-09-25 16:29:40 -07003911 }
3912 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003913 } else {
3914 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003915 }
3916 }
Winson Chung64359a52013-07-08 17:17:08 -07003917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003919 }
3920
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 /**
3922 * Implementation of the method from LauncherModel.Callbacks.
3923 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003924 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003925 Runnable r = new Runnable() {
3926 public void run() {
3927 bindFolders(folders);
3928 }
3929 };
3930 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003931 return;
3932 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003933 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003936 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3937 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3938 view.updateIcon(mIconCache);
3939 item.hostView = view;
3940 item.hostView.updateAppWidget(null);
3941 item.hostView.setOnClickListener(this);
3942 addAppWidgetToWorkspace(item, null, false);
3943 mWorkspace.requestLayout();
3944 }
3945
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 /**
3947 * Add the views for a widget to the workspace.
3948 *
3949 * Implementation of the method from LauncherModel.Callbacks.
3950 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003951 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003952 Runnable r = new Runnable() {
3953 public void run() {
3954 bindAppWidget(item);
3955 }
3956 };
3957 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003958 return;
3959 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003960
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003961 if (mIsSafeModeEnabled) {
3962 bindSafeModeWidget(item);
3963 return;
3964 }
3965
Daniel Sandler843e8602010-06-07 14:59:01 -04003966 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3967 if (DEBUG_WIDGETS) {
3968 Log.d(TAG, "bindAppWidget: " + item);
3969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003971 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003972
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003973 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3974 // If the provider is not ready, bind as a pending widget.
3975 appWidgetInfo = null;
3976 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3977 // The widget id is not valid. Try to find the widget based on the provider info.
3978 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3979 } else {
3980 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3981 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003982
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003983 // If the provider is ready, but the width is not yet restored, try to restore it.
3984 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3985 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003986 if (appWidgetInfo == null) {
3987 if (DEBUG_WIDGETS) {
3988 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3989 + " belongs to component " + item.providerName
3990 + ", as the povider is null");
3991 }
3992 LauncherModel.deleteItemFromDatabase(this, item);
3993 return;
3994 }
Sunny Goyalff572272014-07-23 13:58:07 -07003995
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003996 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003997 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003998 // Note: This assumes that the id remap broadcast is received before this step.
3999 // If that is not the case, the id remap will be ignored and user may see the
4000 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08004001 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004002 pendingInfo.spanX = item.spanX;
4003 pendingInfo.spanY = item.spanY;
4004 pendingInfo.minSpanX = item.minSpanX;
4005 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004006 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004007
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004008 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4009 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4010 newWidgetId, appWidgetInfo, options);
4011
4012 // TODO consider showing a permission dialog when the widget is clicked.
4013 if (!success) {
4014 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4015 if (DEBUG_WIDGETS) {
4016 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4017 + " belongs to component " + item.providerName
4018 + ", as the launcher is unable to bing a new widget id");
4019 }
4020 LauncherModel.deleteItemFromDatabase(this, item);
4021 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004022 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004023
4024 item.appWidgetId = newWidgetId;
4025
4026 // If the widget has a configure activity, it is still needs to set it up, otherwise
4027 // the widget is ready to go.
4028 item.restoreStatus = (appWidgetInfo.configure == null)
4029 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4030 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4031
4032 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004033 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004034 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004035 // The widget was marked as UI not ready, but there is no configure activity to
4036 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004037 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4038 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004039 }
Sunny Goyalff572272014-07-23 13:58:07 -07004040 }
4041
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004042 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004043 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004044 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4045 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004047
Sunny Goyal56c73602015-09-25 12:55:01 -07004048 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004049 if (appWidgetInfo == null) {
4050 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004051 deleteWidgetInfo(item);
4052 return;
4053 }
4054
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004055 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004056 item.minSpanX = appWidgetInfo.minSpanX;
4057 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004058 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004059 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004060 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4061 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004062 view.updateIcon(mIconCache);
4063 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004065 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004066 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004067 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004068 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069
Daniel Sandler843e8602010-06-07 14:59:01 -04004070 if (DEBUG_WIDGETS) {
4071 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4072 + (SystemClock.uptimeMillis()-start) + "ms");
4073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 }
4075
Sunny Goyalff572272014-07-23 13:58:07 -07004076 /**
4077 * Restores a pending widget.
4078 *
4079 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004080 */
4081 private void completeRestoreAppWidget(final int appWidgetId) {
4082 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4083 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4084 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4085 return;
4086 }
4087
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004088 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004089 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4090
4091 mWorkspace.reinflateWidgetsIfNecessary();
4092 LauncherModel.updateItemInDatabase(this, info);
4093 }
4094
Adam Cohen1462de32012-07-24 22:34:36 -07004095 public void onPageBoundSynchronously(int page) {
4096 mSynchronouslyBoundPages.add(page);
4097 }
4098
Sunny Goyal527c7d32015-08-28 15:19:36 -07004099 @Override
4100 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4101 if (mPendingExecutor != null) {
4102 mPendingExecutor.markCompleted();
4103 }
4104 mPendingExecutor = executor;
4105 executor.attachTo(this);
4106 }
4107
4108 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4109 if (mPendingExecutor == executor) {
4110 mPendingExecutor = null;
4111 }
4112 }
4113
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 /**
4115 * Callback saying that there aren't any more items to bind.
4116 *
4117 * Implementation of the method from LauncherModel.Callbacks.
4118 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004119 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004120 Runnable r = new Runnable() {
4121 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004122 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004123 }
4124 };
4125 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004126 return;
4127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004128 if (mSavedState != null) {
4129 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004130 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004132
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 mSavedState = null;
4134 }
4135
Adam Cohen1462de32012-07-24 22:34:36 -07004136 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004138 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004139 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004140
4141 // If we received the result of any pending adds while the loader was running (e.g. the
4142 // widget configuration forced an orientation change), process them now.
4143 if (sPendingAddItem != null) {
4144 final long screenId = completeAdd(sPendingAddItem);
4145
4146 // TODO: this moves the user to the page where the pending item was added. Ideally,
4147 // the screen would be guaranteed to exist after bind, and the page would be set through
4148 // the workspace restore process.
4149 mWorkspace.post(new Runnable() {
4150 @Override
4151 public void run() {
4152 mWorkspace.snapToScreenId(screenId);
4153 }
4154 });
4155 sPendingAddItem = null;
4156 }
4157
Sunny Goyal756adbc2015-04-16 15:20:51 -07004158 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004159
4160 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004161 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004162 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004163 }
4164
Adam Cohen3ed316a2014-07-23 18:21:20 -07004165 private void sendLoadingCompleteBroadcastIfNecessary() {
4166 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4167 String permission =
4168 getResources().getString(R.string.receive_first_load_broadcast_permission);
4169 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4170 sendBroadcast(intent, permission);
4171 SharedPreferences.Editor editor = mSharedPrefs.edit();
4172 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4173 editor.apply();
4174 }
4175 }
4176
Winson Chunga0b7e862013-09-05 16:03:15 -07004177 public boolean isAllAppsButtonRank(int rank) {
4178 if (mHotseat != null) {
4179 return mHotseat.isAllAppsButtonRank(rank);
4180 }
4181 return false;
4182 }
4183
Winson Chunga2413752012-04-03 14:22:34 -07004184 private boolean canRunNewAppsAnimation() {
4185 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004186 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4187 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004188 }
4189
Winson Chungc9168342013-06-26 14:54:55 -07004190 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004191 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004192 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4193 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004194 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004195 return bounceAnim;
4196 }
4197
Adam Cohen27772732013-11-11 14:00:56 +00004198 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004199 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004200 }
4201
Tony Wickham3a3517f2015-10-06 11:27:04 -07004202 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004203 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004204 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004205 }
4206
Tony Wickham55616cd2015-09-23 14:55:17 -07004207 public int getSearchBarHeight() {
4208 if (mLauncherCallbacks != null) {
4209 return mLauncherCallbacks.getSearchBarHeight();
4210 }
4211 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4212 }
4213
Winson Chung88fa7412015-08-03 14:25:28 -07004214 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004215 if (mSearchDropTargetBar == null) {
4216 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004217 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004218 if (mQsb != null) {
4219 mSearchDropTargetBar.removeView(mQsb);
4220 mQsb = null;
4221 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004222 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004223 }
4224
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004225 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004226 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4227 * multiple calls to bind the same list.)
4228 */
4229 @Thunk ArrayList<AppInfo> mTmpAppsList;
4230 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4231 public void run() {
4232 bindAllApplications(mTmpAppsList);
4233 mTmpAppsList = null;
4234 }
4235 };
4236
4237 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 * Add the icons for all apps.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004242 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004243 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4244 mTmpAppsList = apps;
4245 return;
4246 }
4247
Winson Chungb745afb2015-03-02 11:51:23 -08004248 if (mAppsView != null) {
4249 mAppsView.setApps(apps);
4250 }
Adam Cohen9211d422014-10-07 18:14:53 -07004251 if (mLauncherCallbacks != null) {
4252 mLauncherCallbacks.bindAllApplications(apps);
4253 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004254 }
4255
4256 /**
4257 * A package was updated.
4258 *
4259 * Implementation of the method from LauncherModel.Callbacks.
4260 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004261 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004262 Runnable r = new Runnable() {
4263 public void run() {
4264 bindAppsUpdated(apps);
4265 }
4266 };
4267 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004268 return;
4269 }
4270
Winson Chungb745afb2015-03-02 11:51:23 -08004271 if (mAppsView != null) {
4272 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 }
4275
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 @Override
4277 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4278 Runnable r = new Runnable() {
4279 public void run() {
4280 bindWidgetsRestored(widgets);
4281 }
4282 };
4283 if (waitUntilResume(r)) {
4284 return;
4285 }
4286 mWorkspace.widgetsRestored(widgets);
4287 }
4288
Joe Onorato9c1289c2009-08-17 11:03:03 -04004289 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004290 * Some shortcuts were updated in the background.
4291 *
4292 * Implementation of the method from LauncherModel.Callbacks.
4293 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004294 @Override
4295 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4296 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004297 Runnable r = new Runnable() {
4298 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004299 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004300 }
4301 };
4302 if (waitUntilResume(r)) {
4303 return;
4304 }
4305
Sunny Goyal4390ace2014-10-13 11:33:11 -07004306 if (!updated.isEmpty()) {
4307 mWorkspace.updateShortcuts(updated);
4308 }
4309
4310 if (!removed.isEmpty()) {
4311 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4312 for (ShortcutInfo si : removed) {
4313 removedComponents.add(si.getTargetComponent());
4314 }
4315 mWorkspace.removeItemsByComponentName(removedComponents, user);
4316 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004317 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004318 }
4319 }
4320
4321 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004322 * Update the state of a package, typically related to install state.
4323 *
4324 * Implementation of the method from LauncherModel.Callbacks.
4325 */
Sunny Goyale755d462014-07-22 13:48:29 -07004326 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004327 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4328 Runnable r = new Runnable() {
4329 public void run() {
4330 bindRestoreItemsChange(updates);
4331 }
4332 };
4333 if (waitUntilResume(r)) {
4334 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004335 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004336
Sunny Goyal756adbc2015-04-16 15:20:51 -07004337 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004338 }
4339
4340 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004341 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004342 * in addition to specific applications to remove, the reason being that
4343 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004344 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004345 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004346 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004347 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004348 public void bindWorkspaceComponentsRemoved(
4349 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4350 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004351 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004352 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004353 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004354 }
Winson Chungd64d1762013-08-20 14:37:16 -07004355 };
4356 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004357 return;
4358 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004359 if (!packageNames.isEmpty()) {
4360 mWorkspace.removeItemsByPackageName(packageNames, user);
4361 }
4362 if (!components.isEmpty()) {
4363 mWorkspace.removeItemsByComponentName(components, user);
4364 }
4365 // Notify the drag controller
4366 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004367
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004368 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004369
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004370 @Override
4371 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4372 Runnable r = new Runnable() {
4373 public void run() {
4374 bindAppInfosRemoved(appInfos);
4375 }
4376 };
4377 if (waitUntilResume(r)) {
4378 return;
Joe Onorato36115782010-06-17 13:28:48 -04004379 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004380
Winson Chungdf95eb12013-10-16 14:57:07 -07004381 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004382 if (mAppsView != null) {
4383 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004384 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004385 }
Joe Onoratobe386092009-11-17 17:32:16 -08004386
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004387 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004388 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004389 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004390 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004391 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004392
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004393 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004394 public void bindWidgetsModel(WidgetsModel model) {
4395 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004396 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004397 return;
4398 }
4399
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004400 if (mWidgetsView != null && model != null) {
4401 mWidgetsView.addWidgets(model);
4402 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004403 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004404 }
4405
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004406 @Override
4407 public void notifyWidgetProvidersChanged() {
4408 if (mWorkspace.getState().shouldUpdateWidget) {
4409 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4410 }
4411 }
4412
Winson Chung400438b2011-01-16 17:53:48 -08004413 private int mapConfigurationOriActivityInfoOri(int configOri) {
4414 final Display d = getWindowManager().getDefaultDisplay();
4415 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4416 switch (d.getRotation()) {
4417 case Surface.ROTATION_0:
4418 case Surface.ROTATION_180:
4419 // We are currently in the same basic orientation as the natural orientation
4420 naturalOri = configOri;
4421 break;
4422 case Surface.ROTATION_90:
4423 case Surface.ROTATION_270:
4424 // We are currently in the other basic orientation to the natural orientation
4425 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4426 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4427 break;
4428 }
4429
4430 int[] oriMap = {
4431 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4432 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4433 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4434 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4435 };
4436 // Since the map starts at portrait, we need to offset if this device's natural orientation
4437 // is landscape.
4438 int indexOffset = 0;
4439 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4440 indexOffset = 1;
4441 }
4442 return oriMap[(d.getRotation() + indexOffset) % 4];
4443 }
Adam Cohen446e9402011-09-15 18:21:21 -07004444
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004445 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004446 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004447 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004448 if (Utilities.ATLEAST_JB_MR2) {
4449 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4450 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004451 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4452 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004453 }
Winson Chung4b919f82012-05-01 10:44:08 -07004454 }
4455 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004456
Winson Chung4b919f82012-05-01 10:44:08 -07004457 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004458 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004459 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004460 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004461 } else {
4462 mHandler.postDelayed(new Runnable() {
4463 public void run() {
4464 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4465 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004466 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004467 }
Winson Chung4b919f82012-05-01 10:44:08 -07004468 }
Winson Chung400438b2011-01-16 17:53:48 -08004469 }
4470
Winson Chunge43a1e72014-01-15 10:33:02 -08004471 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004472 if (mLauncherCallbacks != null) {
4473 return mLauncherCallbacks.isLauncherPreinstalled();
4474 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004475 PackageManager pm = getPackageManager();
4476 try {
4477 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4478 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4479 return true;
4480 } else {
4481 return false;
4482 }
4483 } catch (NameNotFoundException e) {
4484 e.printStackTrace();
4485 return false;
4486 }
4487 }
4488
Adam Cohenea90f832014-05-21 15:03:34 -07004489 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004490 * To be overridden by subclasses to indicate that there is an activity to launch
4491 * before showing the standard launcher experience.
4492 */
4493 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004494 if (mLauncherCallbacks != null) {
4495 return mLauncherCallbacks.hasFirstRunActivity();
4496 }
Adam Cohen432609a2014-03-13 17:03:22 -07004497 return false;
4498 }
4499
4500 /**
4501 * To be overridden by subclasses to launch any first run activity
4502 */
4503 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004504 if (mLauncherCallbacks != null) {
4505 return mLauncherCallbacks.getFirstRunActivity();
4506 }
Adam Cohen432609a2014-03-13 17:03:22 -07004507 return null;
4508 }
4509
Winson Chunga6945242014-01-08 14:04:34 -08004510 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004511 return !ActivityManager.isRunningInTestHarness() &&
4512 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004513 }
4514
Adam Cohen4a9423d2014-03-28 14:22:31 -07004515 protected boolean hasRunFirstRunActivity() {
4516 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4517 }
4518
Adam Cohen432609a2014-03-13 17:03:22 -07004519 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004520 if (shouldRunFirstRunActivity() &&
4521 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004522 Intent firstRunIntent = getFirstRunActivity();
4523 if (firstRunIntent != null) {
4524 startActivity(firstRunIntent);
4525 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004527 }
4528 }
Adam Cohen432609a2014-03-13 17:03:22 -07004529 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004530 }
4531
4532 private void markFirstRunActivityShown() {
4533 SharedPreferences.Editor editor = mSharedPrefs.edit();
4534 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4535 editor.apply();
4536 }
4537
Adam Cohen432609a2014-03-13 17:03:22 -07004538 /**
4539 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4540 * screen that must be displayed and dismissed.
4541 */
4542 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004543 if (mLauncherCallbacks != null) {
4544 return mLauncherCallbacks.hasDismissableIntroScreen();
4545 }
Adam Cohen432609a2014-03-13 17:03:22 -07004546 return false;
4547 }
4548
4549 /**
4550 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4551 */
4552 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004553 if (mLauncherCallbacks != null) {
4554 return mLauncherCallbacks.getIntroScreen();
4555 }
Adam Cohen432609a2014-03-13 17:03:22 -07004556 return null;
4557 }
4558
4559 /**
4560 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4561 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4562 */
4563 private boolean shouldShowIntroScreen() {
4564 return hasDismissableIntroScreen() &&
4565 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4566 }
4567
4568 protected void showIntroScreen() {
4569 View introScreen = getIntroScreen();
4570 changeWallpaperVisiblity(false);
4571 if (introScreen != null) {
4572 mDragLayer.showOverlayView(introScreen);
4573 }
4574 }
4575
4576 public void dismissIntroScreen() {
4577 markIntroScreenDismissed();
4578 if (showFirstRunActivity()) {
4579 // We delay hiding the intro view until the first run activity is showing. This
4580 // avoids a blip.
4581 mWorkspace.postDelayed(new Runnable() {
4582 @Override
4583 public void run() {
4584 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004585 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004586 }
4587 }, ACTIVITY_START_DELAY);
4588 } else {
4589 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004590 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004591 }
4592 changeWallpaperVisiblity(true);
4593 }
4594
4595 private void markIntroScreenDismissed() {
4596 SharedPreferences.Editor editor = mSharedPrefs.edit();
4597 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4598 editor.apply();
4599 }
4600
Adam Cohen091440a2015-03-18 14:16:05 -07004601 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004602 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4603 // on the device, then we always show the first run cling experience (or if there is no
4604 // launcher2). Otherwise, we prompt the user upon started for migration
4605 LauncherClings launcherClings = new LauncherClings(this);
4606 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004607 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004608 if (mModel.canMigrateFromOldLauncherDb(this)) {
4609 launcherClings.showMigrationCling();
4610 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004611 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004612 }
4613 }
4614 }
4615
Winson Chunga6945242014-01-08 14:04:34 -08004616 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004617 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4618 if (mHotseat != null) mHotseat.setAlpha(1f);
4619 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004620 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4621 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004622 }
4623
4624 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004625 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4626 if (mHotseat != null) mHotseat.setAlpha(0f);
4627 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004628 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4629 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004630 }
4631
Winson Chung5ffd51d2015-06-25 11:36:50 -07004632 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004633 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004634 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004635 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004636 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004637 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004638 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4639 if (userHandle != null) {
4640 user = UserHandleCompat.fromUser(userHandle);
4641 }
4642 }
4643 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004644 }
4645
4646 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004647 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004648 if (user == null) {
4649 user = UserHandleCompat.myUserHandle();
4650 }
4651
4652 // Called from search suggestion, add the profile extra to the intent to ensure that we
4653 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004654 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004655 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004656 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004657 return null;
4658 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004659 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 }
4661
Winson Chung5ffd51d2015-06-25 11:36:50 -07004662 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004663 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4664 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004665 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004666 UserHandleCompat.myUserHandle());
4667 }
4668
Winson Chung5ffd51d2015-06-25 11:36:50 -07004669 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004670 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4671 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004672 mWorkspace.onExternalDragStartedWithItem(dragView);
4673 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004674 }
4675
Winson Chung5ffd51d2015-06-25 11:36:50 -07004676 protected void moveWorkspaceToDefaultScreen() {
4677 mWorkspace.moveToDefaultScreen(false);
4678 }
4679
Anjali Koppalf5d29132014-02-28 13:33:27 -08004680 @Override
4681 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004682 if (mLauncherCallbacks != null) {
4683 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4684 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004685 }
4686
Winson Chung80baf5a2010-08-09 16:03:15 -07004687 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004688 * Returns a FastBitmapDrawable with the icon, accurately sized.
4689 */
4690 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4691 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4692 d.setFilterBitmap(true);
4693 resizeIconDrawable(d);
4694 return d;
4695 }
4696
4697 /**
4698 * Resizes an icon drawable to the correct icon size.
4699 */
Winson5fbe0742015-09-30 15:33:00 -07004700 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004701 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004702 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004703 }
4704
4705 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004706 * Prints out out state for debugging.
4707 */
4708 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004709 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004710 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004711 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4712 Log.d(TAG, "mRestoring=" + mRestoring);
4713 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004714 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004715 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004716 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004717
Daniel Sandler325dc232013-06-05 22:57:57 -04004718 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004719 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004720
4721 @Override
4722 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4723 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004724 // Dump workspace
4725 writer.println(prefix + "Workspace Items");
4726 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4727 writer.println(prefix + " Homescreen " + i);
4728
4729 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4730 for (int j = 0; j < layout.getChildCount(); j++) {
4731 Object tag = layout.getChildAt(j).getTag();
4732 if (tag != null) {
4733 writer.println(prefix + " " + tag.toString());
4734 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004735 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004736 }
Sunny Goyala1365452015-10-01 15:46:24 -07004737
4738 writer.println(prefix + " Hotseat");
4739 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4740 for (int j = 0; j < layout.getChildCount(); j++) {
4741 Object tag = layout.getChildAt(j).getTag();
4742 if (tag != null) {
4743 writer.println(prefix + " " + tag.toString());
4744 }
4745 }
4746
4747 synchronized (sDumpLogs) {
4748 writer.println();
4749 writer.println(prefix + "Debug logs");
4750 for (String log : sDumpLogs) {
4751 writer.println(prefix + " " + log);
4752 }
4753 }
4754
Adam Cohen9211d422014-10-07 18:14:53 -07004755 if (mLauncherCallbacks != null) {
4756 mLauncherCallbacks.dump(prefix, fd, writer, args);
4757 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004758 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004759
Sunny Goyala1365452015-10-01 15:46:24 -07004760 public static void addDumpLog(String tag, String log) {
4761 Log.d(tag, log);
4762 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004763 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004764 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004765 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004766 }
4767
Adam Cohen59400422014-03-05 18:07:04 -08004768 public static CustomAppWidget getCustomAppWidget(String name) {
4769 return sCustomAppWidgets.get(name);
4770 }
4771
4772 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4773 return sCustomAppWidgets;
4774 }
4775
Sunny Goyal29538332016-02-18 09:10:19 -08004776 public static List<View> getFolderContents(View icon) {
4777 if (icon instanceof FolderIcon) {
4778 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4779 } else {
4780 return Collections.EMPTY_LIST;
4781 }
4782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004783}
Michael Jurka2763be32011-02-24 11:19:57 -08004784
Dan Sandlerd5024042014-01-09 15:01:33 -05004785interface DebugIntents {
4786 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4787 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004788}