blob: a58cddde0c283ad9f5c6f76be2870cd4e2c4a9da [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070055import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070087import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070096import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080097import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800103import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700108import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800111import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700113import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyal322d5562015-06-25 19:35:49 -0700116import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700117import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700118import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700119import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700120import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700122
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700123import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700127import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800128import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700129import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
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
Sunny Goyala1365452015-10-01 15:46:24 -0700309 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
310 private static final Date sDateStamp = new Date();
311 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700312 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700313
Winson Chung46353de2012-02-16 14:05:10 -0800314 // We only want to get the SharedPreferences once since it does an FS stat each time we get
315 // it from the context.
316 private SharedPreferences mSharedPrefs;
317
Winson Chungf0c6ae02012-03-21 16:10:31 -0700318 // Holds the page that we need to animate to, and the icon views that we need to animate up
319 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700320 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700321 private Bitmap mFolderIconBitmap;
322 private Canvas mFolderIconCanvas;
323 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700324
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700325 private DeviceProfile mDeviceProfile;
326
Adam Cohen4b66bf32015-09-18 12:15:19 -0700327 private boolean mMoveToDefaultScreenFromNewIntent;
328
Winson Chung13eb5272015-05-11 16:30:13 -0700329 // This is set to the view that launched the activity that navigated the user away from
330 // launcher. Since there is no callback for when the activity has finished launching, enable
331 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 private BubbleTextView mWaitingForResume;
333
Adam Cohen59400422014-03-05 18:07:04 -0800334 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
335 new HashMap<String, CustomAppWidget>();
336
Adam Cohen59400422014-03-05 18:07:04 -0800337 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700338 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
339 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800340 }
341 }
342
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700345 if (mWorkspace != null) {
346 mWorkspace.buildPageHardwareLayers();
347 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 }
349 };
350
Adam Cohendb364c32014-05-20 14:23:40 -0700351 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352
Adam Cohen091440a2015-03-18 14:16:05 -0700353 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 int requestCode;
355 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700356 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700357 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int cellX;
359 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700360 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 }
362
Hyunyoung Songaa953652016-04-19 18:30:24 -0700363 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800364
Adam Cohenf9c184a2016-01-15 16:47:43 -0800365 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700366 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400367
368 @Thunk void setOrientation() {
369 if (mRotationEnabled) {
370 unlockScreenOrientation(true);
371 } else {
372 setRequestedOrientation(
373 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700374 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376
Sunny Goyal7779d622015-06-11 16:18:39 -0700377 private Runnable mUpdateOrientationRunnable = new Runnable() {
378 public void run() {
379 setOrientation();
380 }
381 };
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
399
Adam Cohen9211d422014-10-07 18:14:53 -0700400 if (mLauncherCallbacks != null) {
401 mLauncherCallbacks.preOnCreate();
402 }
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400405
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400406 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700407
Adam Cohen2e6da152015-05-06 11:42:25 -0700408 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700409 mDeviceProfile = getResources().getConfiguration().orientation
410 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700411 app.getInvariantDeviceProfile().landscapeProfile
412 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700413
Sunny Goyalf7258242015-10-19 16:59:07 -0700414 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700415 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mModel = app.setLauncher(this);
417 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700418
Joe Onorato41a12d22009-10-31 18:30:00 -0400419 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700420 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Sunny Goyalffe83f12014-08-14 17:39:34 -0700422 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
425 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
428 // this also ensures that any synchronous binding below doesn't re-trigger another
429 // LauncherModel load.
430 mPaused = false;
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200433 android.os.Debug.startMethodTracing(
434 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
Tony Wickhameef44322015-10-20 13:24:36 -0700439 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
440 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700442 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700443 mExtractedColors = new ExtractedColors();
444 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
Tony Wickhame2217252016-03-22 16:34:23 -0700446 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
447 .addAccessibilityStateChangeListener(this);
448
Joe Onorato7c312c12009-08-13 21:36:53 -0700449 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mSavedState = savedInstanceState;
452 restoreState(mSavedState);
453
454 if (PROFILE_STARTUP) {
455 android.os.Debug.stopMethodTracing();
456 }
457
458 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700459 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 // If the user leaves launcher, then we should just load items asynchronously when
461 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700462 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 } else {
464 // We only load the page synchronously if the user rotates (or triggers a
465 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469
470 // For handling default keys
471 mDefaultKeySsb = new SpannableStringBuilder();
472 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800473
474 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
475 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 }
483
484 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
485 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400486 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700487
Adam Cohen9211d422014-10-07 18:14:53 -0700488 if (mLauncherCallbacks != null) {
489 mLauncherCallbacks.onCreate(savedInstanceState);
490 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700491
492 if (shouldShowIntroScreen()) {
493 showIntroScreen();
494 } else {
495 showFirstRunActivity();
496 showFirstRunClings();
497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
499
Sunny Goyal7779d622015-06-11 16:18:39 -0700500 @Override
501 public void onSettingsChanged(String settings, boolean value) {
502 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
503 mRotationEnabled = value;
504 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
505 mUpdateOrientationRunnable.run();
506 }
507 }
508 }
509
Tony Wickham827cef22016-03-17 15:39:39 -0700510 @Override
511 public void onExtractedColorsChanged() {
512 loadExtractedColorsAndColorItems();
513 }
514
515 private void loadExtractedColorsAndColorItems() {
516 if (mExtractedColors != null) {
517 mExtractedColors.load(this);
518 // TODO: pass mExtractedColors to interested items such as hotseat.
Tony Wickham827cef22016-03-17 15:39:39 -0700519 }
520 }
521
Adam Cohen9211d422014-10-07 18:14:53 -0700522 private LauncherCallbacks mLauncherCallbacks;
523
524 public void onPostCreate(Bundle savedInstanceState) {
525 super.onPostCreate(savedInstanceState);
526 if (mLauncherCallbacks != null) {
527 mLauncherCallbacks.onPostCreate(savedInstanceState);
528 }
529 }
530
Sunny Goyal32554d12015-12-03 15:31:25 -0800531 /**
532 * Call this after onCreate to set or clear overlay.
533 */
534 public void setLauncherOverlay(LauncherOverlay overlay) {
535 if (overlay != null) {
536 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
537 }
538 mWorkspace.setLauncherOverlay(overlay);
539 }
540
Adam Cohen9211d422014-10-07 18:14:53 -0700541 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
542 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 private boolean mWorkspaceImportanceStored = false;
545 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800547 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
549
550 @Override
551 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 return;
554 }
555 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700556 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspace != null) {
558 mWorkspaceImportanceForAccessibility =
559 mWorkspace.getImportantForAccessibility();
560 mWorkspace.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mWorkspaceImportanceStored = true;
563 }
564 if (mHotseat != null) {
565 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
566 mHotseat.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mHotseatImportanceStored = true;
569 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571
572 @Override
573 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700574 if (mWorkspaceImportanceStored && mWorkspace != null) {
575 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
576 }
577 if (mHotseatImportanceStored && mHotseat != null) {
578 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
579 }
580 mWorkspaceImportanceStored = false;
581 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700582 }
583 });
Adam Cohen9211d422014-10-07 18:14:53 -0700584 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700585 }
586
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700588 public void onLauncherProviderChange() {
589 if (mLauncherCallbacks != null) {
590 mLauncherCallbacks.onLauncherProviderChange();
591 }
592 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800632 /**
633 * Logger object is a singleton and does not have to be coupled with the foreground activity.
634 * Since most user event logging is done on the UI, the object is retrieved from the
635 * callback for convenience.
636 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 private UserEventDispatcher createUserEventDispatcher() {
638 return new UserEventDispatcher() {
639 @Override
640 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
641 if (!DEBUG_LOGGING) {
642 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800643 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700644 Log.d("UserEvent", String.format(Locale.US,
645 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
646 LoggerUtils.getActionStr(ev.action),
647 LoggerUtils.getTargetStr(ev.srcTarget[0]),
648 LoggerUtils.getTargetStr(ev.srcTarget[1]),
649 ev.elapsedContainerMillis,
650 ev.elapsedSessionMillis));
651 }
652 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800653 }
654
Hyunyoung Songaa953652016-04-19 18:30:24 -0700655 public UserEventDispatcher getUserEventDispatcher() {
656 if (mLauncherCallbacks != null) {
657 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
658 if (dispatcher != null) {
659 return dispatcher;
660 }
661 }
662
663 if (mUserEventDispatcher == null) {
664 mUserEventDispatcher = createUserEventDispatcher();
665 }
666 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800667 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100668
Hyunyoung Song3f471442015-04-08 19:01:34 -0700669 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700670 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
671 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700672 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700673 }
674
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000675 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700676 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
677 // This cast is safe as long as the id < 0x00FFFFFF
678 // Since we jail all the dynamically generated views, there should be no clashes
679 // with any other views.
680 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000681 }
682
683 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700684 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
685 * a configuration step, this allows the proper animations to run after other transitions.
686 */
Adam Cohendb364c32014-05-20 14:23:40 -0700687 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700688 long screenId = args.screenId;
689 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
690 // When the screen id represents an actual screen (as opposed to a rank) we make sure
691 // that the drop page actually exists.
692 screenId = ensurePendingDropLayoutExists(args.screenId);
693 }
Adam Cohendb364c32014-05-20 14:23:40 -0700694
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700697 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700698 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700699 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700701 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700702 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700703 case REQUEST_RECONFIGURE_APPWIDGET:
704 completeRestoreAppWidget(args.appWidgetId);
705 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800706 }
Michael Jurka27614d22012-04-02 06:40:22 -0700707 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
708 // if you turned the screen off and then back while in All Apps, Launcher would not
709 // return to the workspace. Clearing mAddInfo.container here fixes this issue
710 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700711 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800712 }
713
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800714 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700716 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700717 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700718 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800719 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700720
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 Runnable exitSpringLoaded = new Runnable() {
722 @Override
723 public void run() {
724 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
725 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
726 }
727 };
728
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700730 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
733 if (resultCode == RESULT_CANCELED) {
734 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700735 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700736 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800738 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700740
741 // When the user has granted permission to bind widgets, we should check to see if
742 // we can inflate the default search bar widget.
743 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
745 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
747 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700748 // User could have free-scrolled between pages before picking a wallpaper; make sure
749 // we move to the closest one now.
750 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 // We have special handling for widgets
761 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 final int appWidgetId;
763 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
764 : -1;
765 if (widgetId < 0) {
766 appWidgetId = pendingAddWidgetId;
767 } else {
768 appWidgetId = widgetId;
769 }
770
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800772 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700773 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
774 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 result = RESULT_CANCELED;
776 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 @Override
779 public void run() {
780 exitSpringLoadedDragModeDelayed(false, 0, null);
781 }
782 };
Adam Cohendb364c32014-05-20 14:23:40 -0700783 if (workspaceLocked) {
784 // No need to remove the empty screen if we're mid-binding, as the
785 // the bind will not add the empty screen.
786 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
787 } else {
788 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
789 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
790 }
Winson Chung5aaab772012-04-27 15:24:02 -0700791 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700793 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
794 // When the screen id represents an actual screen (as opposed to a rank)
795 // we make sure that the drop page actually exists.
796 mPendingAddInfo.screenId =
797 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
798 }
Adam Cohendb364c32014-05-20 14:23:40 -0700799 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
800
801 dropLayout.setDropPending(true);
802 final Runnable onComplete = new Runnable() {
803 @Override
804 public void run() {
805 completeTwoStageWidgetDrop(resultCode, appWidgetId);
806 dropLayout.setDropPending(false);
807 }
808 };
809 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
811 } else {
812 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
813 mPendingAddInfo);
814 sPendingAddItem = args;
815 }
Winson Chung5aaab772012-04-27 15:24:02 -0700816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 return;
818 }
819
Sunny Goyalff572272014-07-23 13:58:07 -0700820 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
821 if (resultCode == RESULT_OK) {
822 // Update the widget view.
823 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
824 pendingAddWidgetId, mPendingAddInfo);
825 if (workspaceLocked) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 }
830 }
831 // Leave the widget in the pending state if the user canceled the configure.
832 return;
833 }
834
Sunny Goyalaad90582015-07-09 14:22:50 -0700835 if (requestCode == REQUEST_CREATE_SHORTCUT) {
836 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
837 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
838 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
839 mPendingAddInfo);
840 if (isWorkspaceLocked()) {
841 sPendingAddItem = args;
842 } else {
843 completeAdd(args);
844 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
845 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
846 }
847 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700848 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
849 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800853
854 }
855
856 @Override
857 protected void onActivityResult(
858 final int requestCode, final int resultCode, final Intent data) {
859 handleActivityResult(requestCode, resultCode, data);
860 if (mLauncherCallbacks != null) {
861 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
862 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800863 }
864
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600865 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700866 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600867 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700868 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
869 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
870 View v = null;
871 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
872 if (layout != null) {
873 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
874 }
875 Intent intent = sPendingAddItem.intent;
876 sPendingAddItem = null;
877 if (grantResults.length > 0
878 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
879 startActivity(v, intent, null);
880 } else {
881 // TODO: Show a snack bar with link to settings
882 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
883 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
884 }
885 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600886 if (mLauncherCallbacks != null) {
887 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
888 grantResults);
889 }
890 }
891
Adam Cohendb364c32014-05-20 14:23:40 -0700892 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
893 appWidgetId, ItemInfo info) {
894 PendingAddArguments args = new PendingAddArguments();
895 args.requestCode = requestCode;
896 args.intent = data;
897 args.container = info.container;
898 args.screenId = info.screenId;
899 args.cellX = info.cellX;
900 args.cellY = info.cellY;
901 args.appWidgetId = appWidgetId;
902 return args;
903 }
904
905 /**
906 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
907 *
908 * @param screenId the screen id to check
909 * @return the new screen, or screenId if it exists
910 */
911 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800912 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700913 if (dropLayout == null) {
914 // it's possible that the add screen was removed because it was
915 // empty and a re-bind occurred
916 mWorkspace.addExtraEmptyScreen();
917 return mWorkspace.commitExtraEmptyScreen();
918 } else {
919 return screenId;
920 }
921 }
922
Adam Cohen091440a2015-03-18 14:16:05 -0700923 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800924 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 Runnable onCompleteRunnable = null;
926 int animationType = 0;
927
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 if (resultCode == RESULT_OK) {
930 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
931 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 mPendingAddWidgetInfo);
933 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 onCompleteRunnable = new Runnable() {
935 @Override
936 public void run() {
937 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
940 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
942 };
943 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800944 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 if (mDragLayer.getAnimatedView() != null) {
948 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
949 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
950 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700951 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 // The animated view may be null in the case of a rotation during widget configuration
953 onCompleteRunnable.run();
954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 protected void onStop() {
959 super.onStop();
960 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700961
962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.onStop();
964 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700965 }
966
967 @Override
968 protected void onStart() {
969 super.onStart();
970 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.onStart();
974 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700975 }
976
977 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200979 long startTime = 0;
980 if (DEBUG_RESUME_TIME) {
981 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400982 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200983 }
Adam Cohen9211d422014-10-07 18:14:53 -0700984
985 if (mLauncherCallbacks != null) {
986 mLauncherCallbacks.preOnResume();
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700990 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700991
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700993 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800995 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700996 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700997 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700998 // view after launching an app, as they may be depending on the UI to be static to
999 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001000 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001001 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001002 } else if (mOnResumeState == State.WIDGETS) {
1003 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001004 }
1005 mOnResumeState = State.NONE;
1006
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001007 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001008 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1009 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001010
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001011 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001013 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001014 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1020 // execute them here
1021 long startTimeCallbacks = 0;
1022 if (DEBUG_RESUME_TIME) {
1023 startTimeCallbacks = System.currentTimeMillis();
1024 }
1025
Michael Jurka1e2f4652013-07-08 18:03:46 -07001026 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1027 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001030 if (DEBUG_RESUME_TIME) {
1031 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1032 (System.currentTimeMillis() - startTimeCallbacks));
1033 }
Michael Jurka447bf842013-05-15 14:52:15 +02001034 }
Michael Jurka54554252013-08-01 12:52:23 +02001035 if (mOnResumeCallbacks.size() > 0) {
1036 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1037 mOnResumeCallbacks.get(i).run();
1038 }
1039 mOnResumeCallbacks.clear();
1040 }
Winson Chunge4e50662012-01-23 14:45:13 -08001041
1042 // Reset the pressed state of icons that were locked in the press state while activities
1043 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001044 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001045 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001046 mWaitingForResume.setStayPressed(false);
1047 }
Winson Chung82963d52013-10-09 11:20:57 -07001048
Adam Cohen06dff352012-06-01 17:17:08 -07001049 // It is possible that widgets can receive updates while launcher is not in the foreground.
1050 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1051 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1052 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001053 if (!isWorkspaceLoading()) {
1054 getWorkspace().reinflateWidgetsIfNecessary();
1055 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001056 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001057
Michael Jurka447bf842013-05-15 14:52:15 +02001058 if (DEBUG_RESUME_TIME) {
1059 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1060 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001061
Adam Cohen4b66bf32015-09-18 12:15:19 -07001062 // We want to suppress callbacks about CustomContent being shown if we have just received
1063 // onNewIntent while the user was present within launcher. In that case, we post a call
1064 // to move the user to the main screen (which will occur after onResume). We don't want to
1065 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1066 // suppress here.
1067 if (mWorkspace.getCustomContentCallbacks() != null
1068 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001069 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001070 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001071 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1072 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001073 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 }
1075 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001076 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001077 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001078 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001079
Sunny Goyal756adbc2015-04-16 15:20:51 -07001080 if (!isWorkspaceLoading()) {
1081 // Process any items that were added while Launcher was away.
1082 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1083 }
Adam Cohen9211d422014-10-07 18:14:53 -07001084
1085 if (mLauncherCallbacks != null) {
1086 mLauncherCallbacks.onResume();
1087 }
Adam Cohen06dff352012-06-01 17:17:08 -07001088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001092 // Ensure that items added to Launcher are queued until Launcher returns
1093 InstallShortcutReceiver.enableInstallQueue();
1094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001096 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001097 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001098 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001099
1100 // We call onHide() aggressively. The custom content callbacks should be able to
1101 // debounce excess onHide calls.
1102 if (mWorkspace.getCustomContentCallbacks() != null) {
1103 mWorkspace.getCustomContentCallbacks().onHide();
1104 }
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Adam Cohen9211d422014-10-07 18:14:53 -07001106 if (mLauncherCallbacks != null) {
1107 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
1111 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001112 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1113 // by a onResume or by scrolling otherwise.
1114 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001115
1116 // Custom content is completely hidden
1117 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001118
1119 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1120 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001121
1122 // Indicates whether the user is allowed to scroll away from the custom content.
1123 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 }
1125
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 public interface LauncherOverlay {
1127
1128 /**
1129 * Touch interaction leading to overscroll has begun
1130 */
1131 public void onScrollInteractionBegin();
1132
1133 /**
1134 * Touch interaction related to overscroll has ended
1135 */
1136 public void onScrollInteractionEnd();
1137
1138 /**
1139 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1140 * screen (or in the case of RTL, the rightmost screen).
1141 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001142 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143
1144 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001145 * Called when the launcher is ready to use the overlay
1146 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001148 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 }
1150
Jun Mukai8af0cd82015-05-12 12:32:12 -07001151 public interface LauncherSearchCallbacks {
1152 /**
1153 * Called when the search overlay is shown.
1154 */
1155 public void onSearchOverlayOpened();
1156
1157 /**
1158 * Called when the search overlay is dismissed.
1159 */
1160 public void onSearchOverlayClosed();
1161 }
1162
Adam Cohenc2d6e892014-10-16 09:49:24 -07001163 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001164 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 }
1166
1167 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1168
Sunny Goyalc86df472016-02-25 09:19:38 -08001169 public void onScrollChanged(float progress) {
1170 if (mWorkspace != null) {
1171 mWorkspace.onOverlayScrollChanged(progress);
1172 }
1173 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001174 }
1175
Jorim Jaggid017f882014-01-14 17:08:48 -08001176 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001177 if (mLauncherCallbacks != null) {
1178 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001179 } else {
1180 // On devices with a locked orientation, we will at least have the allow rotation
1181 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001182 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001183 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001184 }
1185
Adam Cohen9211d422014-10-07 18:14:53 -07001186 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001187 CustomContentCallbacks callbacks, String description) {
1188 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001189 }
1190
Adam Cohenedb40762013-07-18 16:45:45 -07001191 // The custom content needs to offset its content to account for the QSB
1192 public int getTopOffsetForCustomContent() {
1193 return mWorkspace.getPaddingTop();
1194 }
1195
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001196 @Override
1197 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001199 if (mModel.isCurrentCallbacks(this)) {
1200 mModel.stopLoader();
1201 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001202 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1203
Romain Guy13c2e7b2010-03-10 19:45:00 -08001204 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001207 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001208 @Override
1209 public void onWindowFocusChanged(boolean hasFocus) {
1210 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001211 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001212
1213 if (mLauncherCallbacks != null) {
1214 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1215 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 private boolean acceptFilter() {
1219 final InputMethodManager inputManager = (InputMethodManager)
1220 getSystemService(Context.INPUT_METHOD_SERVICE);
1221 return !inputManager.isFullscreenMode();
1222 }
1223
1224 @Override
1225 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001226 final int uniChar = event.getUnicodeChar();
1227 final boolean handled = super.onKeyDown(keyCode, event);
1228 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1229 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1231 keyCode, event);
1232 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001234 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 // showSearchDialog()
1236 // If there are multiple keystrokes before the search dialog takes focus,
1237 // onSearchRequested() will be called for every keystroke,
1238 // but it is idempotent, so it's fine.
1239 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241 }
1242
Joe Onorato8a9625e2010-01-28 15:55:35 -08001243 // Eat the long press event so the keyboard doesn't come up.
1244 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1245 return true;
1246 }
1247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return handled;
1249 }
1250
Winson46163472015-09-22 17:31:56 -07001251 @Override
1252 public boolean onKeyUp(int keyCode, KeyEvent event) {
1253 if (keyCode == KeyEvent.KEYCODE_MENU) {
1254 // Ignore the menu key if we are currently dragging or are on the custom content screen
1255 if (!isOnCustomContent() && !mDragController.isDragging()) {
1256 // Close any open folders
1257 closeFolder();
1258
1259 // Stop resizing any widgets
1260 mWorkspace.exitWidgetResizeMode();
1261
1262 // Show the overview mode if we are on the workspace
1263 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1264 !mWorkspace.isSwitchingState()) {
1265 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001266 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001267 }
1268 }
1269 return true;
1270 }
1271 return super.onKeyUp(keyCode, event);
1272 }
1273
Karl Rosaen138a0412009-04-23 19:00:21 -07001274 private String getTypedText() {
1275 return mDefaultKeySsb.toString();
1276 }
1277
1278 private void clearTypedText() {
1279 mDefaultKeySsb.clear();
1280 mDefaultKeySsb.clearSpans();
1281 Selection.setSelection(mDefaultKeySsb, 0);
1282 }
1283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001285 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1286 * State
1287 */
1288 private static State intToState(int stateOrdinal) {
1289 State state = State.WORKSPACE;
1290 final State[] stateValues = State.values();
1291 for (int i = 0; i < stateValues.length; i++) {
1292 if (stateValues[i].ordinal() == stateOrdinal) {
1293 state = stateValues[i];
1294 break;
1295 }
1296 }
1297 return state;
1298 }
1299
1300 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 * Restores the previous state, if it exists.
1302 *
1303 * @param savedState The previous state.
1304 */
1305 private void restoreState(Bundle savedState) {
1306 if (savedState == null) {
1307 return;
1308 }
1309
Michael Jurka883f55b2010-10-21 15:47:14 -07001310 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001311 if (state == State.APPS || state == State.WIDGETS) {
1312 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001313 }
1314
Adam Cohen21cd0022013-10-09 18:57:02 -07001315 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1316 PagedView.INVALID_RESTORE_PAGE);
1317 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001318 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
Sunny Goyal756cd262015-08-20 12:33:21 -07001321 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1322 if (itemValues != null) {
1323 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001324 AppWidgetProviderInfo info = savedState.getParcelable(
1325 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001326 mPendingAddWidgetInfo = info == null ?
1327 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1328
Adam Cohen4637b5a2013-11-04 18:21:24 -08001329 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001330 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 mRestoring = true;
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 /**
1336 * Finds all the views we need and configure them properly.
1337 */
1338 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001339 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001340
Craig Mautner360310b2012-10-26 15:13:08 -07001341 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001342 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001343 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1344 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001345 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001346
Sunny Goyal784f9c32016-03-23 16:56:54 -07001347 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1348 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1349 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001350 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
Winson Chung4c98d922011-05-31 16:50:48 -07001352 // Setup the drag layer
1353 mDragLayer.setup(this, dragController);
1354
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 // Setup the hotseat
1356 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1357 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001358 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 }
1360
Winsone9f27272015-10-13 10:47:51 -07001361 // Setup the overview panel
1362 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363
Winson Chung4c98d922011-05-31 16:50:48 -07001364 // Setup the workspace
1365 mWorkspace.setHapticFeedbackEnabled(false);
1366 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001367 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001368 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001369
Tony Wickham34d2c912015-09-11 09:27:58 -07001370 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001371 mSearchDropTargetBar = (SearchDropTargetBar)
1372 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001373 // Get the app info bar
1374 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1375 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001376
Winson Chungef7f8742015-06-04 17:18:17 -07001377 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001378 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001379 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001380 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1381 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1382 } else {
1383 mAppsView.setSearchBarController(new DefaultAppSearchController());
1384 }
Craig Mautner360310b2012-10-26 15:13:08 -07001385
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001387 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001388 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001389 dragController.setMoveTarget(mWorkspace);
1390 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001391 if (mSearchDropTargetBar != null) {
1392 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001393 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001394 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001395 if (mAppInfoDropTargetBar != null) {
1396 mAppInfoDropTargetBar.setup(this, dragController);
1397 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001398
Sunny Goyal322d5562015-06-25 19:35:49 -07001399 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1400 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
Winsone9f27272015-10-13 10:47:51 -07001404 private void setupOverviewPanel() {
1405 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1406
1407 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1408 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1409 @Override
1410 public boolean onLongClick(View v) {
1411 return v.performClick();
1412 }
1413 };
1414
1415 // Bind wallpaper button actions
1416 View wallpaperButton = findViewById(R.id.wallpaper_button);
1417 wallpaperButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickWallpaperPicker(view);
1422 }
1423 }
1424 });
1425 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1426 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind widget button actions
1429 mWidgetsButton = findViewById(R.id.widget_button);
1430 mWidgetsButton.setOnClickListener(new OnClickListener() {
1431 @Override
1432 public void onClick(View view) {
1433 if (!mWorkspace.isSwitchingState()) {
1434 onClickAddWidgetButton(view);
1435 }
1436 }
1437 });
1438 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1439 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440
1441 // Bind settings actions
1442 View settingsButton = findViewById(R.id.settings_button);
1443 boolean hasSettings = hasSettings();
1444 if (hasSettings) {
1445 settingsButton.setOnClickListener(new OnClickListener() {
1446 @Override
1447 public void onClick(View view) {
1448 if (!mWorkspace.isSwitchingState()) {
1449 onClickSettingsButton(view);
1450 }
1451 }
1452 });
1453 settingsButton.setOnLongClickListener(performClickOnLongClick);
1454 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1455 } else {
1456 settingsButton.setVisibility(View.GONE);
1457 }
1458
1459 mOverviewPanel.setAlpha(0f);
1460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001463 * Sets the all apps button. This method is called from {@link Hotseat}.
1464 */
1465 public void setAllAppsButton(View allAppsButton) {
1466 mAllAppsButton = allAppsButton;
1467 }
1468
1469 public View getAllAppsButton() {
1470 return mAllAppsButton;
1471 }
1472
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001473 public View getWidgetsButton() {
1474 return mWidgetsButton;
1475 }
1476
Anjali Koppal5ad44842014-03-10 20:34:39 -07001477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Creates a view representing a shortcut.
1479 *
1480 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001482 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001483 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485
1486 /**
1487 * Creates a view representing a shortcut inflated from the specified resource.
1488 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 * @param parent The group the shortcut belongs to.
1490 * @param info The data structure describing the shortcut.
1491 *
1492 * @return A View inflated from layoutResId.
1493 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001494 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001495 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001496 parent, false);
1497 favorite.applyFromShortcutInfo(info, mIconCache);
1498 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001500 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return favorite;
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Add a shortcut to the workspace.
1506 *
1507 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 int cellY) {
1511 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001513
Sunny Goyal5c97f512015-05-19 16:03:28 -07001514 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001515 if (info == null) {
1516 return;
1517 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001518 final View view = createShortcut(info);
1519
Sunny Goyal5c97f512015-05-19 16:03:28 -07001520 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001521 // First we check if we already know the exact location where we want to add this item.
1522 if (cellX >= 0 && cellY >= 0) {
1523 cellXY[0] = cellX;
1524 cellXY[1] = cellY;
1525 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001526
1527 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001528 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001529 true, null,null)) {
1530 return;
1531 }
1532 DragObject dragObject = new DragObject();
1533 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1535 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 return;
1537 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001539 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001540 }
1541
1542 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001543 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001544 return;
1545 }
1546
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001547 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
1549 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001551 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553 }
1554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001556 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001558 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 */
Adam Cohen091440a2015-03-18 14:16:05 -07001560 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001561 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1562
1563 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001564 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001565 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Adam Cohen59400422014-03-05 18:07:04 -08001568 if (appWidgetInfo.isCustomWidget) {
1569 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001570 }
1571
Adam Cohen59400422014-03-05 18:07:04 -08001572 LauncherAppWidgetInfo launcherInfo;
1573 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1574 launcherInfo.spanX = info.spanX;
1575 launcherInfo.spanY = info.spanY;
1576 launcherInfo.minSpanX = info.minSpanX;
1577 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001578 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001581 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
1583 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 if (hostView == null) {
1585 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001586 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1587 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 } else {
1589 // The AppWidgetHostView has already been inflated and instantiated
1590 launcherInfo.hostView = hostView;
1591 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001592 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001595 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001598 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1599 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1600 item.hostView.setTag(item);
1601 item.onBindAppWidget(this);
1602
1603 item.hostView.setFocusable(true);
1604 item.hostView.setOnFocusChangeListener(mFocusHandler);
1605
1606 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1607 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1608
1609 if (!item.isCustomWidget()) {
1610 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1611 }
1612 }
1613
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1615 @Override
1616 public void onReceive(Context context, Intent intent) {
1617 final String action = intent.getAction();
1618 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1619 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001620 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001621 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001622
Winson Chungc100e8e2011-08-09 16:02:43 -07001623 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001624 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001625 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001626 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001627 if (!showWorkspace(false)) {
1628 // If we are already on the workspace, then manually reset all apps
1629 mAppsView.reset();
1630 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1633 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001634 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001635 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1636 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001637 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001638 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1639 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1640 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001641 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001642 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1643 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 }
1645 }
1646 };
1647
1648 @Override
1649 public void onAttachedToWindow() {
1650 super.onAttachedToWindow();
1651
1652 // Listen for broadcasts related to user-presence
1653 final IntentFilter filter = new IntentFilter();
1654 filter.addAction(Intent.ACTION_SCREEN_OFF);
1655 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001656 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001657 if (ENABLE_DEBUG_INTENTS) {
1658 filter.addAction(DebugIntents.DELETE_DATABASE);
1659 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1660 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001662 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001663 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001665
1666 if (mLauncherCallbacks != null) {
1667 mLauncherCallbacks.onAttachedToWindow();
1668 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 }
1670
1671 @Override
1672 public void onDetachedFromWindow() {
1673 super.onDetachedFromWindow();
1674 mVisible = false;
1675
Adam Cohend113e0c2010-11-11 10:48:05 -08001676 if (mAttached) {
1677 unregisterReceiver(mReceiver);
1678 mAttached = false;
1679 }
Winson Chungb745afb2015-03-02 11:51:23 -08001680 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001681
1682 if (mLauncherCallbacks != null) {
1683 mLauncherCallbacks.onDetachedFromWindow();
1684 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 }
1686
1687 public void onWindowVisibilityChanged(int visibility) {
1688 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001689 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 // The following code used to be in onResume, but it turns out onResume is called when
1691 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1692 // is a more appropriate event to handle
1693 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001694 if (!mWorkspaceLoading) {
1695 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001696 // We want to let Launcher draw itself at least once before we force it to build
1697 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001698 // apps is nice and speedy.
1699 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001700 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001702 if (mStarted) return;
1703 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001704 // We delay the layer building a bit in order to give
1705 // other message processing a time to run. In particular
1706 // this avoids a delay in hiding the IME if it was
1707 // currently shown, because doing that may involve
1708 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001709 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001710 final ViewTreeObserver.OnDrawListener listener = this;
1711 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001712 public void run() {
1713 if (mWorkspace != null &&
1714 mWorkspace.getViewTreeObserver() != null) {
1715 mWorkspace.getViewTreeObserver().
1716 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001717 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001718 }
1719 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001720 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001721 }
1722 });
1723 }
1724 clearTypedText();
1725 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 }
1727
Adam Cohen091440a2015-03-18 14:16:05 -07001728 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 mHandler.removeMessages(ADVANCE_MSG);
1730 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1731 mHandler.sendMessageDelayed(msg, delay);
1732 mAutoAdvanceSentTime = System.currentTimeMillis();
1733 }
1734
Adam Cohen091440a2015-03-18 14:16:05 -07001735 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1737 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1738 mAutoAdvanceRunning = autoAdvanceRunning;
1739 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 sendAdvanceMessage(delay);
1742 } else {
1743 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001744 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1746 }
1747 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001748 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 }
1750 }
1751 }
1752
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001753 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1754
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 if (msg.what == ADVANCE_MSG) {
1758 int i = 0;
1759 for (View key: mWidgetsToAdvance.keySet()) {
1760 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001761 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001763 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 public void run() {
1765 ((Advanceable) v).advance();
1766 }
1767 }, delay);
1768 }
1769 i++;
1770 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001771 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001773 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001775 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001776
Winsonc0b52fe2015-09-09 16:38:15 -07001777 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001778 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1780 if (v instanceof Advanceable) {
1781 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001782 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001783 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 }
1785 }
1786
Winsonc0b52fe2015-09-09 16:38:15 -07001787 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 if (mWidgetsToAdvance.containsKey(hostView)) {
1789 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001790 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 }
1793
Hyunyoung Song3f471442015-04-08 19:01:34 -07001794 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001795 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1796 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798
Winson Chunga6945242014-01-08 14:04:34 -08001799 public DragLayer getDragLayer() {
1800 return mDragLayer;
1801 }
1802
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001803 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001804 return mAppsView;
1805 }
1806
Hyunyoung Song3f471442015-04-08 19:01:34 -07001807 public WidgetsContainerView getWidgetsView() {
1808 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001809 }
1810
Winson Chunga6945242014-01-08 14:04:34 -08001811 public Workspace getWorkspace() {
1812 return mWorkspace;
1813 }
1814
1815 public Hotseat getHotseat() {
1816 return mHotseat;
1817 }
1818
Jorim Jaggid017f882014-01-14 17:08:48 -08001819 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mOverviewPanel;
1821 }
1822
Winson Chung006ee262015-08-03 14:40:11 -07001823 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001824 return mSearchDropTargetBar;
1825 }
1826
Tony Wickham34d2c912015-09-11 09:27:58 -07001827 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1828 return mAppInfoDropTargetBar;
1829 }
1830
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001831 public LauncherAppWidgetHost getAppWidgetHost() {
1832 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Winson Chunga9abd0e2010-10-27 17:18:37 -07001835 public LauncherModel getModel() {
1836 return mModel;
1837 }
1838
Adam Cohen79d90c52016-04-22 13:29:20 -07001839 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001840 return mSharedPrefs;
1841 }
1842
Adam Cohen2e6da152015-05-06 11:42:25 -07001843 public DeviceProfile getDeviceProfile() {
1844 return mDeviceProfile;
1845 }
1846
Bjorn Bringertc459e522013-06-07 19:36:01 +01001847 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001848 getWindow().closeAllPanels();
1849
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001850 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001851 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 }
1853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 @Override
1855 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001856 long startTime = 0;
1857 if (DEBUG_RESUME_TIME) {
1858 startTime = System.currentTimeMillis();
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 super.onNewIntent(intent);
1861
1862 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001863 Folder openFolder = mWorkspace.getOpenFolder();
1864 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1865 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1866 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1867 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1868 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001869 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001870 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001871
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 if (mWorkspace == null) {
1873 // Can be cases where mWorkspace is null, this prevents a NPE
1874 return;
1875 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 // In all these cases, only animate if we're already on home
1877 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001878
Sunny Goyal935fca12015-10-13 10:19:01 -07001879 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 exitSpringLoadedDragMode();
1881
1882 // If we are already on home, then just animate back to the workspace,
1883 // otherwise, just wait until onResume to set the state back to Workspace
1884 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001885 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001886 } else {
1887 mOnResumeState = State.WORKSPACE;
1888 }
1889
1890 final View v = getWindow().peekDecorView();
1891 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001892 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 INPUT_METHOD_SERVICE);
1894 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1895 }
1896
Winson Chungb745afb2015-03-02 11:51:23 -08001897 // Reset the apps view
1898 if (!alreadyOnHome && mAppsView != null) {
1899 mAppsView.scrollToTop();
1900 }
1901
Hyunyoung Song3f471442015-04-08 19:01:34 -07001902 // Reset the widgets view
1903 if (!alreadyOnHome && mWidgetsView != null) {
1904 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001905 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001906
Adam Cohen9211d422014-10-07 18:14:53 -07001907 if (mLauncherCallbacks != null) {
1908 mLauncherCallbacks.onHomeIntent();
1909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
Adam Cohened307df2013-10-02 09:37:31 -07001911
Adam Cohen9211d422014-10-07 18:14:53 -07001912 if (mLauncherCallbacks != null) {
1913 mLauncherCallbacks.onNewIntent(intent);
1914 }
Winson15f8b172015-08-19 11:02:39 -07001915
1916 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1917 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1918 // animation.
1919 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001920 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1921 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001922 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1923 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001924
1925 // We use this flag to suppress noisy callbacks above custom content state
1926 // from onResume.
1927 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001928 mWorkspace.post(new Runnable() {
1929 @Override
1930 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001931 if (mWorkspace != null) {
1932 mWorkspace.moveToDefaultScreen(true);
1933 }
Winson15f8b172015-08-19 11:02:39 -07001934 }
1935 });
1936 }
1937 }
1938
1939 if (DEBUG_RESUME_TIME) {
1940 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1941 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001942 }
1943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001945 public void onRestoreInstanceState(Bundle state) {
1946 super.onRestoreInstanceState(state);
1947 for (int page: mSynchronouslyBoundPages) {
1948 mWorkspace.restoreInstanceStateForChild(page);
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
1952 @Override
1953 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001954 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001955 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1956 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001957 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001958 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959
Michael Jurka883f55b2010-10-21 15:47:14 -07001960 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001961 // We close any open folder since it will not be re-opened, and we need to make sure
1962 // this state is reflected.
1963 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1964 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001968 ContentValues itemValues = new ContentValues();
1969 mPendingAddInfo.writeToValues(itemValues);
1970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001971 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001972 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
Hyunyoung Song3f471442015-04-08 19:01:34 -07001975 // Save the current widgets tray?
1976 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001977
1978 if (mLauncherCallbacks != null) {
1979 mLauncherCallbacks.onSaveInstanceState(outState);
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
1983 @Override
1984 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001986
Winson Chunge7a03942011-08-05 15:05:12 -07001987 // Remove all pending runnables
1988 mHandler.removeMessages(ADVANCE_MSG);
1989 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001990 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001991
Winson Chungcd2b0142011-06-08 16:02:26 -07001992 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001993 // It's possible to receive onDestroy after a new Launcher activity has
1994 // been created. In this case, don't interfere with the new Launcher.
1995 if (mModel.isCurrentCallbacks(this)) {
1996 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001997 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001998 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002001 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002003 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002005 mAppWidgetHost = null;
2006
2007 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008
2009 TextKeyListener.getInstance().release();
2010
Tony Wickhame2217252016-03-22 16:34:23 -07002011 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2012 .removeAccessibilityStateChangeListener(this);
2013
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002014 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015
Michael Jurka2ecf9952012-06-18 12:52:28 -07002016 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002017
2018 if (mLauncherCallbacks != null) {
2019 mLauncherCallbacks.onDestroy();
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Adam Cohena9cf38f2011-05-02 15:36:58 -07002023 public DragController getDragController() {
2024 return mDragController;
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
2028 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002029 onStartForResult(requestCode);
2030 super.startActivityForResult(intent, requestCode);
2031 }
2032
2033 @Override
2034 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2035 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2036 onStartForResult(requestCode);
2037 try {
2038 super.startIntentSenderForResult(intent, requestCode,
2039 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2040 } catch (IntentSender.SendIntentException e) {
2041 throw new ActivityNotFoundException();
2042 }
2043 }
2044
2045 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002046 if (requestCode >= 0) {
2047 setWaitingForResult(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 /**
2052 * Indicates that we want global search for this activity by setting the globalSearch
2053 * argument for {@link #startSearch} to true.
2054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002056 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 if (initialQuery == null) {
2060 // Use any text typed in the launcher as the initial query
2061 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 if (appSearchData == null) {
2064 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002065 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Winson Chungadf0c182012-08-23 14:59:07 -07002067 Rect sourceBounds = new Rect();
2068 if (mSearchDropTargetBar != null) {
2069 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2070 }
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 if (clearTextImmediately) {
2075 clearTypedText();
2076 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002077
2078 // We need to show the workspace after starting the search
2079 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 }
2081
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 /**
2083 * Start a text search.
2084 *
2085 * @return {@code true} if the search will start immediately, so any further keypresses
2086 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2087 * to buffer keypresses.
2088 */
2089 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002091 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2092 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2093 sourceBounds);
2094 }
2095
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002098 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 }
2100
2101 /**
2102 * Starts the global search activity. This code is a copied from SearchManager
2103 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002104 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002106 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2108 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2109 if (globalSearchActivity == null) {
2110 Log.w(TAG, "No global search activity found.");
2111 return;
2112 }
2113 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2114 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2115 intent.setComponent(globalSearchActivity);
2116 // Make sure that we have a Bundle to put source in
2117 if (appSearchData == null) {
2118 appSearchData = new Bundle();
2119 } else {
2120 appSearchData = new Bundle(appSearchData);
2121 }
Adam Cohen9211d422014-10-07 18:14:53 -07002122 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002123 if (!appSearchData.containsKey("source")) {
2124 appSearchData.putString("source", getPackageName());
2125 }
2126 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2127 if (!TextUtils.isEmpty(initialQuery)) {
2128 intent.putExtra(SearchManager.QUERY, initialQuery);
2129 }
2130 if (selectInitialQuery) {
2131 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2132 }
2133 intent.setSourceBounds(sourceBounds);
2134 try {
2135 startActivity(intent);
2136 } catch (ActivityNotFoundException ex) {
2137 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
2140
Yura4f93ec62014-02-04 14:15:21 +00002141 public boolean isOnCustomContent() {
2142 return mWorkspace.isOnOrMovingToCustomContent();
2143 }
2144
Winson Chung70d72102011-08-12 11:24:35 -07002145 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002146 public boolean onPrepareOptionsMenu(Menu menu) {
2147 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002148 if (mLauncherCallbacks != null) {
2149 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2150 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002151 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002154 @Override
2155 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002156 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002157 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002158 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002159 }
2160
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 public boolean isWorkspaceLocked() {
2162 return mWorkspaceLoading || mWaitingForResult;
2163 }
2164
Adam Cohen517a7f52014-03-01 12:12:59 -08002165 public boolean isWorkspaceLoading() {
2166 return mWorkspaceLoading;
2167 }
2168
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002169 private void setWorkspaceLoading(boolean value) {
2170 boolean isLocked = isWorkspaceLocked();
2171 mWorkspaceLoading = value;
2172 if (isLocked != isWorkspaceLocked()) {
2173 onWorkspaceLockedChanged();
2174 }
2175 }
2176
2177 private void setWaitingForResult(boolean value) {
2178 boolean isLocked = isWorkspaceLocked();
2179 mWaitingForResult = value;
2180 if (isLocked != isWorkspaceLocked()) {
2181 onWorkspaceLockedChanged();
2182 }
2183 }
2184
Adam Cohen9211d422014-10-07 18:14:53 -07002185 protected void onWorkspaceLockedChanged() {
2186 if (mLauncherCallbacks != null) {
2187 mLauncherCallbacks.onWorkspaceLockedChanged();
2188 }
2189 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002190
Michael Jurka0280c3b2010-09-17 15:00:07 -07002191 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002194 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2195 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002196 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002198
Tony Wickhama0628cc2015-10-14 15:23:04 -07002199 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002200 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002201 if (LOGD) {
2202 Log.d(TAG, "Adding widget from drop");
2203 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002204 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2205 }
2206
Sunny Goyale6b63a32015-01-16 16:14:29 -08002207 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002208 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2209 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002210 if (appWidgetInfo.configure != null) {
2211 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002212 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002213
Bjorn Bringert7984c942009-12-09 15:38:25 +00002214 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002215 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2216 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002219 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002220 Runnable onComplete = new Runnable() {
2221 @Override
2222 public void run() {
2223 // Exit spring loaded mode if necessary after adding the widget
2224 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2225 null);
2226 }
2227 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002229 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002230 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
2232 }
Romain Guycbb89e42009-06-08 15:52:54 -07002233
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002234 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002235 // Close any folders that may be open.
2236 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002237 mWorkspace.moveToCustomContentScreen(animate);
2238 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002239
2240 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2241 int[] cell, int spanX, int spanY) {
2242 switch (info.itemType) {
2243 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2244 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2245 int span[] = new int[2];
2246 span[0] = spanX;
2247 span[1] = spanY;
2248 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2249 container, screenId, cell, span);
2250 break;
2251 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2252 processShortcutFromDrop(info.componentName, container, screenId, cell);
2253 break;
2254 default:
2255 throw new IllegalStateException("Unknown item type: " + info.itemType);
2256 }
2257 }
2258
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 /**
2260 * Process a shortcut drop.
2261 *
2262 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002266 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2267 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271
2272 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.cellX = cell[0];
2274 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276
2277 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2278 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002279 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280 }
2281
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 /**
2283 * Process a widget drop.
2284 *
2285 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002289 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2290 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002294 mPendingAddInfo.minSpanX = info.minSpanX;
2295 mPendingAddInfo.minSpanY = info.minSpanY;
2296
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mPendingAddInfo.cellX = cell[0];
2299 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002301 if (span != null) {
2302 mPendingAddInfo.spanX = span[0];
2303 mPendingAddInfo.spanY = span[1];
2304 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 AppWidgetHostView hostView = info.boundWidget;
2307 int appWidgetId;
2308 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002309 // In the case where we've prebound the widget, we remove it from the DragLayer
2310 if (LOGD) {
2311 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2312 }
2313 getDragLayer().removeView(hostView);
2314
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002316 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002317
2318 // Clear the boundWidget so that it doesn't get destroyed.
2319 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002320 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002321 // In this case, we either need to start an activity to get permission to bind
2322 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002323 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002324 Bundle options = info.bindOptions;
2325
Sunny Goyalffe83f12014-08-14 17:39:34 -07002326 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2327 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002328 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002329 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002330 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002331 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002332 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2333 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2334 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2336 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002337 // TODO: we need to make sure that this accounts for the options bundle.
2338 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002339 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2340 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002341 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 }
2343
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002346 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 folderInfo.title = getText(R.string.folder_name);
2348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002350 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2351 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352
2353 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 FolderIcon newFolder =
2355 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002357 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002358 // Force measure the new folder icon
2359 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2360 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002361 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 }
Romain Guycbb89e42009-06-08 15:52:54 -07002363
Winsonc0b52fe2015-09-09 16:38:15 -07002364 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002365 * Unbinds the view for the specified item, and removes the item and all its children.
2366 *
2367 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002368 * @param itemInfo the {@link ItemInfo} for this view.
2369 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002370 */
Winson2949fb52015-09-24 09:56:11 -07002371 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002372 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002373 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002374 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2375 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002376 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002377 } else {
2378 mWorkspace.removeWorkspaceItem(v);
2379 }
Winsonc0b52fe2015-09-09 16:38:15 -07002380 if (deleteFromDb) {
2381 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2382 }
2383 } else if (itemInfo instanceof FolderInfo) {
2384 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Winsonc0b52fe2015-09-09 16:38:15 -07002385 mWorkspace.removeWorkspaceItem(v);
2386 if (deleteFromDb) {
2387 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2388 }
2389 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2390 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002391 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 removeWidgetToAutoAdvance(widgetInfo.hostView);
2393 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002394 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002396 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002397
Winsonc0b52fe2015-09-09 16:38:15 -07002398 } else {
2399 return false;
2400 }
2401 return true;
2402 }
2403
2404 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002405 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002406 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002407 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002408 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002410 // Deleting an app widget ID is a void call but writes to disk before returning
2411 // to the caller...
2412 new AsyncTask<Void, Void, Void>() {
2413 public Void doInBackground(Void ... args) {
2414 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2415 return null;
2416 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002417 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002418 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002419 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 @Override
2423 public boolean dispatchKeyEvent(KeyEvent event) {
2424 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2425 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002428 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002429 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002430 dumpState();
2431 return true;
2432 }
2433 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 }
2435 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2436 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 return true;
2439 }
2440 }
2441
2442 return super.dispatchKeyEvent(event);
2443 }
2444
Joe Onorato88ec0992009-11-19 13:16:06 -08002445 @Override
2446 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002447 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2448 return;
2449 }
2450
Sunny Goyal45478022015-06-08 16:52:41 -07002451 if (mDragController.isDragging()) {
2452 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002453 return;
2454 }
2455
Winson Chungb745afb2015-03-02 11:51:23 -08002456 if (isAppsViewVisible()) {
2457 showWorkspace(true);
2458 } else if (isWidgetsViewVisible()) {
2459 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002460 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002461 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002463 Folder openFolder = mWorkspace.getOpenFolder();
2464 if (openFolder.isEditingName()) {
2465 openFolder.dismissEditingName();
2466 } else {
2467 closeFolder();
2468 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002469 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002470 mWorkspace.exitWidgetResizeMode();
2471
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002472 // Back button is a no-op here, but give at least some feedback for the button press
2473 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002474 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002475 }
2476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002478 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002479 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002480 @Override
2481 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002482 if (mAppWidgetHost != null) {
2483 mAppWidgetHost.startListening();
2484 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002485
2486 // Recreate the QSB, as the widget has been reset.
2487 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002488 }
2489
2490 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 * Launches the intent referred by the clicked shortcut.
2492 *
2493 * @param v The view representing the clicked shortcut.
2494 */
2495 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002496 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2497 // view has detached (it's possible for this to happen if the view is removed mid touch).
2498 if (v.getWindowToken() == null) {
2499 return;
2500 }
2501
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002502 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002503 return;
2504 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002505
Adam Cohen1697b792013-09-17 19:08:21 -07002506 if (v instanceof Workspace) {
2507 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002508 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 }
2510 return;
2511 }
2512
2513 if (v instanceof CellLayout) {
2514 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002515 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2516 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002518 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 }
2520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002522 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002525 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002528 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002529 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002530 } else if (tag instanceof AppInfo) {
2531 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002532 } else if (tag instanceof LauncherAppWidgetInfo) {
2533 if (v instanceof PendingAppWidgetHostView) {
2534 onClickPendingWidget((PendingAppWidgetHostView) v);
2535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 }
2537 }
2538
Sunny Goyal70660032015-05-14 00:07:08 -07002539 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002540 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002541 return false;
2542 }
2543
Michael Jurkaaf442092010-06-10 17:01:57 -07002544 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002545 * Event handler for the app widget view which has not fully restored.
2546 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002548 if (mIsSafeModeEnabled) {
2549 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2550 return;
2551 }
2552
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002554 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002555 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002556 LauncherAppWidgetProviderInfo appWidgetInfo =
2557 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002559 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002560 mPendingAddInfo.copyFrom(info);
2561 mPendingAddWidgetId = widgetId;
2562
Sunny Goyalffe83f12014-08-14 17:39:34 -07002563 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2564 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002565 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002566 } else if (info.installProgress < 0) {
2567 // The install has not been queued
2568 final String packageName = info.providerName.getPackageName();
2569 showBrokenAppInstallDialog(packageName,
2570 new DialogInterface.OnClickListener() {
2571 public void onClick(DialogInterface dialog, int id) {
2572 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2573 }
2574 });
2575 } else {
2576 // Download has started.
2577 final String packageName = info.providerName.getPackageName();
2578 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002579 }
2580 }
2581
2582 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002583 * Event handler for the "grid" button that appears on the home screen, which
2584 * enters all apps mode.
2585 *
2586 * @param v The view that was clicked.
2587 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 protected void onClickAllAppsButton(View v) {
2589 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002590 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002591 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002592 true /* updatePredictedApps */, false /* focusSearchBar */);
2593 }
2594 }
2595
2596 protected void onLongClickAllAppsButton(View v) {
2597 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2598 if (!isAppsViewVisible()) {
2599 showAppsView(true /* animated */, false /* resetListToTop */,
2600 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 }
2602 }
2603
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 private void showBrokenAppInstallDialog(final String packageName,
2605 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002606 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002607 .setTitle(R.string.abandoned_promises_title)
2608 .setMessage(R.string.abandoned_promise_explanation)
2609 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2610 .setNeutralButton(R.string.abandoned_clean_this,
2611 new DialogInterface.OnClickListener() {
2612 public void onClick(DialogInterface dialog, int id) {
2613 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2614 mWorkspace.removeAbandonedPromise(packageName, user);
2615 }
2616 })
2617 .create().show();
2618 return;
2619 }
2620
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 /**
2622 * Event handler for an app shortcut click.
2623 *
2624 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2625 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2628 Object tag = v.getTag();
2629 if (!(tag instanceof ShortcutInfo)) {
2630 throw new IllegalArgumentException("Input must be a Shortcut");
2631 }
2632
2633 // Open shortcut
2634 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002635
2636 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002637 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2638 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002639 // Launch activity anyway, framework will tell the user why the app is suspended.
2640 } else {
2641 int error = R.string.activity_not_available;
2642 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2643 error = R.string.safemode_shortcut_error;
2644 }
2645 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2646 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002647 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002648 }
2649
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002651 if ((v instanceof BubbleTextView)
2652 && shortcut.isPromise()
2653 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002654 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002655 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 new DialogInterface.OnClickListener() {
2657 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002658 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 return;
2662 }
2663
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002664 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002665 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 }
2667
Adam Cohen091440a2015-03-18 14:16:05 -07002668 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002670 final ShortcutInfo shortcut;
2671 final Intent intent;
2672 if (tag instanceof ShortcutInfo) {
2673 shortcut = (ShortcutInfo) tag;
2674 intent = shortcut.intent;
2675 int[] pos = new int[2];
2676 v.getLocationOnScreen(pos);
2677 intent.setSourceBounds(new Rect(pos[0], pos[1],
2678 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002679
Sunny Goyal508da152014-08-14 10:53:27 -07002680 } else if (tag instanceof AppInfo) {
2681 shortcut = null;
2682 intent = ((AppInfo) tag).intent;
2683 } else {
2684 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2685 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686
2687 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002688 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689
2690 if (success && v instanceof BubbleTextView) {
2691 mWaitingForResume = (BubbleTextView) v;
2692 mWaitingForResume.setStayPressed(true);
2693 }
2694 }
2695
2696 /**
2697 * Event handler for a folder icon click.
2698 *
2699 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2700 */
2701 protected void onClickFolderIcon(View v) {
2702 if (LOGD) Log.d(TAG, "onClickFolder");
2703 if (!(v instanceof FolderIcon)){
2704 throw new IllegalArgumentException("Input must be a FolderIcon");
2705 }
2706
2707 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002708 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002710 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 }
Michael Jurka5130e402011-10-13 04:55:35 -07002712 }
2713
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002714 /**
2715 * Event handler for the (Add) Widgets button that appears after a long press
2716 * on the home screen.
2717 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002718 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002720 if (mIsSafeModeEnabled) {
2721 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2722 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002723 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002724 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002725 }
2726
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 /**
2728 * Event handler for the wallpaper picker button that appears after a long press
2729 * on the home screen.
2730 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002731 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002732 if (!Utilities.isWallapaperAllowed(this)) {
2733 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2734 return;
2735 }
2736
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002738 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2739 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal30a71612016-04-12 17:57:41 -07002740 // TODO: Start the system wallpaper picker
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 }
2742
2743 /**
2744 * Event handler for a click on the settings button that appears after a long press
2745 * on the home screen.
2746 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002747 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002748 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002749 if (mLauncherCallbacks != null) {
2750 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002751 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002752 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002753 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002754 }
2755
Adam Cohen61f560d2013-09-30 15:58:20 -07002756 public View.OnTouchListener getHapticFeedbackTouchListener() {
2757 if (mHapticFeedbackTouchListener == null) {
2758 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002759 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002760 @Override
2761 public boolean onTouch(View v, MotionEvent event) {
2762 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2763 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2764 }
2765 return false;
2766 }
2767 };
2768 }
2769 return mHapticFeedbackTouchListener;
2770 }
2771
Tony Wickhame2217252016-03-22 16:34:23 -07002772 @Override
2773 public void onAccessibilityStateChanged(boolean enabled) {
2774 mDragLayer.onAccessibilityStateChanged(enabled);
2775 }
2776
Adam Cohen9211d422014-10-07 18:14:53 -07002777 public void onDragStarted(View view) {
2778 if (isOnCustomContent()) {
2779 // Custom content screen doesn't participate in drag and drop. If on custom
2780 // content screen, move to default.
2781 moveWorkspaceToDefaultScreen();
2782 }
Adam Cohen9211d422014-10-07 18:14:53 -07002783 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002784
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002785 /**
2786 * Called when the user stops interacting with the launcher.
2787 * This implies that the user is now on the homescreen and is not doing housekeeping.
2788 */
Adam Cohen9211d422014-10-07 18:14:53 -07002789 protected void onInteractionEnd() {
2790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onInteractionEnd();
2792 }
2793 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002794
2795 /**
2796 * Called when the user starts interacting with the launcher.
2797 * The possible interactions are:
2798 * - open all apps
2799 * - reorder an app shortcut, or a widget
2800 * - open the overview mode.
2801 * This is a good time to stop doing things that only make sense
2802 * when the user is on the homescreen and not doing housekeeping.
2803 */
Adam Cohen9211d422014-10-07 18:14:53 -07002804 protected void onInteractionBegin() {
2805 if (mLauncherCallbacks != null) {
2806 mLauncherCallbacks.onInteractionBegin();
2807 }
2808 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002809
Winson Chungcd99cd32015-04-29 11:03:24 -07002810 /** Updates the interaction state. */
2811 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002812 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002813 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002814 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2815 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002816 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002817 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002818 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002819 onInteractionEnd();
2820 }
2821 }
2822
Winson Chung8f1eff72015-05-28 17:33:40 -07002823 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002824 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002826 // Only launch using the new animation if the shortcut has not opted out (this is a
2827 // private contract between launcher and may be ignored in the future).
2828 boolean useLaunchAnimation = (v != null) &&
2829 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002830 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2831 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002832
Kenny Guy1317e2d2014-05-08 18:52:50 +01002833 UserHandleCompat user = null;
2834 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2835 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2836 user = userManager.getUserForSerialNumber(serialNumber);
2837 }
2838
2839 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002840 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002841 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002842 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002843 int left = 0, top = 0;
2844 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2845 if (v instanceof TextView) {
2846 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002847 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2848 if (icon != null) {
2849 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002850 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002851 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002852 width = bounds.width();
2853 height = bounds.height();
2854 }
2855 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002856 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2857 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002858 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002859 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002860 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002861 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002862 // On L devices, we use the device default slide-up transition.
2863 // On L MR1 devices, we a custom version of the slide-up transition which
2864 // doesn't have the delay present in the device default.
2865 opts = ActivityOptions.makeCustomAnimation(this,
2866 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002867 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002868 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002869 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002870
2871 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2872 // Could be launching some bookkeeping activity
2873 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002874 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002875 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002876 launcherApps.startActivityForProfile(intent.getComponent(), user,
2877 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002878 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002879 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002881 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2882 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2883 // corresponding permission. Show the appropriate permission prompt if that
2884 // is the case.
2885 if (intent.getComponent() == null
2886 && Intent.ACTION_CALL.equals(intent.getAction())
2887 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2888 PackageManager.PERMISSION_GRANTED) {
2889 // TODO: Rename sPendingAddItem to a generic name.
2890 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2891 0, (ItemInfo) tag);
2892 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2893 REQUEST_PERMISSION_CALL_PHONE);
2894 return false;
2895 }
2896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002898 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002900 "or use the exported attribute for this activity. "
2901 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002902 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002903 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002905
Winson Chungbedf9232015-07-10 12:38:30 -07002906 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002907 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002908 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2909 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2910 return false;
2911 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002912 try {
2913 success = startActivity(v, intent, tag);
2914 } catch (ActivityNotFoundException e) {
2915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2916 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2917 }
2918 return success;
2919 }
2920
Adam Cohen268c4752012-06-06 17:47:33 -07002921 /**
2922 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2923 * in the DragLayer in the exact absolute location of the original FolderIcon.
2924 */
2925 private void copyFolderIconToImage(FolderIcon fi) {
2926 final int width = fi.getMeasuredWidth();
2927 final int height = fi.getMeasuredHeight();
2928
2929 // Lazy load ImageView, Bitmap and Canvas
2930 if (mFolderIconImageView == null) {
2931 mFolderIconImageView = new ImageView(this);
2932 }
2933 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2934 mFolderIconBitmap.getHeight() != height) {
2935 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2936 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2937 }
2938
2939 DragLayer.LayoutParams lp;
2940 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2941 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2942 } else {
2943 lp = new DragLayer.LayoutParams(width, height);
2944 }
2945
Adam Cohen307fe232012-08-16 17:55:58 -07002946 // The layout from which the folder is being opened may be scaled, adjust the starting
2947 // view size by this scale factor.
2948 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002949 lp.customPosition = true;
2950 lp.x = mRectForFolderAnimation.left;
2951 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002952 lp.width = (int) (scale * width);
2953 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002954
2955 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2956 fi.draw(mFolderIconCanvas);
2957 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002958 if (fi.getFolder() != null) {
2959 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2960 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002961 }
Adam Cohen268c4752012-06-06 17:47:33 -07002962 // Just in case this image view is still in the drag layer from a previous animation,
2963 // we remove it and re-add it.
2964 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2965 mDragLayer.removeView(mFolderIconImageView);
2966 }
2967 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002968 if (fi.getFolder() != null) {
2969 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002970 }
Adam Cohen268c4752012-06-06 17:47:33 -07002971 }
2972
Adam Cohen37b2a492016-04-06 18:36:06 -07002973 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002974 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002975 FolderInfo info = (FolderInfo) fi.getTag();
2976 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2977 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002978 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2979 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002980 }
2981
Adam Cohen268c4752012-06-06 17:47:33 -07002982 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2983 copyFolderIconToImage(fi);
2984 fi.setVisibility(View.INVISIBLE);
2985
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002986 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2987 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002988 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002989 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2990 }
2991 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002992 oa.start();
2993 }
2994
Sunny Goyal935fca12015-10-13 10:19:01 -07002995 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002996 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002997 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002998
Adam Cohen268c4752012-06-06 17:47:33 -07002999 // We remove and re-draw the FolderIcon in-case it has changed
3000 mDragLayer.removeView(mFolderIconImageView);
3001 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003002
3003 if (cl != null) {
3004 cl.clearFolderLeaveBehind();
3005 }
3006
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003007 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003008 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003009 oa.addListener(new AnimatorListenerAdapter() {
3010 @Override
3011 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003012 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003013 // Remove the ImageView copy of the FolderIcon and make the original visible.
3014 mDragLayer.removeView(mFolderIconImageView);
3015 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003016 }
3017 }
3018 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003019 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003020 if (!animate) {
3021 oa.end();
3022 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003023 }
3024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003026 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027 * is animated relative to the specified View. If the View is null, no animation
3028 * is played.
3029 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003030 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003032 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003033
Adam Cohenfb91f302012-06-11 15:45:18 -07003034 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003035 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3036 if (openFolder != null && openFolder != folder) {
3037 // Close any open folder before opening a folder.
3038 closeFolder();
3039 }
3040
Adam Cohena9cf38f2011-05-02 15:36:58 -07003041 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042
Adam Cohena9cf38f2011-05-02 15:36:58 -07003043 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044
Sunny Goyalf4066152015-04-15 09:42:19 -07003045 // While the folder is open, the position of the icon cannot change.
3046 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3047
Adam Cohen4554ee12011-08-03 16:13:21 -07003048 // Just verify that the folder hasn't already been added to the DragLayer.
3049 // There was a one-off crash where the folder had a parent already.
3050 if (folder.getParent() == null) {
3051 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003052 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003053 } else {
3054 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3055 folder.getParent() + ").");
3056 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003057 folder.animateOpen();
3058
3059 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003060
3061 // Notify the accessibility manager that this folder "window" has appeared and occluded
3062 // the workspace items
3063 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3064 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003065 }
3066
3067 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003068 closeFolder(true);
3069 }
3070
3071 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003072 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003073 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003074 if (folder.isEditingName()) {
3075 folder.dismissEditingName();
3076 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003078 }
3079 }
3080
Sunny Goyal935fca12015-10-13 10:19:01 -07003081 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003082 animate &= !Utilities.isPowerSaverOn(this);
3083
Adam Cohen4554ee12011-08-03 16:13:21 -07003084 folder.getInfo().opened = false;
3085
3086 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3087 if (parent != null) {
3088 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003089 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003090 if (fi != null) {
3091 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3092 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003093 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003094 if (animate) {
3095 folder.animateClosed();
3096 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003097 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003098 }
Winson Chung83ca4802013-04-12 15:10:52 -07003099
Sunny Goyal935fca12015-10-13 10:19:01 -07003100 // Notify the accessibility manager that this folder "window" has disappeared and no
3101 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003102 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 }
3104
Tony Wickhamdadb3042016-02-24 11:07:00 -08003105 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003107 if (!isDraggingEnabled()) return false;
3108 if (isWorkspaceLocked()) return false;
3109 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110
Winson Chung76648c52015-07-10 14:33:23 -07003111 if (v == mAllAppsButton) {
3112 onLongClickAllAppsButton(v);
3113 return true;
3114 }
3115
Adam Cohen1697b792013-09-17 19:08:21 -07003116 if (v instanceof Workspace) {
3117 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003118 if (!mWorkspace.isTouchActive()) {
3119 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003120 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3121 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3122 return true;
3123 } else {
3124 return false;
3125 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003126 } else {
3127 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003128 }
Adam Cohen1697b792013-09-17 19:08:21 -07003129 }
3130
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003131 CellLayout.CellInfo longClickCellInfo = null;
3132 View itemUnderLongClick = null;
3133 if (v.getTag() instanceof ItemInfo) {
3134 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003135 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003136 itemUnderLongClick = longClickCellInfo.cell;
3137 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 }
3139
Winson Chung3d503fb2011-07-13 17:25:49 -07003140 // The hotseat touch handling does not go through Workspace, and we always allow long press
3141 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003142 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003143 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003144 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003145 // User long pressed on empty space
3146 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3147 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003148 if (mWorkspace.isInOverviewMode()) {
3149 mWorkspace.startReordering(v);
3150 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003151 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003154 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3155 mHotseat.getOrderInHotseat(
3156 longClickCellInfo.cellX,
3157 longClickCellInfo.cellY));
3158 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003160 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 }
3162 }
3163 }
3164 return true;
3165 }
3166
Winson Chung3d503fb2011-07-13 17:25:49 -07003167 boolean isHotseatLayout(View layout) {
3168 return mHotseat != null && layout != null &&
3169 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3170 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003171
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 /**
3173 * Returns the CellLayout of the specified container at the specified screen.
3174 */
Sunny Goyal92820592015-03-02 11:31:35 -08003175 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3177 if (mHotseat != null) {
3178 return mHotseat.getLayout();
3179 } else {
3180 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003181 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003182 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003183 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003184 }
3185 }
3186
Winson Chungb745afb2015-03-02 11:51:23 -08003187 /**
3188 * For overridden classes.
3189 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003190 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003191 return isAppsViewVisible();
3192 }
3193
3194 public boolean isAppsViewVisible() {
3195 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3196 }
3197
3198 public boolean isWidgetsViewVisible() {
3199 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003200 }
3201
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003202 private void setWorkspaceBackground(int background) {
3203 switch (background) {
3204 case WORKSPACE_BACKGROUND_TRANSPARENT:
3205 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3206 break;
3207 case WORKSPACE_BACKGROUND_BLACK:
3208 getWindow().setBackgroundDrawable(null);
3209 break;
3210 default:
3211 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3212 }
Craig Mautner360310b2012-10-26 15:13:08 -07003213 }
3214
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003215 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003216 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3217 int curflags = getWindow().getAttributes().flags
3218 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3219 if (wpflags != curflags) {
3220 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3221 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003222 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003223 }
3224
Michael Jurkae326f182011-11-21 14:05:46 -08003225 @Override
3226 public void onTrimMemory(int level) {
3227 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003228 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3229 // The widget preview db can result in holding onto over
3230 // 3MB of memory for caching which isn't necessary.
3231 SQLiteDatabase.releaseMemory();
3232
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003233 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003234 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003235 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003236 if (mLauncherCallbacks != null) {
3237 mLauncherCallbacks.onTrimMemory(level);
3238 }
Michael Jurkae326f182011-11-21 14:05:46 -08003239 }
3240
Winson5c6bdbb2015-09-03 11:36:19 -07003241 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003242 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003243 }
3244
Winson5c6bdbb2015-09-03 11:36:19 -07003245 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003246 boolean changed = mState != State.WORKSPACE ||
3247 mWorkspace.getState() != Workspace.State.NORMAL;
3248 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003249 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003250 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003251 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003252
Michael Jurkab3e22d92011-10-31 15:58:33 -07003253 // Set focus to the AppsCustomize button
3254 if (mAllAppsButton != null) {
3255 mAllAppsButton.requestFocus();
3256 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003257 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003258
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003259 // Change the state *after* we've called all the transition code
3260 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003261
Winson Chung5fb63472011-02-02 17:03:37 -08003262 // Resume the auto-advance of widgets
3263 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003264 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003265
Winson Chung0f785722015-04-08 10:27:49 -07003266 if (changed) {
3267 // Send an accessibility event to announce the context change
3268 getWindow().getDecorView()
3269 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003270 }
Winson5c6bdbb2015-09-03 11:36:19 -07003271 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003272 }
3273
Winsone9f27272015-10-13 10:47:51 -07003274 /**
3275 * Shows the overview button.
3276 */
Adam Cohened307df2013-10-02 09:37:31 -07003277 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003278 showOverviewMode(animated, false);
3279 }
3280
3281 /**
3282 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3283 * onto one of the overview panel buttons.
3284 */
3285 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3286 Runnable postAnimRunnable = null;
3287 if (requestButtonFocus) {
3288 postAnimRunnable = new Runnable() {
3289 @Override
3290 public void run() {
3291 // Hitting the menu button when in touch mode does not trigger touch mode to
3292 // be disabled, so if requested, force focus on one of the overview panel
3293 // buttons.
3294 mOverviewPanel.requestFocusFromTouch();
3295 }
3296 };
3297 }
Adam Cohened307df2013-10-02 09:37:31 -07003298 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003299 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003300 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003301 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003302 }
3303
Winson Chungb745afb2015-03-02 11:51:23 -08003304 /**
3305 * Shows the apps view.
3306 */
Winson Chung76648c52015-07-10 14:33:23 -07003307 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3308 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003309 if (resetListToTop) {
3310 mAppsView.scrollToTop();
3311 }
Winson Chung4ac30062015-05-08 17:34:17 -07003312 if (updatePredictedApps) {
3313 tryAndUpdatePredictedApps();
3314 }
Winson Chung76648c52015-07-10 14:33:23 -07003315 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003316 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317
Winson Chungb745afb2015-03-02 11:51:23 -08003318 /**
3319 * Shows the widgets view.
3320 */
3321 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003322 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003323 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003324 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003325 }
Winson Chung76648c52015-07-10 14:33:23 -07003326 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003327
3328 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003329 @Override
3330 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003331 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003332 }
3333 });
Winson Chungb745afb2015-03-02 11:51:23 -08003334 }
3335
3336 /**
3337 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003338 *
3339 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003340 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003341 // TODO: calling method should use the return value so that when {@code false} is returned
3342 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003343 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003344 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3345 mState != State.WIDGETS_SPRING_LOADED) {
3346 return false;
3347 }
3348 if (toState != State.APPS && toState != State.WIDGETS) {
3349 return false;
3350 }
Winson Chungb745afb2015-03-02 11:51:23 -08003351
3352 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003353 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3354 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003355 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003356 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003357 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358
Michael Jurkab3e22d92011-10-31 15:58:33 -07003359 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003360 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361
3362 // Pause the auto-advance of widgets until we are out of AllApps
3363 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003364 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365 closeFolder();
3366
3367 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003368 getWindow().getDecorView()
3369 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003370 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003371 }
3372
Winson Chungcd99cd32015-04-29 11:03:24 -07003373 /**
3374 * Updates the workspace and interaction state on state change, and return the animation to this
3375 * new state.
3376 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003377 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003378 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003379 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003380 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003381 updateInteraction(fromState, toState);
3382 return anim;
3383 }
3384
Jun Mukaif0033da2015-08-04 18:34:30 -07003385 public void onLauncherClingShown() {
3386 // When a launcher cling appears, it should cover the underlying layers, so their focus
3387 // should be blocked.
3388 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3389 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3390 }
3391 }
3392
3393 public void onLauncherClingDismissed() {
3394 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3395 }
3396
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003398 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003399 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003400 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003401 }
Winson Chungb745afb2015-03-02 11:51:23 -08003402
Winson Chung006ee262015-08-03 14:40:11 -07003403 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003404 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003405 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003406
3407 if (isAppsViewVisible()) {
3408 mState = State.APPS_SPRING_LOADED;
3409 } else if (isWidgetsViewVisible()) {
3410 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003411 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003412 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003413 } else {
3414 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003415 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003416 }
Adam Cohen7777d962011-08-18 18:58:38 -07003417
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003419 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003420 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003421
Winson Chunge7a03942011-08-05 15:05:12 -07003422 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003423 @Override
3424 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003425 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3427 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003428 // Before we show workspace, hide all apps again because
3429 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3430 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003432 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003433 } else {
3434 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003435 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003436 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003437 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003438 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439
Tony Wickhame0c33232016-02-08 11:37:04 -08003440 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003441 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3442 || mState == State.WIDGETS_SPRING_LOADED;
3443 }
3444
Michael Jurkad3ef3062010-11-23 16:23:58 -08003445 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003446 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003447 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003448 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003449 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003450 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003451 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3452 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003453 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003454 }
3455
Winson Chung4ac30062015-05-08 17:34:17 -07003456 /**
3457 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3458 * resumed.
3459 */
3460 private void tryAndUpdatePredictedApps() {
3461 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003462 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003463 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003464 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003465 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003466 }
3467 }
3468 }
3469
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470 void lockAllApps() {
3471 // TODO
3472 }
3473
3474 void unlockAllApps() {
3475 // TODO
3476 }
3477
Winsonf768d932015-09-25 16:12:35 -07003478 public boolean launcherCallbacksProvidesSearch() {
3479 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3480 }
3481
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003482 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003483 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003484 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003485 }
3486
Adam Cohen24ce0b32014-01-14 16:18:14 -08003487 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003488 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3489 if (searchProvider == null) {
3490 return null;
3491 }
3492
3493 Bundle opts = new Bundle();
3494 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003495 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003496
Tony Wickham3a3517f2015-10-06 11:27:04 -07003497 // Determine the min and max dimensions of the widget.
3498 LauncherAppState app = LauncherAppState.getInstance();
3499 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3500 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3501 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003502 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3503 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003504 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003505 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003506 int minWidth = maxWidth;
3507 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003508 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3509 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3510 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3511 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3512 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003513 }
3514 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3515 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3516 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3517 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003518 if (LOGD) {
3519 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3520 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3521 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003522
Tony Wickham775455c2015-10-16 09:49:32 -07003523 if (mLauncherCallbacks != null) {
3524 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3525 }
3526
Sunny Goyalf7258242015-10-19 16:59:07 -07003527 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003528 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003529 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003530 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003531 || (widgetInfo == null)
3532 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003533 // A valid widget is not already bound.
3534 if (widgetId > -1) {
3535 mAppWidgetHost.deleteAppWidgetId(widgetId);
3536 widgetId = -1;
3537 }
3538
3539 // Try to bind a new widget
3540 widgetId = mAppWidgetHost.allocateAppWidgetId();
3541
3542 if (!AppWidgetManagerCompat.getInstance(this)
3543 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3544 mAppWidgetHost.deleteAppWidgetId(widgetId);
3545 widgetId = -1;
3546 }
3547
Sunny Goyalf7258242015-10-19 16:59:07 -07003548 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003549 .putInt(QSB_WIDGET_ID, widgetId)
3550 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003551 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003552 }
3553
Sunny Goyal8d595092015-07-06 12:22:14 -07003554 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 if (widgetId != -1) {
3556 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003557 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003558 mQsb.updateAppWidgetOptions(opts);
3559 mQsb.setPadding(0, 0, 0, 0);
3560 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003561 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003562 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003563 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003564 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003565 }
3566
Sunny Goyal22235bc2015-04-03 09:23:43 -07003567 private void reinflateQSBIfNecessary() {
3568 if (mQsb instanceof LauncherAppWidgetHostView &&
3569 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3570 mSearchDropTargetBar.removeView(mQsb);
3571 mQsb = null;
3572 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3573 }
3574 }
3575
Michael Jurkad7c28052012-04-27 15:43:36 -07003576 @Override
3577 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003578 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003579 final List<CharSequence> text = event.getText();
3580 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003581 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003582 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003583 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003584 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003585 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003586 } else if (mWorkspace != null) {
3587 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003588 } else {
3589 text.add(getString(R.string.all_apps_home_button_label));
3590 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003591 return result;
3592 }
3593
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003594 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003595 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003596 */
Adam Cohen091440a2015-03-18 14:16:05 -07003597 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003598 @Override
3599 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003600 closeSystemDialogs();
3601 }
3602 }
3603
3604 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 * If the activity is currently paused, signal that we need to run the passed Runnable
3606 * in onResume.
3607 *
3608 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003609 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3610 * wrong when we're not running, and if the activity comes back to what the configuration was
3611 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003612 *
3613 * Implementation of the method from LauncherModel.Callbacks.
3614 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003615 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003616 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003617 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003618 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003619 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003620 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003621 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003622 }
3623 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003624 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003625 return true;
3626 } else {
3627 return false;
3628 }
3629 }
3630
Michael Jurkac402cd92013-05-20 15:49:32 +02003631 private boolean waitUntilResume(Runnable run) {
3632 return waitUntilResume(run, false);
3633 }
3634
Michael Jurka1e2f4652013-07-08 18:03:46 -07003635 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003636 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003637 }
3638
Michael Jurka7607c2f2013-04-03 14:33:19 -07003639 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003640 * If the activity is currently paused, signal that we need to re-run the loader
3641 * in onResume.
3642 *
3643 * This needs to be called from incoming places where resources might have been loaded
3644 * while we are paused. That is becaues the Configuration might be wrong
3645 * when we're not running, and if it comes back to what it was when we
3646 * were paused, we are not restarted.
3647 *
3648 * Implementation of the method from LauncherModel.Callbacks.
3649 *
3650 * @return true if we are currently paused. The caller might be able to
3651 * skip some work in that case since we will come back again.
3652 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003653 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003654 public boolean setLoadOnResume() {
3655 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003656 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003657 mOnResumeNeedsLoad = true;
3658 return true;
3659 } else {
3660 return false;
3661 }
3662 }
3663
3664 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003665 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003667 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003668 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003669 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003670 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003671 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003672 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003673 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003675
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003677 * Clear any pending bind callbacks. This is called when is loader is planning to
3678 * perform a full rebind from scratch.
3679 */
3680 @Override
3681 public void clearPendingBinds() {
3682 mBindOnResumeCallbacks.clear();
3683 if (mPendingExecutor != null) {
3684 mPendingExecutor.markCompleted();
3685 mPendingExecutor = null;
3686 }
3687 }
3688
3689 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 * Refreshes the shortcuts shown on the workspace.
3691 *
3692 * Implementation of the method from LauncherModel.Callbacks.
3693 */
3694 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003695 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003696
Winson Chungd64d1762013-08-20 14:37:16 -07003697 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003698 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003699 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003700
Michael Jurka05bf644e2011-11-30 20:28:53 -08003701 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003702 if (mHotseat != null) {
3703 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003704 }
3705 }
3706
Adam Cohendcd297f2013-06-18 13:13:40 -07003707 @Override
3708 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003709 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003710
Adam Cohen5084cba2013-09-03 12:01:16 -07003711 // If there are no screens, we need to have an empty screen
3712 if (orderedScreenIds.size() == 0) {
3713 mWorkspace.addExtraEmptyScreen();
3714 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003715
3716 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003717 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003718 if (hasCustomContentToLeft()) {
3719 mWorkspace.createCustomContentContainer();
3720 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003721 }
Winson Chung64359a52013-07-08 17:17:08 -07003722 }
3723
3724 @Override
3725 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003726 int count = orderedScreenIds.size();
3727 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003728 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003729 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003730 }
3731
Winson Chungd64d1762013-08-20 14:37:16 -07003732 public void bindAppsAdded(final ArrayList<Long> newScreens,
3733 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003734 final ArrayList<ItemInfo> addAnimated,
3735 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 Runnable r = new Runnable() {
3737 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003738 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003739 }
3740 };
3741 if (waitUntilResume(r)) {
3742 return;
3743 }
3744
Winson Chungd64d1762013-08-20 14:37:16 -07003745 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003746 if (newScreens != null) {
3747 bindAddScreens(newScreens);
3748 }
Winson Chungd64d1762013-08-20 14:37:16 -07003749
3750 // We add the items without animation on non-visible pages, and with
3751 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003752 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003753 bindItems(addNotAnimated, 0,
3754 addNotAnimated.size(), false);
3755 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003756 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003757 bindItems(addAnimated, 0,
3758 addAnimated.size(), true);
3759 }
Winson Chungc58497e2013-09-03 17:48:37 -07003760
Winson Chung87412982013-10-03 18:34:14 -07003761 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003762 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003763
Winson Chungb745afb2015-03-02 11:51:23 -08003764 if (addedApps != null && mAppsView != null) {
3765 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003766 }
Winson Chungd64d1762013-08-20 14:37:16 -07003767 }
3768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 * Bind the items start-end from the list.
3771 *
3772 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003773 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003774 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003775 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3776 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003777 Runnable r = new Runnable() {
3778 public void run() {
3779 bindItems(shortcuts, start, end, forceAnimateIcons);
3780 }
3781 };
3782 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003783 return;
3784 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003785
Winson Chungf0c6ae02012-03-21 16:10:31 -07003786 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003787 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3788 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003789 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003790 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003791 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003792 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003794
3795 // Short circuit if we are loading dock items for a configuration which has no dock
3796 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3797 mHotseat == null) {
3798 continue;
3799 }
3800
Sunny Goyal639e9062015-08-19 19:17:06 -07003801 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 switch (item.itemType) {
3803 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3804 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003805 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003806 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003807
Winson Chung64359a52013-07-08 17:17:08 -07003808 /*
3809 * TODO: FIX collision case
3810 */
Winson Chungce376632013-07-11 16:12:41 -07003811 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3812 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3813 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003814 View v = cl.getChildAt(item.cellX, item.cellY);
3815 Object tag = v.getTag();
3816 String desc = "Collision while binding workspace item: " + item
3817 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003818 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003819 throw (new RuntimeException(desc));
3820 } else {
3821 Log.d(TAG, desc);
3822 }
Winson Chungce376632013-07-11 16:12:41 -07003823 }
Winson Chung64359a52013-07-08 17:17:08 -07003824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003826 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003827 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003828 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003829 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003831 default:
3832 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003834
3835 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3836 item.cellY, 1, 1);
3837 if (animateIcons) {
3838 // Animate all the applications up now
3839 view.setAlpha(0f);
3840 view.setScaleX(0f);
3841 view.setScaleY(0f);
3842 bounceAnims.add(createNewAppBounceAnimation(view, i));
3843 newShortcutsScreenId = item.screenId;
3844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003845 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003846
Winson Chung997a9232013-07-24 15:33:46 -07003847 if (animateIcons) {
3848 // Animate to the correct page
3849 if (newShortcutsScreenId > -1) {
3850 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003851 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003852 final Runnable startBounceAnimRunnable = new Runnable() {
3853 public void run() {
3854 anim.playTogether(bounceAnims);
3855 anim.start();
3856 }
3857 };
Winson Chung997a9232013-07-24 15:33:46 -07003858 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003859 // We post the animation slightly delayed to prevent slowdowns
3860 // when we are loading right after we return to launcher.
3861 mWorkspace.postDelayed(new Runnable() {
3862 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003863 if (mWorkspace != null) {
3864 mWorkspace.snapToPage(newScreenIndex);
3865 mWorkspace.postDelayed(startBounceAnimRunnable,
3866 NEW_APPS_ANIMATION_DELAY);
3867 }
Winson Chung94d67682013-09-25 16:29:40 -07003868 }
3869 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003870 } else {
3871 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003872 }
3873 }
Winson Chung64359a52013-07-08 17:17:08 -07003874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003876 }
3877
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003878 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3879 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3880 view.updateIcon(mIconCache);
3881 item.hostView = view;
3882 item.hostView.updateAppWidget(null);
3883 item.hostView.setOnClickListener(this);
3884 addAppWidgetToWorkspace(item, null, false);
3885 mWorkspace.requestLayout();
3886 }
3887
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 /**
3889 * Add the views for a widget to the workspace.
3890 *
3891 * Implementation of the method from LauncherModel.Callbacks.
3892 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003893 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003894 Runnable r = new Runnable() {
3895 public void run() {
3896 bindAppWidget(item);
3897 }
3898 };
3899 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003900 return;
3901 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003902
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003903 if (mIsSafeModeEnabled) {
3904 bindSafeModeWidget(item);
3905 return;
3906 }
3907
Daniel Sandler843e8602010-06-07 14:59:01 -04003908 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3909 if (DEBUG_WIDGETS) {
3910 Log.d(TAG, "bindAppWidget: " + item);
3911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003913 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003914
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003915 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3916 // If the provider is not ready, bind as a pending widget.
3917 appWidgetInfo = null;
3918 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3919 // The widget id is not valid. Try to find the widget based on the provider info.
3920 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3921 } else {
3922 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3923 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003924
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003925 // If the provider is ready, but the width is not yet restored, try to restore it.
3926 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3927 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003928 if (appWidgetInfo == null) {
3929 if (DEBUG_WIDGETS) {
3930 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3931 + " belongs to component " + item.providerName
3932 + ", as the povider is null");
3933 }
3934 LauncherModel.deleteItemFromDatabase(this, item);
3935 return;
3936 }
Sunny Goyalff572272014-07-23 13:58:07 -07003937
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003938 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003939 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003940 // Note: This assumes that the id remap broadcast is received before this step.
3941 // If that is not the case, the id remap will be ignored and user may see the
3942 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003943 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003944 pendingInfo.spanX = item.spanX;
3945 pendingInfo.spanY = item.spanY;
3946 pendingInfo.minSpanX = item.minSpanX;
3947 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003948 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003949
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003950 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3951 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3952 newWidgetId, appWidgetInfo, options);
3953
3954 // TODO consider showing a permission dialog when the widget is clicked.
3955 if (!success) {
3956 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3959 + " belongs to component " + item.providerName
3960 + ", as the launcher is unable to bing a new widget id");
3961 }
3962 LauncherModel.deleteItemFromDatabase(this, item);
3963 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003964 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003965
3966 item.appWidgetId = newWidgetId;
3967
3968 // If the widget has a configure activity, it is still needs to set it up, otherwise
3969 // the widget is ready to go.
3970 item.restoreStatus = (appWidgetInfo.configure == null)
3971 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3972 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3973
3974 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003975 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003976 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003977 // The widget was marked as UI not ready, but there is no configure activity to
3978 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003979 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3980 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003981 }
Sunny Goyalff572272014-07-23 13:58:07 -07003982 }
3983
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003984 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003985 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003986 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3987 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003988 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003989
Sunny Goyal56c73602015-09-25 12:55:01 -07003990 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003991 if (appWidgetInfo == null) {
3992 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003993 deleteWidgetInfo(item);
3994 return;
3995 }
3996
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003997 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003998 item.minSpanX = appWidgetInfo.minSpanX;
3999 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004000 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004001 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004002 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4003 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004004 view.updateIcon(mIconCache);
4005 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004006 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004007 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004008 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004009 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004010 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004011
Daniel Sandler843e8602010-06-07 14:59:01 -04004012 if (DEBUG_WIDGETS) {
4013 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4014 + (SystemClock.uptimeMillis()-start) + "ms");
4015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004016 }
4017
Sunny Goyalff572272014-07-23 13:58:07 -07004018 /**
4019 * Restores a pending widget.
4020 *
4021 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004022 */
4023 private void completeRestoreAppWidget(final int appWidgetId) {
4024 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4025 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4026 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4027 return;
4028 }
4029
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004030 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004031 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4032
4033 mWorkspace.reinflateWidgetsIfNecessary();
4034 LauncherModel.updateItemInDatabase(this, info);
4035 }
4036
Adam Cohen1462de32012-07-24 22:34:36 -07004037 public void onPageBoundSynchronously(int page) {
4038 mSynchronouslyBoundPages.add(page);
4039 }
4040
Sunny Goyal527c7d32015-08-28 15:19:36 -07004041 @Override
4042 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4043 if (mPendingExecutor != null) {
4044 mPendingExecutor.markCompleted();
4045 }
4046 mPendingExecutor = executor;
4047 executor.attachTo(this);
4048 }
4049
4050 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4051 if (mPendingExecutor == executor) {
4052 mPendingExecutor = null;
4053 }
4054 }
4055
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 /**
4057 * Callback saying that there aren't any more items to bind.
4058 *
4059 * Implementation of the method from LauncherModel.Callbacks.
4060 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004061 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004062 Runnable r = new Runnable() {
4063 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004064 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004065 }
4066 };
4067 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004068 return;
4069 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 if (mSavedState != null) {
4071 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004072 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004074
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 mSavedState = null;
4076 }
4077
Adam Cohen1462de32012-07-24 22:34:36 -07004078 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004080 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004081
4082 // If we received the result of any pending adds while the loader was running (e.g. the
4083 // widget configuration forced an orientation change), process them now.
4084 if (sPendingAddItem != null) {
4085 final long screenId = completeAdd(sPendingAddItem);
4086
4087 // TODO: this moves the user to the page where the pending item was added. Ideally,
4088 // the screen would be guaranteed to exist after bind, and the page would be set through
4089 // the workspace restore process.
4090 mWorkspace.post(new Runnable() {
4091 @Override
4092 public void run() {
4093 mWorkspace.snapToScreenId(screenId);
4094 }
4095 });
4096 sPendingAddItem = null;
4097 }
4098
Sunny Goyal756adbc2015-04-16 15:20:51 -07004099 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004100
4101 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004102 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004103 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004104 }
4105
Winson Chunga0b7e862013-09-05 16:03:15 -07004106 public boolean isAllAppsButtonRank(int rank) {
4107 if (mHotseat != null) {
4108 return mHotseat.isAllAppsButtonRank(rank);
4109 }
4110 return false;
4111 }
4112
Winson Chunga2413752012-04-03 14:22:34 -07004113 private boolean canRunNewAppsAnimation() {
4114 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004115 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4116 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004117 }
4118
Winson Chungc9168342013-06-26 14:54:55 -07004119 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004120 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004121 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4122 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004123 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004124 return bounceAnim;
4125 }
4126
Adam Cohen27772732013-11-11 14:00:56 +00004127 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004128 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004129 }
4130
Tony Wickham3a3517f2015-10-06 11:27:04 -07004131 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004132 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004133 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004134 }
4135
Tony Wickham55616cd2015-09-23 14:55:17 -07004136 public int getSearchBarHeight() {
4137 if (mLauncherCallbacks != null) {
4138 return mLauncherCallbacks.getSearchBarHeight();
4139 }
4140 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4141 }
4142
Winson Chung88fa7412015-08-03 14:25:28 -07004143 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004144 if (mSearchDropTargetBar == null) {
4145 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004146 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004147 if (mQsb != null) {
4148 mSearchDropTargetBar.removeView(mQsb);
4149 mQsb = null;
4150 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004151 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004152 }
4153
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004154 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004155 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4156 * multiple calls to bind the same list.)
4157 */
4158 @Thunk ArrayList<AppInfo> mTmpAppsList;
4159 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4160 public void run() {
4161 bindAllApplications(mTmpAppsList);
4162 mTmpAppsList = null;
4163 }
4164 };
4165
4166 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 * Add the icons for all apps.
4168 *
4169 * Implementation of the method from LauncherModel.Callbacks.
4170 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004171 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004172 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4173 mTmpAppsList = apps;
4174 return;
4175 }
4176
Winson Chungb745afb2015-03-02 11:51:23 -08004177 if (mAppsView != null) {
4178 mAppsView.setApps(apps);
4179 }
Adam Cohen9211d422014-10-07 18:14:53 -07004180 if (mLauncherCallbacks != null) {
4181 mLauncherCallbacks.bindAllApplications(apps);
4182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004183 }
4184
4185 /**
4186 * A package was updated.
4187 *
4188 * Implementation of the method from LauncherModel.Callbacks.
4189 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004190 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004191 Runnable r = new Runnable() {
4192 public void run() {
4193 bindAppsUpdated(apps);
4194 }
4195 };
4196 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004197 return;
4198 }
4199
Winson Chungb745afb2015-03-02 11:51:23 -08004200 if (mAppsView != null) {
4201 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004203 }
4204
Sunny Goyal4390ace2014-10-13 11:33:11 -07004205 @Override
4206 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4207 Runnable r = new Runnable() {
4208 public void run() {
4209 bindWidgetsRestored(widgets);
4210 }
4211 };
4212 if (waitUntilResume(r)) {
4213 return;
4214 }
4215 mWorkspace.widgetsRestored(widgets);
4216 }
4217
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004219 * Some shortcuts were updated in the background.
4220 *
4221 * Implementation of the method from LauncherModel.Callbacks.
4222 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 @Override
4224 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4225 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004226 Runnable r = new Runnable() {
4227 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004228 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004229 }
4230 };
4231 if (waitUntilResume(r)) {
4232 return;
4233 }
4234
Sunny Goyal4390ace2014-10-13 11:33:11 -07004235 if (!updated.isEmpty()) {
4236 mWorkspace.updateShortcuts(updated);
4237 }
4238
4239 if (!removed.isEmpty()) {
4240 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4241 for (ShortcutInfo si : removed) {
4242 removedComponents.add(si.getTargetComponent());
4243 }
4244 mWorkspace.removeItemsByComponentName(removedComponents, user);
4245 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004246 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004247 }
4248 }
4249
4250 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004251 * Update the state of a package, typically related to install state.
4252 *
4253 * Implementation of the method from LauncherModel.Callbacks.
4254 */
Sunny Goyale755d462014-07-22 13:48:29 -07004255 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004256 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4257 Runnable r = new Runnable() {
4258 public void run() {
4259 bindRestoreItemsChange(updates);
4260 }
4261 };
4262 if (waitUntilResume(r)) {
4263 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004264 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004265
Sunny Goyal756adbc2015-04-16 15:20:51 -07004266 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004267 }
4268
4269 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004270 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004271 * in addition to specific applications to remove, the reason being that
4272 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004273 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004274 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004275 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004276 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004277 public void bindWorkspaceComponentsRemoved(
4278 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4279 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004280 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004281 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004282 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004283 }
Winson Chungd64d1762013-08-20 14:37:16 -07004284 };
4285 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004286 return;
4287 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004288 if (!packageNames.isEmpty()) {
4289 mWorkspace.removeItemsByPackageName(packageNames, user);
4290 }
4291 if (!components.isEmpty()) {
4292 mWorkspace.removeItemsByComponentName(components, user);
4293 }
4294 // Notify the drag controller
4295 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004296
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004297 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004298
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004299 @Override
4300 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4301 Runnable r = new Runnable() {
4302 public void run() {
4303 bindAppInfosRemoved(appInfos);
4304 }
4305 };
4306 if (waitUntilResume(r)) {
4307 return;
Joe Onorato36115782010-06-17 13:28:48 -04004308 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004309
Winson Chungdf95eb12013-10-16 14:57:07 -07004310 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004311 if (mAppsView != null) {
4312 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004314 }
Joe Onoratobe386092009-11-17 17:32:16 -08004315
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004316 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004317 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004318 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004319 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004320 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004321
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004322 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004323 public void bindWidgetsModel(WidgetsModel model) {
4324 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004325 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004326 return;
4327 }
4328
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004329 if (mWidgetsView != null && model != null) {
4330 mWidgetsView.addWidgets(model);
4331 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004332 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004333 }
4334
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004335 @Override
4336 public void notifyWidgetProvidersChanged() {
4337 if (mWorkspace.getState().shouldUpdateWidget) {
4338 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4339 }
4340 }
4341
Winson Chung400438b2011-01-16 17:53:48 -08004342 private int mapConfigurationOriActivityInfoOri(int configOri) {
4343 final Display d = getWindowManager().getDefaultDisplay();
4344 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4345 switch (d.getRotation()) {
4346 case Surface.ROTATION_0:
4347 case Surface.ROTATION_180:
4348 // We are currently in the same basic orientation as the natural orientation
4349 naturalOri = configOri;
4350 break;
4351 case Surface.ROTATION_90:
4352 case Surface.ROTATION_270:
4353 // We are currently in the other basic orientation to the natural orientation
4354 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4355 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4356 break;
4357 }
4358
4359 int[] oriMap = {
4360 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4361 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4362 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4363 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4364 };
4365 // Since the map starts at portrait, we need to offset if this device's natural orientation
4366 // is landscape.
4367 int indexOffset = 0;
4368 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4369 indexOffset = 1;
4370 }
4371 return oriMap[(d.getRotation() + indexOffset) % 4];
4372 }
Adam Cohen446e9402011-09-15 18:21:21 -07004373
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004374 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004375 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004376 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004377 if (Utilities.ATLEAST_JB_MR2) {
4378 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4379 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004380 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4381 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004382 }
Winson Chung4b919f82012-05-01 10:44:08 -07004383 }
4384 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004385
Winson Chung4b919f82012-05-01 10:44:08 -07004386 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004387 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004388 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004389 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004390 } else {
4391 mHandler.postDelayed(new Runnable() {
4392 public void run() {
4393 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4394 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004395 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004396 }
Winson Chung4b919f82012-05-01 10:44:08 -07004397 }
Winson Chung400438b2011-01-16 17:53:48 -08004398 }
4399
Winson Chunge43a1e72014-01-15 10:33:02 -08004400 protected boolean isLauncherPreinstalled() {
4401 PackageManager pm = getPackageManager();
4402 try {
4403 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4404 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4405 return true;
4406 } else {
4407 return false;
4408 }
4409 } catch (NameNotFoundException e) {
4410 e.printStackTrace();
4411 return false;
4412 }
4413 }
4414
Adam Cohenea90f832014-05-21 15:03:34 -07004415 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004416 * To be overridden by subclasses to indicate that there is an activity to launch
4417 * before showing the standard launcher experience.
4418 */
4419 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004420 if (mLauncherCallbacks != null) {
4421 return mLauncherCallbacks.hasFirstRunActivity();
4422 }
Adam Cohen432609a2014-03-13 17:03:22 -07004423 return false;
4424 }
4425
4426 /**
4427 * To be overridden by subclasses to launch any first run activity
4428 */
4429 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004430 if (mLauncherCallbacks != null) {
4431 return mLauncherCallbacks.getFirstRunActivity();
4432 }
Adam Cohen432609a2014-03-13 17:03:22 -07004433 return null;
4434 }
4435
Winson Chunga6945242014-01-08 14:04:34 -08004436 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004437 return !ActivityManager.isRunningInTestHarness() &&
4438 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004439 }
4440
Adam Cohen4a9423d2014-03-28 14:22:31 -07004441 protected boolean hasRunFirstRunActivity() {
4442 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4443 }
4444
Adam Cohen432609a2014-03-13 17:03:22 -07004445 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004446 if (shouldRunFirstRunActivity() &&
4447 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004448 Intent firstRunIntent = getFirstRunActivity();
4449 if (firstRunIntent != null) {
4450 startActivity(firstRunIntent);
4451 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004452 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004453 }
4454 }
Adam Cohen432609a2014-03-13 17:03:22 -07004455 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004456 }
4457
4458 private void markFirstRunActivityShown() {
4459 SharedPreferences.Editor editor = mSharedPrefs.edit();
4460 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4461 editor.apply();
4462 }
4463
Adam Cohen432609a2014-03-13 17:03:22 -07004464 /**
4465 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4466 * screen that must be displayed and dismissed.
4467 */
4468 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004469 if (mLauncherCallbacks != null) {
4470 return mLauncherCallbacks.hasDismissableIntroScreen();
4471 }
Adam Cohen432609a2014-03-13 17:03:22 -07004472 return false;
4473 }
4474
4475 /**
4476 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4477 */
4478 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004479 if (mLauncherCallbacks != null) {
4480 return mLauncherCallbacks.getIntroScreen();
4481 }
Adam Cohen432609a2014-03-13 17:03:22 -07004482 return null;
4483 }
4484
4485 /**
4486 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4487 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4488 */
4489 private boolean shouldShowIntroScreen() {
4490 return hasDismissableIntroScreen() &&
4491 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4492 }
4493
4494 protected void showIntroScreen() {
4495 View introScreen = getIntroScreen();
4496 changeWallpaperVisiblity(false);
4497 if (introScreen != null) {
4498 mDragLayer.showOverlayView(introScreen);
4499 }
4500 }
4501
4502 public void dismissIntroScreen() {
4503 markIntroScreenDismissed();
4504 if (showFirstRunActivity()) {
4505 // We delay hiding the intro view until the first run activity is showing. This
4506 // avoids a blip.
4507 mWorkspace.postDelayed(new Runnable() {
4508 @Override
4509 public void run() {
4510 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004511 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004512 }
4513 }, ACTIVITY_START_DELAY);
4514 } else {
4515 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004516 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004517 }
4518 changeWallpaperVisiblity(true);
4519 }
4520
4521 private void markIntroScreenDismissed() {
4522 SharedPreferences.Editor editor = mSharedPrefs.edit();
4523 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4524 editor.apply();
4525 }
4526
Adam Cohen091440a2015-03-18 14:16:05 -07004527 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004528 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4529 // on the device, then we always show the first run cling experience (or if there is no
4530 // launcher2). Otherwise, we prompt the user upon started for migration
4531 LauncherClings launcherClings = new LauncherClings(this);
4532 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004533 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004534 if (mModel.canMigrateFromOldLauncherDb(this)) {
4535 launcherClings.showMigrationCling();
4536 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004537 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004538 }
4539 }
4540 }
4541
Winson Chunga6945242014-01-08 14:04:34 -08004542 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004543 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4544 if (mHotseat != null) mHotseat.setAlpha(1f);
4545 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004546 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4547 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004548 }
4549
4550 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004551 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4552 if (mHotseat != null) mHotseat.setAlpha(0f);
4553 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004554 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4555 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004556 }
4557
Winson Chung5ffd51d2015-06-25 11:36:50 -07004558 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004559 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004560 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004561 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004562 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004563 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004564 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4565 if (userHandle != null) {
4566 user = UserHandleCompat.fromUser(userHandle);
4567 }
4568 }
4569 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004570 }
4571
4572 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004573 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004574 if (user == null) {
4575 user = UserHandleCompat.myUserHandle();
4576 }
4577
4578 // Called from search suggestion, add the profile extra to the intent to ensure that we
4579 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004580 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004581 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004582 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004583 return null;
4584 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004585 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004586 }
4587
Winson Chung5ffd51d2015-06-25 11:36:50 -07004588 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004589 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4590 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004591 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004592 UserHandleCompat.myUserHandle());
4593 }
4594
Winson Chung5ffd51d2015-06-25 11:36:50 -07004595 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004596 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4597 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004598 mWorkspace.onExternalDragStartedWithItem(dragView);
4599 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004600 }
4601
Winson Chung5ffd51d2015-06-25 11:36:50 -07004602 protected void moveWorkspaceToDefaultScreen() {
4603 mWorkspace.moveToDefaultScreen(false);
4604 }
4605
Winson Chung80baf5a2010-08-09 16:03:15 -07004606 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004607 * Returns a FastBitmapDrawable with the icon, accurately sized.
4608 */
4609 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4610 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4611 d.setFilterBitmap(true);
4612 resizeIconDrawable(d);
4613 return d;
4614 }
4615
4616 /**
4617 * Resizes an icon drawable to the correct icon size.
4618 */
Winson5fbe0742015-09-30 15:33:00 -07004619 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004620 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004621 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004622 }
4623
4624 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004625 * Prints out out state for debugging.
4626 */
4627 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004628 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004629 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004630 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4631 Log.d(TAG, "mRestoring=" + mRestoring);
4632 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004633 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004634 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004635
Daniel Sandler325dc232013-06-05 22:57:57 -04004636 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004637 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004638
4639 @Override
4640 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4641 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004642 // Dump workspace
4643 writer.println(prefix + "Workspace Items");
4644 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4645 writer.println(prefix + " Homescreen " + i);
4646
4647 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4648 for (int j = 0; j < layout.getChildCount(); j++) {
4649 Object tag = layout.getChildAt(j).getTag();
4650 if (tag != null) {
4651 writer.println(prefix + " " + tag.toString());
4652 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004653 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004654 }
Sunny Goyala1365452015-10-01 15:46:24 -07004655
4656 writer.println(prefix + " Hotseat");
4657 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4658 for (int j = 0; j < layout.getChildCount(); j++) {
4659 Object tag = layout.getChildAt(j).getTag();
4660 if (tag != null) {
4661 writer.println(prefix + " " + tag.toString());
4662 }
4663 }
4664
4665 synchronized (sDumpLogs) {
4666 writer.println();
4667 writer.println(prefix + "Debug logs");
4668 for (String log : sDumpLogs) {
4669 writer.println(prefix + " " + log);
4670 }
4671 }
4672
Adam Cohen9211d422014-10-07 18:14:53 -07004673 if (mLauncherCallbacks != null) {
4674 mLauncherCallbacks.dump(prefix, fd, writer, args);
4675 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004676 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004677
Sunny Goyala1365452015-10-01 15:46:24 -07004678 public static void addDumpLog(String tag, String log) {
4679 Log.d(tag, log);
4680 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004681 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004682 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004683 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004684 }
4685
Adam Cohen59400422014-03-05 18:07:04 -08004686 public static CustomAppWidget getCustomAppWidget(String name) {
4687 return sCustomAppWidgets.get(name);
4688 }
4689
4690 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4691 return sCustomAppWidgets;
4692 }
4693
Sunny Goyal29538332016-02-18 09:10:19 -08004694 public static List<View> getFolderContents(View icon) {
4695 if (icon instanceof FolderIcon) {
4696 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4697 } else {
4698 return Collections.EMPTY_LIST;
4699 }
4700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004701}
Michael Jurka2763be32011-02-24 11:19:57 -08004702
Dan Sandlerd5024042014-01-09 15:01:33 -05004703interface DebugIntents {
4704 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4705 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004706}