blob: 21adcb7e11df9984dc83d63aeb5962e45669b740 [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;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080048import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
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;
Tony Wickhame2217252016-03-22 16:34:23 -070088import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800112import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700113import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700114import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700117import com.android.launcher3.logging.FileLog;
Sunny Goyal322d5562015-06-25 19:35:49 -0700118import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700120import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800133import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700139 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
140 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700141 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700149 static final boolean DEBUG_LOGGING = 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
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 // adb shell setprop logTap.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
Sunny Goyal594d76d2014-11-06 10:12:54 -0800197 private static final String QSB_WIDGET_ID = "qsb_widget_id";
198 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
199
Winson Chunga6945242014-01-08 14:04:34 -0800200 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700203 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
204 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700214 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800226 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
230 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Sunny Goyalffe83f12014-08-14 17:39:34 -0700232 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700235 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800236 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800237 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Winson Chungc51db6a2011-10-05 11:44:49 -0700247 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700248 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700249
250 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700251 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Sunny Goyal594d76d2014-11-06 10:12:54 -0800257 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
261 // scroll issues (because the workspace may not have been measured yet) and extra work.
262 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
263 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
265 private SpannableStringBuilder mDefaultKeySsb = null;
266
Adam Cohen091440a2015-03-18 14:16:05 -0700267 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800269 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private boolean mRestoring;
271 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700272 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700275 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700276 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700280 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700283 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Sunny Goyal639e9062015-08-19 19:17:06 -0700286 private LauncherClings mClings;
287
Adam Cohen61f560d2013-09-30 15:58:20 -0700288 private View.OnTouchListener mHapticFeedbackTouchListener;
289
Adam Cohended9f8d2010-11-03 13:25:16 -0700290 // Related to the auto-advancing of widgets
291 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700292 private static final int ADVANCE_INTERVAL = 20000;
293 private static final int ADVANCE_STAGGER = 250;
294
295 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 private long mAutoAdvanceSentTime;
297 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700299
Winson Chung400438b2011-01-16 17:53:48 -0800300 // Determines how long to wait after a rotation before restoring the screen orientation to
301 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800303
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700307 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
312
Winson Chungf0c6ae02012-03-21 16:10:31 -0700313 // Holds the page that we need to animate to, and the icon views that we need to animate up
314 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700320 private DeviceProfile mDeviceProfile;
321
Adam Cohen4b66bf32015-09-18 12:15:19 -0700322 private boolean mMoveToDefaultScreenFromNewIntent;
323
Winson Chung13eb5272015-05-11 16:30:13 -0700324 // This is set to the view that launched the activity that navigated the user away from
325 // launcher. Since there is no callback for when the activity has finished launching, enable
326 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 private BubbleTextView mWaitingForResume;
328
Adam Cohen59400422014-03-05 18:07:04 -0800329 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
330 new HashMap<String, CustomAppWidget>();
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700333 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
334 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800335 }
336 }
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Adam Cohendb364c32014-05-20 14:23:40 -0700346 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800349 int requestCode;
350 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700351 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 int cellX;
354 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700355 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 }
357
Hyunyoung Songaa953652016-04-19 18:30:24 -0700358 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800359
Adam Cohenf9c184a2016-01-15 16:47:43 -0800360 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700361 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400362
363 @Thunk void setOrientation() {
364 if (mRotationEnabled) {
365 unlockScreenOrientation(true);
366 } else {
367 setRequestedOrientation(
368 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700369 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700371
Sunny Goyal745bad92016-05-02 10:54:12 -0700372 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700376 if (DEBUG_STRICT_MODE) {
377 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378 .detectDiskReads()
379 .detectDiskWrites()
380 .detectNetwork() // or .detectAll() for all detectable problems
381 .penaltyLog()
382 .build());
383 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384 .detectLeakedSqlLiteObjects()
385 .detectLeakedClosableObjects()
386 .penaltyLog()
387 .penaltyDeath()
388 .build());
389 }
390
Adam Cohen9211d422014-10-07 18:14:53 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.preOnCreate();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400397 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 mDeviceProfile = getResources().getConfiguration().orientation
401 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700402 app.getInvariantDeviceProfile().landscapeProfile
403 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyalf7258242015-10-19 16:59:07 -0700405 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700406 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700409
Joe Onorato41a12d22009-10-31 18:30:00 -0400410 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700411 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700412
Sunny Goyalffe83f12014-08-14 17:39:34 -0700413 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700414
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700429
Tony Wickhameef44322015-10-20 13:24:36 -0700430 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
431 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700433 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700434 mExtractedColors = new ExtractedColors();
435 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436
Tony Wickhame2217252016-03-22 16:34:23 -0700437 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
438 .addAccessibilityStateChangeListener(this);
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 mSavedState = savedInstanceState;
443 restoreState(mSavedState);
444
445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700450 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700453 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700457 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800468 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700469 // In case we are on a device with locked rotation, we should look at preferences to check
470 // if the user has specifically allowed rotation.
471 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700472 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700473 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
474 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 }
476
477 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
478 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400479 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onCreate(savedInstanceState);
483 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700484
485 if (shouldShowIntroScreen()) {
486 showIntroScreen();
487 } else {
488 showFirstRunActivity();
489 showFirstRunClings();
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
492
Sunny Goyal7779d622015-06-11 16:18:39 -0700493 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700494 public void onExtractedColorsChanged() {
495 loadExtractedColorsAndColorItems();
496 }
497
498 private void loadExtractedColorsAndColorItems() {
499 if (mExtractedColors != null) {
500 mExtractedColors.load(this);
501 // TODO: pass mExtractedColors to interested items such as hotseat.
Tony Wickham827cef22016-03-17 15:39:39 -0700502 }
503 }
504
Adam Cohen9211d422014-10-07 18:14:53 -0700505 private LauncherCallbacks mLauncherCallbacks;
506
507 public void onPostCreate(Bundle savedInstanceState) {
508 super.onPostCreate(savedInstanceState);
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onPostCreate(savedInstanceState);
511 }
512 }
513
Sunny Goyal32554d12015-12-03 15:31:25 -0800514 /**
515 * Call this after onCreate to set or clear overlay.
516 */
517 public void setLauncherOverlay(LauncherOverlay overlay) {
518 if (overlay != null) {
519 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
520 }
521 mWorkspace.setLauncherOverlay(overlay);
522 }
523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
525 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700526 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700527 private boolean mWorkspaceImportanceStored = false;
528 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800530 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
532
533 @Override
534 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 return;
537 }
538 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700539 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspace != null) {
541 mWorkspaceImportanceForAccessibility =
542 mWorkspace.getImportantForAccessibility();
543 mWorkspace.setImportantForAccessibility(
544 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
545 mWorkspaceImportanceStored = true;
546 }
547 if (mHotseat != null) {
548 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
549 mHotseat.setImportantForAccessibility(
550 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
551 mHotseatImportanceStored = true;
552 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 }
554
555 @Override
556 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspaceImportanceStored && mWorkspace != null) {
558 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
559 }
560 if (mHotseatImportanceStored && mHotseat != null) {
561 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
562 }
563 mWorkspaceImportanceStored = false;
564 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566 });
Adam Cohen9211d422014-10-07 18:14:53 -0700567 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700568 }
569
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700570 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700571 public void onLauncherProviderChange() {
572 if (mLauncherCallbacks != null) {
573 mLauncherCallbacks.onLauncherProviderChange();
574 }
575 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700576
Adam Cohen9211d422014-10-07 18:14:53 -0700577 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700578 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700579 if (mLauncherCallbacks != null) {
580 return mLauncherCallbacks.hasCustomContentToLeft();
581 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700582 return false;
583 }
584
Dave Hawkeya8881582013-09-17 15:55:33 -0600585 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500586 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 * {@link #addToCustomContentPage}. This will only be invoked if
588 * {@link #hasCustomContentToLeft()} is {@code true}.
589 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500590 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 mLauncherCallbacks.populateCustomContentContainer();
593 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 }
595
596 /**
597 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
598 * ensure the custom content page is added or removed if necessary.
599 */
600 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600601 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 // Not bound yet, wait for bindScreens to be called.
603 return;
604 }
605
606 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
607 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500608 mWorkspace.createCustomContentContainer();
609 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
611 mWorkspace.removeCustomContentPage();
612 }
613 }
614
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800615 /**
616 * Logger object is a singleton and does not have to be coupled with the foreground activity.
617 * Since most user event logging is done on the UI, the object is retrieved from the
618 * callback for convenience.
619 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700620 private UserEventDispatcher createUserEventDispatcher() {
621 return new UserEventDispatcher() {
622 @Override
623 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
624 if (!DEBUG_LOGGING) {
625 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800626 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700627 Log.d("UserEvent", String.format(Locale.US,
628 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
629 LoggerUtils.getActionStr(ev.action),
630 LoggerUtils.getTargetStr(ev.srcTarget[0]),
631 LoggerUtils.getTargetStr(ev.srcTarget[1]),
632 ev.elapsedContainerMillis,
633 ev.elapsedSessionMillis));
634 }
635 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800636 }
637
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 public UserEventDispatcher getUserEventDispatcher() {
639 if (mLauncherCallbacks != null) {
640 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
641 if (dispatcher != null) {
642 return dispatcher;
643 }
644 }
645
646 if (mUserEventDispatcher == null) {
647 mUserEventDispatcher = createUserEventDispatcher();
648 }
649 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800650 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100651
Hyunyoung Song3f471442015-04-08 19:01:34 -0700652 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700653 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
654 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700655 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 }
657
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000658 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700659 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
660 // This cast is safe as long as the id < 0x00FFFFFF
661 // Since we jail all the dynamically generated views, there should be no clashes
662 // with any other views.
663 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000664 }
665
666 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
668 * a configuration step, this allows the proper animations to run after other transitions.
669 */
Adam Cohendb364c32014-05-20 14:23:40 -0700670 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700671 long screenId = args.screenId;
672 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
673 // When the screen id represents an actual screen (as opposed to a rank) we make sure
674 // that the drop page actually exists.
675 screenId = ensurePendingDropLayoutExists(args.screenId);
676 }
Adam Cohendb364c32014-05-20 14:23:40 -0700677
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700680 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700681 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700682 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700684 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700685 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700686 case REQUEST_RECONFIGURE_APPWIDGET:
687 completeRestoreAppWidget(args.appWidgetId);
688 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 }
Michael Jurka27614d22012-04-02 06:40:22 -0700690 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
691 // if you turned the screen off and then back while in All Apps, Launcher would not
692 // return to the workspace. Clearing mAddInfo.container here fixes this issue
693 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700694 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 }
696
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800697 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700698 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700699 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700700 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700701 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800702 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700703
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 Runnable exitSpringLoaded = new Runnable() {
705 @Override
706 public void run() {
707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
708 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
709 }
710 };
711
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700713 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
716 if (resultCode == RESULT_CANCELED) {
717 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700718 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800721 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700723
724 // When the user has granted permission to bind widgets, we should check to see if
725 // we can inflate the default search bar widget.
726 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
728 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
730 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700731 // User could have free-scrolled between pages before picking a wallpaper; make sure
732 // we move to the closest one now.
733 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700734 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200735 }
736 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700740 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700741
Adam Cohendb364c32014-05-20 14:23:40 -0700742 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 // We have special handling for widgets
744 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 final int appWidgetId;
746 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
747 : -1;
748 if (widgetId < 0) {
749 appWidgetId = pendingAddWidgetId;
750 } else {
751 appWidgetId = widgetId;
752 }
753
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800755 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700756 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
757 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 result = RESULT_CANCELED;
759 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700760 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 @Override
762 public void run() {
763 exitSpringLoadedDragModeDelayed(false, 0, null);
764 }
765 };
Adam Cohendb364c32014-05-20 14:23:40 -0700766 if (workspaceLocked) {
767 // No need to remove the empty screen if we're mid-binding, as the
768 // the bind will not add the empty screen.
769 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
770 } else {
771 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
773 }
Winson Chung5aaab772012-04-27 15:24:02 -0700774 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700775 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700776 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
777 // When the screen id represents an actual screen (as opposed to a rank)
778 // we make sure that the drop page actually exists.
779 mPendingAddInfo.screenId =
780 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
781 }
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
783
784 dropLayout.setDropPending(true);
785 final Runnable onComplete = new Runnable() {
786 @Override
787 public void run() {
788 completeTwoStageWidgetDrop(resultCode, appWidgetId);
789 dropLayout.setDropPending(false);
790 }
791 };
792 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
793 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
794 } else {
795 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
796 mPendingAddInfo);
797 sPendingAddItem = args;
798 }
Winson Chung5aaab772012-04-27 15:24:02 -0700799 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800800 return;
801 }
802
Sunny Goyalff572272014-07-23 13:58:07 -0700803 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
804 if (resultCode == RESULT_OK) {
805 // Update the widget view.
806 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
807 pendingAddWidgetId, mPendingAddInfo);
808 if (workspaceLocked) {
809 sPendingAddItem = args;
810 } else {
811 completeAdd(args);
812 }
813 }
814 // Leave the widget in the pending state if the user canceled the configure.
815 return;
816 }
817
Sunny Goyalaad90582015-07-09 14:22:50 -0700818 if (requestCode == REQUEST_CREATE_SHORTCUT) {
819 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
820 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
821 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
822 mPendingAddInfo);
823 if (isWorkspaceLocked()) {
824 sPendingAddItem = args;
825 } else {
826 completeAdd(args);
827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829 }
830 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800836
837 }
838
839 @Override
840 protected void onActivityResult(
841 final int requestCode, final int resultCode, final Intent data) {
842 handleActivityResult(requestCode, resultCode, data);
843 if (mLauncherCallbacks != null) {
844 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
845 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 }
847
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600848 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700849 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700851 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
852 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
853 View v = null;
854 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
855 if (layout != null) {
856 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
857 }
858 Intent intent = sPendingAddItem.intent;
859 sPendingAddItem = null;
860 if (grantResults.length > 0
861 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
862 startActivity(v, intent, null);
863 } else {
864 // TODO: Show a snack bar with link to settings
865 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
866 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
867 }
868 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600869 if (mLauncherCallbacks != null) {
870 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
871 grantResults);
872 }
873 }
874
Adam Cohendb364c32014-05-20 14:23:40 -0700875 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
876 appWidgetId, ItemInfo info) {
877 PendingAddArguments args = new PendingAddArguments();
878 args.requestCode = requestCode;
879 args.intent = data;
880 args.container = info.container;
881 args.screenId = info.screenId;
882 args.cellX = info.cellX;
883 args.cellY = info.cellY;
884 args.appWidgetId = appWidgetId;
885 return args;
886 }
887
888 /**
889 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
890 *
891 * @param screenId the screen id to check
892 * @return the new screen, or screenId if it exists
893 */
894 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800895 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700896 if (dropLayout == null) {
897 // it's possible that the add screen was removed because it was
898 // empty and a re-bind occurred
899 mWorkspace.addExtraEmptyScreen();
900 return mWorkspace.commitExtraEmptyScreen();
901 } else {
902 return screenId;
903 }
904 }
905
Adam Cohen091440a2015-03-18 14:16:05 -0700906 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800907 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 Runnable onCompleteRunnable = null;
909 int animationType = 0;
910
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700911 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 if (resultCode == RESULT_OK) {
913 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
914 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 mPendingAddWidgetInfo);
916 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 onCompleteRunnable = new Runnable() {
918 @Override
919 public void run() {
920 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700922 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
923 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 }
925 };
926 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800927 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 if (mDragLayer.getAnimatedView() != null) {
931 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
932 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
933 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 // The animated view may be null in the case of a rotation during widget configuration
936 onCompleteRunnable.run();
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700941 protected void onStop() {
942 super.onStop();
943 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700944
945 if (mLauncherCallbacks != null) {
946 mLauncherCallbacks.onStop();
947 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700948 }
949
950 @Override
951 protected void onStart() {
952 super.onStart();
953 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700954
955 if (mLauncherCallbacks != null) {
956 mLauncherCallbacks.onStart();
957 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 }
959
960 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200962 long startTime = 0;
963 if (DEBUG_RESUME_TIME) {
964 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400965 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200966 }
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.preOnResume();
970 }
971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700973 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700974
Winson Chung4a2afa32012-07-19 14:53:05 -0700975 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700976 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800978 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700979 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700980 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700981 // view after launching an app, as they may be depending on the UI to be static to
982 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700983 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700984 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800985 } else if (mOnResumeState == State.WIDGETS) {
986 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 }
988 mOnResumeState = State.NONE;
989
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700990 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700991 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
992 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700993
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800994 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700995 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700996 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700997 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001001 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001002 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1003 // execute them here
1004 long startTimeCallbacks = 0;
1005 if (DEBUG_RESUME_TIME) {
1006 startTimeCallbacks = System.currentTimeMillis();
1007 }
1008
Michael Jurka1e2f4652013-07-08 18:03:46 -07001009 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1010 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001012 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 if (DEBUG_RESUME_TIME) {
1014 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1015 (System.currentTimeMillis() - startTimeCallbacks));
1016 }
Michael Jurka447bf842013-05-15 14:52:15 +02001017 }
Michael Jurka54554252013-08-01 12:52:23 +02001018 if (mOnResumeCallbacks.size() > 0) {
1019 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1020 mOnResumeCallbacks.get(i).run();
1021 }
1022 mOnResumeCallbacks.clear();
1023 }
Winson Chunge4e50662012-01-23 14:45:13 -08001024
1025 // Reset the pressed state of icons that were locked in the press state while activities
1026 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001027 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001028 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 mWaitingForResume.setStayPressed(false);
1030 }
Winson Chung82963d52013-10-09 11:20:57 -07001031
Adam Cohen06dff352012-06-01 17:17:08 -07001032 // It is possible that widgets can receive updates while launcher is not in the foreground.
1033 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1034 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1035 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001036 if (!isWorkspaceLoading()) {
1037 getWorkspace().reinflateWidgetsIfNecessary();
1038 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001039 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001040
Michael Jurka447bf842013-05-15 14:52:15 +02001041 if (DEBUG_RESUME_TIME) {
1042 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1043 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001044
Adam Cohen4b66bf32015-09-18 12:15:19 -07001045 // We want to suppress callbacks about CustomContent being shown if we have just received
1046 // onNewIntent while the user was present within launcher. In that case, we post a call
1047 // to move the user to the main screen (which will occur after onResume). We don't want to
1048 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1049 // suppress here.
1050 if (mWorkspace.getCustomContentCallbacks() != null
1051 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001052 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001053 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1055 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001056 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 }
1058 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001059 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001060 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001061 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001062
Sunny Goyal756adbc2015-04-16 15:20:51 -07001063 if (!isWorkspaceLoading()) {
1064 // Process any items that were added while Launcher was away.
1065 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1066 }
Adam Cohen9211d422014-10-07 18:14:53 -07001067
1068 if (mLauncherCallbacks != null) {
1069 mLauncherCallbacks.onResume();
1070 }
Adam Cohen06dff352012-06-01 17:17:08 -07001071 }
1072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001074 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001075 // Ensure that items added to Launcher are queued until Launcher returns
1076 InstallShortcutReceiver.enableInstallQueue();
1077
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001079 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001080 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001081 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001082
1083 // We call onHide() aggressively. The custom content callbacks should be able to
1084 // debounce excess onHide calls.
1085 if (mWorkspace.getCustomContentCallbacks() != null) {
1086 mWorkspace.getCustomContentCallbacks().onHide();
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohen9211d422014-10-07 18:14:53 -07001089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
1094 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001095 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1096 // by a onResume or by scrolling otherwise.
1097 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001098
1099 // Custom content is completely hidden
1100 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001101
1102 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1103 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001104
1105 // Indicates whether the user is allowed to scroll away from the custom content.
1106 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109 public interface LauncherOverlay {
1110
1111 /**
1112 * Touch interaction leading to overscroll has begun
1113 */
1114 public void onScrollInteractionBegin();
1115
1116 /**
1117 * Touch interaction related to overscroll has ended
1118 */
1119 public void onScrollInteractionEnd();
1120
1121 /**
1122 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1123 * screen (or in the case of RTL, the rightmost screen).
1124 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001125 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126
1127 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001128 * Called when the launcher is ready to use the overlay
1129 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001131 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 }
1133
Jun Mukai8af0cd82015-05-12 12:32:12 -07001134 public interface LauncherSearchCallbacks {
1135 /**
1136 * Called when the search overlay is shown.
1137 */
1138 public void onSearchOverlayOpened();
1139
1140 /**
1141 * Called when the search overlay is dismissed.
1142 */
1143 public void onSearchOverlayClosed();
1144 }
1145
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148 }
1149
1150 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1151
Sunny Goyalc86df472016-02-25 09:19:38 -08001152 public void onScrollChanged(float progress) {
1153 if (mWorkspace != null) {
1154 mWorkspace.onOverlayScrollChanged(progress);
1155 }
1156 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157 }
1158
Jorim Jaggid017f882014-01-14 17:08:48 -08001159 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001160 if (mLauncherCallbacks != null) {
1161 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001162 } else {
1163 // On devices with a locked orientation, we will at least have the allow rotation
1164 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001165 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001166 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001167 }
1168
Adam Cohen9211d422014-10-07 18:14:53 -07001169 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001170 CustomContentCallbacks callbacks, String description) {
1171 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001172 }
1173
Adam Cohenedb40762013-07-18 16:45:45 -07001174 // The custom content needs to offset its content to account for the QSB
1175 public int getTopOffsetForCustomContent() {
1176 return mWorkspace.getPaddingTop();
1177 }
1178
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001179 @Override
1180 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001182 if (mModel.isCurrentCallbacks(this)) {
1183 mModel.stopLoader();
1184 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001185 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1186
Romain Guy13c2e7b2010-03-10 19:45:00 -08001187 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001188 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001190 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 @Override
1192 public void onWindowFocusChanged(boolean hasFocus) {
1193 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001194 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001195
1196 if (mLauncherCallbacks != null) {
1197 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1198 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001199 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 private boolean acceptFilter() {
1202 final InputMethodManager inputManager = (InputMethodManager)
1203 getSystemService(Context.INPUT_METHOD_SERVICE);
1204 return !inputManager.isFullscreenMode();
1205 }
1206
1207 @Override
1208 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001209 final int uniChar = event.getUnicodeChar();
1210 final boolean handled = super.onKeyDown(keyCode, event);
1211 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1212 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1214 keyCode, event);
1215 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001216 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001217 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 // showSearchDialog()
1219 // If there are multiple keystrokes before the search dialog takes focus,
1220 // onSearchRequested() will be called for every keystroke,
1221 // but it is idempotent, so it's fine.
1222 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224 }
1225
Joe Onorato8a9625e2010-01-28 15:55:35 -08001226 // Eat the long press event so the keyboard doesn't come up.
1227 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1228 return true;
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 return handled;
1232 }
1233
Winson46163472015-09-22 17:31:56 -07001234 @Override
1235 public boolean onKeyUp(int keyCode, KeyEvent event) {
1236 if (keyCode == KeyEvent.KEYCODE_MENU) {
1237 // Ignore the menu key if we are currently dragging or are on the custom content screen
1238 if (!isOnCustomContent() && !mDragController.isDragging()) {
1239 // Close any open folders
1240 closeFolder();
1241
1242 // Stop resizing any widgets
1243 mWorkspace.exitWidgetResizeMode();
1244
1245 // Show the overview mode if we are on the workspace
1246 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1247 !mWorkspace.isSwitchingState()) {
1248 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001249 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001250 }
1251 }
1252 return true;
1253 }
1254 return super.onKeyUp(keyCode, event);
1255 }
1256
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 private String getTypedText() {
1258 return mDefaultKeySsb.toString();
1259 }
1260
1261 private void clearTypedText() {
1262 mDefaultKeySsb.clear();
1263 mDefaultKeySsb.clearSpans();
1264 Selection.setSelection(mDefaultKeySsb, 0);
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001268 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1269 * State
1270 */
1271 private static State intToState(int stateOrdinal) {
1272 State state = State.WORKSPACE;
1273 final State[] stateValues = State.values();
1274 for (int i = 0; i < stateValues.length; i++) {
1275 if (stateValues[i].ordinal() == stateOrdinal) {
1276 state = stateValues[i];
1277 break;
1278 }
1279 }
1280 return state;
1281 }
1282
1283 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 * Restores the previous state, if it exists.
1285 *
1286 * @param savedState The previous state.
1287 */
1288 private void restoreState(Bundle savedState) {
1289 if (savedState == null) {
1290 return;
1291 }
1292
Michael Jurka883f55b2010-10-21 15:47:14 -07001293 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001294 if (state == State.APPS || state == State.WIDGETS) {
1295 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001296 }
1297
Adam Cohen21cd0022013-10-09 18:57:02 -07001298 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1299 PagedView.INVALID_RESTORE_PAGE);
1300 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001301 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
Sunny Goyal756cd262015-08-20 12:33:21 -07001304 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1305 if (itemValues != null) {
1306 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001307 AppWidgetProviderInfo info = savedState.getParcelable(
1308 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001309 mPendingAddWidgetInfo = info == null ?
1310 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1311
Adam Cohen4637b5a2013-11-04 18:21:24 -08001312 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001313 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 mRestoring = true;
1315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
1318 /**
1319 * Finds all the views we need and configure them properly.
1320 */
1321 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001322 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001323
Craig Mautner360310b2012-10-26 15:13:08 -07001324 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001325 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001326 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1327 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001328 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001329
Sunny Goyal784f9c32016-03-23 16:56:54 -07001330 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1331 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1332 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001333 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
Winson Chung4c98d922011-05-31 16:50:48 -07001335 // Setup the drag layer
1336 mDragLayer.setup(this, dragController);
1337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 // Setup the hotseat
1339 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1340 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001341 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 }
1343
Winsone9f27272015-10-13 10:47:51 -07001344 // Setup the overview panel
1345 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001346
Winson Chung4c98d922011-05-31 16:50:48 -07001347 // Setup the workspace
1348 mWorkspace.setHapticFeedbackEnabled(false);
1349 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001350 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001351 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001352
Tony Wickham34d2c912015-09-11 09:27:58 -07001353 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001354 mSearchDropTargetBar = (SearchDropTargetBar)
1355 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001356 // Get the app info bar
1357 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1358 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001359
Winson Chungef7f8742015-06-04 17:18:17 -07001360 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001361 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001362 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001363 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1364 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1365 } else {
1366 mAppsView.setSearchBarController(new DefaultAppSearchController());
1367 }
Craig Mautner360310b2012-10-26 15:13:08 -07001368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001370 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001371 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001372 dragController.setMoveTarget(mWorkspace);
1373 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001374 if (mSearchDropTargetBar != null) {
1375 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001376 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001377 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001378 if (mAppInfoDropTargetBar != null) {
1379 mAppInfoDropTargetBar.setup(this, dragController);
1380 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001381
Sunny Goyal322d5562015-06-25 19:35:49 -07001382 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1383 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
Winsone9f27272015-10-13 10:47:51 -07001387 private void setupOverviewPanel() {
1388 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1389
1390 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1391 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1392 @Override
1393 public boolean onLongClick(View v) {
1394 return v.performClick();
1395 }
1396 };
1397
1398 // Bind wallpaper button actions
1399 View wallpaperButton = findViewById(R.id.wallpaper_button);
1400 wallpaperButton.setOnClickListener(new OnClickListener() {
1401 @Override
1402 public void onClick(View view) {
1403 if (!mWorkspace.isSwitchingState()) {
1404 onClickWallpaperPicker(view);
1405 }
1406 }
1407 });
1408 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1409 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1410
1411 // Bind widget button actions
1412 mWidgetsButton = findViewById(R.id.widget_button);
1413 mWidgetsButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickAddWidgetButton(view);
1418 }
1419 }
1420 });
1421 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1422 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 // Bind settings actions
1425 View settingsButton = findViewById(R.id.settings_button);
1426 boolean hasSettings = hasSettings();
1427 if (hasSettings) {
1428 settingsButton.setOnClickListener(new OnClickListener() {
1429 @Override
1430 public void onClick(View view) {
1431 if (!mWorkspace.isSwitchingState()) {
1432 onClickSettingsButton(view);
1433 }
1434 }
1435 });
1436 settingsButton.setOnLongClickListener(performClickOnLongClick);
1437 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438 } else {
1439 settingsButton.setVisibility(View.GONE);
1440 }
1441
1442 mOverviewPanel.setAlpha(0f);
1443 }
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001446 * Sets the all apps button. This method is called from {@link Hotseat}.
1447 */
1448 public void setAllAppsButton(View allAppsButton) {
1449 mAllAppsButton = allAppsButton;
1450 }
1451
1452 public View getAllAppsButton() {
1453 return mAllAppsButton;
1454 }
1455
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001456 public View getWidgetsButton() {
1457 return mWidgetsButton;
1458 }
1459
Anjali Koppal5ad44842014-03-10 20:34:39 -07001460 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * Creates a view representing a shortcut.
1462 *
1463 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001465 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001466 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 /**
1470 * Creates a view representing a shortcut inflated from the specified resource.
1471 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 * @param parent The group the shortcut belongs to.
1473 * @param info The data structure describing the shortcut.
1474 *
1475 * @return A View inflated from layoutResId.
1476 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001477 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001478 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 parent, false);
1480 favorite.applyFromShortcutInfo(info, mIconCache);
1481 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001483 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 return favorite;
1485 }
1486
1487 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 * Add a shortcut to the workspace.
1489 *
1490 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001493 int cellY) {
1494 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001495 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001496
Sunny Goyal5c97f512015-05-19 16:03:28 -07001497 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001498 if (info == null) {
1499 return;
1500 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001501 final View view = createShortcut(info);
1502
Sunny Goyal5c97f512015-05-19 16:03:28 -07001503 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 // First we check if we already know the exact location where we want to add this item.
1505 if (cellX >= 0 && cellY >= 0) {
1506 cellXY[0] = cellX;
1507 cellXY[1] = cellY;
1508 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001509
1510 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001511 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001512 true, null,null)) {
1513 return;
1514 }
1515 DragObject dragObject = new DragObject();
1516 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001517 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1518 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001519 return;
1520 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001521 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001522 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001523 }
1524
1525 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001526 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 return;
1528 }
1529
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001530 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
1532 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536 }
1537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001539 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001541 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 */
Adam Cohen091440a2015-03-18 14:16:05 -07001543 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001544 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1545
1546 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001548 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 }
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Adam Cohen59400422014-03-05 18:07:04 -08001551 if (appWidgetInfo.isCustomWidget) {
1552 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001553 }
1554
Adam Cohen59400422014-03-05 18:07:04 -08001555 LauncherAppWidgetInfo launcherInfo;
1556 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1557 launcherInfo.spanX = info.spanX;
1558 launcherInfo.spanY = info.spanY;
1559 launcherInfo.minSpanX = info.minSpanX;
1560 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001561 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001564 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 if (hostView == null) {
1568 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001569 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1570 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 } else {
1572 // The AppWidgetHostView has already been inflated and instantiated
1573 launcherInfo.hostView = hostView;
1574 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001575 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001578 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1582 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1583 item.hostView.setTag(item);
1584 item.onBindAppWidget(this);
1585
1586 item.hostView.setFocusable(true);
1587 item.hostView.setOnFocusChangeListener(mFocusHandler);
1588
1589 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1590 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1591
1592 if (!item.isCustomWidget()) {
1593 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1594 }
1595 }
1596
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1598 @Override
1599 public void onReceive(Context context, Intent intent) {
1600 final String action = intent.getAction();
1601 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1602 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001603 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001604 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001605
Winson Chungc100e8e2011-08-09 16:02:43 -07001606 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001607 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001608 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001609 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001610 if (!showWorkspace(false)) {
1611 // If we are already on the workspace, then manually reset all apps
1612 mAppsView.reset();
1613 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1616 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001617 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001618 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1619 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001620 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001621 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1622 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1623 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001624 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001625 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1626 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 }
1628 }
1629 };
1630
1631 @Override
1632 public void onAttachedToWindow() {
1633 super.onAttachedToWindow();
1634
1635 // Listen for broadcasts related to user-presence
1636 final IntentFilter filter = new IntentFilter();
1637 filter.addAction(Intent.ACTION_SCREEN_OFF);
1638 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001639 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001640 if (ENABLE_DEBUG_INTENTS) {
1641 filter.addAction(DebugIntents.DELETE_DATABASE);
1642 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1643 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001646 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001648
1649 if (mLauncherCallbacks != null) {
1650 mLauncherCallbacks.onAttachedToWindow();
1651 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653
1654 @Override
1655 public void onDetachedFromWindow() {
1656 super.onDetachedFromWindow();
1657 mVisible = false;
1658
Adam Cohend113e0c2010-11-11 10:48:05 -08001659 if (mAttached) {
1660 unregisterReceiver(mReceiver);
1661 mAttached = false;
1662 }
Winson Chungb745afb2015-03-02 11:51:23 -08001663 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001664
1665 if (mLauncherCallbacks != null) {
1666 mLauncherCallbacks.onDetachedFromWindow();
1667 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 }
1669
1670 public void onWindowVisibilityChanged(int visibility) {
1671 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001672 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001673 // The following code used to be in onResume, but it turns out onResume is called when
1674 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1675 // is a more appropriate event to handle
1676 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 if (!mWorkspaceLoading) {
1678 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001679 // We want to let Launcher draw itself at least once before we force it to build
1680 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001681 // apps is nice and speedy.
1682 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001683 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001684 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001685 if (mStarted) return;
1686 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001687 // We delay the layer building a bit in order to give
1688 // other message processing a time to run. In particular
1689 // this avoids a delay in hiding the IME if it was
1690 // currently shown, because doing that may involve
1691 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001692 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001693 final ViewTreeObserver.OnDrawListener listener = this;
1694 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001695 public void run() {
1696 if (mWorkspace != null &&
1697 mWorkspace.getViewTreeObserver() != null) {
1698 mWorkspace.getViewTreeObserver().
1699 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001700 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001701 }
1702 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001703 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 }
1705 });
1706 }
1707 clearTypedText();
1708 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 }
1710
Adam Cohen091440a2015-03-18 14:16:05 -07001711 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 mHandler.removeMessages(ADVANCE_MSG);
1713 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1714 mHandler.sendMessageDelayed(msg, delay);
1715 mAutoAdvanceSentTime = System.currentTimeMillis();
1716 }
1717
Adam Cohen091440a2015-03-18 14:16:05 -07001718 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1720 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1721 mAutoAdvanceRunning = autoAdvanceRunning;
1722 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001723 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 sendAdvanceMessage(delay);
1725 } else {
1726 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1729 }
1730 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001731 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 }
1733 }
1734 }
1735
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1737
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 if (msg.what == ADVANCE_MSG) {
1741 int i = 0;
1742 for (View key: mWidgetsToAdvance.keySet()) {
1743 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001744 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001746 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 public void run() {
1748 ((Advanceable) v).advance();
1749 }
1750 }, delay);
1751 }
1752 i++;
1753 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001754 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001758 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001759
Winsonc0b52fe2015-09-09 16:38:15 -07001760 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001761 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1763 if (v instanceof Advanceable) {
1764 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001765 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001766 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768 }
1769
Winsonc0b52fe2015-09-09 16:38:15 -07001770 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 if (mWidgetsToAdvance.containsKey(hostView)) {
1772 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001773 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 }
1776
Hyunyoung Song3f471442015-04-08 19:01:34 -07001777 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001778 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1779 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781
Winson Chunga6945242014-01-08 14:04:34 -08001782 public DragLayer getDragLayer() {
1783 return mDragLayer;
1784 }
1785
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001786 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001787 return mAppsView;
1788 }
1789
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 public WidgetsContainerView getWidgetsView() {
1791 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001792 }
1793
Winson Chunga6945242014-01-08 14:04:34 -08001794 public Workspace getWorkspace() {
1795 return mWorkspace;
1796 }
1797
1798 public Hotseat getHotseat() {
1799 return mHotseat;
1800 }
1801
Jorim Jaggid017f882014-01-14 17:08:48 -08001802 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001803 return mOverviewPanel;
1804 }
1805
Winson Chung006ee262015-08-03 14:40:11 -07001806 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001807 return mSearchDropTargetBar;
1808 }
1809
Tony Wickham34d2c912015-09-11 09:27:58 -07001810 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1811 return mAppInfoDropTargetBar;
1812 }
1813
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001814 public LauncherAppWidgetHost getAppWidgetHost() {
1815 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Winson Chunga9abd0e2010-10-27 17:18:37 -07001818 public LauncherModel getModel() {
1819 return mModel;
1820 }
1821
Adam Cohen79d90c52016-04-22 13:29:20 -07001822 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001823 return mSharedPrefs;
1824 }
1825
Adam Cohen2e6da152015-05-06 11:42:25 -07001826 public DeviceProfile getDeviceProfile() {
1827 return mDeviceProfile;
1828 }
1829
Bjorn Bringertc459e522013-06-07 19:36:01 +01001830 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 getWindow().closeAllPanels();
1832
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001833 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001834 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 @Override
1838 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001839 long startTime = 0;
1840 if (DEBUG_RESUME_TIME) {
1841 startTime = System.currentTimeMillis();
1842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 super.onNewIntent(intent);
1844
1845 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001846 Folder openFolder = mWorkspace.getOpenFolder();
1847 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1848 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1849 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1850 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1851 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001852 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001853 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001854
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 if (mWorkspace == null) {
1856 // Can be cases where mWorkspace is null, this prevents a NPE
1857 return;
1858 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 // In all these cases, only animate if we're already on home
1860 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001861
Sunny Goyal935fca12015-10-13 10:19:01 -07001862 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 exitSpringLoadedDragMode();
1864
1865 // If we are already on home, then just animate back to the workspace,
1866 // otherwise, just wait until onResume to set the state back to Workspace
1867 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001868 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001869 } else {
1870 mOnResumeState = State.WORKSPACE;
1871 }
1872
1873 final View v = getWindow().peekDecorView();
1874 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001875 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 INPUT_METHOD_SERVICE);
1877 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1878 }
1879
Winson Chungb745afb2015-03-02 11:51:23 -08001880 // Reset the apps view
1881 if (!alreadyOnHome && mAppsView != null) {
1882 mAppsView.scrollToTop();
1883 }
1884
Hyunyoung Song3f471442015-04-08 19:01:34 -07001885 // Reset the widgets view
1886 if (!alreadyOnHome && mWidgetsView != null) {
1887 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001889
Adam Cohen9211d422014-10-07 18:14:53 -07001890 if (mLauncherCallbacks != null) {
1891 mLauncherCallbacks.onHomeIntent();
1892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
Adam Cohened307df2013-10-02 09:37:31 -07001894
Adam Cohen9211d422014-10-07 18:14:53 -07001895 if (mLauncherCallbacks != null) {
1896 mLauncherCallbacks.onNewIntent(intent);
1897 }
Winson15f8b172015-08-19 11:02:39 -07001898
1899 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1900 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1901 // animation.
1902 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001903 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1904 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001905 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1906 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001907
1908 // We use this flag to suppress noisy callbacks above custom content state
1909 // from onResume.
1910 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001911 mWorkspace.post(new Runnable() {
1912 @Override
1913 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001914 if (mWorkspace != null) {
1915 mWorkspace.moveToDefaultScreen(true);
1916 }
Winson15f8b172015-08-19 11:02:39 -07001917 }
1918 });
1919 }
1920 }
1921
1922 if (DEBUG_RESUME_TIME) {
1923 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1924 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001928 public void onRestoreInstanceState(Bundle state) {
1929 super.onRestoreInstanceState(state);
1930 for (int page: mSynchronouslyBoundPages) {
1931 mWorkspace.restoreInstanceStateForChild(page);
1932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934
1935 @Override
1936 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001937 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001938 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1939 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001940 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001941 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942
Michael Jurka883f55b2010-10-21 15:47:14 -07001943 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001944 // We close any open folder since it will not be re-opened, and we need to make sure
1945 // this state is reflected.
1946 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1947 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001950 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001951 ContentValues itemValues = new ContentValues();
1952 mPendingAddInfo.writeToValues(itemValues);
1953 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001955 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
Hyunyoung Song3f471442015-04-08 19:01:34 -07001958 // Save the current widgets tray?
1959 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001960
1961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onSaveInstanceState(outState);
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 @Override
1967 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Winson Chunge7a03942011-08-05 15:05:12 -07001970 // Remove all pending runnables
1971 mHandler.removeMessages(ADVANCE_MSG);
1972 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001973 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001974
Winson Chungcd2b0142011-06-08 16:02:26 -07001975 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001976 // It's possible to receive onDestroy after a new Launcher activity has
1977 // been created. In this case, don't interfere with the new Launcher.
1978 if (mModel.isCurrentCallbacks(this)) {
1979 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001980 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001981 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001982
Sunny Goyal745bad92016-05-02 10:54:12 -07001983 if (mRotationPrefChangeHandler != null) {
1984 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001988 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001990 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001992 mAppWidgetHost = null;
1993
1994 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995
1996 TextKeyListener.getInstance().release();
1997
Tony Wickhame2217252016-03-22 16:34:23 -07001998 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1999 .removeAccessibilityStateChangeListener(this);
2000
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002001 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002002
Michael Jurka2ecf9952012-06-18 12:52:28 -07002003 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002004
2005 if (mLauncherCallbacks != null) {
2006 mLauncherCallbacks.onDestroy();
2007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
Adam Cohena9cf38f2011-05-02 15:36:58 -07002010 public DragController getDragController() {
2011 return mDragController;
2012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 @Override
2015 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002016 onStartForResult(requestCode);
2017 super.startActivityForResult(intent, requestCode);
2018 }
2019
2020 @Override
2021 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2022 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2023 onStartForResult(requestCode);
2024 try {
2025 super.startIntentSenderForResult(intent, requestCode,
2026 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2027 } catch (IntentSender.SendIntentException e) {
2028 throw new ActivityNotFoundException();
2029 }
2030 }
2031
2032 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002033 if (requestCode >= 0) {
2034 setWaitingForResult(true);
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
Winson Chung70d72102011-08-12 11:24:35 -07002038 /**
2039 * Indicates that we want global search for this activity by setting the globalSearch
2040 * argument for {@link #startSearch} to true.
2041 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002043 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002045
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 if (initialQuery == null) {
2047 // Use any text typed in the launcher as the initial query
2048 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 if (appSearchData == null) {
2051 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002052 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
Winson Chungadf0c182012-08-23 14:59:07 -07002054 Rect sourceBounds = new Rect();
2055 if (mSearchDropTargetBar != null) {
2056 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2057 }
Romain Guycbb89e42009-06-08 15:52:54 -07002058
Ian Parkinson047036e2014-09-01 15:40:53 +01002059 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002060 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002061 if (clearTextImmediately) {
2062 clearTypedText();
2063 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002064
2065 // We need to show the workspace after starting the search
2066 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002067 }
2068
Ian Parkinson047036e2014-09-01 15:40:53 +01002069 /**
2070 * Start a text search.
2071 *
2072 * @return {@code true} if the search will start immediately, so any further keypresses
2073 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2074 * to buffer keypresses.
2075 */
2076 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002078 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2079 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2080 sourceBounds);
2081 }
2082
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002085 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002086 }
2087
2088 /**
2089 * Starts the global search activity. This code is a copied from SearchManager
2090 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002093 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002094 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2095 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2096 if (globalSearchActivity == null) {
2097 Log.w(TAG, "No global search activity found.");
2098 return;
2099 }
2100 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2102 intent.setComponent(globalSearchActivity);
2103 // Make sure that we have a Bundle to put source in
2104 if (appSearchData == null) {
2105 appSearchData = new Bundle();
2106 } else {
2107 appSearchData = new Bundle(appSearchData);
2108 }
Adam Cohen9211d422014-10-07 18:14:53 -07002109 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002110 if (!appSearchData.containsKey("source")) {
2111 appSearchData.putString("source", getPackageName());
2112 }
2113 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2114 if (!TextUtils.isEmpty(initialQuery)) {
2115 intent.putExtra(SearchManager.QUERY, initialQuery);
2116 }
2117 if (selectInitialQuery) {
2118 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2119 }
2120 intent.setSourceBounds(sourceBounds);
2121 try {
2122 startActivity(intent);
2123 } catch (ActivityNotFoundException ex) {
2124 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
Yura4f93ec62014-02-04 14:15:21 +00002128 public boolean isOnCustomContent() {
2129 return mWorkspace.isOnOrMovingToCustomContent();
2130 }
2131
Winson Chung70d72102011-08-12 11:24:35 -07002132 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002133 public boolean onPrepareOptionsMenu(Menu menu) {
2134 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002135 if (mLauncherCallbacks != null) {
2136 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2137 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002138 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002141 @Override
2142 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002143 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002144 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002145 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002146 }
2147
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 public boolean isWorkspaceLocked() {
2149 return mWorkspaceLoading || mWaitingForResult;
2150 }
2151
Adam Cohen517a7f52014-03-01 12:12:59 -08002152 public boolean isWorkspaceLoading() {
2153 return mWorkspaceLoading;
2154 }
2155
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002156 private void setWorkspaceLoading(boolean value) {
2157 boolean isLocked = isWorkspaceLocked();
2158 mWorkspaceLoading = value;
2159 if (isLocked != isWorkspaceLocked()) {
2160 onWorkspaceLockedChanged();
2161 }
2162 }
2163
2164 private void setWaitingForResult(boolean value) {
2165 boolean isLocked = isWorkspaceLocked();
2166 mWaitingForResult = value;
2167 if (isLocked != isWorkspaceLocked()) {
2168 onWorkspaceLockedChanged();
2169 }
2170 }
2171
Adam Cohen9211d422014-10-07 18:14:53 -07002172 protected void onWorkspaceLockedChanged() {
2173 if (mLauncherCallbacks != null) {
2174 mLauncherCallbacks.onWorkspaceLockedChanged();
2175 }
2176 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002177
Michael Jurka0280c3b2010-09-17 15:00:07 -07002178 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2182 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002183 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002185
Tony Wickhama0628cc2015-10-14 15:23:04 -07002186 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002187 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002188 if (LOGD) {
2189 Log.d(TAG, "Adding widget from drop");
2190 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002191 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2192 }
2193
Sunny Goyale6b63a32015-01-16 16:14:29 -08002194 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002195 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2196 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002197 if (appWidgetInfo.configure != null) {
2198 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002199 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002200
Bjorn Bringert7984c942009-12-09 15:38:25 +00002201 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002202 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2203 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002206 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002207 Runnable onComplete = new Runnable() {
2208 @Override
2209 public void run() {
2210 // Exit spring loaded mode if necessary after adding the widget
2211 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2212 null);
2213 }
2214 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002216 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002217 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219 }
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002221 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002222 // Close any folders that may be open.
2223 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002224 mWorkspace.moveToCustomContentScreen(animate);
2225 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002226
2227 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2228 int[] cell, int spanX, int spanY) {
2229 switch (info.itemType) {
2230 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2231 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2232 int span[] = new int[2];
2233 span[0] = spanX;
2234 span[1] = spanY;
2235 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2236 container, screenId, cell, span);
2237 break;
2238 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2239 processShortcutFromDrop(info.componentName, container, screenId, cell);
2240 break;
2241 default:
2242 throw new IllegalStateException("Unknown item type: " + info.itemType);
2243 }
2244 }
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 /**
2247 * Process a shortcut drop.
2248 *
2249 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002253 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2254 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258
2259 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.cellX = cell[0];
2261 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263
2264 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2265 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002266 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 }
2268
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 /**
2270 * Process a widget drop.
2271 *
2272 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002276 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2277 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 mPendingAddInfo.minSpanX = info.minSpanX;
2282 mPendingAddInfo.minSpanY = info.minSpanY;
2283
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.cellX = cell[0];
2286 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002288 if (span != null) {
2289 mPendingAddInfo.spanX = span[0];
2290 mPendingAddInfo.spanY = span[1];
2291 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 AppWidgetHostView hostView = info.boundWidget;
2294 int appWidgetId;
2295 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002296 // In the case where we've prebound the widget, we remove it from the DragLayer
2297 if (LOGD) {
2298 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2299 }
2300 getDragLayer().removeView(hostView);
2301
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002303 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002304
2305 // Clear the boundWidget so that it doesn't get destroyed.
2306 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002308 // In this case, we either need to start an activity to get permission to bind
2309 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002311 Bundle options = info.bindOptions;
2312
Sunny Goyalffe83f12014-08-14 17:39:34 -07002313 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2314 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002315 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002316 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002318 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002319 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002322 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2323 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002324 // TODO: we need to make sure that this accounts for the options bundle.
2325 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002326 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2327 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
2330
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002333 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 folderInfo.title = getText(R.string.folder_name);
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002337 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2338 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339
2340 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 FolderIcon newFolder =
2342 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002345 // Force measure the new folder icon
2346 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2347 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002348 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 }
Romain Guycbb89e42009-06-08 15:52:54 -07002350
Winsonc0b52fe2015-09-09 16:38:15 -07002351 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002352 * Unbinds the view for the specified item, and removes the item and all its children.
2353 *
2354 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002355 * @param itemInfo the {@link ItemInfo} for this view.
2356 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002357 */
Winson2949fb52015-09-24 09:56:11 -07002358 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002359 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002360 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002361 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2362 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002363 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002364 } else {
2365 mWorkspace.removeWorkspaceItem(v);
2366 }
Winsonc0b52fe2015-09-09 16:38:15 -07002367 if (deleteFromDb) {
2368 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2369 }
2370 } else if (itemInfo instanceof FolderInfo) {
2371 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002372 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 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002393 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002395 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002397 // Deleting an app widget ID is a void call but writes to disk before returning
2398 // to the caller...
2399 new AsyncTask<Void, Void, Void>() {
2400 public Void doInBackground(Void ... args) {
2401 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2402 return null;
2403 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002404 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002405 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002406 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
2408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 @Override
2410 public boolean dispatchKeyEvent(KeyEvent event) {
2411 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2412 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002415 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002416 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002417 dumpState();
2418 return true;
2419 }
2420 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 }
2422 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2423 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 return true;
2426 }
2427 }
2428
2429 return super.dispatchKeyEvent(event);
2430 }
2431
Joe Onorato88ec0992009-11-19 13:16:06 -08002432 @Override
2433 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002434 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2435 return;
2436 }
2437
Sunny Goyal45478022015-06-08 16:52:41 -07002438 if (mDragController.isDragging()) {
2439 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002440 return;
2441 }
2442
Winson Chungb745afb2015-03-02 11:51:23 -08002443 if (isAppsViewVisible()) {
2444 showWorkspace(true);
2445 } else if (isWidgetsViewVisible()) {
2446 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002447 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002448 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002449 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002450 Folder openFolder = mWorkspace.getOpenFolder();
2451 if (openFolder.isEditingName()) {
2452 openFolder.dismissEditingName();
2453 } else {
2454 closeFolder();
2455 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002456 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002457 mWorkspace.exitWidgetResizeMode();
2458
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002459 // Back button is a no-op here, but give at least some feedback for the button press
2460 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002461 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002462 }
2463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002465 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002466 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002467 @Override
2468 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002469 if (mAppWidgetHost != null) {
2470 mAppWidgetHost.startListening();
2471 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002472
2473 // Recreate the QSB, as the widget has been reset.
2474 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002475 }
2476
2477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 * Launches the intent referred by the clicked shortcut.
2479 *
2480 * @param v The view representing the clicked shortcut.
2481 */
2482 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002483 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2484 // view has detached (it's possible for this to happen if the view is removed mid touch).
2485 if (v.getWindowToken() == null) {
2486 return;
2487 }
2488
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002489 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 return;
2491 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002492
Adam Cohen1697b792013-09-17 19:08:21 -07002493 if (v instanceof Workspace) {
2494 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002495 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 }
2497 return;
2498 }
2499
2500 if (v instanceof CellLayout) {
2501 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002502 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2503 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002504 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002505 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002509 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002515 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002517 } else if (tag instanceof AppInfo) {
2518 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002519 } else if (tag instanceof LauncherAppWidgetInfo) {
2520 if (v instanceof PendingAppWidgetHostView) {
2521 onClickPendingWidget((PendingAppWidgetHostView) v);
2522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525
Sunny Goyal70660032015-05-14 00:07:08 -07002526 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002527 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002528 return false;
2529 }
2530
Michael Jurkaaf442092010-06-10 17:01:57 -07002531 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002532 * Event handler for the app widget view which has not fully restored.
2533 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002535 if (mIsSafeModeEnabled) {
2536 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2537 return;
2538 }
2539
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002541 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002542 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002543 LauncherAppWidgetProviderInfo appWidgetInfo =
2544 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002545 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002546 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002547 mPendingAddInfo.copyFrom(info);
2548 mPendingAddWidgetId = widgetId;
2549
Sunny Goyalffe83f12014-08-14 17:39:34 -07002550 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2551 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002552 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 } else if (info.installProgress < 0) {
2554 // The install has not been queued
2555 final String packageName = info.providerName.getPackageName();
2556 showBrokenAppInstallDialog(packageName,
2557 new DialogInterface.OnClickListener() {
2558 public void onClick(DialogInterface dialog, int id) {
2559 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2560 }
2561 });
2562 } else {
2563 // Download has started.
2564 final String packageName = info.providerName.getPackageName();
2565 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
2567 }
2568
2569 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002570 * Event handler for the "grid" button that appears on the home screen, which
2571 * enters all apps mode.
2572 *
2573 * @param v The view that was clicked.
2574 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 protected void onClickAllAppsButton(View v) {
2576 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002577 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002578 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002579 true /* updatePredictedApps */, false /* focusSearchBar */);
2580 }
2581 }
2582
2583 protected void onLongClickAllAppsButton(View v) {
2584 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2585 if (!isAppsViewVisible()) {
2586 showAppsView(true /* animated */, false /* resetListToTop */,
2587 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 }
2589 }
2590
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002591 private void showBrokenAppInstallDialog(final String packageName,
2592 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002593 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 .setTitle(R.string.abandoned_promises_title)
2595 .setMessage(R.string.abandoned_promise_explanation)
2596 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2597 .setNeutralButton(R.string.abandoned_clean_this,
2598 new DialogInterface.OnClickListener() {
2599 public void onClick(DialogInterface dialog, int id) {
2600 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2601 mWorkspace.removeAbandonedPromise(packageName, user);
2602 }
2603 })
2604 .create().show();
2605 return;
2606 }
2607
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 /**
2609 * Event handler for an app shortcut click.
2610 *
2611 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2612 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002613 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2615 Object tag = v.getTag();
2616 if (!(tag instanceof ShortcutInfo)) {
2617 throw new IllegalArgumentException("Input must be a Shortcut");
2618 }
2619
2620 // Open shortcut
2621 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002622
2623 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002624 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2625 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002626 // Launch activity anyway, framework will tell the user why the app is suspended.
2627 } else {
2628 int error = R.string.activity_not_available;
2629 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2630 error = R.string.safemode_shortcut_error;
2631 }
2632 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2633 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002634 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002635 }
2636
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002638 if ((v instanceof BubbleTextView)
2639 && shortcut.isPromise()
2640 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002641 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002642 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 new DialogInterface.OnClickListener() {
2644 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002645 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 return;
2649 }
2650
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002652 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 }
2654
Adam Cohen091440a2015-03-18 14:16:05 -07002655 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002657 final ShortcutInfo shortcut;
2658 final Intent intent;
2659 if (tag instanceof ShortcutInfo) {
2660 shortcut = (ShortcutInfo) tag;
2661 intent = shortcut.intent;
2662 int[] pos = new int[2];
2663 v.getLocationOnScreen(pos);
2664 intent.setSourceBounds(new Rect(pos[0], pos[1],
2665 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002666
Sunny Goyal508da152014-08-14 10:53:27 -07002667 } else if (tag instanceof AppInfo) {
2668 shortcut = null;
2669 intent = ((AppInfo) tag).intent;
2670 } else {
2671 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2672 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673
2674 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002675 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676
2677 if (success && v instanceof BubbleTextView) {
2678 mWaitingForResume = (BubbleTextView) v;
2679 mWaitingForResume.setStayPressed(true);
2680 }
2681 }
2682
2683 /**
2684 * Event handler for a folder icon click.
2685 *
2686 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2687 */
2688 protected void onClickFolderIcon(View v) {
2689 if (LOGD) Log.d(TAG, "onClickFolder");
2690 if (!(v instanceof FolderIcon)){
2691 throw new IllegalArgumentException("Input must be a FolderIcon");
2692 }
2693
2694 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002695 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002697 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 }
Michael Jurka5130e402011-10-13 04:55:35 -07002699 }
2700
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002701 /**
2702 * Event handler for the (Add) Widgets button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002707 if (mIsSafeModeEnabled) {
2708 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2709 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002710 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002711 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002712 }
2713
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 /**
2715 * Event handler for the wallpaper picker button that appears after a long press
2716 * on the home screen.
2717 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002718 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002719 if (!Utilities.isWallapaperAllowed(this)) {
2720 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2721 return;
2722 }
2723
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002725 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2726 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal30a71612016-04-12 17:57:41 -07002727 // TODO: Start the system wallpaper picker
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002728 }
2729
2730 /**
2731 * Event handler for a click on the settings button that appears after a long press
2732 * on the home screen.
2733 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002734 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002736 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2737 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002738 }
2739
Adam Cohen61f560d2013-09-30 15:58:20 -07002740 public View.OnTouchListener getHapticFeedbackTouchListener() {
2741 if (mHapticFeedbackTouchListener == null) {
2742 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002743 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002744 @Override
2745 public boolean onTouch(View v, MotionEvent event) {
2746 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2747 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2748 }
2749 return false;
2750 }
2751 };
2752 }
2753 return mHapticFeedbackTouchListener;
2754 }
2755
Tony Wickhame2217252016-03-22 16:34:23 -07002756 @Override
2757 public void onAccessibilityStateChanged(boolean enabled) {
2758 mDragLayer.onAccessibilityStateChanged(enabled);
2759 }
2760
Adam Cohen9211d422014-10-07 18:14:53 -07002761 public void onDragStarted(View view) {
2762 if (isOnCustomContent()) {
2763 // Custom content screen doesn't participate in drag and drop. If on custom
2764 // content screen, move to default.
2765 moveWorkspaceToDefaultScreen();
2766 }
Adam Cohen9211d422014-10-07 18:14:53 -07002767 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002768
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002769 /**
2770 * Called when the user stops interacting with the launcher.
2771 * This implies that the user is now on the homescreen and is not doing housekeeping.
2772 */
Adam Cohen9211d422014-10-07 18:14:53 -07002773 protected void onInteractionEnd() {
2774 if (mLauncherCallbacks != null) {
2775 mLauncherCallbacks.onInteractionEnd();
2776 }
2777 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002778
2779 /**
2780 * Called when the user starts interacting with the launcher.
2781 * The possible interactions are:
2782 * - open all apps
2783 * - reorder an app shortcut, or a widget
2784 * - open the overview mode.
2785 * This is a good time to stop doing things that only make sense
2786 * when the user is on the homescreen and not doing housekeeping.
2787 */
Adam Cohen9211d422014-10-07 18:14:53 -07002788 protected void onInteractionBegin() {
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onInteractionBegin();
2791 }
2792 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002793
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 /** Updates the interaction state. */
2795 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002796 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002797 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002798 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2799 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002800 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002801 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 onInteractionEnd();
2804 }
2805 }
2806
Winson Chung8f1eff72015-05-28 17:33:40 -07002807 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002808 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002810 // Only launch using the new animation if the shortcut has not opted out (this is a
2811 // private contract between launcher and may be ignored in the future).
2812 boolean useLaunchAnimation = (v != null) &&
2813 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002814 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2815 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002816
Kenny Guy1317e2d2014-05-08 18:52:50 +01002817 UserHandleCompat user = null;
2818 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2819 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2820 user = userManager.getUserForSerialNumber(serialNumber);
2821 }
2822
2823 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002824 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002825 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002826 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002827 int left = 0, top = 0;
2828 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2829 if (v instanceof TextView) {
2830 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002831 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2832 if (icon != null) {
2833 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002834 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002835 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002836 width = bounds.width();
2837 height = bounds.height();
2838 }
2839 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002840 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2841 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002842 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002843 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002844 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002845 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002846 // On L devices, we use the device default slide-up transition.
2847 // On L MR1 devices, we a custom version of the slide-up transition which
2848 // doesn't have the delay present in the device default.
2849 opts = ActivityOptions.makeCustomAnimation(this,
2850 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002851 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002852 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002853 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002854
2855 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2856 // Could be launching some bookkeeping activity
2857 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002858 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002859 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002860 launcherApps.startActivityForProfile(intent.getComponent(), user,
2861 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002862 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002863 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002865 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2866 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2867 // corresponding permission. Show the appropriate permission prompt if that
2868 // is the case.
2869 if (intent.getComponent() == null
2870 && Intent.ACTION_CALL.equals(intent.getAction())
2871 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2872 PackageManager.PERMISSION_GRANTED) {
2873 // TODO: Rename sPendingAddItem to a generic name.
2874 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2875 0, (ItemInfo) tag);
2876 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2877 REQUEST_PERMISSION_CALL_PHONE);
2878 return false;
2879 }
2880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002882 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002884 "or use the exported attribute for this activity. "
2885 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002887 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002889
Winson Chungbedf9232015-07-10 12:38:30 -07002890 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002891 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002892 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2893 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2894 return false;
2895 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002896 try {
2897 success = startActivity(v, intent, tag);
2898 } catch (ActivityNotFoundException e) {
2899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2900 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2901 }
2902 return success;
2903 }
2904
Adam Cohen268c4752012-06-06 17:47:33 -07002905 /**
2906 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2907 * in the DragLayer in the exact absolute location of the original FolderIcon.
2908 */
2909 private void copyFolderIconToImage(FolderIcon fi) {
2910 final int width = fi.getMeasuredWidth();
2911 final int height = fi.getMeasuredHeight();
2912
2913 // Lazy load ImageView, Bitmap and Canvas
2914 if (mFolderIconImageView == null) {
2915 mFolderIconImageView = new ImageView(this);
2916 }
2917 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2918 mFolderIconBitmap.getHeight() != height) {
2919 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2920 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2921 }
2922
2923 DragLayer.LayoutParams lp;
2924 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2925 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2926 } else {
2927 lp = new DragLayer.LayoutParams(width, height);
2928 }
2929
Adam Cohen307fe232012-08-16 17:55:58 -07002930 // The layout from which the folder is being opened may be scaled, adjust the starting
2931 // view size by this scale factor.
2932 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002933 lp.customPosition = true;
2934 lp.x = mRectForFolderAnimation.left;
2935 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002936 lp.width = (int) (scale * width);
2937 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002938
2939 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2940 fi.draw(mFolderIconCanvas);
2941 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002942 if (fi.getFolder() != null) {
2943 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2944 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002945 }
Adam Cohen268c4752012-06-06 17:47:33 -07002946 // Just in case this image view is still in the drag layer from a previous animation,
2947 // we remove it and re-add it.
2948 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2949 mDragLayer.removeView(mFolderIconImageView);
2950 }
2951 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002952 if (fi.getFolder() != null) {
2953 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002954 }
Adam Cohen268c4752012-06-06 17:47:33 -07002955 }
2956
Adam Cohen37b2a492016-04-06 18:36:06 -07002957 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002958 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002959 FolderInfo info = (FolderInfo) fi.getTag();
2960 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2961 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002962 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2963 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002964 }
2965
Adam Cohen268c4752012-06-06 17:47:33 -07002966 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2967 copyFolderIconToImage(fi);
2968 fi.setVisibility(View.INVISIBLE);
2969
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002970 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2971 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002972 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002973 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2974 }
2975 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002976 oa.start();
2977 }
2978
Sunny Goyal935fca12015-10-13 10:19:01 -07002979 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002980 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002981 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002982
Adam Cohen268c4752012-06-06 17:47:33 -07002983 // We remove and re-draw the FolderIcon in-case it has changed
2984 mDragLayer.removeView(mFolderIconImageView);
2985 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002986
2987 if (cl != null) {
2988 cl.clearFolderLeaveBehind();
2989 }
2990
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002991 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002992 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002993 oa.addListener(new AnimatorListenerAdapter() {
2994 @Override
2995 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002996 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002997 // Remove the ImageView copy of the FolderIcon and make the original visible.
2998 mDragLayer.removeView(mFolderIconImageView);
2999 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 }
3001 }
3002 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003003 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 if (!animate) {
3005 oa.end();
3006 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003007 }
3008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003010 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 * is animated relative to the specified View. If the View is null, no animation
3012 * is played.
3013 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003014 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003016 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003017
Adam Cohenfb91f302012-06-11 15:45:18 -07003018 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003019 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3020 if (openFolder != null && openFolder != folder) {
3021 // Close any open folder before opening a folder.
3022 closeFolder();
3023 }
3024
Adam Cohena9cf38f2011-05-02 15:36:58 -07003025 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026
Adam Cohena9cf38f2011-05-02 15:36:58 -07003027 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028
Sunny Goyalf4066152015-04-15 09:42:19 -07003029 // While the folder is open, the position of the icon cannot change.
3030 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3031
Adam Cohen4554ee12011-08-03 16:13:21 -07003032 // Just verify that the folder hasn't already been added to the DragLayer.
3033 // There was a one-off crash where the folder had a parent already.
3034 if (folder.getParent() == null) {
3035 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003036 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003037 } else {
3038 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3039 folder.getParent() + ").");
3040 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003041 folder.animateOpen();
3042
3043 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003044
3045 // Notify the accessibility manager that this folder "window" has appeared and occluded
3046 // the workspace items
3047 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3048 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003049 }
3050
3051 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003052 closeFolder(true);
3053 }
3054
3055 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003056 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003058 if (folder.isEditingName()) {
3059 folder.dismissEditingName();
3060 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003061 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 }
3063 }
3064
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003066 animate &= !Utilities.isPowerSaverOn(this);
3067
Adam Cohen4554ee12011-08-03 16:13:21 -07003068 folder.getInfo().opened = false;
3069
3070 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3071 if (parent != null) {
3072 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003073 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003074 if (fi != null) {
3075 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3076 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003077 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 if (animate) {
3079 folder.animateClosed();
3080 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003081 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003082 }
Winson Chung83ca4802013-04-12 15:10:52 -07003083
Sunny Goyal935fca12015-10-13 10:19:01 -07003084 // Notify the accessibility manager that this folder "window" has disappeared and no
3085 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003086 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 }
3088
Tony Wickhamdadb3042016-02-24 11:07:00 -08003089 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003091 if (!isDraggingEnabled()) return false;
3092 if (isWorkspaceLocked()) return false;
3093 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094
Winson Chung76648c52015-07-10 14:33:23 -07003095 if (v == mAllAppsButton) {
3096 onLongClickAllAppsButton(v);
3097 return true;
3098 }
3099
Adam Cohen1697b792013-09-17 19:08:21 -07003100 if (v instanceof Workspace) {
3101 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003102 if (!mWorkspace.isTouchActive()) {
3103 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003104 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3105 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3106 return true;
3107 } else {
3108 return false;
3109 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003110 } else {
3111 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003112 }
Adam Cohen1697b792013-09-17 19:08:21 -07003113 }
3114
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003115 CellLayout.CellInfo longClickCellInfo = null;
3116 View itemUnderLongClick = null;
3117 if (v.getTag() instanceof ItemInfo) {
3118 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003119 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003120 itemUnderLongClick = longClickCellInfo.cell;
3121 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
3123
Winson Chung3d503fb2011-07-13 17:25:49 -07003124 // The hotseat touch handling does not go through Workspace, and we always allow long press
3125 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003126 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003127 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003128 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003129 // User long pressed on empty space
3130 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3131 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003132 if (mWorkspace.isInOverviewMode()) {
3133 mWorkspace.startReordering(v);
3134 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003135 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003138 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3139 mHotseat.getOrderInHotseat(
3140 longClickCellInfo.cellX,
3141 longClickCellInfo.cellY));
3142 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003144 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146 }
3147 }
3148 return true;
3149 }
3150
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 boolean isHotseatLayout(View layout) {
3152 return mHotseat != null && layout != null &&
3153 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3154 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 /**
3157 * Returns the CellLayout of the specified container at the specified screen.
3158 */
Sunny Goyal92820592015-03-02 11:31:35 -08003159 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3161 if (mHotseat != null) {
3162 return mHotseat.getLayout();
3163 } else {
3164 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003165 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003167 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003168 }
3169 }
3170
Winson Chungb745afb2015-03-02 11:51:23 -08003171 /**
3172 * For overridden classes.
3173 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003174 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003175 return isAppsViewVisible();
3176 }
3177
3178 public boolean isAppsViewVisible() {
3179 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3180 }
3181
3182 public boolean isWidgetsViewVisible() {
3183 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003184 }
3185
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003186 private void setWorkspaceBackground(int background) {
3187 switch (background) {
3188 case WORKSPACE_BACKGROUND_TRANSPARENT:
3189 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3190 break;
3191 case WORKSPACE_BACKGROUND_BLACK:
3192 getWindow().setBackgroundDrawable(null);
3193 break;
3194 default:
3195 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3196 }
Craig Mautner360310b2012-10-26 15:13:08 -07003197 }
3198
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003199 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003200 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3201 int curflags = getWindow().getAttributes().flags
3202 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3203 if (wpflags != curflags) {
3204 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3205 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003206 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003207 }
3208
Michael Jurkae326f182011-11-21 14:05:46 -08003209 @Override
3210 public void onTrimMemory(int level) {
3211 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003212 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3213 // The widget preview db can result in holding onto over
3214 // 3MB of memory for caching which isn't necessary.
3215 SQLiteDatabase.releaseMemory();
3216
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003217 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003218 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003219 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003220 if (mLauncherCallbacks != null) {
3221 mLauncherCallbacks.onTrimMemory(level);
3222 }
Michael Jurkae326f182011-11-21 14:05:46 -08003223 }
3224
Winson5c6bdbb2015-09-03 11:36:19 -07003225 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003226 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003227 }
3228
Winson5c6bdbb2015-09-03 11:36:19 -07003229 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003230 boolean changed = mState != State.WORKSPACE ||
3231 mWorkspace.getState() != Workspace.State.NORMAL;
3232 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003233 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003234 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003235 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003236
Michael Jurkab3e22d92011-10-31 15:58:33 -07003237 // Set focus to the AppsCustomize button
3238 if (mAllAppsButton != null) {
3239 mAllAppsButton.requestFocus();
3240 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003241 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003242
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003243 // Change the state *after* we've called all the transition code
3244 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003245
Winson Chung5fb63472011-02-02 17:03:37 -08003246 // Resume the auto-advance of widgets
3247 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003248 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003249
Winson Chung0f785722015-04-08 10:27:49 -07003250 if (changed) {
3251 // Send an accessibility event to announce the context change
3252 getWindow().getDecorView()
3253 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003254 }
Winson5c6bdbb2015-09-03 11:36:19 -07003255 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003256 }
3257
Winsone9f27272015-10-13 10:47:51 -07003258 /**
3259 * Shows the overview button.
3260 */
Adam Cohened307df2013-10-02 09:37:31 -07003261 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003262 showOverviewMode(animated, false);
3263 }
3264
3265 /**
3266 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3267 * onto one of the overview panel buttons.
3268 */
3269 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3270 Runnable postAnimRunnable = null;
3271 if (requestButtonFocus) {
3272 postAnimRunnable = new Runnable() {
3273 @Override
3274 public void run() {
3275 // Hitting the menu button when in touch mode does not trigger touch mode to
3276 // be disabled, so if requested, force focus on one of the overview panel
3277 // buttons.
3278 mOverviewPanel.requestFocusFromTouch();
3279 }
3280 };
3281 }
Adam Cohened307df2013-10-02 09:37:31 -07003282 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003283 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003284 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003285 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 /**
3289 * Shows the apps view.
3290 */
Winson Chung76648c52015-07-10 14:33:23 -07003291 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3292 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003293 if (resetListToTop) {
3294 mAppsView.scrollToTop();
3295 }
Winson Chung4ac30062015-05-08 17:34:17 -07003296 if (updatePredictedApps) {
3297 tryAndUpdatePredictedApps();
3298 }
Winson Chung76648c52015-07-10 14:33:23 -07003299 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003300 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301
Winson Chungb745afb2015-03-02 11:51:23 -08003302 /**
3303 * Shows the widgets view.
3304 */
3305 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003306 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003307 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003308 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003309 }
Winson Chung76648c52015-07-10 14:33:23 -07003310 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311
3312 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003313 @Override
3314 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003315 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003316 }
3317 });
Winson Chungb745afb2015-03-02 11:51:23 -08003318 }
3319
3320 /**
3321 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 *
3323 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003324 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003325 // TODO: calling method should use the return value so that when {@code false} is returned
3326 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003327 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003328 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3329 mState != State.WIDGETS_SPRING_LOADED) {
3330 return false;
3331 }
3332 if (toState != State.APPS && toState != State.WIDGETS) {
3333 return false;
3334 }
Winson Chungb745afb2015-03-02 11:51:23 -08003335
3336 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003337 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3338 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003339 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003340 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003341 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003344 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003345
3346 // Pause the auto-advance of widgets until we are out of AllApps
3347 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003348 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 closeFolder();
3350
3351 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003352 getWindow().getDecorView()
3353 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003354 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 }
3356
Winson Chungcd99cd32015-04-29 11:03:24 -07003357 /**
3358 * Updates the workspace and interaction state on state change, and return the animation to this
3359 * new state.
3360 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003361 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003362 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003363 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003364 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003365 updateInteraction(fromState, toState);
3366 return anim;
3367 }
3368
Jun Mukaif0033da2015-08-04 18:34:30 -07003369 public void onLauncherClingShown() {
3370 // When a launcher cling appears, it should cover the underlying layers, so their focus
3371 // should be blocked.
3372 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3373 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3374 }
3375 }
3376
3377 public void onLauncherClingDismissed() {
3378 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3379 }
3380
Hyunyoung Song3f471442015-04-08 19:01:34 -07003381 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003382 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003383 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003384 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003385 }
Winson Chungb745afb2015-03-02 11:51:23 -08003386
Winson Chung006ee262015-08-03 14:40:11 -07003387 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003388 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003389 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003390
3391 if (isAppsViewVisible()) {
3392 mState = State.APPS_SPRING_LOADED;
3393 } else if (isWidgetsViewVisible()) {
3394 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003395 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003396 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003397 } else {
3398 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003399 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003400 }
Adam Cohen7777d962011-08-18 18:58:38 -07003401
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003403 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003405
Winson Chunge7a03942011-08-05 15:05:12 -07003406 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003407 @Override
3408 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003409 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3411 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003412 // Before we show workspace, hide all apps again because
3413 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3414 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003416 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003417 } else {
3418 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003419 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003420 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003421 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003422 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423
Tony Wickhame0c33232016-02-08 11:37:04 -08003424 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003425 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3426 || mState == State.WIDGETS_SPRING_LOADED;
3427 }
3428
Michael Jurkad3ef3062010-11-23 16:23:58 -08003429 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003430 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003431 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003432 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003433 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003434 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003435 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3436 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003438 }
3439
Winson Chung4ac30062015-05-08 17:34:17 -07003440 /**
3441 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3442 * resumed.
3443 */
3444 private void tryAndUpdatePredictedApps() {
3445 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003446 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003447 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003448 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003449 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003450 }
3451 }
3452 }
3453
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 void lockAllApps() {
3455 // TODO
3456 }
3457
3458 void unlockAllApps() {
3459 // TODO
3460 }
3461
Winsonf768d932015-09-25 16:12:35 -07003462 public boolean launcherCallbacksProvidesSearch() {
3463 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3464 }
3465
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003466 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003467 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003468 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003469 }
3470
Adam Cohen24ce0b32014-01-14 16:18:14 -08003471 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003472 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3473 if (searchProvider == null) {
3474 return null;
3475 }
3476
3477 Bundle opts = new Bundle();
3478 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003479 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003480
Tony Wickham3a3517f2015-10-06 11:27:04 -07003481 // Determine the min and max dimensions of the widget.
3482 LauncherAppState app = LauncherAppState.getInstance();
3483 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3484 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3485 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003486 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3487 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003488 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003489 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003490 int minWidth = maxWidth;
3491 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003492 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3493 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3494 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3495 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3496 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003497 }
3498 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3499 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3500 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3501 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003502 if (LOGD) {
3503 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3504 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3505 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003506
Tony Wickham775455c2015-10-16 09:49:32 -07003507 if (mLauncherCallbacks != null) {
3508 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3509 }
3510
Sunny Goyalf7258242015-10-19 16:59:07 -07003511 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003512 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003513 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003514 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003515 || (widgetInfo == null)
3516 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003517 // A valid widget is not already bound.
3518 if (widgetId > -1) {
3519 mAppWidgetHost.deleteAppWidgetId(widgetId);
3520 widgetId = -1;
3521 }
3522
3523 // Try to bind a new widget
3524 widgetId = mAppWidgetHost.allocateAppWidgetId();
3525
3526 if (!AppWidgetManagerCompat.getInstance(this)
3527 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3528 mAppWidgetHost.deleteAppWidgetId(widgetId);
3529 widgetId = -1;
3530 }
3531
Sunny Goyalf7258242015-10-19 16:59:07 -07003532 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003533 .putInt(QSB_WIDGET_ID, widgetId)
3534 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003535 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003536 }
3537
Sunny Goyal8d595092015-07-06 12:22:14 -07003538 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003539 if (widgetId != -1) {
3540 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003541 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003542 mQsb.updateAppWidgetOptions(opts);
3543 mQsb.setPadding(0, 0, 0, 0);
3544 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003545 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003546 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003547 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003548 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003549 }
3550
Sunny Goyal22235bc2015-04-03 09:23:43 -07003551 private void reinflateQSBIfNecessary() {
3552 if (mQsb instanceof LauncherAppWidgetHostView &&
3553 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3554 mSearchDropTargetBar.removeView(mQsb);
3555 mQsb = null;
3556 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3557 }
3558 }
3559
Michael Jurkad7c28052012-04-27 15:43:36 -07003560 @Override
3561 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003562 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003563 final List<CharSequence> text = event.getText();
3564 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003565 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003566 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003567 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003568 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003569 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003570 } else if (mWorkspace != null) {
3571 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003572 } else {
3573 text.add(getString(R.string.all_apps_home_button_label));
3574 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003575 return result;
3576 }
3577
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003578 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003579 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003580 */
Adam Cohen091440a2015-03-18 14:16:05 -07003581 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003582 @Override
3583 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003584 closeSystemDialogs();
3585 }
3586 }
3587
3588 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003589 * If the activity is currently paused, signal that we need to run the passed Runnable
3590 * in onResume.
3591 *
3592 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003593 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3594 * wrong when we're not running, and if the activity comes back to what the configuration was
3595 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
3598 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003599 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003600 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003601 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003603 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003604 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003605 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003606 }
3607 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003608 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003609 return true;
3610 } else {
3611 return false;
3612 }
3613 }
3614
Michael Jurkac402cd92013-05-20 15:49:32 +02003615 private boolean waitUntilResume(Runnable run) {
3616 return waitUntilResume(run, false);
3617 }
3618
Michael Jurka1e2f4652013-07-08 18:03:46 -07003619 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003620 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003621 }
3622
Michael Jurka7607c2f2013-04-03 14:33:19 -07003623 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003624 * If the activity is currently paused, signal that we need to re-run the loader
3625 * in onResume.
3626 *
3627 * This needs to be called from incoming places where resources might have been loaded
3628 * while we are paused. That is becaues the Configuration might be wrong
3629 * when we're not running, and if it comes back to what it was when we
3630 * were paused, we are not restarted.
3631 *
3632 * Implementation of the method from LauncherModel.Callbacks.
3633 *
3634 * @return true if we are currently paused. The caller might be able to
3635 * skip some work in that case since we will come back again.
3636 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003637 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003638 public boolean setLoadOnResume() {
3639 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003640 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003641 mOnResumeNeedsLoad = true;
3642 return true;
3643 } else {
3644 return false;
3645 }
3646 }
3647
3648 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003651 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003653 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003654 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003655 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003656 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003659
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003661 * Clear any pending bind callbacks. This is called when is loader is planning to
3662 * perform a full rebind from scratch.
3663 */
3664 @Override
3665 public void clearPendingBinds() {
3666 mBindOnResumeCallbacks.clear();
3667 if (mPendingExecutor != null) {
3668 mPendingExecutor.markCompleted();
3669 mPendingExecutor = null;
3670 }
3671 }
3672
3673 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 * Refreshes the shortcuts shown on the workspace.
3675 *
3676 * Implementation of the method from LauncherModel.Callbacks.
3677 */
3678 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003679 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003680
Winson Chungd64d1762013-08-20 14:37:16 -07003681 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003682 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003683 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003684
Michael Jurka05bf644e2011-11-30 20:28:53 -08003685 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003686 if (mHotseat != null) {
3687 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003688 }
3689 }
3690
Adam Cohendcd297f2013-06-18 13:13:40 -07003691 @Override
3692 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003693 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003694
Adam Cohen5084cba2013-09-03 12:01:16 -07003695 // If there are no screens, we need to have an empty screen
3696 if (orderedScreenIds.size() == 0) {
3697 mWorkspace.addExtraEmptyScreen();
3698 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003699
3700 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003701 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003702 if (hasCustomContentToLeft()) {
3703 mWorkspace.createCustomContentContainer();
3704 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003705 }
Winson Chung64359a52013-07-08 17:17:08 -07003706 }
3707
3708 @Override
3709 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003710 int count = orderedScreenIds.size();
3711 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003712 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003713 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003714 }
3715
Winson Chungd64d1762013-08-20 14:37:16 -07003716 public void bindAppsAdded(final ArrayList<Long> newScreens,
3717 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003718 final ArrayList<ItemInfo> addAnimated,
3719 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003720 Runnable r = new Runnable() {
3721 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003722 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003723 }
3724 };
3725 if (waitUntilResume(r)) {
3726 return;
3727 }
3728
Winson Chungd64d1762013-08-20 14:37:16 -07003729 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003730 if (newScreens != null) {
3731 bindAddScreens(newScreens);
3732 }
Winson Chungd64d1762013-08-20 14:37:16 -07003733
3734 // We add the items without animation on non-visible pages, and with
3735 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003736 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003737 bindItems(addNotAnimated, 0,
3738 addNotAnimated.size(), false);
3739 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003740 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003741 bindItems(addAnimated, 0,
3742 addAnimated.size(), true);
3743 }
Winson Chungc58497e2013-09-03 17:48:37 -07003744
Winson Chung87412982013-10-03 18:34:14 -07003745 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003746 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003747
Winson Chungb745afb2015-03-02 11:51:23 -08003748 if (addedApps != null && mAppsView != null) {
3749 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003750 }
Winson Chungd64d1762013-08-20 14:37:16 -07003751 }
3752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003753 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 * Bind the items start-end from the list.
3755 *
3756 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003758 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003759 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3760 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003761 Runnable r = new Runnable() {
3762 public void run() {
3763 bindItems(shortcuts, start, end, forceAnimateIcons);
3764 }
3765 };
3766 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003767 return;
3768 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003769
Winson Chungf0c6ae02012-03-21 16:10:31 -07003770 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003771 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3772 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003773 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003774 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003775 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003776 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003778
3779 // Short circuit if we are loading dock items for a configuration which has no dock
3780 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3781 mHotseat == null) {
3782 continue;
3783 }
3784
Sunny Goyal639e9062015-08-19 19:17:06 -07003785 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 switch (item.itemType) {
3787 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3788 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003789 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003790 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003791
Winson Chung64359a52013-07-08 17:17:08 -07003792 /*
3793 * TODO: FIX collision case
3794 */
Winson Chungce376632013-07-11 16:12:41 -07003795 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3796 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3797 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003798 View v = cl.getChildAt(item.cellX, item.cellY);
3799 Object tag = v.getTag();
3800 String desc = "Collision while binding workspace item: " + item
3801 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003802 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003803 throw (new RuntimeException(desc));
3804 } else {
3805 Log.d(TAG, desc);
3806 }
Winson Chungce376632013-07-11 16:12:41 -07003807 }
Winson Chung64359a52013-07-08 17:17:08 -07003808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003810 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003811 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003812 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003813 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003815 default:
3816 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003818
3819 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3820 item.cellY, 1, 1);
3821 if (animateIcons) {
3822 // Animate all the applications up now
3823 view.setAlpha(0f);
3824 view.setScaleX(0f);
3825 view.setScaleY(0f);
3826 bounceAnims.add(createNewAppBounceAnimation(view, i));
3827 newShortcutsScreenId = item.screenId;
3828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003829 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003830
Winson Chung997a9232013-07-24 15:33:46 -07003831 if (animateIcons) {
3832 // Animate to the correct page
3833 if (newShortcutsScreenId > -1) {
3834 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003835 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003836 final Runnable startBounceAnimRunnable = new Runnable() {
3837 public void run() {
3838 anim.playTogether(bounceAnims);
3839 anim.start();
3840 }
3841 };
Winson Chung997a9232013-07-24 15:33:46 -07003842 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003843 // We post the animation slightly delayed to prevent slowdowns
3844 // when we are loading right after we return to launcher.
3845 mWorkspace.postDelayed(new Runnable() {
3846 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003847 if (mWorkspace != null) {
3848 mWorkspace.snapToPage(newScreenIndex);
3849 mWorkspace.postDelayed(startBounceAnimRunnable,
3850 NEW_APPS_ANIMATION_DELAY);
3851 }
Winson Chung94d67682013-09-25 16:29:40 -07003852 }
3853 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003854 } else {
3855 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003856 }
3857 }
Winson Chung64359a52013-07-08 17:17:08 -07003858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003860 }
3861
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3863 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3864 view.updateIcon(mIconCache);
3865 item.hostView = view;
3866 item.hostView.updateAppWidget(null);
3867 item.hostView.setOnClickListener(this);
3868 addAppWidgetToWorkspace(item, null, false);
3869 mWorkspace.requestLayout();
3870 }
3871
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 /**
3873 * Add the views for a widget to the workspace.
3874 *
3875 * Implementation of the method from LauncherModel.Callbacks.
3876 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003877 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003878 Runnable r = new Runnable() {
3879 public void run() {
3880 bindAppWidget(item);
3881 }
3882 };
3883 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003884 return;
3885 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003886
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003887 if (mIsSafeModeEnabled) {
3888 bindSafeModeWidget(item);
3889 return;
3890 }
3891
Daniel Sandler843e8602010-06-07 14:59:01 -04003892 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3893 if (DEBUG_WIDGETS) {
3894 Log.d(TAG, "bindAppWidget: " + item);
3895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003898
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003899 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3900 // If the provider is not ready, bind as a pending widget.
3901 appWidgetInfo = null;
3902 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3903 // The widget id is not valid. Try to find the widget based on the provider info.
3904 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3905 } else {
3906 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3907 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003908
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003909 // If the provider is ready, but the width is not yet restored, try to restore it.
3910 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3911 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003912 if (appWidgetInfo == null) {
3913 if (DEBUG_WIDGETS) {
3914 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3915 + " belongs to component " + item.providerName
3916 + ", as the povider is null");
3917 }
3918 LauncherModel.deleteItemFromDatabase(this, item);
3919 return;
3920 }
Sunny Goyalff572272014-07-23 13:58:07 -07003921
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003922 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003923 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003924 // Note: This assumes that the id remap broadcast is received before this step.
3925 // If that is not the case, the id remap will be ignored and user may see the
3926 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003927 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003928 pendingInfo.spanX = item.spanX;
3929 pendingInfo.spanY = item.spanY;
3930 pendingInfo.minSpanX = item.minSpanX;
3931 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003932 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003933
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003934 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3935 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3936 newWidgetId, appWidgetInfo, options);
3937
3938 // TODO consider showing a permission dialog when the widget is clicked.
3939 if (!success) {
3940 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3941 if (DEBUG_WIDGETS) {
3942 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3943 + " belongs to component " + item.providerName
3944 + ", as the launcher is unable to bing a new widget id");
3945 }
3946 LauncherModel.deleteItemFromDatabase(this, item);
3947 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003948 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003949
3950 item.appWidgetId = newWidgetId;
3951
3952 // If the widget has a configure activity, it is still needs to set it up, otherwise
3953 // the widget is ready to go.
3954 item.restoreStatus = (appWidgetInfo.configure == null)
3955 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3956 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3957
3958 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003959 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003960 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003961 // The widget was marked as UI not ready, but there is no configure activity to
3962 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003963 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3964 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003965 }
Sunny Goyalff572272014-07-23 13:58:07 -07003966 }
3967
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003968 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003969 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003970 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3971 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003972 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003973
Sunny Goyal56c73602015-09-25 12:55:01 -07003974 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003975 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003976 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003977 deleteWidgetInfo(item);
3978 return;
3979 }
3980
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003981 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003982 item.minSpanX = appWidgetInfo.minSpanX;
3983 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003984 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003985 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003986 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3987 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003988 view.updateIcon(mIconCache);
3989 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003991 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003992 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003994 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995
Daniel Sandler843e8602010-06-07 14:59:01 -04003996 if (DEBUG_WIDGETS) {
3997 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3998 + (SystemClock.uptimeMillis()-start) + "ms");
3999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004000 }
4001
Sunny Goyalff572272014-07-23 13:58:07 -07004002 /**
4003 * Restores a pending widget.
4004 *
4005 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004006 */
4007 private void completeRestoreAppWidget(final int appWidgetId) {
4008 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4009 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4010 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4011 return;
4012 }
4013
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004014 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004015 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4016
4017 mWorkspace.reinflateWidgetsIfNecessary();
4018 LauncherModel.updateItemInDatabase(this, info);
4019 }
4020
Adam Cohen1462de32012-07-24 22:34:36 -07004021 public void onPageBoundSynchronously(int page) {
4022 mSynchronouslyBoundPages.add(page);
4023 }
4024
Sunny Goyal527c7d32015-08-28 15:19:36 -07004025 @Override
4026 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4027 if (mPendingExecutor != null) {
4028 mPendingExecutor.markCompleted();
4029 }
4030 mPendingExecutor = executor;
4031 executor.attachTo(this);
4032 }
4033
4034 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4035 if (mPendingExecutor == executor) {
4036 mPendingExecutor = null;
4037 }
4038 }
4039
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040 /**
4041 * Callback saying that there aren't any more items to bind.
4042 *
4043 * Implementation of the method from LauncherModel.Callbacks.
4044 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004045 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004046 Runnable r = new Runnable() {
4047 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004048 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004049 }
4050 };
4051 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 return;
4053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 if (mSavedState != null) {
4055 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004056 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004058
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 mSavedState = null;
4060 }
4061
Adam Cohen1462de32012-07-24 22:34:36 -07004062 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004064 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004065
4066 // If we received the result of any pending adds while the loader was running (e.g. the
4067 // widget configuration forced an orientation change), process them now.
4068 if (sPendingAddItem != null) {
4069 final long screenId = completeAdd(sPendingAddItem);
4070
4071 // TODO: this moves the user to the page where the pending item was added. Ideally,
4072 // the screen would be guaranteed to exist after bind, and the page would be set through
4073 // the workspace restore process.
4074 mWorkspace.post(new Runnable() {
4075 @Override
4076 public void run() {
4077 mWorkspace.snapToScreenId(screenId);
4078 }
4079 });
4080 sPendingAddItem = null;
4081 }
4082
Sunny Goyal756adbc2015-04-16 15:20:51 -07004083 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004084
4085 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004086 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004087 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004088 }
4089
Winson Chunga0b7e862013-09-05 16:03:15 -07004090 public boolean isAllAppsButtonRank(int rank) {
4091 if (mHotseat != null) {
4092 return mHotseat.isAllAppsButtonRank(rank);
4093 }
4094 return false;
4095 }
4096
Winson Chunga2413752012-04-03 14:22:34 -07004097 private boolean canRunNewAppsAnimation() {
4098 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004099 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4100 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004101 }
4102
Winson Chungc9168342013-06-26 14:54:55 -07004103 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004104 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004105 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4106 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004107 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004108 return bounceAnim;
4109 }
4110
Adam Cohen27772732013-11-11 14:00:56 +00004111 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004112 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004113 }
4114
Tony Wickham3a3517f2015-10-06 11:27:04 -07004115 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004116 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004117 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004118 }
4119
Tony Wickham55616cd2015-09-23 14:55:17 -07004120 public int getSearchBarHeight() {
4121 if (mLauncherCallbacks != null) {
4122 return mLauncherCallbacks.getSearchBarHeight();
4123 }
4124 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4125 }
4126
Winson Chung88fa7412015-08-03 14:25:28 -07004127 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004128 if (mSearchDropTargetBar == null) {
4129 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004130 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004131 if (mQsb != null) {
4132 mSearchDropTargetBar.removeView(mQsb);
4133 mQsb = null;
4134 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004135 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004136 }
4137
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004138 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004139 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4140 * multiple calls to bind the same list.)
4141 */
4142 @Thunk ArrayList<AppInfo> mTmpAppsList;
4143 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4144 public void run() {
4145 bindAllApplications(mTmpAppsList);
4146 mTmpAppsList = null;
4147 }
4148 };
4149
4150 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004151 * Add the icons for all apps.
4152 *
4153 * Implementation of the method from LauncherModel.Callbacks.
4154 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004155 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004156 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4157 mTmpAppsList = apps;
4158 return;
4159 }
4160
Winson Chungb745afb2015-03-02 11:51:23 -08004161 if (mAppsView != null) {
4162 mAppsView.setApps(apps);
4163 }
Adam Cohen9211d422014-10-07 18:14:53 -07004164 if (mLauncherCallbacks != null) {
4165 mLauncherCallbacks.bindAllApplications(apps);
4166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 }
4168
4169 /**
4170 * A package was updated.
4171 *
4172 * Implementation of the method from LauncherModel.Callbacks.
4173 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004174 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004175 Runnable r = new Runnable() {
4176 public void run() {
4177 bindAppsUpdated(apps);
4178 }
4179 };
4180 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004181 return;
4182 }
4183
Winson Chungb745afb2015-03-02 11:51:23 -08004184 if (mAppsView != null) {
4185 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 }
4188
Sunny Goyal4390ace2014-10-13 11:33:11 -07004189 @Override
4190 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4191 Runnable r = new Runnable() {
4192 public void run() {
4193 bindWidgetsRestored(widgets);
4194 }
4195 };
4196 if (waitUntilResume(r)) {
4197 return;
4198 }
4199 mWorkspace.widgetsRestored(widgets);
4200 }
4201
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004203 * Some shortcuts were updated in the background.
4204 *
4205 * Implementation of the method from LauncherModel.Callbacks.
4206 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004207 @Override
4208 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4209 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 Runnable r = new Runnable() {
4211 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004212 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004213 }
4214 };
4215 if (waitUntilResume(r)) {
4216 return;
4217 }
4218
Sunny Goyal4390ace2014-10-13 11:33:11 -07004219 if (!updated.isEmpty()) {
4220 mWorkspace.updateShortcuts(updated);
4221 }
4222
4223 if (!removed.isEmpty()) {
4224 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4225 for (ShortcutInfo si : removed) {
4226 removedComponents.add(si.getTargetComponent());
4227 }
4228 mWorkspace.removeItemsByComponentName(removedComponents, user);
4229 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004230 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004231 }
4232 }
4233
4234 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004235 * Update the state of a package, typically related to install state.
4236 *
4237 * Implementation of the method from LauncherModel.Callbacks.
4238 */
Sunny Goyale755d462014-07-22 13:48:29 -07004239 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004240 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4241 Runnable r = new Runnable() {
4242 public void run() {
4243 bindRestoreItemsChange(updates);
4244 }
4245 };
4246 if (waitUntilResume(r)) {
4247 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004248 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004249
Sunny Goyal756adbc2015-04-16 15:20:51 -07004250 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004251 }
4252
4253 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004254 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004255 * in addition to specific applications to remove, the reason being that
4256 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004257 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004258 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004260 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004261 public void bindWorkspaceComponentsRemoved(
4262 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4263 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004264 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004265 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004266 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004267 }
Winson Chungd64d1762013-08-20 14:37:16 -07004268 };
4269 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004270 return;
4271 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004272 if (!packageNames.isEmpty()) {
4273 mWorkspace.removeItemsByPackageName(packageNames, user);
4274 }
4275 if (!components.isEmpty()) {
4276 mWorkspace.removeItemsByComponentName(components, user);
4277 }
4278 // Notify the drag controller
4279 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004280
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004281 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004282
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004283 @Override
4284 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4285 Runnable r = new Runnable() {
4286 public void run() {
4287 bindAppInfosRemoved(appInfos);
4288 }
4289 };
4290 if (waitUntilResume(r)) {
4291 return;
Joe Onorato36115782010-06-17 13:28:48 -04004292 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004293
Winson Chungdf95eb12013-10-16 14:57:07 -07004294 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004295 if (mAppsView != null) {
4296 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004298 }
Joe Onoratobe386092009-11-17 17:32:16 -08004299
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004300 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004301 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004302 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004303 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004304 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004305
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004306 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004307 public void bindWidgetsModel(WidgetsModel model) {
4308 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004309 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004310 return;
4311 }
4312
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004313 if (mWidgetsView != null && model != null) {
4314 mWidgetsView.addWidgets(model);
4315 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004316 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004317 }
4318
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004319 @Override
4320 public void notifyWidgetProvidersChanged() {
4321 if (mWorkspace.getState().shouldUpdateWidget) {
4322 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4323 }
4324 }
4325
Winson Chung400438b2011-01-16 17:53:48 -08004326 private int mapConfigurationOriActivityInfoOri(int configOri) {
4327 final Display d = getWindowManager().getDefaultDisplay();
4328 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4329 switch (d.getRotation()) {
4330 case Surface.ROTATION_0:
4331 case Surface.ROTATION_180:
4332 // We are currently in the same basic orientation as the natural orientation
4333 naturalOri = configOri;
4334 break;
4335 case Surface.ROTATION_90:
4336 case Surface.ROTATION_270:
4337 // We are currently in the other basic orientation to the natural orientation
4338 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4339 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4340 break;
4341 }
4342
4343 int[] oriMap = {
4344 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4345 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4346 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4347 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4348 };
4349 // Since the map starts at portrait, we need to offset if this device's natural orientation
4350 // is landscape.
4351 int indexOffset = 0;
4352 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4353 indexOffset = 1;
4354 }
4355 return oriMap[(d.getRotation() + indexOffset) % 4];
4356 }
Adam Cohen446e9402011-09-15 18:21:21 -07004357
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004358 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004359 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004360 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004361 if (Utilities.ATLEAST_JB_MR2) {
4362 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4363 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004364 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4365 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004366 }
Winson Chung4b919f82012-05-01 10:44:08 -07004367 }
4368 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004369
Winson Chung4b919f82012-05-01 10:44:08 -07004370 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004371 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004372 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004373 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004374 } else {
4375 mHandler.postDelayed(new Runnable() {
4376 public void run() {
4377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4378 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004379 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004380 }
Winson Chung4b919f82012-05-01 10:44:08 -07004381 }
Winson Chung400438b2011-01-16 17:53:48 -08004382 }
4383
Winson Chunge43a1e72014-01-15 10:33:02 -08004384 protected boolean isLauncherPreinstalled() {
4385 PackageManager pm = getPackageManager();
4386 try {
4387 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4388 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4389 return true;
4390 } else {
4391 return false;
4392 }
4393 } catch (NameNotFoundException e) {
4394 e.printStackTrace();
4395 return false;
4396 }
4397 }
4398
Adam Cohenea90f832014-05-21 15:03:34 -07004399 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004400 * To be overridden by subclasses to indicate that there is an activity to launch
4401 * before showing the standard launcher experience.
4402 */
4403 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004404 if (mLauncherCallbacks != null) {
4405 return mLauncherCallbacks.hasFirstRunActivity();
4406 }
Adam Cohen432609a2014-03-13 17:03:22 -07004407 return false;
4408 }
4409
4410 /**
4411 * To be overridden by subclasses to launch any first run activity
4412 */
4413 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004414 if (mLauncherCallbacks != null) {
4415 return mLauncherCallbacks.getFirstRunActivity();
4416 }
Adam Cohen432609a2014-03-13 17:03:22 -07004417 return null;
4418 }
4419
Winson Chunga6945242014-01-08 14:04:34 -08004420 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004421 return !ActivityManager.isRunningInTestHarness() &&
4422 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004423 }
4424
Adam Cohen4a9423d2014-03-28 14:22:31 -07004425 protected boolean hasRunFirstRunActivity() {
4426 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4427 }
4428
Adam Cohen432609a2014-03-13 17:03:22 -07004429 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004430 if (shouldRunFirstRunActivity() &&
4431 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004432 Intent firstRunIntent = getFirstRunActivity();
4433 if (firstRunIntent != null) {
4434 startActivity(firstRunIntent);
4435 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004436 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004437 }
4438 }
Adam Cohen432609a2014-03-13 17:03:22 -07004439 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004440 }
4441
4442 private void markFirstRunActivityShown() {
4443 SharedPreferences.Editor editor = mSharedPrefs.edit();
4444 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4445 editor.apply();
4446 }
4447
Adam Cohen432609a2014-03-13 17:03:22 -07004448 /**
4449 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4450 * screen that must be displayed and dismissed.
4451 */
4452 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004453 if (mLauncherCallbacks != null) {
4454 return mLauncherCallbacks.hasDismissableIntroScreen();
4455 }
Adam Cohen432609a2014-03-13 17:03:22 -07004456 return false;
4457 }
4458
4459 /**
4460 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4461 */
4462 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004463 if (mLauncherCallbacks != null) {
4464 return mLauncherCallbacks.getIntroScreen();
4465 }
Adam Cohen432609a2014-03-13 17:03:22 -07004466 return null;
4467 }
4468
4469 /**
4470 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4471 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4472 */
4473 private boolean shouldShowIntroScreen() {
4474 return hasDismissableIntroScreen() &&
4475 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4476 }
4477
4478 protected void showIntroScreen() {
4479 View introScreen = getIntroScreen();
4480 changeWallpaperVisiblity(false);
4481 if (introScreen != null) {
4482 mDragLayer.showOverlayView(introScreen);
4483 }
4484 }
4485
4486 public void dismissIntroScreen() {
4487 markIntroScreenDismissed();
4488 if (showFirstRunActivity()) {
4489 // We delay hiding the intro view until the first run activity is showing. This
4490 // avoids a blip.
4491 mWorkspace.postDelayed(new Runnable() {
4492 @Override
4493 public void run() {
4494 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004495 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004496 }
4497 }, ACTIVITY_START_DELAY);
4498 } else {
4499 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004500 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004501 }
4502 changeWallpaperVisiblity(true);
4503 }
4504
4505 private void markIntroScreenDismissed() {
4506 SharedPreferences.Editor editor = mSharedPrefs.edit();
4507 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4508 editor.apply();
4509 }
4510
Adam Cohen091440a2015-03-18 14:16:05 -07004511 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004512 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4513 // on the device, then we always show the first run cling experience (or if there is no
4514 // launcher2). Otherwise, we prompt the user upon started for migration
4515 LauncherClings launcherClings = new LauncherClings(this);
4516 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004517 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004518 if (mModel.canMigrateFromOldLauncherDb(this)) {
4519 launcherClings.showMigrationCling();
4520 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004521 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004522 }
4523 }
4524 }
4525
Winson Chunga6945242014-01-08 14:04:34 -08004526 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004527 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4528 if (mHotseat != null) mHotseat.setAlpha(1f);
4529 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004530 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4531 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004532 }
4533
4534 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004535 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4536 if (mHotseat != null) mHotseat.setAlpha(0f);
4537 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004538 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4539 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004540 }
4541
Winson Chung5ffd51d2015-06-25 11:36:50 -07004542 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004543 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004544 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004545 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004547 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004548 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4549 if (userHandle != null) {
4550 user = UserHandleCompat.fromUser(userHandle);
4551 }
4552 }
4553 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004554 }
4555
4556 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004557 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004558 if (user == null) {
4559 user = UserHandleCompat.myUserHandle();
4560 }
4561
4562 // Called from search suggestion, add the profile extra to the intent to ensure that we
4563 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004564 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004565 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004566 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 return null;
4568 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004569 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 }
4571
Winson Chung5ffd51d2015-06-25 11:36:50 -07004572 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4574 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004575 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004576 UserHandleCompat.myUserHandle());
4577 }
4578
Winson Chung5ffd51d2015-06-25 11:36:50 -07004579 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004580 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4581 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004582 mWorkspace.onExternalDragStartedWithItem(dragView);
4583 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004584 }
4585
Winson Chung5ffd51d2015-06-25 11:36:50 -07004586 protected void moveWorkspaceToDefaultScreen() {
4587 mWorkspace.moveToDefaultScreen(false);
4588 }
4589
Winson Chung80baf5a2010-08-09 16:03:15 -07004590 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004591 * Returns a FastBitmapDrawable with the icon, accurately sized.
4592 */
4593 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4594 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4595 d.setFilterBitmap(true);
4596 resizeIconDrawable(d);
4597 return d;
4598 }
4599
4600 /**
4601 * Resizes an icon drawable to the correct icon size.
4602 */
Winson5fbe0742015-09-30 15:33:00 -07004603 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004604 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004605 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004606 }
4607
4608 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004609 * Prints out out state for debugging.
4610 */
4611 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004612 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004613 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004614 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4615 Log.d(TAG, "mRestoring=" + mRestoring);
4616 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004617 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004618 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004619
Daniel Sandler325dc232013-06-05 22:57:57 -04004620 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004621 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004622
4623 @Override
4624 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4625 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004626 // Dump workspace
4627 writer.println(prefix + "Workspace Items");
4628 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4629 writer.println(prefix + " Homescreen " + i);
4630
4631 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4632 for (int j = 0; j < layout.getChildCount(); j++) {
4633 Object tag = layout.getChildAt(j).getTag();
4634 if (tag != null) {
4635 writer.println(prefix + " " + tag.toString());
4636 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004637 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004638 }
Sunny Goyala1365452015-10-01 15:46:24 -07004639
4640 writer.println(prefix + " Hotseat");
4641 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4642 for (int j = 0; j < layout.getChildCount(); j++) {
4643 Object tag = layout.getChildAt(j).getTag();
4644 if (tag != null) {
4645 writer.println(prefix + " " + tag.toString());
4646 }
4647 }
4648
Sunny Goyal713edfc2016-05-06 09:58:34 -07004649 try {
4650 FileLog.flushAll(writer);
4651 } catch (Exception e) {
4652 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004653 }
4654
Adam Cohen9211d422014-10-07 18:14:53 -07004655 if (mLauncherCallbacks != null) {
4656 mLauncherCallbacks.dump(prefix, fd, writer, args);
4657 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004658 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004659
Adam Cohen59400422014-03-05 18:07:04 -08004660 public static CustomAppWidget getCustomAppWidget(String name) {
4661 return sCustomAppWidgets.get(name);
4662 }
4663
4664 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4665 return sCustomAppWidgets;
4666 }
4667
Sunny Goyal29538332016-02-18 09:10:19 -08004668 public static List<View> getFolderContents(View icon) {
4669 if (icon instanceof FolderIcon) {
4670 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4671 } else {
4672 return Collections.EMPTY_LIST;
4673 }
4674 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004675
4676 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4677
4678 @Override
4679 public void onSharedPreferenceChanged(
4680 SharedPreferences sharedPreferences, String key) {
4681 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4682 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4683 if (!waitUntilResume(this, true)) {
4684 run();
4685 }
4686 }
4687 }
4688
4689 @Override
4690 public void run() {
4691 setOrientation();
4692 }
4693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004694}
Michael Jurka2763be32011-02-24 11:19:57 -08004695
Dan Sandlerd5024042014-01-09 15:01:33 -05004696interface DebugIntents {
4697 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4698 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004699}