blob: 36b9ab024275e9f01b737f4ccf8e6749b3630e9a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070055import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800111import com.android.launcher3.logging.UserEventLogger;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800113import com.android.launcher3.logging.LoggerUtils;
114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700116import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700117import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.WidgetsContainerView;
Sunny Goyalbd822502016-02-18 15:09:21 -0800123import com.android.wallpaperpicker.WallpaperUtils;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700131import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800135import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700141 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700142 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
176 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Adam Cohen3ed316a2014-07-23 18:21:20 -0700197 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
198 static final String ACTION_FIRST_LOAD_COMPLETE =
199 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
200
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 private static final String QSB_WIDGET_ID = "qsb_widget_id";
202 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700207 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
208 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700209
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700213 private boolean mIsSafeModeEnabled;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800216 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700218 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800230 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700239 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800240 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Winson Chungc51db6a2011-10-05 11:44:49 -0700251 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700252 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
254 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Sunny Goyal594d76d2014-11-06 10:12:54 -0800261 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
265 // scroll issues (because the workspace may not have been measured yet) and extra work.
266 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
267 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
269 private SpannableStringBuilder mDefaultKeySsb = null;
270
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800273 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private boolean mRestoring;
275 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700276 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700280 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800283 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700286 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Sunny Goyal639e9062015-08-19 19:17:06 -0700289 private LauncherClings mClings;
290
Sunny Goyale2df0622015-04-24 11:27:00 -0700291 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700313
Sunny Goyala1365452015-10-01 15:46:24 -0700314 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
315 private static final Date sDateStamp = new Date();
316 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700317 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800369 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800370
Adam Cohenf9c184a2016-01-15 16:47:43 -0800371 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal7779d622015-06-11 16:18:39 -0700383 private Runnable mUpdateOrientationRunnable = new Runnable() {
384 public void run() {
385 setOrientation();
386 }
387 };
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700415 mDeviceProfile = getResources().getConfiguration().orientation
416 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700417 app.getInvariantDeviceProfile().landscapeProfile
418 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700419
Sunny Goyalf7258242015-10-19 16:59:07 -0700420 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800429 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
Tony Wickhameef44322015-10-20 13:24:36 -0700448 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
449 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700451 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
458 if (PROFILE_STARTUP) {
459 android.os.Debug.stopMethodTracing();
460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700470 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473
474 // For handling default keys
475 mDefaultKeySsb = new SpannableStringBuilder();
476 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800477
478 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
479 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800480
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800481 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 // In case we are on a device with locked rotation, we should look at preferences to check
483 // if the user has specifically allowed rotation.
484 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700485 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
505 public void onSettingsChanged(String settings, boolean value) {
506 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
507 mRotationEnabled = value;
508 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
509 mUpdateOrientationRunnable.run();
510 }
511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
Sunny Goyal32554d12015-12-03 15:31:25 -0800523 /**
524 * Call this after onCreate to set or clear overlay.
525 */
526 public void setLauncherOverlay(LauncherOverlay overlay) {
527 if (overlay != null) {
528 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
529 }
530 mWorkspace.setLauncherOverlay(overlay);
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700548 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Adam Cohen9211d422014-10-07 18:14:53 -0700586 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700588 if (mLauncherCallbacks != null) {
589 return mLauncherCallbacks.hasCustomContentToLeft();
590 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 return false;
592 }
593
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 * {@link #addToCustomContentPage}. This will only be invoked if
597 * {@link #hasCustomContentToLeft()} is {@code true}.
598 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700600 if (mLauncherCallbacks != null) {
601 mLauncherCallbacks.populateCustomContentContainer();
602 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 }
604
605 /**
606 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
607 * ensure the custom content page is added or removed if necessary.
608 */
609 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600610 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 // Not bound yet, wait for bindScreens to be called.
612 return;
613 }
614
615 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
616 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500617 mWorkspace.createCustomContentContainer();
618 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
620 mWorkspace.removeCustomContentPage();
621 }
622 }
623
Anton Hanssona2f665f2013-09-26 12:18:32 +0100624 public Stats getStats() {
625 return mStats;
626 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800627
628 /**
629 * Logger object is a singleton and does not have to be coupled with the foreground activity.
630 * Since most user event logging is done on the UI, the object is retrieved from the
631 * callback for convenience.
632 */
633 private void initLogger() {
634 if (mLauncherCallbacks != null) {
635 mUserEventLogger = mLauncherCallbacks.getLogger();
636 }
637 if (mUserEventLogger == null) {
638 mUserEventLogger = new UserEventLogger() {
639 @Override
640 public void processEvent(LauncherLogProto.LauncherEvent ev) {
641 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
642 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
643 LoggerUtils.getActionStr(ev.action),
644 LoggerUtils.getTargetStr(ev.srcTarget),
645 ev.elapsedContainerMillis,
646 ev.elapsedSessionMillis));
647 }
648 }
649 };
650 }
651 }
652
653 public UserEventLogger getLogger() {
654 return mUserEventLogger;
655 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100656
Hyunyoung Song3f471442015-04-08 19:01:34 -0700657 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
659 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700660 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 }
662
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000663 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700664 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
665 // This cast is safe as long as the id < 0x00FFFFFF
666 // Since we jail all the dynamically generated views, there should be no clashes
667 // with any other views.
668 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000669 }
670
671 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
673 * a configuration step, this allows the proper animations to run after other transitions.
674 */
Adam Cohendb364c32014-05-20 14:23:40 -0700675 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700676 long screenId = args.screenId;
677 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
678 // When the screen id represents an actual screen (as opposed to a rank) we make sure
679 // that the drop page actually exists.
680 screenId = ensurePendingDropLayoutExists(args.screenId);
681 }
Adam Cohendb364c32014-05-20 14:23:40 -0700682
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700685 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700689 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700691 case REQUEST_RECONFIGURE_APPWIDGET:
692 completeRestoreAppWidget(args.appWidgetId);
693 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700699 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800702 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700704 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700705 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700706 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
712 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
713 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
714 }
715 };
716
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700718 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
721 if (resultCode == RESULT_CANCELED) {
722 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800726 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700728
729 // When the user has granted permission to bind widgets, we should check to see if
730 // we can inflate the default search bar widget.
731 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 }
733 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
735 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700736 // User could have free-scrolled between pages before picking a wallpaper; make sure
737 // we move to the closest one now.
738 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700739 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740 }
741 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700745 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700746
Adam Cohendb364c32014-05-20 14:23:40 -0700747 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 // We have special handling for widgets
749 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800750 final int appWidgetId;
751 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
752 : -1;
753 if (widgetId < 0) {
754 appWidgetId = pendingAddWidgetId;
755 } else {
756 appWidgetId = widgetId;
757 }
758
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800760 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700761 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
762 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 result = RESULT_CANCELED;
764 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700765 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed(false, 0, null);
769 }
770 };
Adam Cohendb364c32014-05-20 14:23:40 -0700771 if (workspaceLocked) {
772 // No need to remove the empty screen if we're mid-binding, as the
773 // the bind will not add the empty screen.
774 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
775 } else {
776 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
777 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
778 }
Winson Chung5aaab772012-04-27 15:24:02 -0700779 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700780 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700781 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
782 // When the screen id represents an actual screen (as opposed to a rank)
783 // we make sure that the drop page actually exists.
784 mPendingAddInfo.screenId =
785 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
786 }
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
788
789 dropLayout.setDropPending(true);
790 final Runnable onComplete = new Runnable() {
791 @Override
792 public void run() {
793 completeTwoStageWidgetDrop(resultCode, appWidgetId);
794 dropLayout.setDropPending(false);
795 }
796 };
797 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
799 } else {
800 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
801 mPendingAddInfo);
802 sPendingAddItem = args;
803 }
Winson Chung5aaab772012-04-27 15:24:02 -0700804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 return;
806 }
807
Sunny Goyalff572272014-07-23 13:58:07 -0700808 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
809 if (resultCode == RESULT_OK) {
810 // Update the widget view.
811 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
812 pendingAddWidgetId, mPendingAddInfo);
813 if (workspaceLocked) {
814 sPendingAddItem = args;
815 } else {
816 completeAdd(args);
817 }
818 }
819 // Leave the widget in the pending state if the user canceled the configure.
820 return;
821 }
822
Sunny Goyalaad90582015-07-09 14:22:50 -0700823 if (requestCode == REQUEST_CREATE_SHORTCUT) {
824 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
825 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
826 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
827 mPendingAddInfo);
828 if (isWorkspaceLocked()) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
834 }
835 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800841
842 }
843
844 @Override
845 protected void onActivityResult(
846 final int requestCode, final int resultCode, final Intent data) {
847 handleActivityResult(requestCode, resultCode, data);
848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
850 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800851 }
852
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700854 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700856 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
857 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
858 View v = null;
859 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
860 if (layout != null) {
861 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
862 }
863 Intent intent = sPendingAddItem.intent;
864 sPendingAddItem = null;
865 if (grantResults.length > 0
866 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
867 startActivity(v, intent, null);
868 } else {
869 // TODO: Show a snack bar with link to settings
870 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
871 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
872 }
873 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
876 grantResults);
877 }
878 }
879
Adam Cohendb364c32014-05-20 14:23:40 -0700880 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
881 appWidgetId, ItemInfo info) {
882 PendingAddArguments args = new PendingAddArguments();
883 args.requestCode = requestCode;
884 args.intent = data;
885 args.container = info.container;
886 args.screenId = info.screenId;
887 args.cellX = info.cellX;
888 args.cellY = info.cellY;
889 args.appWidgetId = appWidgetId;
890 return args;
891 }
892
893 /**
894 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
895 *
896 * @param screenId the screen id to check
897 * @return the new screen, or screenId if it exists
898 */
899 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800900 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800912 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.preOnResume();
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800978 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700979
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700981 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700982 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800983 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700984 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700985 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700986 // view after launching an app, as they may be depending on the UI to be static to
987 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700988 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700989 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800990 } else if (mOnResumeState == State.WIDGETS) {
991 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 }
993 mOnResumeState = State.NONE;
994
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700995 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700996 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
997 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700998
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800999 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001001 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001002 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1008 // execute them here
1009 long startTimeCallbacks = 0;
1010 if (DEBUG_RESUME_TIME) {
1011 startTimeCallbacks = System.currentTimeMillis();
1012 }
1013
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1015 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001018 if (DEBUG_RESUME_TIME) {
1019 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1020 (System.currentTimeMillis() - startTimeCallbacks));
1021 }
Michael Jurka447bf842013-05-15 14:52:15 +02001022 }
Michael Jurka54554252013-08-01 12:52:23 +02001023 if (mOnResumeCallbacks.size() > 0) {
1024 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1025 mOnResumeCallbacks.get(i).run();
1026 }
1027 mOnResumeCallbacks.clear();
1028 }
Winson Chunge4e50662012-01-23 14:45:13 -08001029
1030 // Reset the pressed state of icons that were locked in the press state while activities
1031 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001032 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001033 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 mWaitingForResume.setStayPressed(false);
1035 }
Winson Chung82963d52013-10-09 11:20:57 -07001036
Adam Cohen06dff352012-06-01 17:17:08 -07001037 // It is possible that widgets can receive updates while launcher is not in the foreground.
1038 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1039 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1040 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001041 if (!isWorkspaceLoading()) {
1042 getWorkspace().reinflateWidgetsIfNecessary();
1043 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001044 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001045
Michael Jurka447bf842013-05-15 14:52:15 +02001046 if (DEBUG_RESUME_TIME) {
1047 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1048 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001049
Adam Cohen4b66bf32015-09-18 12:15:19 -07001050 // We want to suppress callbacks about CustomContent being shown if we have just received
1051 // onNewIntent while the user was present within launcher. In that case, we post a call
1052 // to move the user to the main screen (which will occur after onResume). We don't want to
1053 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1054 // suppress here.
1055 if (mWorkspace.getCustomContentCallbacks() != null
1056 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001058 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1060 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001061 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001062 }
1063 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001064 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001065 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001066 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001067
Sunny Goyal756adbc2015-04-16 15:20:51 -07001068 if (!isWorkspaceLoading()) {
1069 // Process any items that were added while Launcher was away.
1070 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1071 }
Adam Cohen9211d422014-10-07 18:14:53 -07001072
1073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onResume();
1075 }
Adam Cohen06dff352012-06-01 17:17:08 -07001076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001080 // Ensure that items added to Launcher are queued until Launcher returns
1081 InstallShortcutReceiver.enableInstallQueue();
1082
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001084 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001085 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001086 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 // We call onHide() aggressively. The custom content callbacks should be able to
1089 // debounce excess onHide calls.
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 mWorkspace.getCustomContentCallbacks().onHide();
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlay {
1115
1116 /**
1117 * Touch interaction leading to overscroll has begun
1118 */
1119 public void onScrollInteractionBegin();
1120
1121 /**
1122 * Touch interaction related to overscroll has ended
1123 */
1124 public void onScrollInteractionEnd();
1125
1126 /**
1127 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1128 * screen (or in the case of RTL, the rightmost screen).
1129 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001130 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131
1132 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001133 * Called when the launcher is ready to use the overlay
1134 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001136 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137 }
1138
Jun Mukai8af0cd82015-05-12 12:32:12 -07001139 public interface LauncherSearchCallbacks {
1140 /**
1141 * Called when the search overlay is shown.
1142 */
1143 public void onSearchOverlayOpened();
1144
1145 /**
1146 * Called when the search overlay is dismissed.
1147 */
1148 public void onSearchOverlayClosed();
1149 }
1150
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001152 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
Sunny Goyalc86df472016-02-25 09:19:38 -08001157 public void onScrollChanged(float progress) {
1158 if (mWorkspace != null) {
1159 mWorkspace.onOverlayScrollChanged(progress);
1160 }
1161 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001162 }
1163
Jorim Jaggid017f882014-01-14 17:08:48 -08001164 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001165 if (mLauncherCallbacks != null) {
1166 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001167 } else {
1168 // On devices with a locked orientation, we will at least have the allow rotation
1169 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001170 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001171 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 }
1173
Adam Cohen9211d422014-10-07 18:14:53 -07001174 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001175 CustomContentCallbacks callbacks, String description) {
1176 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001177 }
1178
Adam Cohenedb40762013-07-18 16:45:45 -07001179 // The custom content needs to offset its content to account for the QSB
1180 public int getTopOffsetForCustomContent() {
1181 return mWorkspace.getPaddingTop();
1182 }
1183
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001184 @Override
1185 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001187 if (mModel.isCurrentCallbacks(this)) {
1188 mModel.stopLoader();
1189 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001190 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1191
Romain Guy13c2e7b2010-03-10 19:45:00 -08001192 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001193 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001194
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001196 @Override
1197 public void onWindowFocusChanged(boolean hasFocus) {
1198 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001199 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001200
1201 if (mLauncherCallbacks != null) {
1202 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 private boolean acceptFilter() {
1207 final InputMethodManager inputManager = (InputMethodManager)
1208 getSystemService(Context.INPUT_METHOD_SERVICE);
1209 return !inputManager.isFullscreenMode();
1210 }
1211
1212 @Override
1213 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001214 final int uniChar = event.getUnicodeChar();
1215 final boolean handled = super.onKeyDown(keyCode, event);
1216 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1217 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1219 keyCode, event);
1220 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001222 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 // showSearchDialog()
1224 // If there are multiple keystrokes before the search dialog takes focus,
1225 // onSearchRequested() will be called for every keystroke,
1226 // but it is idempotent, so it's fine.
1227 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229 }
1230
Joe Onorato8a9625e2010-01-28 15:55:35 -08001231 // Eat the long press event so the keyboard doesn't come up.
1232 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1233 return true;
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 return handled;
1237 }
1238
Winson46163472015-09-22 17:31:56 -07001239 @Override
1240 public boolean onKeyUp(int keyCode, KeyEvent event) {
1241 if (keyCode == KeyEvent.KEYCODE_MENU) {
1242 // Ignore the menu key if we are currently dragging or are on the custom content screen
1243 if (!isOnCustomContent() && !mDragController.isDragging()) {
1244 // Close any open folders
1245 closeFolder();
1246
1247 // Stop resizing any widgets
1248 mWorkspace.exitWidgetResizeMode();
1249
1250 // Show the overview mode if we are on the workspace
1251 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1252 !mWorkspace.isSwitchingState()) {
1253 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001254 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001255 }
1256 }
1257 return true;
1258 }
1259 return super.onKeyUp(keyCode, event);
1260 }
1261
Karl Rosaen138a0412009-04-23 19:00:21 -07001262 private String getTypedText() {
1263 return mDefaultKeySsb.toString();
1264 }
1265
1266 private void clearTypedText() {
1267 mDefaultKeySsb.clear();
1268 mDefaultKeySsb.clearSpans();
1269 Selection.setSelection(mDefaultKeySsb, 0);
1270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001273 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1274 * State
1275 */
1276 private static State intToState(int stateOrdinal) {
1277 State state = State.WORKSPACE;
1278 final State[] stateValues = State.values();
1279 for (int i = 0; i < stateValues.length; i++) {
1280 if (stateValues[i].ordinal() == stateOrdinal) {
1281 state = stateValues[i];
1282 break;
1283 }
1284 }
1285 return state;
1286 }
1287
1288 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 * Restores the previous state, if it exists.
1290 *
1291 * @param savedState The previous state.
1292 */
1293 private void restoreState(Bundle savedState) {
1294 if (savedState == null) {
1295 return;
1296 }
1297
Michael Jurka883f55b2010-10-21 15:47:14 -07001298 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001299 if (state == State.APPS || state == State.WIDGETS) {
1300 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
1302
Adam Cohen21cd0022013-10-09 18:57:02 -07001303 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1304 PagedView.INVALID_RESTORE_PAGE);
1305 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001306 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Sunny Goyal756cd262015-08-20 12:33:21 -07001309 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1310 if (itemValues != null) {
1311 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001312 AppWidgetProviderInfo info = savedState.getParcelable(
1313 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001314 mPendingAddWidgetInfo = info == null ?
1315 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1316
Adam Cohen4637b5a2013-11-04 18:21:24 -08001317 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001318 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 mRestoring = true;
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 /**
1324 * Finds all the views we need and configure them properly.
1325 */
1326 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001327 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001328
Craig Mautner360310b2012-10-26 15:13:08 -07001329 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001330 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001331 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1332 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001333 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001334 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001335
John Spurlock77e1f472013-09-11 10:09:51 -04001336 mLauncherView.setSystemUiVisibility(
1337 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001338 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Winson Chung4c98d922011-05-31 16:50:48 -07001340 // Setup the drag layer
1341 mDragLayer.setup(this, dragController);
1342
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 // Setup the hotseat
1344 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1345 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001346 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 }
1348
Winsone9f27272015-10-13 10:47:51 -07001349 // Setup the overview panel
1350 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001351
Winson Chung4c98d922011-05-31 16:50:48 -07001352 // Setup the workspace
1353 mWorkspace.setHapticFeedbackEnabled(false);
1354 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001355 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001356 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001357
Tony Wickham34d2c912015-09-11 09:27:58 -07001358 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001359 mSearchDropTargetBar = (SearchDropTargetBar)
1360 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001361 // Get the app info bar
1362 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1363 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001364
Winson Chungef7f8742015-06-04 17:18:17 -07001365 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001366 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001367 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001368 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1369 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1370 } else {
1371 mAppsView.setSearchBarController(new DefaultAppSearchController());
1372 }
Craig Mautner360310b2012-10-26 15:13:08 -07001373
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001375 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001376 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001377 dragController.setMoveTarget(mWorkspace);
1378 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001379 if (mSearchDropTargetBar != null) {
1380 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001381 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001382 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001383 if (mAppInfoDropTargetBar != null) {
1384 mAppInfoDropTargetBar.setup(this, dragController);
1385 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001386
Sunny Goyal322d5562015-06-25 19:35:49 -07001387 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1388 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391
Winsone9f27272015-10-13 10:47:51 -07001392 private void setupOverviewPanel() {
1393 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1394
1395 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1396 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1397 @Override
1398 public boolean onLongClick(View v) {
1399 return v.performClick();
1400 }
1401 };
1402
1403 // Bind wallpaper button actions
1404 View wallpaperButton = findViewById(R.id.wallpaper_button);
1405 wallpaperButton.setOnClickListener(new OnClickListener() {
1406 @Override
1407 public void onClick(View view) {
1408 if (!mWorkspace.isSwitchingState()) {
1409 onClickWallpaperPicker(view);
1410 }
1411 }
1412 });
1413 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1414 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 // Bind widget button actions
1417 mWidgetsButton = findViewById(R.id.widget_button);
1418 mWidgetsButton.setOnClickListener(new OnClickListener() {
1419 @Override
1420 public void onClick(View view) {
1421 if (!mWorkspace.isSwitchingState()) {
1422 onClickAddWidgetButton(view);
1423 }
1424 }
1425 });
1426 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1427 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428
1429 // Bind settings actions
1430 View settingsButton = findViewById(R.id.settings_button);
1431 boolean hasSettings = hasSettings();
1432 if (hasSettings) {
1433 settingsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickSettingsButton(view);
1438 }
1439 }
1440 });
1441 settingsButton.setOnLongClickListener(performClickOnLongClick);
1442 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443 } else {
1444 settingsButton.setVisibility(View.GONE);
1445 }
1446
1447 mOverviewPanel.setAlpha(0f);
1448 }
1449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001451 * Sets the all apps button. This method is called from {@link Hotseat}.
1452 */
1453 public void setAllAppsButton(View allAppsButton) {
1454 mAllAppsButton = allAppsButton;
1455 }
1456
1457 public View getAllAppsButton() {
1458 return mAllAppsButton;
1459 }
1460
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001461 public View getWidgetsButton() {
1462 return mWidgetsButton;
1463 }
1464
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * Creates a view representing a shortcut.
1467 *
1468 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001470 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001471 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * @param parent The group the shortcut belongs to.
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from layoutResId.
1481 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001482 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001483 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 parent, false);
1485 favorite.applyFromShortcutInfo(info, mIconCache);
1486 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001488 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 return favorite;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Add a shortcut to the workspace.
1494 *
1495 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001500 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Sunny Goyal5c97f512015-05-19 16:03:28 -07001502 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001503 if (info == null) {
1504 return;
1505 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 final View view = createShortcut(info);
1507
Sunny Goyal5c97f512015-05-19 16:03:28 -07001508 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 // First we check if we already know the exact location where we want to add this item.
1510 if (cellX >= 0 && cellY >= 0) {
1511 cellXY[0] = cellX;
1512 cellXY[1] = cellY;
1513 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001514
1515 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001516 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 true, null,null)) {
1518 return;
1519 }
1520 DragObject dragObject = new DragObject();
1521 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1523 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 return;
1525 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 }
1529
1530 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 return;
1533 }
1534
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001535 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001539 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001544 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001546 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohen091440a2015-03-18 14:16:05 -07001548 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001549 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1550
1551 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001553 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohen59400422014-03-05 18:07:04 -08001556 if (appWidgetInfo.isCustomWidget) {
1557 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001558 }
1559
Adam Cohen59400422014-03-05 18:07:04 -08001560 LauncherAppWidgetInfo launcherInfo;
1561 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1562 launcherInfo.spanX = info.spanX;
1563 launcherInfo.spanY = info.spanY;
1564 launcherInfo.minSpanX = info.minSpanX;
1565 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001566 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001569 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (hostView == null) {
1573 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001574 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1575 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 } else {
1577 // The AppWidgetHostView has already been inflated and instantiated
1578 launcherInfo.hostView = hostView;
1579 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001580 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001583 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001586 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1587 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1588 item.hostView.setTag(item);
1589 item.onBindAppWidget(this);
1590
1591 item.hostView.setFocusable(true);
1592 item.hostView.setOnFocusChangeListener(mFocusHandler);
1593
1594 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1595 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1596
1597 if (!item.isCustomWidget()) {
1598 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1599 }
1600 }
1601
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1603 @Override
1604 public void onReceive(Context context, Intent intent) {
1605 final String action = intent.getAction();
1606 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1607 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001608 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001609 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001610
Winson Chungc100e8e2011-08-09 16:02:43 -07001611 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001612 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001613 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001614 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001615 if (!showWorkspace(false)) {
1616 // If we are already on the workspace, then manually reset all apps
1617 mAppsView.reset();
1618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1621 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001622 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001623 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1624 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001625 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1627 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1628 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001629 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001630 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1631 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 }
1633 }
1634 };
1635
1636 @Override
1637 public void onAttachedToWindow() {
1638 super.onAttachedToWindow();
1639
1640 // Listen for broadcasts related to user-presence
1641 final IntentFilter filter = new IntentFilter();
1642 filter.addAction(Intent.ACTION_SCREEN_OFF);
1643 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001644 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001645 if (ENABLE_DEBUG_INTENTS) {
1646 filter.addAction(DebugIntents.DELETE_DATABASE);
1647 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001651 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001653
1654 if (mLauncherCallbacks != null) {
1655 mLauncherCallbacks.onAttachedToWindow();
1656 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001657 }
1658
1659 @Override
1660 public void onDetachedFromWindow() {
1661 super.onDetachedFromWindow();
1662 mVisible = false;
1663
Adam Cohend113e0c2010-11-11 10:48:05 -08001664 if (mAttached) {
1665 unregisterReceiver(mReceiver);
1666 mAttached = false;
1667 }
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001669
1670 if (mLauncherCallbacks != null) {
1671 mLauncherCallbacks.onDetachedFromWindow();
1672 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674
1675 public void onWindowVisibilityChanged(int visibility) {
1676 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001677 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 // The following code used to be in onResume, but it turns out onResume is called when
1679 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1680 // is a more appropriate event to handle
1681 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001682 if (!mWorkspaceLoading) {
1683 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001684 // We want to let Launcher draw itself at least once before we force it to build
1685 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 // apps is nice and speedy.
1687 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001690 if (mStarted) return;
1691 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001692 // We delay the layer building a bit in order to give
1693 // other message processing a time to run. In particular
1694 // this avoids a delay in hiding the IME if it was
1695 // currently shown, because doing that may involve
1696 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001697 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 final ViewTreeObserver.OnDrawListener listener = this;
1699 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001700 public void run() {
1701 if (mWorkspace != null &&
1702 mWorkspace.getViewTreeObserver() != null) {
1703 mWorkspace.getViewTreeObserver().
1704 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001706 }
1707 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001708 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001709 }
1710 });
1711 }
1712 clearTypedText();
1713 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 }
1715
Adam Cohen091440a2015-03-18 14:16:05 -07001716 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 mHandler.removeMessages(ADVANCE_MSG);
1718 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1719 mHandler.sendMessageDelayed(msg, delay);
1720 mAutoAdvanceSentTime = System.currentTimeMillis();
1721 }
1722
Adam Cohen091440a2015-03-18 14:16:05 -07001723 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1725 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1726 mAutoAdvanceRunning = autoAdvanceRunning;
1727 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 sendAdvanceMessage(delay);
1730 } else {
1731 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001732 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1734 }
1735 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001736 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 }
1738 }
1739 }
1740
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1742
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (msg.what == ADVANCE_MSG) {
1746 int i = 0;
1747 for (View key: mWidgetsToAdvance.keySet()) {
1748 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001749 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001751 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 public void run() {
1753 ((Advanceable) v).advance();
1754 }
1755 }, delay);
1756 }
1757 i++;
1758 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001759 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001761 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001763 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001764
Winsonc0b52fe2015-09-09 16:38:15 -07001765 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001766 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1768 if (v instanceof Advanceable) {
1769 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001770 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001771 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
1773 }
1774
Winsonc0b52fe2015-09-09 16:38:15 -07001775 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 if (mWidgetsToAdvance.containsKey(hostView)) {
1777 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001778 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 }
1781
Hyunyoung Song3f471442015-04-08 19:01:34 -07001782 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001783 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1784 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
1786
Winson Chunga6945242014-01-08 14:04:34 -08001787 public DragLayer getDragLayer() {
1788 return mDragLayer;
1789 }
1790
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001791 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001792 return mAppsView;
1793 }
1794
Hyunyoung Song3f471442015-04-08 19:01:34 -07001795 public WidgetsContainerView getWidgetsView() {
1796 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001797 }
1798
Winson Chunga6945242014-01-08 14:04:34 -08001799 public Workspace getWorkspace() {
1800 return mWorkspace;
1801 }
1802
1803 public Hotseat getHotseat() {
1804 return mHotseat;
1805 }
1806
Jorim Jaggid017f882014-01-14 17:08:48 -08001807 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001808 return mOverviewPanel;
1809 }
1810
Winson Chung006ee262015-08-03 14:40:11 -07001811 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001812 return mSearchDropTargetBar;
1813 }
1814
Tony Wickham34d2c912015-09-11 09:27:58 -07001815 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1816 return mAppInfoDropTargetBar;
1817 }
1818
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001819 public LauncherAppWidgetHost getAppWidgetHost() {
1820 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Winson Chunga9abd0e2010-10-27 17:18:37 -07001823 public LauncherModel getModel() {
1824 return mModel;
1825 }
1826
Winson Chunga6945242014-01-08 14:04:34 -08001827 protected SharedPreferences getSharedPrefs() {
1828 return mSharedPrefs;
1829 }
1830
Adam Cohen2e6da152015-05-06 11:42:25 -07001831 public DeviceProfile getDeviceProfile() {
1832 return mDeviceProfile;
1833 }
1834
Bjorn Bringertc459e522013-06-07 19:36:01 +01001835 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001836 getWindow().closeAllPanels();
1837
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001838 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001839 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 @Override
1843 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001844 long startTime = 0;
1845 if (DEBUG_RESUME_TIME) {
1846 startTime = System.currentTimeMillis();
1847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 super.onNewIntent(intent);
1849
1850 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001851 Folder openFolder = mWorkspace.getOpenFolder();
1852 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1853 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1854 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1855 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1856 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001857 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001858 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001859
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 if (mWorkspace == null) {
1861 // Can be cases where mWorkspace is null, this prevents a NPE
1862 return;
1863 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001864 // In all these cases, only animate if we're already on home
1865 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001866
Sunny Goyal935fca12015-10-13 10:19:01 -07001867 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 exitSpringLoadedDragMode();
1869
1870 // If we are already on home, then just animate back to the workspace,
1871 // otherwise, just wait until onResume to set the state back to Workspace
1872 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001873 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 } else {
1875 mOnResumeState = State.WORKSPACE;
1876 }
1877
1878 final View v = getWindow().peekDecorView();
1879 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001880 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 INPUT_METHOD_SERVICE);
1882 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1883 }
1884
Winson Chungb745afb2015-03-02 11:51:23 -08001885 // Reset the apps view
1886 if (!alreadyOnHome && mAppsView != null) {
1887 mAppsView.scrollToTop();
1888 }
1889
Hyunyoung Song3f471442015-04-08 19:01:34 -07001890 // Reset the widgets view
1891 if (!alreadyOnHome && mWidgetsView != null) {
1892 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001894
Adam Cohen9211d422014-10-07 18:14:53 -07001895 if (mLauncherCallbacks != null) {
1896 mLauncherCallbacks.onHomeIntent();
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Adam Cohened307df2013-10-02 09:37:31 -07001899
Adam Cohen9211d422014-10-07 18:14:53 -07001900 if (mLauncherCallbacks != null) {
1901 mLauncherCallbacks.onNewIntent(intent);
1902 }
Winson15f8b172015-08-19 11:02:39 -07001903
1904 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1905 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1906 // animation.
1907 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001908 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1909 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001910 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1911 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001912
1913 // We use this flag to suppress noisy callbacks above custom content state
1914 // from onResume.
1915 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001916 mWorkspace.post(new Runnable() {
1917 @Override
1918 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001919 if (mWorkspace != null) {
1920 mWorkspace.moveToDefaultScreen(true);
1921 }
Winson15f8b172015-08-19 11:02:39 -07001922 }
1923 });
1924 }
1925 }
1926
1927 if (DEBUG_RESUME_TIME) {
1928 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1929 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001933 public void onRestoreInstanceState(Bundle state) {
1934 super.onRestoreInstanceState(state);
1935 for (int page: mSynchronouslyBoundPages) {
1936 mWorkspace.restoreInstanceStateForChild(page);
1937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939
1940 @Override
1941 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001943 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1944 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001945 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001946 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Michael Jurka883f55b2010-10-21 15:47:14 -07001948 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001949 // We close any open folder since it will not be re-opened, and we need to make sure
1950 // this state is reflected.
1951 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1952 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001955 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001956 ContentValues itemValues = new ContentValues();
1957 mPendingAddInfo.writeToValues(itemValues);
1958 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001959 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001960 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
Hyunyoung Song3f471442015-04-08 19:01:34 -07001963 // Save the current widgets tray?
1964 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001965
1966 if (mLauncherCallbacks != null) {
1967 mLauncherCallbacks.onSaveInstanceState(outState);
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 @Override
1972 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001974
Winson Chunge7a03942011-08-05 15:05:12 -07001975 // Remove all pending runnables
1976 mHandler.removeMessages(ADVANCE_MSG);
1977 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001978 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001979
Winson Chungcd2b0142011-06-08 16:02:26 -07001980 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001981 // It's possible to receive onDestroy after a new Launcher activity has
1982 // been created. In this case, don't interfere with the new Launcher.
1983 if (mModel.isCurrentCallbacks(this)) {
1984 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001985 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001986 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001991 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993 mAppWidgetHost = null;
1994
1995 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
1997 TextKeyListener.getInstance().release();
1998
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001999 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002000
Michael Jurka2ecf9952012-06-18 12:52:28 -07002001 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002002
2003 if (mLauncherCallbacks != null) {
2004 mLauncherCallbacks.onDestroy();
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Adam Cohena9cf38f2011-05-02 15:36:58 -07002008 public DragController getDragController() {
2009 return mDragController;
2010 }
2011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 @Override
2013 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002014 onStartForResult(requestCode);
2015 super.startActivityForResult(intent, requestCode);
2016 }
2017
2018 @Override
2019 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2020 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2021 onStartForResult(requestCode);
2022 try {
2023 super.startIntentSenderForResult(intent, requestCode,
2024 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2025 } catch (IntentSender.SendIntentException e) {
2026 throw new ActivityNotFoundException();
2027 }
2028 }
2029
2030 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002031 if (requestCode >= 0) {
2032 setWaitingForResult(true);
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
Winson Chung70d72102011-08-12 11:24:35 -07002036 /**
2037 * Indicates that we want global search for this activity by setting the globalSearch
2038 * argument for {@link #startSearch} to true.
2039 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002041 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002043
Karl Rosaen138a0412009-04-23 19:00:21 -07002044 if (initialQuery == null) {
2045 // Use any text typed in the launcher as the initial query
2046 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 if (appSearchData == null) {
2049 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002050 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
Winson Chungadf0c182012-08-23 14:59:07 -07002052 Rect sourceBounds = new Rect();
2053 if (mSearchDropTargetBar != null) {
2054 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2055 }
Romain Guycbb89e42009-06-08 15:52:54 -07002056
Ian Parkinson047036e2014-09-01 15:40:53 +01002057 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002059 if (clearTextImmediately) {
2060 clearTypedText();
2061 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002062
2063 // We need to show the workspace after starting the search
2064 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002065 }
2066
Ian Parkinson047036e2014-09-01 15:40:53 +01002067 /**
2068 * Start a text search.
2069 *
2070 * @return {@code true} if the search will start immediately, so any further keypresses
2071 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2072 * to buffer keypresses.
2073 */
2074 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002076 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2077 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2078 sourceBounds);
2079 }
2080
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002082 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002083 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 }
2085
2086 /**
2087 * Starts the global search activity. This code is a copied from SearchManager
2088 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002091 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2093 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2094 if (globalSearchActivity == null) {
2095 Log.w(TAG, "No global search activity found.");
2096 return;
2097 }
2098 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2099 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2100 intent.setComponent(globalSearchActivity);
2101 // Make sure that we have a Bundle to put source in
2102 if (appSearchData == null) {
2103 appSearchData = new Bundle();
2104 } else {
2105 appSearchData = new Bundle(appSearchData);
2106 }
Adam Cohen9211d422014-10-07 18:14:53 -07002107 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002108 if (!appSearchData.containsKey("source")) {
2109 appSearchData.putString("source", getPackageName());
2110 }
2111 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2112 if (!TextUtils.isEmpty(initialQuery)) {
2113 intent.putExtra(SearchManager.QUERY, initialQuery);
2114 }
2115 if (selectInitialQuery) {
2116 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2117 }
2118 intent.setSourceBounds(sourceBounds);
2119 try {
2120 startActivity(intent);
2121 } catch (ActivityNotFoundException ex) {
2122 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125
Yura4f93ec62014-02-04 14:15:21 +00002126 public boolean isOnCustomContent() {
2127 return mWorkspace.isOnOrMovingToCustomContent();
2128 }
2129
Winson Chung70d72102011-08-12 11:24:35 -07002130 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002131 public boolean onPrepareOptionsMenu(Menu menu) {
2132 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002133 if (mLauncherCallbacks != null) {
2134 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2135 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002136 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002139 @Override
2140 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002141 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002142 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002143 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002144 }
2145
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 public boolean isWorkspaceLocked() {
2147 return mWorkspaceLoading || mWaitingForResult;
2148 }
2149
Adam Cohen517a7f52014-03-01 12:12:59 -08002150 public boolean isWorkspaceLoading() {
2151 return mWorkspaceLoading;
2152 }
2153
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002154 private void setWorkspaceLoading(boolean value) {
2155 boolean isLocked = isWorkspaceLocked();
2156 mWorkspaceLoading = value;
2157 if (isLocked != isWorkspaceLocked()) {
2158 onWorkspaceLockedChanged();
2159 }
2160 }
2161
2162 private void setWaitingForResult(boolean value) {
2163 boolean isLocked = isWorkspaceLocked();
2164 mWaitingForResult = value;
2165 if (isLocked != isWorkspaceLocked()) {
2166 onWorkspaceLockedChanged();
2167 }
2168 }
2169
Adam Cohen9211d422014-10-07 18:14:53 -07002170 protected void onWorkspaceLockedChanged() {
2171 if (mLauncherCallbacks != null) {
2172 mLauncherCallbacks.onWorkspaceLockedChanged();
2173 }
2174 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002175
Michael Jurka0280c3b2010-09-17 15:00:07 -07002176 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002177 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002178 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2180 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002181 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002182 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002183
Tony Wickhama0628cc2015-10-14 15:23:04 -07002184 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002185 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002186 if (LOGD) {
2187 Log.d(TAG, "Adding widget from drop");
2188 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002189 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2190 }
2191
Sunny Goyale6b63a32015-01-16 16:14:29 -08002192 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002193 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2194 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 if (appWidgetInfo.configure != null) {
2196 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002197 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002198
Bjorn Bringert7984c942009-12-09 15:38:25 +00002199 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002200 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2201 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002204 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002205 Runnable onComplete = new Runnable() {
2206 @Override
2207 public void run() {
2208 // Exit spring loaded mode if necessary after adding the widget
2209 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2210 null);
2211 }
2212 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002213 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002215 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
Romain Guycbb89e42009-06-08 15:52:54 -07002218
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002219 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002220 // Close any folders that may be open.
2221 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 mWorkspace.moveToCustomContentScreen(animate);
2223 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002224
2225 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2226 int[] cell, int spanX, int spanY) {
2227 switch (info.itemType) {
2228 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2229 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2230 int span[] = new int[2];
2231 span[0] = spanX;
2232 span[1] = spanY;
2233 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2234 container, screenId, cell, span);
2235 break;
2236 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2237 processShortcutFromDrop(info.componentName, container, screenId, cell);
2238 break;
2239 default:
2240 throw new IllegalStateException("Unknown item type: " + info.itemType);
2241 }
2242 }
2243
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 /**
2245 * Process a shortcut drop.
2246 *
2247 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002251 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2252 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256
2257 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.cellX = cell[0];
2259 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261
2262 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2263 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002264 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 }
2266
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 /**
2268 * Process a widget drop.
2269 *
2270 * @param info The PendingAppWidgetInfo of the widget being added.
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 addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2275 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002279 mPendingAddInfo.minSpanX = info.minSpanX;
2280 mPendingAddInfo.minSpanY = info.minSpanY;
2281
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002283 mPendingAddInfo.cellX = cell[0];
2284 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002286 if (span != null) {
2287 mPendingAddInfo.spanX = span[0];
2288 mPendingAddInfo.spanY = span[1];
2289 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 AppWidgetHostView hostView = info.boundWidget;
2292 int appWidgetId;
2293 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002294 // In the case where we've prebound the widget, we remove it from the DragLayer
2295 if (LOGD) {
2296 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2297 }
2298 getDragLayer().removeView(hostView);
2299
Adam Cohened66b2b2012-01-23 17:28:51 -08002300 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002301 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002302
2303 // Clear the boundWidget so that it doesn't get destroyed.
2304 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002306 // In this case, we either need to start an activity to get permission to bind
2307 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002309 Bundle options = info.bindOptions;
2310
Sunny Goyalffe83f12014-08-14 17:39:34 -07002311 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2312 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002313 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002314 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002316 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2318 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2319 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002320 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2321 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002322 // TODO: we need to make sure that this accounts for the options bundle.
2323 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002324 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2325 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002326 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 }
2328
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002331 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 folderInfo.title = getText(R.string.folder_name);
2333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002335 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2336 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002337 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338
2339 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 FolderIcon newFolder =
2341 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002344 // Force measure the new folder icon
2345 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2346 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002347 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 }
Romain Guycbb89e42009-06-08 15:52:54 -07002349
Winsonc0b52fe2015-09-09 16:38:15 -07002350 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002351 * Unbinds the view for the specified item, and removes the item and all its children.
2352 *
2353 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002354 * @param itemInfo the {@link ItemInfo} for this view.
2355 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002356 */
Winson2949fb52015-09-24 09:56:11 -07002357 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002358 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002359 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002360 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2361 if (folderInfo != null) {
2362 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002363 } else {
2364 mWorkspace.removeWorkspaceItem(v);
2365 }
Winsonc0b52fe2015-09-09 16:38:15 -07002366 if (deleteFromDb) {
2367 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2368 }
2369 } else if (itemInfo instanceof FolderInfo) {
2370 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2371 unbindFolder(folderInfo);
2372 mWorkspace.removeWorkspaceItem(v);
2373 if (deleteFromDb) {
2374 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2375 }
2376 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2377 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002378 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002379 removeWidgetToAutoAdvance(widgetInfo.hostView);
2380 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002381 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002383 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002384
Winsonc0b52fe2015-09-09 16:38:15 -07002385 } else {
2386 return false;
2387 }
2388 return true;
2389 }
2390
2391 /**
2392 * Unbinds any launcher references to the folder.
2393 */
2394 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002395 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 }
2397
Winsonc0b52fe2015-09-09 16:38:15 -07002398 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002400 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002401 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002402 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002403 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002404 // Deleting an app widget ID is a void call but writes to disk before returning
2405 // to the caller...
2406 new AsyncTask<Void, Void, Void>() {
2407 public Void doInBackground(Void ... args) {
2408 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2409 return null;
2410 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002411 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002412 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002413 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002414 }
2415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 @Override
2417 public boolean dispatchKeyEvent(KeyEvent event) {
2418 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2419 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002422 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002423 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002424 dumpState();
2425 return true;
2426 }
2427 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002428 }
2429 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2430 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002431 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 return true;
2433 }
2434 }
2435
2436 return super.dispatchKeyEvent(event);
2437 }
2438
Joe Onorato88ec0992009-11-19 13:16:06 -08002439 @Override
2440 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002441 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2442 return;
2443 }
2444
Sunny Goyal45478022015-06-08 16:52:41 -07002445 if (mDragController.isDragging()) {
2446 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002447 return;
2448 }
2449
Winson Chungb745afb2015-03-02 11:51:23 -08002450 if (isAppsViewVisible()) {
2451 showWorkspace(true);
2452 } else if (isWidgetsViewVisible()) {
2453 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002454 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002455 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002456 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002457 Folder openFolder = mWorkspace.getOpenFolder();
2458 if (openFolder.isEditingName()) {
2459 openFolder.dismissEditingName();
2460 } else {
2461 closeFolder();
2462 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002463 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002464 mWorkspace.exitWidgetResizeMode();
2465
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002466 // Back button is a no-op here, but give at least some feedback for the button press
2467 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002468 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002469 }
2470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002472 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002473 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002474 @Override
2475 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002476 if (mAppWidgetHost != null) {
2477 mAppWidgetHost.startListening();
2478 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002479
2480 // Recreate the QSB, as the widget has been reset.
2481 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002482 }
2483
2484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 * Launches the intent referred by the clicked shortcut.
2486 *
2487 * @param v The view representing the clicked shortcut.
2488 */
2489 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2491 // view has detached (it's possible for this to happen if the view is removed mid touch).
2492 if (v.getWindowToken() == null) {
2493 return;
2494 }
2495
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002496 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 return;
2498 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002499
Adam Cohen1697b792013-09-17 19:08:21 -07002500 if (v instanceof Workspace) {
2501 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002502 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
2504 return;
2505 }
2506
2507 if (v instanceof CellLayout) {
2508 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002509 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2510 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002512 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002516 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002521 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002522 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002524 } else if (tag instanceof AppInfo) {
2525 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002526 } else if (tag instanceof LauncherAppWidgetInfo) {
2527 if (v instanceof PendingAppWidgetHostView) {
2528 onClickPendingWidget((PendingAppWidgetHostView) v);
2529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 }
2531 }
2532
Sunny Goyal70660032015-05-14 00:07:08 -07002533 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002534 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002535 return false;
2536 }
2537
Michael Jurkaaf442092010-06-10 17:01:57 -07002538 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002539 * Event handler for the app widget view which has not fully restored.
2540 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002541 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002542 if (mIsSafeModeEnabled) {
2543 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2544 return;
2545 }
2546
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002548 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002549 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002550 LauncherAppWidgetProviderInfo appWidgetInfo =
2551 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002552 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002553 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002554 mPendingAddInfo.copyFrom(info);
2555 mPendingAddWidgetId = widgetId;
2556
Sunny Goyalffe83f12014-08-14 17:39:34 -07002557 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2558 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002559 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 } else if (info.installProgress < 0) {
2561 // The install has not been queued
2562 final String packageName = info.providerName.getPackageName();
2563 showBrokenAppInstallDialog(packageName,
2564 new DialogInterface.OnClickListener() {
2565 public void onClick(DialogInterface dialog, int id) {
2566 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2567 }
2568 });
2569 } else {
2570 // Download has started.
2571 final String packageName = info.providerName.getPackageName();
2572 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002573 }
2574 }
2575
2576 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002577 * Event handler for the "grid" button that appears on the home screen, which
2578 * enters all apps mode.
2579 *
2580 * @param v The view that was clicked.
2581 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002582 protected void onClickAllAppsButton(View v) {
2583 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002584 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002585 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002586 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002587
2588 if (mLauncherCallbacks != null) {
2589 mLauncherCallbacks.onClickAllAppsButton(v);
2590 }
Winson Chung76648c52015-07-10 14:33:23 -07002591 }
2592 }
2593
2594 protected void onLongClickAllAppsButton(View v) {
2595 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2596 if (!isAppsViewVisible()) {
2597 showAppsView(true /* animated */, false /* resetListToTop */,
2598 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
2600 }
2601
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002602 private void showBrokenAppInstallDialog(final String packageName,
2603 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002604 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002605 .setTitle(R.string.abandoned_promises_title)
2606 .setMessage(R.string.abandoned_promise_explanation)
2607 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2608 .setNeutralButton(R.string.abandoned_clean_this,
2609 new DialogInterface.OnClickListener() {
2610 public void onClick(DialogInterface dialog, int id) {
2611 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2612 mWorkspace.removeAbandonedPromise(packageName, user);
2613 }
2614 })
2615 .create().show();
2616 return;
2617 }
2618
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 /**
2620 * Event handler for an app shortcut click.
2621 *
2622 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2623 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2626 Object tag = v.getTag();
2627 if (!(tag instanceof ShortcutInfo)) {
2628 throw new IllegalArgumentException("Input must be a Shortcut");
2629 }
2630
2631 // Open shortcut
2632 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633
2634 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002635 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2636 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002637 // Launch activity anyway, framework will tell the user why the app is suspended.
2638 } else {
2639 int error = R.string.activity_not_available;
2640 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2641 error = R.string.safemode_shortcut_error;
2642 }
2643 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2644 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002645 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002646 }
2647
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002649 if ((v instanceof BubbleTextView)
2650 && shortcut.isPromise()
2651 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002652 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002653 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 new DialogInterface.OnClickListener() {
2655 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002656 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 return;
2660 }
2661
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002662 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002663 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002664
2665 if (mLauncherCallbacks != null) {
2666 mLauncherCallbacks.onClickAppShortcut(v);
2667 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 }
2669
Adam Cohen091440a2015-03-18 14:16:05 -07002670 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002672 final ShortcutInfo shortcut;
2673 final Intent intent;
2674 if (tag instanceof ShortcutInfo) {
2675 shortcut = (ShortcutInfo) tag;
2676 intent = shortcut.intent;
2677 int[] pos = new int[2];
2678 v.getLocationOnScreen(pos);
2679 intent.setSourceBounds(new Rect(pos[0], pos[1],
2680 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002681
Sunny Goyal508da152014-08-14 10:53:27 -07002682 } else if (tag instanceof AppInfo) {
2683 shortcut = null;
2684 intent = ((AppInfo) tag).intent;
2685 } else {
2686 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2687 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688
2689 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002690 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691
2692 if (success && v instanceof BubbleTextView) {
2693 mWaitingForResume = (BubbleTextView) v;
2694 mWaitingForResume.setStayPressed(true);
2695 }
2696 }
2697
2698 /**
2699 * Event handler for a folder icon click.
2700 *
2701 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2702 */
2703 protected void onClickFolderIcon(View v) {
2704 if (LOGD) Log.d(TAG, "onClickFolder");
2705 if (!(v instanceof FolderIcon)){
2706 throw new IllegalArgumentException("Input must be a FolderIcon");
2707 }
2708
2709 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002710 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002712 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 }
Adam Cohen9211d422014-10-07 18:14:53 -07002714
2715 if (mLauncherCallbacks != null) {
2716 mLauncherCallbacks.onClickFolderIcon(v);
2717 }
Michael Jurka5130e402011-10-13 04:55:35 -07002718 }
2719
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002720 /**
2721 * Event handler for the (Add) Widgets button that appears after a long press
2722 * on the home screen.
2723 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002724 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002726 if (mIsSafeModeEnabled) {
2727 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2728 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002729 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002730 if (mLauncherCallbacks != null) {
2731 mLauncherCallbacks.onClickAddWidgetButton(view);
2732 }
Adam Cohen9211d422014-10-07 18:14:53 -07002733 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002734 }
2735
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 /**
2737 * Event handler for the wallpaper picker button that appears after a long press
2738 * on the home screen.
2739 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002740 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002742 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2743 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2744 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal5abb9f82016-02-09 15:22:42 -08002745 .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002746 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002747
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickWallpaperPicker(v);
2750 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002751 }
2752
2753 /**
2754 * Event handler for a click on the settings button that appears after a long press
2755 * on the home screen.
2756 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002757 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002759 if (mLauncherCallbacks != null) {
2760 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002761 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002762 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002763 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002764 }
2765
Adam Cohen61f560d2013-09-30 15:58:20 -07002766 public View.OnTouchListener getHapticFeedbackTouchListener() {
2767 if (mHapticFeedbackTouchListener == null) {
2768 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002769 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002770 @Override
2771 public boolean onTouch(View v, MotionEvent event) {
2772 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2773 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2774 }
2775 return false;
2776 }
2777 };
2778 }
2779 return mHapticFeedbackTouchListener;
2780 }
2781
Adam Cohen9211d422014-10-07 18:14:53 -07002782 public void onDragStarted(View view) {
2783 if (isOnCustomContent()) {
2784 // Custom content screen doesn't participate in drag and drop. If on custom
2785 // content screen, move to default.
2786 moveWorkspaceToDefaultScreen();
2787 }
2788
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onDragStarted(view);
2791 }
2792 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002793
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002794 /**
2795 * Called when the user stops interacting with the launcher.
2796 * This implies that the user is now on the homescreen and is not doing housekeeping.
2797 */
Adam Cohen9211d422014-10-07 18:14:53 -07002798 protected void onInteractionEnd() {
2799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onInteractionEnd();
2801 }
2802 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002803
2804 /**
2805 * Called when the user starts interacting with the launcher.
2806 * The possible interactions are:
2807 * - open all apps
2808 * - reorder an app shortcut, or a widget
2809 * - open the overview mode.
2810 * This is a good time to stop doing things that only make sense
2811 * when the user is on the homescreen and not doing housekeeping.
2812 */
Adam Cohen9211d422014-10-07 18:14:53 -07002813 protected void onInteractionBegin() {
2814 if (mLauncherCallbacks != null) {
2815 mLauncherCallbacks.onInteractionBegin();
2816 }
2817 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002818
Winson Chungcd99cd32015-04-29 11:03:24 -07002819 /** Updates the interaction state. */
2820 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002821 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002822 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002823 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2824 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002825 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002826 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002827 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 onInteractionEnd();
2829 }
2830 }
2831
Kenny Guyf07af7b2014-07-31 11:39:16 +01002832 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002833 try {
2834 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002835 launcherApps.showAppDetailsForProfile(componentName, user);
2836 } catch (SecurityException e) {
2837 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2838 Log.e(TAG, "Launcher does not have permission to launch settings");
2839 } catch (ActivityNotFoundException e) {
2840 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2841 Log.e(TAG, "Unable to launch settings");
2842 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002843 }
2844
Michael Jurka1e2f4652013-07-08 18:03:46 -07002845 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002846 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2847 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002848 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002849 // System applications cannot be installed. For now, show a toast explaining that.
2850 // We may give them the option of disabling apps this way.
2851 int messageId = R.string.uninstall_system_app_text;
2852 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002853 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002854 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002855 String packageName = componentName.getPackageName();
2856 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002857 Intent intent = new Intent(
2858 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002859 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2860 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002861 if (user != null) {
2862 user.addToIntent(intent, Intent.EXTRA_USER);
2863 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002864 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002865 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002866 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002867 }
2868
Winson Chung8f1eff72015-05-28 17:33:40 -07002869 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002870 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002872 // Only launch using the new animation if the shortcut has not opted out (this is a
2873 // private contract between launcher and may be ignored in the future).
2874 boolean useLaunchAnimation = (v != null) &&
2875 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002876 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2877 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002878
Kenny Guy1317e2d2014-05-08 18:52:50 +01002879 UserHandleCompat user = null;
2880 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2881 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2882 user = userManager.getUserForSerialNumber(serialNumber);
2883 }
2884
2885 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002886 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002887 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002888 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002889 int left = 0, top = 0;
2890 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2891 if (v instanceof TextView) {
2892 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002893 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2894 if (icon != null) {
2895 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002896 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002897 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002898 width = bounds.width();
2899 height = bounds.height();
2900 }
2901 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002902 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2903 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002904 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002905 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002906 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002907 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002908 // On L devices, we use the device default slide-up transition.
2909 // On L MR1 devices, we a custom version of the slide-up transition which
2910 // doesn't have the delay present in the device default.
2911 opts = ActivityOptions.makeCustomAnimation(this,
2912 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002913 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002914 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002915 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002916
2917 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2918 // Could be launching some bookkeeping activity
2919 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002920 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002921 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002922 launcherApps.startActivityForProfile(intent.getComponent(), user,
2923 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002924 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002925 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002927 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2928 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2929 // corresponding permission. Show the appropriate permission prompt if that
2930 // is the case.
2931 if (intent.getComponent() == null
2932 && Intent.ACTION_CALL.equals(intent.getAction())
2933 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2934 PackageManager.PERMISSION_GRANTED) {
2935 // TODO: Rename sPendingAddItem to a generic name.
2936 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2937 0, (ItemInfo) tag);
2938 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2939 REQUEST_PERMISSION_CALL_PHONE);
2940 return false;
2941 }
2942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002944 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002946 "or use the exported attribute for this activity. "
2947 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002949 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002951
Winson Chungbedf9232015-07-10 12:38:30 -07002952 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002953 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002954 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2955 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2956 return false;
2957 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002958 try {
2959 success = startActivity(v, intent, tag);
2960 } catch (ActivityNotFoundException e) {
2961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2962 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2963 }
2964 return success;
2965 }
2966
Adam Cohen268c4752012-06-06 17:47:33 -07002967 /**
2968 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2969 * in the DragLayer in the exact absolute location of the original FolderIcon.
2970 */
2971 private void copyFolderIconToImage(FolderIcon fi) {
2972 final int width = fi.getMeasuredWidth();
2973 final int height = fi.getMeasuredHeight();
2974
2975 // Lazy load ImageView, Bitmap and Canvas
2976 if (mFolderIconImageView == null) {
2977 mFolderIconImageView = new ImageView(this);
2978 }
2979 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2980 mFolderIconBitmap.getHeight() != height) {
2981 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2982 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2983 }
2984
2985 DragLayer.LayoutParams lp;
2986 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2987 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2988 } else {
2989 lp = new DragLayer.LayoutParams(width, height);
2990 }
2991
Adam Cohen307fe232012-08-16 17:55:58 -07002992 // The layout from which the folder is being opened may be scaled, adjust the starting
2993 // view size by this scale factor.
2994 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002995 lp.customPosition = true;
2996 lp.x = mRectForFolderAnimation.left;
2997 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002998 lp.width = (int) (scale * width);
2999 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003000
3001 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3002 fi.draw(mFolderIconCanvas);
3003 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003004 if (fi.getFolder() != null) {
3005 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3006 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003007 }
Adam Cohen268c4752012-06-06 17:47:33 -07003008 // Just in case this image view is still in the drag layer from a previous animation,
3009 // we remove it and re-add it.
3010 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3011 mDragLayer.removeView(mFolderIconImageView);
3012 }
3013 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003014 if (fi.getFolder() != null) {
3015 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003016 }
Adam Cohen268c4752012-06-06 17:47:33 -07003017 }
3018
Sunny Goyal08442b82015-10-21 17:15:08 -07003019 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003020 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003021 FolderInfo info = (FolderInfo) fi.getTag();
3022 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3023 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003024 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3025 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003026 }
3027
Adam Cohen268c4752012-06-06 17:47:33 -07003028 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3029 copyFolderIconToImage(fi);
3030 fi.setVisibility(View.INVISIBLE);
3031
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003032 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3033 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003034 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003035 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3036 }
3037 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003038 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003039 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003040 oa.end();
3041 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003042 }
3043
Sunny Goyal935fca12015-10-13 10:19:01 -07003044 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003045 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003046 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003047
Adam Cohen268c4752012-06-06 17:47:33 -07003048 // We remove and re-draw the FolderIcon in-case it has changed
3049 mDragLayer.removeView(mFolderIconImageView);
3050 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003051
3052 if (cl != null) {
3053 cl.clearFolderLeaveBehind();
3054 }
3055
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003056 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003057 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003058 oa.addListener(new AnimatorListenerAdapter() {
3059 @Override
3060 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003061 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003062 // Remove the ImageView copy of the FolderIcon and make the original visible.
3063 mDragLayer.removeView(mFolderIconImageView);
3064 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003065 }
3066 }
3067 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003069 if (!animate) {
3070 oa.end();
3071 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003072 }
3073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003075 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 * is animated relative to the specified View. If the View is null, no animation
3077 * is played.
3078 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003079 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003081 public void openFolder(FolderIcon folderIcon, boolean animate) {
3082 animate &= !Utilities.isPowerSaverOn(this);
3083
Adam Cohenfb91f302012-06-11 15:45:18 -07003084 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003085 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3086 if (openFolder != null && openFolder != folder) {
3087 // Close any open folder before opening a folder.
3088 closeFolder();
3089 }
3090
Adam Cohena9cf38f2011-05-02 15:36:58 -07003091 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092
Adam Cohena9cf38f2011-05-02 15:36:58 -07003093 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094
Sunny Goyalf4066152015-04-15 09:42:19 -07003095 // While the folder is open, the position of the icon cannot change.
3096 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3097
Adam Cohen4554ee12011-08-03 16:13:21 -07003098 // Just verify that the folder hasn't already been added to the DragLayer.
3099 // There was a one-off crash where the folder had a parent already.
3100 if (folder.getParent() == null) {
3101 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003102 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 } else {
3104 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3105 folder.getParent() + ").");
3106 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003107 if (animate) {
3108 folder.animateOpen();
3109 } else {
3110 folder.open();
3111 }
3112 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003113
3114 // Notify the accessibility manager that this folder "window" has appeared and occluded
3115 // the workspace items
3116 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3117 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 }
3119
3120 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 closeFolder(true);
3122 }
3123
3124 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003125 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003127 if (folder.isEditingName()) {
3128 folder.dismissEditingName();
3129 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003130 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003131 }
3132 }
3133
Sunny Goyal935fca12015-10-13 10:19:01 -07003134 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003135 animate &= !Utilities.isPowerSaverOn(this);
3136
Adam Cohen4554ee12011-08-03 16:13:21 -07003137 folder.getInfo().opened = false;
3138
3139 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3140 if (parent != null) {
3141 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003142 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003143 if (fi != null) {
3144 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3145 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003146 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003147 if (animate) {
3148 folder.animateClosed();
3149 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003150 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003151 }
Winson Chung83ca4802013-04-12 15:10:52 -07003152
Sunny Goyal935fca12015-10-13 10:19:01 -07003153 // Notify the accessibility manager that this folder "window" has disappeared and no
3154 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003155 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 }
3157
Tony Wickhamdadb3042016-02-24 11:07:00 -08003158 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003160 if (!isDraggingEnabled()) return false;
3161 if (isWorkspaceLocked()) return false;
3162 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003163
Winson Chung76648c52015-07-10 14:33:23 -07003164 if (v == mAllAppsButton) {
3165 onLongClickAllAppsButton(v);
3166 return true;
3167 }
3168
Adam Cohen1697b792013-09-17 19:08:21 -07003169 if (v instanceof Workspace) {
3170 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003171 if (!mWorkspace.isTouchActive()) {
3172 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003173 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3174 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3175 return true;
3176 } else {
3177 return false;
3178 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003179 } else {
3180 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003181 }
Adam Cohen1697b792013-09-17 19:08:21 -07003182 }
3183
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003184 CellLayout.CellInfo longClickCellInfo = null;
3185 View itemUnderLongClick = null;
3186 if (v.getTag() instanceof ItemInfo) {
3187 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003188 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003189 itemUnderLongClick = longClickCellInfo.cell;
3190 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 }
3192
Winson Chung3d503fb2011-07-13 17:25:49 -07003193 // The hotseat touch handling does not go through Workspace, and we always allow long press
3194 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003195 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003196 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003197 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003198 // User long pressed on empty space
3199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003201 if (mWorkspace.isInOverviewMode()) {
3202 mWorkspace.startReordering(v);
3203 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003204 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003207 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3208 mHotseat.getOrderInHotseat(
3209 longClickCellInfo.cellX,
3210 longClickCellInfo.cellY));
3211 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003213 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 }
3215 }
3216 }
3217 return true;
3218 }
3219
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 boolean isHotseatLayout(View layout) {
3221 return mHotseat != null && layout != null &&
3222 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3223 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003224
Winson Chung3d503fb2011-07-13 17:25:49 -07003225 /**
3226 * Returns the CellLayout of the specified container at the specified screen.
3227 */
Sunny Goyal92820592015-03-02 11:31:35 -08003228 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003229 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3230 if (mHotseat != null) {
3231 return mHotseat.getLayout();
3232 } else {
3233 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003234 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003236 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003237 }
3238 }
3239
Winson Chungb745afb2015-03-02 11:51:23 -08003240 /**
3241 * For overridden classes.
3242 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003243 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003244 return isAppsViewVisible();
3245 }
3246
3247 public boolean isAppsViewVisible() {
3248 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3249 }
3250
3251 public boolean isWidgetsViewVisible() {
3252 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003253 }
3254
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003255 private void setWorkspaceBackground(int background) {
3256 switch (background) {
3257 case WORKSPACE_BACKGROUND_TRANSPARENT:
3258 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3259 break;
3260 case WORKSPACE_BACKGROUND_BLACK:
3261 getWindow().setBackgroundDrawable(null);
3262 break;
3263 default:
3264 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3265 }
Craig Mautner360310b2012-10-26 15:13:08 -07003266 }
3267
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003268 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003269 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3270 int curflags = getWindow().getAttributes().flags
3271 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3272 if (wpflags != curflags) {
3273 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3274 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003275 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003276 }
3277
Michael Jurkae326f182011-11-21 14:05:46 -08003278 @Override
3279 public void onTrimMemory(int level) {
3280 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003281 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3282 // The widget preview db can result in holding onto over
3283 // 3MB of memory for caching which isn't necessary.
3284 SQLiteDatabase.releaseMemory();
3285
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003286 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003287 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003288 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003289 if (mLauncherCallbacks != null) {
3290 mLauncherCallbacks.onTrimMemory(level);
3291 }
Michael Jurkae326f182011-11-21 14:05:46 -08003292 }
3293
Winson5c6bdbb2015-09-03 11:36:19 -07003294 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003295 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003296 }
3297
Winson5c6bdbb2015-09-03 11:36:19 -07003298 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003299 boolean changed = mState != State.WORKSPACE ||
3300 mWorkspace.getState() != Workspace.State.NORMAL;
3301 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003302 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003303 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003304 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003305
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306 // Set focus to the AppsCustomize button
3307 if (mAllAppsButton != null) {
3308 mAllAppsButton.requestFocus();
3309 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003310 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003311
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003312 // Change the state *after* we've called all the transition code
3313 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003314
Winson Chung5fb63472011-02-02 17:03:37 -08003315 // Resume the auto-advance of widgets
3316 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003317 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003318
Winson Chung0f785722015-04-08 10:27:49 -07003319 if (changed) {
3320 // Send an accessibility event to announce the context change
3321 getWindow().getDecorView()
3322 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003323 }
Winson5c6bdbb2015-09-03 11:36:19 -07003324 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003325 }
3326
Winsone9f27272015-10-13 10:47:51 -07003327 /**
3328 * Shows the overview button.
3329 */
Adam Cohened307df2013-10-02 09:37:31 -07003330 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003331 showOverviewMode(animated, false);
3332 }
3333
3334 /**
3335 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3336 * onto one of the overview panel buttons.
3337 */
3338 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3339 Runnable postAnimRunnable = null;
3340 if (requestButtonFocus) {
3341 postAnimRunnable = new Runnable() {
3342 @Override
3343 public void run() {
3344 // Hitting the menu button when in touch mode does not trigger touch mode to
3345 // be disabled, so if requested, force focus on one of the overview panel
3346 // buttons.
3347 mOverviewPanel.requestFocusFromTouch();
3348 }
3349 };
3350 }
Adam Cohened307df2013-10-02 09:37:31 -07003351 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003352 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003353 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003354 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003355 }
3356
Winson Chungb745afb2015-03-02 11:51:23 -08003357 /**
3358 * Shows the apps view.
3359 */
Winson Chung76648c52015-07-10 14:33:23 -07003360 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3361 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003362 if (resetListToTop) {
3363 mAppsView.scrollToTop();
3364 }
Winson Chung4ac30062015-05-08 17:34:17 -07003365 if (updatePredictedApps) {
3366 tryAndUpdatePredictedApps();
3367 }
Winson Chung76648c52015-07-10 14:33:23 -07003368 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003369 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370
Winson Chungb745afb2015-03-02 11:51:23 -08003371 /**
3372 * Shows the widgets view.
3373 */
3374 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003375 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003376 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003377 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003378 }
Winson Chung76648c52015-07-10 14:33:23 -07003379 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003380
3381 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003382 @Override
3383 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003384 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003385 }
3386 });
Winson Chungb745afb2015-03-02 11:51:23 -08003387 }
3388
3389 /**
3390 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003391 *
3392 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003393 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003394 // TODO: calling method should use the return value so that when {@code false} is returned
3395 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003396 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003397 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3398 mState != State.WIDGETS_SPRING_LOADED) {
3399 return false;
3400 }
3401 if (toState != State.APPS && toState != State.WIDGETS) {
3402 return false;
3403 }
Winson Chungb745afb2015-03-02 11:51:23 -08003404
3405 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003406 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3407 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003408 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003409 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003410 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003413 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003414
3415 // Pause the auto-advance of widgets until we are out of AllApps
3416 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003417 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003418 closeFolder();
3419
3420 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003421 getWindow().getDecorView()
3422 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003423 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424 }
3425
Winson Chungcd99cd32015-04-29 11:03:24 -07003426 /**
3427 * Updates the workspace and interaction state on state change, and return the animation to this
3428 * new state.
3429 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003430 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003431 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003432 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003433 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003434 updateInteraction(fromState, toState);
3435 return anim;
3436 }
3437
Jun Mukaif0033da2015-08-04 18:34:30 -07003438 public void onLauncherClingShown() {
3439 // When a launcher cling appears, it should cover the underlying layers, so their focus
3440 // should be blocked.
3441 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3442 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3443 }
3444 }
3445
3446 public void onLauncherClingDismissed() {
3447 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3448 }
3449
Hyunyoung Song3f471442015-04-08 19:01:34 -07003450 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003451 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003452 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003453 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003454 }
Winson Chungb745afb2015-03-02 11:51:23 -08003455
Winson Chung006ee262015-08-03 14:40:11 -07003456 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003457 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003458 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003459
3460 if (isAppsViewVisible()) {
3461 mState = State.APPS_SPRING_LOADED;
3462 } else if (isWidgetsViewVisible()) {
3463 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003464 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003465 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003466 } else {
3467 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003468 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003469 }
Adam Cohen7777d962011-08-18 18:58:38 -07003470
Hyunyoung Song3f471442015-04-08 19:01:34 -07003471 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003472 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003473 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003474
Winson Chunge7a03942011-08-05 15:05:12 -07003475 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003476 @Override
3477 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003478 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003479 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3480 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003481 // Before we show workspace, hide all apps again because
3482 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3483 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003484 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003485 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003486 } else {
3487 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003488 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003489 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003490 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003491 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003492
Tony Wickhame0c33232016-02-08 11:37:04 -08003493 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003494 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3495 || mState == State.WIDGETS_SPRING_LOADED;
3496 }
3497
Michael Jurkad3ef3062010-11-23 16:23:58 -08003498 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003499 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003500 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003501 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003502 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003503 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003504 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3505 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003506 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003507 }
3508
Winson Chung4ac30062015-05-08 17:34:17 -07003509 /**
3510 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3511 * resumed.
3512 */
3513 private void tryAndUpdatePredictedApps() {
3514 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003515 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003516 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003517 mAppsView.setPredictedApps(apps);
3518 }
3519 }
3520 }
3521
Michael Jurkab3e22d92011-10-31 15:58:33 -07003522 void lockAllApps() {
3523 // TODO
3524 }
3525
3526 void unlockAllApps() {
3527 // TODO
3528 }
3529
Winsonf768d932015-09-25 16:12:35 -07003530 public boolean launcherCallbacksProvidesSearch() {
3531 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3532 }
3533
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003534 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003535 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003536 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003537 }
3538
Adam Cohen24ce0b32014-01-14 16:18:14 -08003539 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003540 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3541 if (searchProvider == null) {
3542 return null;
3543 }
3544
3545 Bundle opts = new Bundle();
3546 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003547 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548
Tony Wickham3a3517f2015-10-06 11:27:04 -07003549 // Determine the min and max dimensions of the widget.
3550 LauncherAppState app = LauncherAppState.getInstance();
3551 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3552 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3553 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003554 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3555 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003556 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003557 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003558 int minWidth = maxWidth;
3559 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003560 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3561 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3562 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3563 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3564 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003565 }
3566 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3567 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3568 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3569 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003570 if (LOGD) {
3571 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3572 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3573 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003574
Tony Wickham775455c2015-10-16 09:49:32 -07003575 if (mLauncherCallbacks != null) {
3576 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3577 }
3578
Sunny Goyalf7258242015-10-19 16:59:07 -07003579 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003580 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003581 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003582 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003583 || (widgetInfo == null)
3584 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003585 // A valid widget is not already bound.
3586 if (widgetId > -1) {
3587 mAppWidgetHost.deleteAppWidgetId(widgetId);
3588 widgetId = -1;
3589 }
3590
3591 // Try to bind a new widget
3592 widgetId = mAppWidgetHost.allocateAppWidgetId();
3593
3594 if (!AppWidgetManagerCompat.getInstance(this)
3595 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3596 mAppWidgetHost.deleteAppWidgetId(widgetId);
3597 widgetId = -1;
3598 }
3599
Sunny Goyalf7258242015-10-19 16:59:07 -07003600 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003601 .putInt(QSB_WIDGET_ID, widgetId)
3602 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003603 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003604 }
3605
Sunny Goyal8d595092015-07-06 12:22:14 -07003606 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003607 if (widgetId != -1) {
3608 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003609 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003610 mQsb.updateAppWidgetOptions(opts);
3611 mQsb.setPadding(0, 0, 0, 0);
3612 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003613 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003614 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003615 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003616 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003617 }
3618
Sunny Goyal22235bc2015-04-03 09:23:43 -07003619 private void reinflateQSBIfNecessary() {
3620 if (mQsb instanceof LauncherAppWidgetHostView &&
3621 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3622 mSearchDropTargetBar.removeView(mQsb);
3623 mQsb = null;
3624 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3625 }
3626 }
3627
Michael Jurkad7c28052012-04-27 15:43:36 -07003628 @Override
3629 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003630 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003631 final List<CharSequence> text = event.getText();
3632 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003633 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003634 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003635 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003636 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003637 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003638 } else if (mWorkspace != null) {
3639 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003640 } else {
3641 text.add(getString(R.string.all_apps_home_button_label));
3642 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003643 return result;
3644 }
3645
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003646 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003647 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003648 */
Adam Cohen091440a2015-03-18 14:16:05 -07003649 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003650 @Override
3651 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003652 closeSystemDialogs();
3653 }
3654 }
3655
3656 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003657 * If the activity is currently paused, signal that we need to run the passed Runnable
3658 * in onResume.
3659 *
3660 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003661 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3662 * wrong when we're not running, and if the activity comes back to what the configuration was
3663 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003664 *
3665 * Implementation of the method from LauncherModel.Callbacks.
3666 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003667 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003669 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003670 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003671 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003672 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003673 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003674 }
3675 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003676 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 return true;
3678 } else {
3679 return false;
3680 }
3681 }
3682
Michael Jurkac402cd92013-05-20 15:49:32 +02003683 private boolean waitUntilResume(Runnable run) {
3684 return waitUntilResume(run, false);
3685 }
3686
Michael Jurka1e2f4652013-07-08 18:03:46 -07003687 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003688 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003689 }
3690
Michael Jurka7607c2f2013-04-03 14:33:19 -07003691 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003692 * If the activity is currently paused, signal that we need to re-run the loader
3693 * in onResume.
3694 *
3695 * This needs to be called from incoming places where resources might have been loaded
3696 * while we are paused. That is becaues the Configuration might be wrong
3697 * when we're not running, and if it comes back to what it was when we
3698 * were paused, we are not restarted.
3699 *
3700 * Implementation of the method from LauncherModel.Callbacks.
3701 *
3702 * @return true if we are currently paused. The caller might be able to
3703 * skip some work in that case since we will come back again.
3704 */
3705 public boolean setLoadOnResume() {
3706 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003707 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003708 mOnResumeNeedsLoad = true;
3709 return true;
3710 } else {
3711 return false;
3712 }
3713 }
3714
3715 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003716 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003717 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003719 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003720 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003721 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003722 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003725
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003727 * Clear any pending bind callbacks. This is called when is loader is planning to
3728 * perform a full rebind from scratch.
3729 */
3730 @Override
3731 public void clearPendingBinds() {
3732 mBindOnResumeCallbacks.clear();
3733 if (mPendingExecutor != null) {
3734 mPendingExecutor.markCompleted();
3735 mPendingExecutor = null;
3736 }
3737 }
3738
3739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 * Refreshes the shortcuts shown on the workspace.
3741 *
3742 * Implementation of the method from LauncherModel.Callbacks.
3743 */
3744 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003745 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003746
Winson Chungd64d1762013-08-20 14:37:16 -07003747 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003748 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003749 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003750
Michael Jurka05bf644e2011-11-30 20:28:53 -08003751 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003752 if (mHotseat != null) {
3753 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754 }
3755 }
3756
Adam Cohendcd297f2013-06-18 13:13:40 -07003757 @Override
3758 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003759 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003760
Adam Cohen5084cba2013-09-03 12:01:16 -07003761 // If there are no screens, we need to have an empty screen
3762 if (orderedScreenIds.size() == 0) {
3763 mWorkspace.addExtraEmptyScreen();
3764 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003765
3766 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003767 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003768 if (hasCustomContentToLeft()) {
3769 mWorkspace.createCustomContentContainer();
3770 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003771 }
Winson Chung64359a52013-07-08 17:17:08 -07003772 }
3773
3774 @Override
3775 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003776 int count = orderedScreenIds.size();
3777 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003778 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003779 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 }
3781
Winson Chungd64d1762013-08-20 14:37:16 -07003782 public void bindAppsAdded(final ArrayList<Long> newScreens,
3783 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003784 final ArrayList<ItemInfo> addAnimated,
3785 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003786 Runnable r = new Runnable() {
3787 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003788 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003789 }
3790 };
3791 if (waitUntilResume(r)) {
3792 return;
3793 }
3794
Winson Chungd64d1762013-08-20 14:37:16 -07003795 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003796 if (newScreens != null) {
3797 bindAddScreens(newScreens);
3798 }
Winson Chungd64d1762013-08-20 14:37:16 -07003799
3800 // We add the items without animation on non-visible pages, and with
3801 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003802 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 bindItems(addNotAnimated, 0,
3804 addNotAnimated.size(), false);
3805 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003806 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 bindItems(addAnimated, 0,
3808 addAnimated.size(), true);
3809 }
Winson Chungc58497e2013-09-03 17:48:37 -07003810
Winson Chung87412982013-10-03 18:34:14 -07003811 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003812 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003813
Winson Chungb745afb2015-03-02 11:51:23 -08003814 if (addedApps != null && mAppsView != null) {
3815 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003816 }
Winson Chungd64d1762013-08-20 14:37:16 -07003817 }
3818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003819 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 * Bind the items start-end from the list.
3821 *
3822 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003823 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003824 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003825 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3826 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003827 Runnable r = new Runnable() {
3828 public void run() {
3829 bindItems(shortcuts, start, end, forceAnimateIcons);
3830 }
3831 };
3832 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003833 return;
3834 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003835
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003837 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3838 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003839 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003840 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003841 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003842 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003844
3845 // Short circuit if we are loading dock items for a configuration which has no dock
3846 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3847 mHotseat == null) {
3848 continue;
3849 }
3850
Sunny Goyal639e9062015-08-19 19:17:06 -07003851 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 switch (item.itemType) {
3853 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3854 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003856 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003857
Winson Chung64359a52013-07-08 17:17:08 -07003858 /*
3859 * TODO: FIX collision case
3860 */
Winson Chungce376632013-07-11 16:12:41 -07003861 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3862 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3863 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003864 View v = cl.getChildAt(item.cellX, item.cellY);
3865 Object tag = v.getTag();
3866 String desc = "Collision while binding workspace item: " + item
3867 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003868 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003869 throw (new RuntimeException(desc));
3870 } else {
3871 Log.d(TAG, desc);
3872 }
Winson Chungce376632013-07-11 16:12:41 -07003873 }
Winson Chung64359a52013-07-08 17:17:08 -07003874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003876 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003877 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003878 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003879 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003880 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003881 default:
3882 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003883 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003884
3885 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3886 item.cellY, 1, 1);
3887 if (animateIcons) {
3888 // Animate all the applications up now
3889 view.setAlpha(0f);
3890 view.setScaleX(0f);
3891 view.setScaleY(0f);
3892 bounceAnims.add(createNewAppBounceAnimation(view, i));
3893 newShortcutsScreenId = item.screenId;
3894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003895 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003896
Winson Chung997a9232013-07-24 15:33:46 -07003897 if (animateIcons) {
3898 // Animate to the correct page
3899 if (newShortcutsScreenId > -1) {
3900 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003901 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003902 final Runnable startBounceAnimRunnable = new Runnable() {
3903 public void run() {
3904 anim.playTogether(bounceAnims);
3905 anim.start();
3906 }
3907 };
Winson Chung997a9232013-07-24 15:33:46 -07003908 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003909 // We post the animation slightly delayed to prevent slowdowns
3910 // when we are loading right after we return to launcher.
3911 mWorkspace.postDelayed(new Runnable() {
3912 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003913 if (mWorkspace != null) {
3914 mWorkspace.snapToPage(newScreenIndex);
3915 mWorkspace.postDelayed(startBounceAnimRunnable,
3916 NEW_APPS_ANIMATION_DELAY);
3917 }
Winson Chung94d67682013-09-25 16:29:40 -07003918 }
3919 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003920 } else {
3921 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003922 }
3923 }
Winson Chung64359a52013-07-08 17:17:08 -07003924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
3927
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 /**
3929 * Implementation of the method from LauncherModel.Callbacks.
3930 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003931 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003932 Runnable r = new Runnable() {
3933 public void run() {
3934 bindFolders(folders);
3935 }
3936 };
3937 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003938 return;
3939 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003940 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003943 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3944 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3945 view.updateIcon(mIconCache);
3946 item.hostView = view;
3947 item.hostView.updateAppWidget(null);
3948 item.hostView.setOnClickListener(this);
3949 addAppWidgetToWorkspace(item, null, false);
3950 mWorkspace.requestLayout();
3951 }
3952
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 /**
3954 * Add the views for a widget to the workspace.
3955 *
3956 * Implementation of the method from LauncherModel.Callbacks.
3957 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003958 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003959 Runnable r = new Runnable() {
3960 public void run() {
3961 bindAppWidget(item);
3962 }
3963 };
3964 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 return;
3966 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003967
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003968 if (mIsSafeModeEnabled) {
3969 bindSafeModeWidget(item);
3970 return;
3971 }
3972
Daniel Sandler843e8602010-06-07 14:59:01 -04003973 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3974 if (DEBUG_WIDGETS) {
3975 Log.d(TAG, "bindAppWidget: " + item);
3976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003978 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003979
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003980 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3981 // If the provider is not ready, bind as a pending widget.
3982 appWidgetInfo = null;
3983 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3984 // The widget id is not valid. Try to find the widget based on the provider info.
3985 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3986 } else {
3987 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3988 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003989
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003990 // If the provider is ready, but the width is not yet restored, try to restore it.
3991 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3992 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003993 if (appWidgetInfo == null) {
3994 if (DEBUG_WIDGETS) {
3995 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3996 + " belongs to component " + item.providerName
3997 + ", as the povider is null");
3998 }
3999 LauncherModel.deleteItemFromDatabase(this, item);
4000 return;
4001 }
Sunny Goyalff572272014-07-23 13:58:07 -07004002
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004003 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004004 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004005 // Note: This assumes that the id remap broadcast is received before this step.
4006 // If that is not the case, the id remap will be ignored and user may see the
4007 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08004008 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004009 pendingInfo.spanX = item.spanX;
4010 pendingInfo.spanY = item.spanY;
4011 pendingInfo.minSpanX = item.minSpanX;
4012 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004013 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004014
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004015 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4016 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4017 newWidgetId, appWidgetInfo, options);
4018
4019 // TODO consider showing a permission dialog when the widget is clicked.
4020 if (!success) {
4021 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4022 if (DEBUG_WIDGETS) {
4023 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4024 + " belongs to component " + item.providerName
4025 + ", as the launcher is unable to bing a new widget id");
4026 }
4027 LauncherModel.deleteItemFromDatabase(this, item);
4028 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004029 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004030
4031 item.appWidgetId = newWidgetId;
4032
4033 // If the widget has a configure activity, it is still needs to set it up, otherwise
4034 // the widget is ready to go.
4035 item.restoreStatus = (appWidgetInfo.configure == null)
4036 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4037 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4038
4039 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004040 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004041 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004042 // The widget was marked as UI not ready, but there is no configure activity to
4043 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004044 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4045 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004046 }
Sunny Goyalff572272014-07-23 13:58:07 -07004047 }
4048
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004049 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004051 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4052 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004053 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004054
Sunny Goyal56c73602015-09-25 12:55:01 -07004055 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004056 if (appWidgetInfo == null) {
4057 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004058 deleteWidgetInfo(item);
4059 return;
4060 }
4061
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004062 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004063 item.minSpanX = appWidgetInfo.minSpanX;
4064 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004065 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004066 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004067 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4068 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004069 view.updateIcon(mIconCache);
4070 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004071 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004072 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004073 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004074 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004075 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4079 + (SystemClock.uptimeMillis()-start) + "ms");
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
Sunny Goyalff572272014-07-23 13:58:07 -07004083 /**
4084 * Restores a pending widget.
4085 *
4086 * @param appWidgetId The app widget id
4087 * @param cellInfo The position on screen where to create the widget.
4088 */
4089 private void completeRestoreAppWidget(final int appWidgetId) {
4090 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4091 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4092 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4093 return;
4094 }
4095
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004096 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004097 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4098
4099 mWorkspace.reinflateWidgetsIfNecessary();
4100 LauncherModel.updateItemInDatabase(this, info);
4101 }
4102
Adam Cohen1462de32012-07-24 22:34:36 -07004103 public void onPageBoundSynchronously(int page) {
4104 mSynchronouslyBoundPages.add(page);
4105 }
4106
Sunny Goyal527c7d32015-08-28 15:19:36 -07004107 @Override
4108 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4109 if (mPendingExecutor != null) {
4110 mPendingExecutor.markCompleted();
4111 }
4112 mPendingExecutor = executor;
4113 executor.attachTo(this);
4114 }
4115
4116 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4117 if (mPendingExecutor == executor) {
4118 mPendingExecutor = null;
4119 }
4120 }
4121
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 /**
4123 * Callback saying that there aren't any more items to bind.
4124 *
4125 * Implementation of the method from LauncherModel.Callbacks.
4126 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004127 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004128 Runnable r = new Runnable() {
4129 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004130 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004131 }
4132 };
4133 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004134 return;
4135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004136 if (mSavedState != null) {
4137 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004138 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004140
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 mSavedState = null;
4142 }
4143
Adam Cohen1462de32012-07-24 22:34:36 -07004144 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004145
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004146 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004147 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004148
4149 // If we received the result of any pending adds while the loader was running (e.g. the
4150 // widget configuration forced an orientation change), process them now.
4151 if (sPendingAddItem != null) {
4152 final long screenId = completeAdd(sPendingAddItem);
4153
4154 // TODO: this moves the user to the page where the pending item was added. Ideally,
4155 // the screen would be guaranteed to exist after bind, and the page would be set through
4156 // the workspace restore process.
4157 mWorkspace.post(new Runnable() {
4158 @Override
4159 public void run() {
4160 mWorkspace.snapToScreenId(screenId);
4161 }
4162 });
4163 sPendingAddItem = null;
4164 }
4165
Sunny Goyal756adbc2015-04-16 15:20:51 -07004166 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004167
4168 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004169 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004170 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004171 }
4172
Adam Cohen3ed316a2014-07-23 18:21:20 -07004173 private void sendLoadingCompleteBroadcastIfNecessary() {
4174 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4175 String permission =
4176 getResources().getString(R.string.receive_first_load_broadcast_permission);
4177 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4178 sendBroadcast(intent, permission);
4179 SharedPreferences.Editor editor = mSharedPrefs.edit();
4180 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4181 editor.apply();
4182 }
4183 }
4184
Winson Chunga0b7e862013-09-05 16:03:15 -07004185 public boolean isAllAppsButtonRank(int rank) {
4186 if (mHotseat != null) {
4187 return mHotseat.isAllAppsButtonRank(rank);
4188 }
4189 return false;
4190 }
4191
Winson Chunga2413752012-04-03 14:22:34 -07004192 private boolean canRunNewAppsAnimation() {
4193 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004194 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4195 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004196 }
4197
Winson Chungc9168342013-06-26 14:54:55 -07004198 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004199 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004200 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4201 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004202 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004203 return bounceAnim;
4204 }
4205
Adam Cohen27772732013-11-11 14:00:56 +00004206 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004207 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004208 }
4209
Tony Wickham3a3517f2015-10-06 11:27:04 -07004210 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004211 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004212 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004213 }
4214
Tony Wickham55616cd2015-09-23 14:55:17 -07004215 public int getSearchBarHeight() {
4216 if (mLauncherCallbacks != null) {
4217 return mLauncherCallbacks.getSearchBarHeight();
4218 }
4219 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4220 }
4221
Winson Chung88fa7412015-08-03 14:25:28 -07004222 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004223 if (mSearchDropTargetBar == null) {
4224 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004225 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004226 if (mQsb != null) {
4227 mSearchDropTargetBar.removeView(mQsb);
4228 mQsb = null;
4229 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004230 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004231 }
4232
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004233 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004234 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4235 * multiple calls to bind the same list.)
4236 */
4237 @Thunk ArrayList<AppInfo> mTmpAppsList;
4238 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4239 public void run() {
4240 bindAllApplications(mTmpAppsList);
4241 mTmpAppsList = null;
4242 }
4243 };
4244
4245 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246 * Add the icons for all apps.
4247 *
4248 * Implementation of the method from LauncherModel.Callbacks.
4249 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004250 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004251 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4252 mTmpAppsList = apps;
4253 return;
4254 }
4255
Winson Chungb745afb2015-03-02 11:51:23 -08004256 if (mAppsView != null) {
4257 mAppsView.setApps(apps);
4258 }
Adam Cohen9211d422014-10-07 18:14:53 -07004259 if (mLauncherCallbacks != null) {
4260 mLauncherCallbacks.bindAllApplications(apps);
4261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004262 }
4263
4264 /**
4265 * A package was updated.
4266 *
4267 * Implementation of the method from LauncherModel.Callbacks.
4268 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004269 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004270 Runnable r = new Runnable() {
4271 public void run() {
4272 bindAppsUpdated(apps);
4273 }
4274 };
4275 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004276 return;
4277 }
4278
Winson Chungb745afb2015-03-02 11:51:23 -08004279 if (mAppsView != null) {
4280 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004281 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004282 }
4283
Sunny Goyal4390ace2014-10-13 11:33:11 -07004284 @Override
4285 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4286 Runnable r = new Runnable() {
4287 public void run() {
4288 bindWidgetsRestored(widgets);
4289 }
4290 };
4291 if (waitUntilResume(r)) {
4292 return;
4293 }
4294 mWorkspace.widgetsRestored(widgets);
4295 }
4296
Joe Onorato9c1289c2009-08-17 11:03:03 -04004297 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004298 * Some shortcuts were updated in the background.
4299 *
4300 * Implementation of the method from LauncherModel.Callbacks.
4301 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004302 @Override
4303 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4304 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004305 Runnable r = new Runnable() {
4306 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004307 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004308 }
4309 };
4310 if (waitUntilResume(r)) {
4311 return;
4312 }
4313
Sunny Goyal4390ace2014-10-13 11:33:11 -07004314 if (!updated.isEmpty()) {
4315 mWorkspace.updateShortcuts(updated);
4316 }
4317
4318 if (!removed.isEmpty()) {
4319 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4320 for (ShortcutInfo si : removed) {
4321 removedComponents.add(si.getTargetComponent());
4322 }
4323 mWorkspace.removeItemsByComponentName(removedComponents, user);
4324 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004325 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004326 }
4327 }
4328
4329 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004330 * Update the state of a package, typically related to install state.
4331 *
4332 * Implementation of the method from LauncherModel.Callbacks.
4333 */
Sunny Goyale755d462014-07-22 13:48:29 -07004334 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004335 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4336 Runnable r = new Runnable() {
4337 public void run() {
4338 bindRestoreItemsChange(updates);
4339 }
4340 };
4341 if (waitUntilResume(r)) {
4342 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004343 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004344
Sunny Goyal756adbc2015-04-16 15:20:51 -07004345 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004346 }
4347
4348 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004349 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004350 * in addition to specific applications to remove, the reason being that
4351 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004352 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004353 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004354 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004355 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004356 public void bindWorkspaceComponentsRemoved(
4357 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4358 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004359 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004360 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004361 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004362 }
Winson Chungd64d1762013-08-20 14:37:16 -07004363 };
4364 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004365 return;
4366 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004367 if (!packageNames.isEmpty()) {
4368 mWorkspace.removeItemsByPackageName(packageNames, user);
4369 }
4370 if (!components.isEmpty()) {
4371 mWorkspace.removeItemsByComponentName(components, user);
4372 }
4373 // Notify the drag controller
4374 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004375
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004376 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004377
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004378 @Override
4379 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4380 Runnable r = new Runnable() {
4381 public void run() {
4382 bindAppInfosRemoved(appInfos);
4383 }
4384 };
4385 if (waitUntilResume(r)) {
4386 return;
Joe Onorato36115782010-06-17 13:28:48 -04004387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004388
Winson Chungdf95eb12013-10-16 14:57:07 -07004389 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004390 if (mAppsView != null) {
4391 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004393 }
Joe Onoratobe386092009-11-17 17:32:16 -08004394
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004395 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004396 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004397 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004398 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004399 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004400
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004401 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004402 public void bindWidgetsModel(WidgetsModel model) {
4403 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004404 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004405 return;
4406 }
4407
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004408 if (mWidgetsView != null && model != null) {
4409 mWidgetsView.addWidgets(model);
4410 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004411 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004412 }
4413
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004414 @Override
4415 public void notifyWidgetProvidersChanged() {
4416 if (mWorkspace.getState().shouldUpdateWidget) {
4417 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4418 }
4419 }
4420
Winson Chung400438b2011-01-16 17:53:48 -08004421 private int mapConfigurationOriActivityInfoOri(int configOri) {
4422 final Display d = getWindowManager().getDefaultDisplay();
4423 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4424 switch (d.getRotation()) {
4425 case Surface.ROTATION_0:
4426 case Surface.ROTATION_180:
4427 // We are currently in the same basic orientation as the natural orientation
4428 naturalOri = configOri;
4429 break;
4430 case Surface.ROTATION_90:
4431 case Surface.ROTATION_270:
4432 // We are currently in the other basic orientation to the natural orientation
4433 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4434 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4435 break;
4436 }
4437
4438 int[] oriMap = {
4439 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4440 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4441 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4442 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4443 };
4444 // Since the map starts at portrait, we need to offset if this device's natural orientation
4445 // is landscape.
4446 int indexOffset = 0;
4447 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4448 indexOffset = 1;
4449 }
4450 return oriMap[(d.getRotation() + indexOffset) % 4];
4451 }
Adam Cohen446e9402011-09-15 18:21:21 -07004452
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004453 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004454 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004455 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004456 if (Utilities.ATLEAST_JB_MR2) {
4457 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4458 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004459 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4460 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004461 }
Winson Chung4b919f82012-05-01 10:44:08 -07004462 }
4463 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004464
Winson Chung4b919f82012-05-01 10:44:08 -07004465 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004466 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004467 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004468 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004469 } else {
4470 mHandler.postDelayed(new Runnable() {
4471 public void run() {
4472 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4473 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004474 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004475 }
Winson Chung4b919f82012-05-01 10:44:08 -07004476 }
Winson Chung400438b2011-01-16 17:53:48 -08004477 }
4478
Winson Chunge43a1e72014-01-15 10:33:02 -08004479 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004480 if (mLauncherCallbacks != null) {
4481 return mLauncherCallbacks.isLauncherPreinstalled();
4482 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004483 PackageManager pm = getPackageManager();
4484 try {
4485 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4486 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4487 return true;
4488 } else {
4489 return false;
4490 }
4491 } catch (NameNotFoundException e) {
4492 e.printStackTrace();
4493 return false;
4494 }
4495 }
4496
Adam Cohenea90f832014-05-21 15:03:34 -07004497 /**
4498 * This method indicates whether or not we should suggest default wallpaper dimensions
4499 * when our wallpaper cropper was not yet used to set a wallpaper.
4500 */
4501 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004502 if (mLauncherCallbacks != null) {
4503 return mLauncherCallbacks.overrideWallpaperDimensions();
4504 }
Adam Cohenea90f832014-05-21 15:03:34 -07004505 return true;
4506 }
4507
Adam Cohen432609a2014-03-13 17:03:22 -07004508 /**
4509 * To be overridden by subclasses to indicate that there is an activity to launch
4510 * before showing the standard launcher experience.
4511 */
4512 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004513 if (mLauncherCallbacks != null) {
4514 return mLauncherCallbacks.hasFirstRunActivity();
4515 }
Adam Cohen432609a2014-03-13 17:03:22 -07004516 return false;
4517 }
4518
4519 /**
4520 * To be overridden by subclasses to launch any first run activity
4521 */
4522 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004523 if (mLauncherCallbacks != null) {
4524 return mLauncherCallbacks.getFirstRunActivity();
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return null;
4527 }
4528
Winson Chunga6945242014-01-08 14:04:34 -08004529 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004530 return !ActivityManager.isRunningInTestHarness() &&
4531 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004532 }
4533
Adam Cohen4a9423d2014-03-28 14:22:31 -07004534 protected boolean hasRunFirstRunActivity() {
4535 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4536 }
4537
Adam Cohen432609a2014-03-13 17:03:22 -07004538 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004539 if (shouldRunFirstRunActivity() &&
4540 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004541 Intent firstRunIntent = getFirstRunActivity();
4542 if (firstRunIntent != null) {
4543 startActivity(firstRunIntent);
4544 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004545 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004546 }
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004549 }
4550
4551 private void markFirstRunActivityShown() {
4552 SharedPreferences.Editor editor = mSharedPrefs.edit();
4553 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4554 editor.apply();
4555 }
4556
Adam Cohen432609a2014-03-13 17:03:22 -07004557 /**
4558 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4559 * screen that must be displayed and dismissed.
4560 */
4561 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004562 if (mLauncherCallbacks != null) {
4563 return mLauncherCallbacks.hasDismissableIntroScreen();
4564 }
Adam Cohen432609a2014-03-13 17:03:22 -07004565 return false;
4566 }
4567
4568 /**
4569 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4570 */
4571 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004572 if (mLauncherCallbacks != null) {
4573 return mLauncherCallbacks.getIntroScreen();
4574 }
Adam Cohen432609a2014-03-13 17:03:22 -07004575 return null;
4576 }
4577
4578 /**
4579 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4580 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4581 */
4582 private boolean shouldShowIntroScreen() {
4583 return hasDismissableIntroScreen() &&
4584 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4585 }
4586
4587 protected void showIntroScreen() {
4588 View introScreen = getIntroScreen();
4589 changeWallpaperVisiblity(false);
4590 if (introScreen != null) {
4591 mDragLayer.showOverlayView(introScreen);
4592 }
4593 }
4594
4595 public void dismissIntroScreen() {
4596 markIntroScreenDismissed();
4597 if (showFirstRunActivity()) {
4598 // We delay hiding the intro view until the first run activity is showing. This
4599 // avoids a blip.
4600 mWorkspace.postDelayed(new Runnable() {
4601 @Override
4602 public void run() {
4603 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004604 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004605 }
4606 }, ACTIVITY_START_DELAY);
4607 } else {
4608 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004610 }
4611 changeWallpaperVisiblity(true);
4612 }
4613
4614 private void markIntroScreenDismissed() {
4615 SharedPreferences.Editor editor = mSharedPrefs.edit();
4616 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4617 editor.apply();
4618 }
4619
Adam Cohen091440a2015-03-18 14:16:05 -07004620 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004621 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4622 // on the device, then we always show the first run cling experience (or if there is no
4623 // launcher2). Otherwise, we prompt the user upon started for migration
4624 LauncherClings launcherClings = new LauncherClings(this);
4625 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004626 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004627 if (mModel.canMigrateFromOldLauncherDb(this)) {
4628 launcherClings.showMigrationCling();
4629 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004630 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004631 }
4632 }
4633 }
4634
Winson Chunga6945242014-01-08 14:04:34 -08004635 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004636 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4637 if (mHotseat != null) mHotseat.setAlpha(1f);
4638 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004639 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4640 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004641 }
4642
4643 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004644 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4645 if (mHotseat != null) mHotseat.setAlpha(0f);
4646 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004647 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4648 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004649 }
4650
Winson Chung5ffd51d2015-06-25 11:36:50 -07004651 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004652 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004653 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004654 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004655 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004656 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004657 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4658 if (userHandle != null) {
4659 user = UserHandleCompat.fromUser(userHandle);
4660 }
4661 }
4662 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004663 }
4664
4665 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004666 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004667 if (user == null) {
4668 user = UserHandleCompat.myUserHandle();
4669 }
4670
4671 // Called from search suggestion, add the profile extra to the intent to ensure that we
4672 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004673 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004674 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004675 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004676 return null;
4677 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004678 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004679 }
4680
Winson Chung5ffd51d2015-06-25 11:36:50 -07004681 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004682 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4683 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004684 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004685 UserHandleCompat.myUserHandle());
4686 }
4687
Winson Chung5ffd51d2015-06-25 11:36:50 -07004688 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004689 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4690 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004691 mWorkspace.onExternalDragStartedWithItem(dragView);
4692 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004693 }
4694
Winson Chung5ffd51d2015-06-25 11:36:50 -07004695 protected void moveWorkspaceToDefaultScreen() {
4696 mWorkspace.moveToDefaultScreen(false);
4697 }
4698
Anjali Koppalf5d29132014-02-28 13:33:27 -08004699 @Override
4700 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004701 if (mLauncherCallbacks != null) {
4702 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4703 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004704 }
4705
Winson Chung80baf5a2010-08-09 16:03:15 -07004706 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004707 * Returns a FastBitmapDrawable with the icon, accurately sized.
4708 */
4709 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4710 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4711 d.setFilterBitmap(true);
4712 resizeIconDrawable(d);
4713 return d;
4714 }
4715
4716 /**
4717 * Resizes an icon drawable to the correct icon size.
4718 */
Winson5fbe0742015-09-30 15:33:00 -07004719 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004720 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004721 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004722 }
4723
4724 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004725 * Prints out out state for debugging.
4726 */
4727 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004728 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004729 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004730 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4731 Log.d(TAG, "mRestoring=" + mRestoring);
4732 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004733 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004734 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004735 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004736
Daniel Sandler325dc232013-06-05 22:57:57 -04004737 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004738 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004739
4740 @Override
4741 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4742 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004743 // Dump workspace
4744 writer.println(prefix + "Workspace Items");
4745 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4746 writer.println(prefix + " Homescreen " + i);
4747
4748 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4749 for (int j = 0; j < layout.getChildCount(); j++) {
4750 Object tag = layout.getChildAt(j).getTag();
4751 if (tag != null) {
4752 writer.println(prefix + " " + tag.toString());
4753 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004754 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004755 }
Sunny Goyala1365452015-10-01 15:46:24 -07004756
4757 writer.println(prefix + " Hotseat");
4758 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4759 for (int j = 0; j < layout.getChildCount(); j++) {
4760 Object tag = layout.getChildAt(j).getTag();
4761 if (tag != null) {
4762 writer.println(prefix + " " + tag.toString());
4763 }
4764 }
4765
4766 synchronized (sDumpLogs) {
4767 writer.println();
4768 writer.println(prefix + "Debug logs");
4769 for (String log : sDumpLogs) {
4770 writer.println(prefix + " " + log);
4771 }
4772 }
4773
Adam Cohen9211d422014-10-07 18:14:53 -07004774 if (mLauncherCallbacks != null) {
4775 mLauncherCallbacks.dump(prefix, fd, writer, args);
4776 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004777 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004778
Sunny Goyala1365452015-10-01 15:46:24 -07004779 public static void addDumpLog(String tag, String log) {
4780 Log.d(tag, log);
4781 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004782 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004783 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004784 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004785 }
4786
Adam Cohen59400422014-03-05 18:07:04 -08004787 public static CustomAppWidget getCustomAppWidget(String name) {
4788 return sCustomAppWidgets.get(name);
4789 }
4790
4791 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4792 return sCustomAppWidgets;
4793 }
4794
Sunny Goyal29538332016-02-18 09:10:19 -08004795 public static List<View> getFolderContents(View icon) {
4796 if (icon instanceof FolderIcon) {
4797 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4798 } else {
4799 return Collections.EMPTY_LIST;
4800 }
4801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004802}
Michael Jurka2763be32011-02-24 11:19:57 -08004803
Dan Sandlerd5024042014-01-09 15:01:33 -05004804interface DebugIntents {
4805 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4806 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004807}